javascript
hystrix 全局熔断_跟我学Spring Cloud(Finchley版)14Feign使用Hystrix
Feign默認(rèn)已經(jīng)整合了Hystrix,本節(jié)詳細(xì)探討Feign使用Hystrix的具體細(xì)節(jié)。
服務(wù)降級(jí)
1 加配置,默認(rèn)Feign是不啟用Hystrix的,需要添加如下配置啟用Hystrix,這樣所有的Feign Client都會(huì)受到Hystrix保護(hù)!
feign:
?hystrix:
? ?enabled: true
2 提供Fallback:
@FeignClient(name = "microservice-provider-user", fallback = UserFeignClientFallback.class)
public interface UserFeignClient {
?@GetMapping("/users/{id}")
?User findById(@PathVariable("id") Long id);
}
@Component
class UserFeignClientFallback implements UserFeignClient {
?@Override
?public User findById(Long id) {
? ?return new User(id, "默認(rèn)用戶", "默認(rèn)用戶", 0, new BigDecimal(1));
?}
}
獲得造成fallback的原因
@FeignClient(name = "microservice-provider-user", fallbackFactory = UserFeignClientFallbackFactory.class)
public interface UserFeignClient {
?@GetMapping("/users/{id}")
?User findById(@PathVariable("id") Long id);
}
@Component
@Slf4j
class UserFeignClientFallbackFactory implements FallbackFactory<UserFeignClient> {
?@Override
?public UserFeignClient create(Throwable throwable) {
? ?return new UserFeignClient() {
? ? ?@Override
? ? ?public User findById(Long id) {
? ? ? ?log.error("進(jìn)入回退邏輯", throwable);
? ? ? ?return new User(id, "默認(rèn)用戶", "默認(rèn)用戶", 0, new BigDecimal(1));
? ? ?}
? ?};
?}
}
Feign啟用/禁用Hystrix
全局啟用
feign.hystrix.enabled: true
全局禁用
feign.hystrix.enabled: false
或直接省略不寫。
局部啟用
利用Feign配置的自定義,為指定Feign Client指定如下配置類即可,Feign配置自定義詳見:跟我學(xué)Spring Cloud(Finchley版)-10-Feign深入
public class FeignDisableHystrixConfiguration {
? ?@Bean
? ?@Scope("prototype")
? ?public HystrixFeign.Builder feignBuilder() {
? ? ? ?return HystrixFeign.builder();
? ?}
}
局部禁用
public class FeignDisableHystrixConfiguration {
? ?@Bean
? ?@Scope("prototype")
? ?public Feign.Builder feignBuilder() {
? ? ? ?return Feign.builder();
? ?}
}
配套代碼
服務(wù)降級(jí):
1 GitHub:https://github.com/eacdy/spring-cloud-study/tree/master/2018-Finchley/microservice-consumer-movie-feign-hystrix
2 Gitee:https://gitee.com/itmuch/spring-cloud-study/tree/master/2018-Finchley/microservice-consumer-movie-feign-hystrix
獲得造成fallback的原因:
1 GitHub:https://github.com/eacdy/spring-cloud-study/tree/master/2018-Finchley/microservice-consumer-movie-feign-hystrix-fallback-factory
2 Gitee:https://gitee.com/itmuch/spring-cloud-study/tree/master/2018-Finchley/microservice-consumer-movie-feign-hystrix-fallback-factory
相關(guān)熱文
實(shí)用技巧:Hystrix傳播ThreadLocal對(duì)象(兩種方案)
生產(chǎn)技巧:Feign如何控制Hystrix的啟停、超時(shí)、熔斷?
Hystrix停止開發(fā),我們?cè)摵稳ズ螐?#xff1f;
Spring Cloud 源碼學(xué)習(xí)之 Hystrix 熔斷器
Spring Cloud Edgware新特性之二:如何配置Zuul的Hystrix線程池
跟我學(xué)Spring Cloud(Finchley版)-12-微服務(wù)容錯(cuò)三板斧
跟我學(xué)Spring Cloud(Finchley版)-13-通用方式使用Hystrix
您的關(guān)注是對(duì)我最大的支持!
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的hystrix 全局熔断_跟我学Spring Cloud(Finchley版)14Feign使用Hystrix的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 信用卡找人代还的步骤
- 下一篇: 动态新增表字段_制作动态的数据透视表(一