springcloud(9)-断路器
1.pom.xml 增加断路器相关包
<!--断路器-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2.ProductClientFeign注解修改
由原来的
@FeignClient(value = "PRODUCT-DATA-SERVICE")
变为
@FeignClient(value = "PRODUCT-DATA-SERVICE",fallback = ProductClientFeignHystrix.class)
3.增加ProductClientFeignHystrix.class
@Component
public class ProductClientFeignHystrix implements ProductClientFeign{
public List<Product> listProdcuts(){
List<Product> result = new ArrayList<>();
result.add(new Product(0,"产品数据微服务不可用",0));
return result;
}
}
4.修改配置文件application.yml,增加如下:
feign.hystrix.enabled: true
5.测试:http://localhost:8012/products
![image-20201116143203432](https://i.loli.net/2020/11/16/Hgj4PlFp5KTBMOE.png)