服务故障的“雪崩”:
服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,甚至瘫痪,这就是服务故障的“雪崩”效应。
断路器预防“雪崩”:
当对特定的服务的调用的不可用达到一个阀值(Hystric 是5秒20次) 断路器将会被打开。
1ribbon中使用断路器
第1步:Application中通过注解@EnableHystrix开启断路器的功能:
第2步:在方法中通过注解实现对方法的容错。
@HystrixCommand(fallbackMethod = "indexError")
public String index(){
return restTemplate.getForObject("http://HELLOWORLDSERVICE/index",String.class);
}
断路器的验证:
当微服务没有开启,执行http://localhost:8801/index:
当微服务开启时,执行http://localhost:8801/index:
2 feign中使用断路器
第1步:application.properties的配置文件打开断路器:
feign.hystrix.enabled=true
第2步:在FeignClient接口加上fallback的类。如下所示:
@FeignClient(value = "HelloWorldService", fallback = FeignServiceHystric.class)
断路器的验证:
当微服务没有开启,执行http://localhost:8801/index:
当微服务开启时,执行http://localhost:8801/index:
代码参考:https://github.com/linghufeixia/springcloud-simplechapter3-1和chapter3-2