springcloud-服务之间的调用

openFegin(声明式服务调用组件)主要是用于服务之间的调用:

所需要的依赖:

 <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

无参数的情况下:


@FeignClient("provider")
@Service
public interface HelloService {

    @GetMapping("/provider/hello")
    String hello();



}

有参数的情况:

1.有参数的情况要绑定参数

2.如果要通过header来传参数一定要中文转码

   @GetMapping("/hello2")
    String hello2(@RequestParam("name") String name);
    
//方法的提供方

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello(){
        return "hello SpringCloud";
    }

    @GetMapping("/hello2")
    public String hello2(String name){
        return "hello" + name;
    }

以上我们基本就实现了服务之间的调用了

原文地址:https://www.cnblogs.com/yjfb/p/12751821.html