Feign的使用以及配置项说明

2021-03-18  本文已影响0人  G__yuan

项目中Feign的使用

1.Feign组件的引入

在项目pom文件引入以下依赖

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

2.在启动类上面添加注解

@EnableFeignClients
public class NeofaithWechatApiApplication {
    public static void main(String[] args) {
        SpringApplication.run(NeofaithWechatApiApplication.class, args);
    }
}

3.编写Feign客户端代码

@FeignClient(name = "neofaith-wechat-web")
public interface TestFeignClient {
    @GetMapping("/wechat-web/test/get")
    public String getTestInfo();
}

4.业务逻辑代码中使用编写的Feign客户端

@RestController
@RequestMapping("test")
public class TestController {
    @Autowired
    private TestFeignClient testFeignClient;
    @GetMapping("get")
    public String test(){
        return testFeignClient.getTestInfo();
    }
}

5.Feign的其他配置项说明

image.png

Ribbon的配置和Feign配置的对比

image.png

配置的优先级说明

细粒度属性>细粒度代码>全局属性>全局代码

上一篇 下一篇

猜你喜欢

热点阅读