spring-cloud之hystrix解决雪崩
一、消费者服务降级
1、首先要在pom增加hystrix功能,如下:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
2、在application中配置如下,开启服务降级的断路器
@EnableCircuitBreaker
3、在消费者的service的方法中配置如下:
@HystrixCommand(fallbackMethod = "fallback",commandProperties = {
//默认10秒;如果并发数达到该设置值,请求会被拒绝和抛出异常并且fallback不会被调用。
@HystrixProperty(name= HystrixPropertiesManager.FALLBACK_ISOLATION_SEMAPHORE_MAX_CONCURRENT_REQUESTS, value="15")})
4、编写fallback的托底方法
public List<Product> fallback(){
List<Product> list=new ArrayList<Product>();
list.add(new Product(-1,"fallback"));
return list;
}
备注:在什么条件下调用 fallback方法。