简介
hystrix断路器组件,支持熔断,即在访问微服务不可达时,可以通过fallback自定义占位内容,避免了页面出现404,500等错误,能提升用户体验,当然可以添加逻辑将当前数据存到数据库,之后手动做不可达服务的数据同步;
前置内容:
SpringCloud极简入门>消息总线#bus
实战
1、添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2、yml配置
开启支持hystrix;
#支持hystrix
feign:
hystrix:
enabled: true
3、接口方法调整
- feign方法添加fallback方法;
@FeignClient(value = "SERVER-ACCOUNT", fallback = AccountFeignHystrix.class)
public interface TestFeign {
@GetMapping("account")
String getAccount();
}
- 新增的服务访问失败的占位方法;
@Component
public class AccountFeignHystrix implements TestFeign {
@Override
public String getAccount() {
return "account服务不可用!";
}
}
4、测试效果
1、分别开启《server-center》、《server-config》、《server-account》、《server-order》服务,发起请求,可以获取《server-account》的数据;