跟我学Spring Cloud(Finchley版)-14-Feign使用Hystrix
Feign默认已经整合了Hystrix,本节详细探讨Feign使用Hystrix的具体细节。
服务降级
加配置,默认Feign是不启用Hystrix的,需要添加如下配置启用Hystrix,这样所有的Feign Client都会受到Hystrix保护!
1
2
3feign:
hystrix:
enabled: true提供Fallback:
1
2
3
4
5
6
7
8
9
10
11
12
13
public interface UserFeignClient {
User findById( Long id);
}
class UserFeignClientFallback implements UserFeignClient {
public User findById(Long id) {
return new User(id, "默认用户", "默认用户", 0, new BigDecimal(1));
}
}
获得造成fallback的原因
1 |
|
Feign启用/禁用Hystrix
全局启用
1 | feign.hystrix.enabled: true |
全局禁用
1 | feign.hystrix.enabled: false |
或直接省略不写。
局部启用
利用Feign配置的自定义,为指定Feign Client指定如下配置类即可,Feign配置自定义详见:跟我学Spring Cloud(Finchley版)-10-Feign深入
1 | public class FeignDisableHystrixConfiguration { |
局部禁用
1 | public class FeignDisableHystrixConfiguration { |
配套代码
服务降级:
- GitHub:https://github.com/eacdy/spring-cloud-study/tree/master/2018-Finchley/microservice-consumer-movie-feign-hystrix
- Gitee:https://gitee.com/itmuch/spring-cloud-study/tree/master/2018-Finchley/microservice-consumer-movie-feign-hystrix
获得造成fallback的原因:
- GitHub:https://github.com/eacdy/spring-cloud-study/tree/master/2018-Finchley/microservice-consumer-movie-feign-hystrix-fallback-factory
- Gitee:https://gitee.com/itmuch/spring-cloud-study/tree/master/2018-Finchley/microservice-consumer-movie-feign-hystrix-fallback-factory
评论系统未开启,无法评论!