这篇文章主要给大家介绍了Spring Cloud中关于Feign的常见问题,文中通过示例代码介绍的很详细,需要的朋友可以参考借鉴,下面来一起看看吧。
一、FeignClient接口,不能使用@GettingMapping 之类的组合注解
代码示例:
1
2
3
4
5
6
@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
代码示例:
1
2
3
4
5
6
@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 服务,并且参数有多个例如:http://microservice-provider-user/query-by?id=1&username=张三 要怎么办呢?
直接使用复杂对象:
1
2
3
4
5
6
@FeignClient(“microservice-provider-user”)
public interface UserFeignClient {
@RequestMapping(value = “/query-by”, method = RequestMethod.GET)
public User queryBy(User user);
…
}
该请求不会成功,只要参数是复杂对象,即使指定了是GET方法,feign依然会以POST方法进行发送请求。
正确的写法:
写法1:
1
2
3
4
5
@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:
1
2
3
4
5
@FeignClient(name = “microservice-provider-user”)
public interface UserFeignClient {
@RequestMapping(value = “/query-by”, method = RequestMethod.GET)
public List queryBy(@RequestParam Map<String, Object> param);
}
四、Feign如果想要使用Hystrix Stream,需要做一些额外操作
我们知道Feign本身就是支持Hystrix的,可以直接使用@FeignClient(value = “microservice-provider-user”, fallback = XXX.class) 来指定fallback的类,这个fallback类集成@FeignClient所标注的接口即可。
但是假设我们需要使用Hystrix Stream进行监控,默认情况下,访问http://IP:PORT/hystrix.stream 是个404。如何为Feign增加Hystrix Stream支持呢?
需要以下两步:
第一步:添加依赖,示例:
1
2
3
4
5
org.springframework.cloud
spring-cloud-starter-hystrix
第二步:在启动类上添加@EnableCircuitBreaker 注解,示例:
1
2
3
4
5
6
7
8
9
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
@EnableCircuitBreaker
public class MovieFeignHystrixApplication {
public static void main(String[] args) {
SpringApplication.run(MovieFeignHystrixApplication.class, args);
}
}
这样修改以后,访问任意的API后,再访问http://IP:PORT/hystrix.stream,就会展示出一大堆的API监控数据了。
五、如果需要自定义单个Feign配置,Feign的@Configuration 注解的类不能与@ComponentScan 的包重叠
如果包重叠,将会导致所有的Feign Client都会使用该配置。
六、首次请求失败
详见:解决Spring Cloud中Feign/Ribbon第一次请求失败的方法
在Spring Cloud中,Feign和Ribbon在整合了Hystrix后,可能会出现首次调用失败的问题,要如何解决该问题呢?
造成该问题的原因
Hystrix默认的超时时间是1秒,如果超过这个时间尚未响应,将会进入fallback代码。而首次请求往往会比较慢(因为Spring的懒加载机制,要实例化一些类),这个响应时间可能就大于1秒了。知道原因后,我们来总结一下解决放你。
解决方案有三种,以feign为例。
方法一
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
该配置是让Hystrix的超时时间改为5秒
方法二
hystrix.command.default.execution.timeout.enabled: false
该配置,用于禁用Hystrix的超时时间
方法三
feign.hystrix.enabled: false
该配置,用于索性禁用feign的hystrix。该做法除非一些特殊场景,不推荐使用。
总结
以上就是关于Spring Cloud中解决Feign/Ribbon第一次请求失败的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。
七、@FeignClient 的属性注意点
(1) serviceId属性已经失效,尽量使用name属性。例如:
1
@FeignClient(serviceId = “microservice-provider-user”)
这么写是不推荐的,应写为:
1
@FeignClient(name = “microservice-provider-user”)
(2) 在使用url属性时,在老版本的Spring Cloud中,不需要提供name属性,但是在新版本(例如Brixton、Camden)@FeignClient必须提供name属性,并且name、url属性支持占位符。例如:
1
@FeignClient(name = “feign.name",url="{feign.name}", url = "feign.name",url="{feign.url}”)
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。
————————————————
版权声明:本文为优快云博主「trusause」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/trusause/article/details/83786445
本文探讨了SpringCloud中Feign的常见问题,包括FeignClient接口的使用限制,多参数构造方法,HystrixStream的配置,自定义配置注意事项,首次请求失败的解决方案,以及@FeignClient属性的正确使用。
167万+

被折叠的 条评论
为什么被折叠?



