优先核心服务,非核心服务不可用或是弱可用
通过HystrixCommand注解指定
fallbackMethod(回退函数)中具体实现降级逻辑
Hystrix如何解决依赖隔离:
熔断器:Circuit Breaker:
每个熔断器默认维护10个bucket,每秒一个bucket,每个bucket记录成功,失败,超时,拒绝的状态, 默认错误超过50%且10秒内超过20个请求进行中断拦截.
// hystrix依赖
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
@Service
public class FeignService {
@Autowired
private ClientService clientService;
@HystrixCommand(commandProperties = {
//设置熔断
@HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
//时间滚动中最小请求参数,只有在一个统计窗口内处理的请求数量达到这个阈值,才会进行熔断与否的判断
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
//休眠时间窗
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "20000"),
//错误百分比,判断熔断的阈值,默认值50,表示在一个统计窗口内有50%的请求处理失败,会触发熔断
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "40")
})
public String helloClient() {
return clientService.helloClient();
}
}
Application启动类添加@EnableCircuitBreaker注解,或换成@SpringCloudApplication
@EnableFeignClients
@SpringCloudApplication
public class ConsumeApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumeApplication.class, args);
}
}
修改原有的Service代码,增加内部类及实现:
// @FeignClient注解添加fallback属性
@FeignClient(name = "client", fallback = ClientService.ClientServiceFallback.class)
public interface ClientService {
@GetMapping("home/hello")
String helloClient();
@Component
class ClientServiceFallback implements ClientService{
@Override
public String helloClient() {
return "请稍后再试~~~";
}
}
}
一篇详细的博文链接:https://www.jianshu.com/p/138f92aa83dc