服务容错保护 Spring Cloud Hystrix
Spring Cloud Hystrix是基于Netflix的开源框架Hystrix实现的,其目的是为了通过控制那些访问远程系统、服务和第三方的节点,从而对延迟和故障提供强大的容错能力。Hystrix具备了服务降级、服务熔断、线程隔离、请求缓存、请求合并以及服务监控等强大功能 断路器类似于我们家里面强电箱里面用到的漏电断路保护器,当服务单元出现故障(类似于电器发生短路),通过断路器的故障监控功能(类似于保险丝),向调用方返回一个错误响应,避免长时间等待,从而避免故障蔓延到整个系统。
当我们调用一个服务出错时,会展示类似以下页面的错误页:
1 2 3 4 5 6 Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Thu Jan 24 14:11:27 CST 2019 There was an unexpected error (type=Internal Server Error, status=500). Index: 1, Size: 0
从而导致整个系统的崩溃,为了维持系统的正常运行,当某一单元出问题时,通过服务容错保护机制,来实现服务的降级和隔离,确保整个系统的正常运行。
我们将上篇文章中Ribbon的工程拷贝一份改名为service-consumer-ribbon-hystrix,并在其pom文件中加入引用:
1 2 3 4 5 6 7 8 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> </dependency>
spring-cloud-starter-hystrix-dashboard依赖是开启Hystrix监控面板
配置文件bootstrap.yml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 eureka: client: healthcheck: enabled: true #健康检查开启 serviceUrl: defaultZone: http://localhost:6060/eureka/ #注册中心服务地址 server: port: 7040 #当前服务端口 spring: ## 从配置中心读取文件 cloud: config: uri: http://localhost:6010/ label: master profile: dev name: service-consumer-ribbon-hystrix application: name: service-consumer-ribbon-hystrix #当前服务ID zipkin: base-url: http://localhost:6040 #zipkin服务地址 sleuth: enabled: true #服务追踪开启 sampler: percentage: 1 #zipkin收集率
工程主类需要加入@EnableCircuitBreaker或@EnableHystrix声明开启断路器服务,加入@EnableHystrixDashboard开启监控面板:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 /** * @author wkedong * 2019/1/5 * Ribbon */ @EnableHystrixDashboard @EnableCircuitBreaker @SpringBootApplication @EnableDiscoveryClient public class ServiceConsumerRibbonHystrixApplication { @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { new SpringApplicationBuilder(ServiceConsumerRibbonHystrixApplication.class).web(true).run(args); } }
定义一个HystrixController来实现/testHystrix接口:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 /** * @author wkedong * RobbinDemo * 2019/1/5 */ @RestController public class HystrixController { private final Logger logger = Logger.getLogger(getClass()); @Autowired HystrixService hystrixService; @GetMapping("/testHystrix") public String testHystrix() { logger.info("===<call testHystrix>==="); return hystrixService.testHystrix(); } }
相对应的服务和服务实现类分别如下:HystrixService
1 2 3 4 5 6 7 8 /** * @author wkedong * <p> * 2019/1/15 */ public interface HystrixService { String testHystrix(); }
HystrixServiceImpl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 /** * @author wkedong * Hystrix * 2019/1/15 */ @Service public class HystrixServiceImpl implements HystrixService { private final Logger logger = Logger.getLogger(getClass()); @Autowired RestTemplate restTemplate; @Override @HystrixCommand(fallbackMethod = "fallback") public String testHystrix() { //执行http请求service-producer服务的testHystrix映射地址,返回的数据为字符串类型 //服务提供者(service-producer服务)的注册服务ID //testHystrix :消费方法 return restTemplate.getForObject("http://service-producer/testHystrix", String.class); } public String fallback() { logger.info("===<call testHystrix fail fallback>==="); return "service-producer /testHystrix is error"; } }
这里我们在需要进行保护的服务调用方法上添加注解并给于它一个fallback方法的声明@HystrixCommand(fallbackMethod = "fallback"),确保在该方法调用出错时会自动调用该类中的fallback方法。
工程至此创建完成了,我们分别启动注册中心,配置中心,服务提供方和该工程, 直接访问服务提供端 http://localhost:6070/testHystrix 会在延迟后出现:
1 Hello, Spring Cloud! My port is 6070 This is a testHystrix result
访问该工程加了Hystrix保护的 http://localhost:7040/testHystrix 出现:
1 service-producer /testHystrix is error
Spring Cloud Hystrix Dashboard 下面介绍下如何查看监控面板,访问 http://localhost:7040/hystrix 出现以下页面: 这是Hystrix Dashboard的监控首页,该页面中并没有具体的监控信息。从页面的文字内容中我们可以知道,Hystrix Dashboard共支持三种不同的监控方式,依次为:
前两者都对集群的监控,需要整合Turbine才能实现,这里我们先来实现单个服务实例的监控。
页面上方还有两个参数:
Delay :该参数用来控制服务器上轮询监控信息的延迟时间,默认为2000毫秒,我们可以通过配置该属性来降低客户端的网络和CPU消耗。
Title :该参数对应了上图头部标题Hystrix Stream之后的内容,默认会使用具体监控实例的URL,我们可以通过配置该信息来展示更合适的标题。
在Hystrix Dashboard的首页输入已启动的服务 http://localhost:7040/hystrix.stream ,已启动对“service-consumer-ribbon-hystrix”的监控,点击“Monitor Stream”按钮,出现如下页面:
实心圆:共有两种含义。它通过颜色的变化代表了实例的健康程度,如下图所示,它的健康度从绿色、黄色、橙色、红色递减。该实心圆除了颜色的变化之外,它的大小也会根据实例的请求流量发生变化,流量越大该实心圆就越大。
曲线:用来记录2分钟内流量的相对变化,我们可以通过它来观察到流量的上升和下降趋势。
右上角那里的数字颜色对应Success | Short-Circuited | Bad Request | Timeout | Rejected | Failure 的颜色
本项目内容为Spring Cloud程序的样例:
样例列表:(SpringCloud版本基于Edgware.SR5)