博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Cloud - 关于Feign的常见问题总结
阅读量:6913 次
发布时间:2019-06-27

本文共 2735 字,大约阅读时间需要 9 分钟。

hot3.png

Spring Cloud中,Feign常见问题的总结。

FeignClient接口,不能使用@GettingMapping 之类的组合注解

代码示例:

@FeignClient("microservice-provider-user")public interface UserFeignClient {@RequestMapping(value = "/simple/{id}", method = RequestMethod.GET)public User findById(@PathVariable("id") Long id);...}

这边的@RequestMapping(value = "/simple/{id}", method = RequestMethod.GET) 不能写成@GetMapping("/simple/{id}")

 

FeignClient接口中,如果使用到@PathVariable ,必须指定其value

代码示例:

@FeignClient("microservice-provider-user")public interface UserFeignClient {@RequestMapping(value = "/simple/{id}", method = RequestMethod.GET)public User findById(@PathVariable("id") Long id);...}

这边的@PathVariable("id") 中的”id”,不能省略,必须指定。

 

FeignClient多参数的构造

如果想要请求microservice-provider-user 服务,并且参数有多个例如: 要怎么办呢?

直接使用复杂对象:

@FeignClient("microservice-provider-user")public interface UserFeignClient {@RequestMapping(value = "/query-by", method = RequestMethod.GET)public User queryBy(User user);...}

正确的写法:该请求不会成功,只要参数是复杂对象,即使指定了是GET方法,feign依然会以POST方法进行发送请求。

写法1:

@FeignClient("microservice-provider-user")public interface UserFeignClient {@RequestMapping(value = "/query-by", method = RequestMethod.GET)public User queryBy(@RequestParam("id")Long id, @RequestParam("username")String username);}

写法2:

@FeignClient(name = "microservice-provider-user")public interface UserFeignClient {@RequestMapping(value = "/query-by", method = RequestMethod.GET)public List
queryBy(@RequestParam Map
param);}

 

Feign如果想要使用Hystrix Stream,需要做一些额外操作

我们知道Feign本身就是支持Hystrix的,可以直接使用@FeignClient(value = "microservice-provider-user", fallback = XXX.class) 来指定fallback的类,这个fallback类集成@FeignClient所标注的接口即可。

但是假设我们需要使用Hystrix Stream进行监控,默认情况下,访问 是个404。如何为Feign增加Hystrix Stream支持呢?

需要以下两步:

第一步:添加依赖,示例:

org.springframework.cloud
spring-cloud-starter-hystrix

第二步:在启动类上添加@EnableCircuitBreaker 注解,示例:

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

这样修改以后,访问任意的API后,再访问

如果需要自定义单个Feign配置,Feign的@Configuration 注解的类不能与@ComponentScan 的包重叠

如果包重叠,将会导致所有的Feign Client都会使用该配置。

 

首次请求失败

详见:

@FeignClient 的属性注意点

(1) serviceId属性已经失效,尽量使用name属性。例如:

@FeignClient(serviceId = "microservice-provider-user")

这么写是不推荐的,应写为:

@FeignClient(name = "microservice-provider-user")

(2) 在使用url属性时,在老版本的Spring Cloud中,不需要提供name属性,但是在新版本(例如Brixton、Camden)@FeignClient必须提供name属性,并且name、url属性支持占位符。例如:

@FeignClient(name = "${feign.name}", url = "${feign.url}")

 

 

本文链接

转载声明:本博客由周立创作,采用 许可协议。可自由转载、引用,但需署名作者且注明文章出处。如转载至微信公众号,请在文末添加作者公众号二维码

转载于:https://my.oschina.net/xiaowangqiongyou/blog/1635106

你可能感兴趣的文章
创建VLAN、中继链路和参与以太网捆绑的详细配置和截图
查看>>
健康的办公族作息时刻表
查看>>
新博客地址此博客不再更新baishuchao.github.io
查看>>
Git问题Everything up-to-date解决
查看>>
淘宝Tengine安装指南
查看>>
nginx-mysql-php安装配置
查看>>
div加链接 html给div加超链接实现点击div跳转的方法
查看>>
layer 旋转
查看>>
写一个算法计算n的阶乘末尾0的个数
查看>>
Java正则表达式语法大全
查看>>
apache设置自定义header
查看>>
WPS田字格的做法
查看>>
Linux账号登录安全
查看>>
Linux 基础命令 – watch
查看>>
我的友情链接
查看>>
snavigator安装步骤
查看>>
python抓取jenkins slave信息写道mysql并展现到grafana
查看>>
debian 常用的源
查看>>
博为峰Java技术题-JavaSE 之标识符、注释
查看>>
陈松松:如何保证每天录制一个视频,一年365个原创视频
查看>>