本节详细讲解使用Hystrix的通用方式。git
Hystrix是由Netflix开源的一个延迟和容错库,用于隔离访问远程系统、服务或者第三方库,防止级联失败,从而提高系统的可用性与容错性。Hystrix主要经过如下几点实现延迟和容错。github
使用HystrixCommand(或HystrixObservableCommand)包裹对依赖的调用逻辑,每一个命令在独立线程中执行。这使用到了设计模式中的“命令模式”。spring
当某服务的错误率超过必定阈值时,Hystrix能够自动或者手动跳闸,中止请求该服务一段时间。设计模式
Hystrix为每一个依赖都维护了一个小型的线程池(或者信号量)。若是该线程池已满,发往该依赖的请求就被当即拒绝,而不是排队等候,从而加速失败断定。并发
Hystrix能够近乎实时地监控运行指标和配置的变化,例如成功、失败、超时、以及被拒绝的请求等。app
当请求失败、超时、被拒绝,或当断路器打开时,执行回退逻辑。回退逻辑可由开发人员自行提供,例如返回一个缺省值。ide
断路器打开一段时间后,会自动进入“半开”状态。断路器打开、关闭、半开的逻辑转换,前面咱们已经详细探讨过了,再也不赘述。微服务
加依赖:高并发
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
加注解:在启动类上添加@EnableCircuitBreaker
注解。测试
使用:
@HystrixCommand(fallbackMethod = "findByIdFallback") @GetMapping("/users/{id}") public User findById(@PathVariable Long id) { // 这里用到了RestTemplate的占位符能力 User user = this.restTemplate.getForObject( "http://microservice-provider-user/users/{id}", User.class, id ); // ...电影微服务的业务... return user; } public User findByIdFallback(Long id) { return new User(id, "默认用户", "默认用户", 0, new BigDecimal(1)); }
由代码可知,只需使用@HystrixCommand
注解,就可保护该API。这里的”保护“,其实带有三层含义——”超时机制“、”仓壁模式“、”断路器“!若是您不了解这些是什么,或者想要探寻其中原理,可详见:跟我学Spring Cloud(Finchley版)-12-微服务容错三板斧
TIPS
本例使用了fallbackMethod
属性,指定了一个降级方法,如不指定,Hystrix会有一个默认的降级方案,那就是抛异常,哈哈哈。
**如何知道断路器打开仍是关闭呢?**还记得健康检查吗?只需访问应用的/actuator/health
端点,便可查看!断路器的状态——固然,你必须添加以下配置:
management: endpoint: health: show-details: always
若是您对这部分知识有遗忘,可前往跟我学Spring Cloud(Finchley版)-03-监控:强大的Spring Boot Actuator 简单复习一下。
启动microservice-discovery-eureka
启动microservice-provider-user
启动microservice-consumer-movie-ribbon-hystrix-common
访问http://localhost:8010/movies/users/1
,能正常返回结果
关闭microservice-provider-user
,再次访问http://localhost:8010/movies/users/1
,可返回相似以下结果,说明当服务提供者时,服务消费者进入了回退方法。
{"id":1,"username":"默认用户","name":"默认用户","age":0,"balance":1}
访问http://localhost:8010/actuator/health
,可得到相似以下结果:
{ "status": "UP", "details": { "diskSpace": ..., "refreshScope": ..., "discoveryComposite": ..., "hystrix": { "status": "UP" } } }
由结果不难发现,此时断路器并未打开!这是为何呢? 缘由是:此时只请求了一次,没有达到Hystrix的阈值——Hystrix设计来保护高并发应用的,它要求10秒(可用hystrix.command.default.metrics.rollingStats.timeInMilliseconds
自定义)之内API错误次数超过20次(用circuitBreaker.requestVolumeThreshold
自定义),此时才可能触发断路器。
持续不断地访问http://localhost:8010/movies/users/1
屡次(至少20次)
再次访问http://localhost:8010/actuator/health
,可得到相似以下结果:
{ "status": "UP", "details": { "diskSpace": ..., "refreshScope": ..., "discoveryComposite": ..., "hystrix": { "status": "CIRCUIT_OPEN", "details": { "openCircuitBreakers": ["MovieController::findById"] } } } }
由结果可知,此时断路器已经打开,而且列出了是哪一个API的断路器被打开了。
在实际项目中,极可能须要得到形成fallback的缘由,此时可将代码修改成以下:
@HystrixCommand(fallbackMethod = "findByIdFallback") @GetMapping("/users/{id}") public User findById(@PathVariable Long id) { // 这里用到了RestTemplate的占位符能力 User user = this.restTemplate.getForObject( "http://microservice-provider-user/users/{id}", User.class, id ); // ...电影微服务的业务... return user; } public User findByIdFallback(Long id, Throwable throwable) { log.error("进入回退方法", throwable); return new User(id, "默认用户", "默认用户", 0, new BigDecimal(1)); }
http://www.itmuch.com/spring-cloud/finchley-13/