接上篇《18.Hystrix的commandProperties配置》 Spring Cloud版本为Finchley.SR2版
上一篇我们简单介绍了Hystrix的Command Properties配置,以及线程隔离策略与传播上下文的相关知识。本篇我们来继续学习Hystrix的健康指数监控以及指标流。
本部分官方文档:https://cloud.spring.io/spring-cloud-static/Finchley.SR2/single/spring-cloud.html#_health_indicator_4
一、Health Indicator健康指数监控
当使用Hystrix时,项目可以通过“/actuator/health”服务查看相关项目的健康指数,健康指数数据类似如下:
{
"hystrix": {
"openCircuitBreakers": [
"StoreIntegration::getStoresByLocationLink"
],
"status": "CIRCUIT_OPEN"
},
"status": "UP"
}
该服务是spring-boot的actuator提供的,所以使用这个服务首先我们要确保引入了actuator组件:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
然后需要确保配置文件的management.endpoint.health.show-details 详细信息显示的值为always(总是显示),management.endpoints.web.exposure.include配置暴露所有端点:
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include:
"*"
exclude:
-
然后我们访问“/actuator/health”,就可以看到当前应用的健康状态,以及断路器的开启状态。
我们下面在之前的实例上做一下测试,回顾一下之前在movie做的使用Hystrix断路器,并配置了降级方法的服务:
@RestController
public class MovieController {
//上面代码省略...
@Autowired
private UserFeignClient userFeignClient;
@GetMapping("/movie/{id}")
@HystrixCommand(fallbackMethod = "findUserByIdFallback")
public User findUserById(@PathVariable Long id){
return userFeignClient.findById(id);
}
public User findUserByIdFallback(){
User user = new User();
user.setId(0L);
return user;
}
//下面代码省略...
}
该服务是用来访问user微服务的findById服务的。
这里我们启动movie、user以及Eureka Server注册中心:
然后我们直接访问“/movie/1”是没有问题的:
此时我们访问movie模块的“/actuator/health”服务:
可以看到,此时我们的Hystrix的状态是UP的,即启动状态。
此时我们关闭user服务,再次请求“/movie/1”,发现已经进入降级方法中,返回了id为0的数据:
说明断路器起了作用,我们持续快速的多次访问“/movie/1”,来触发失败率达到阈值。一段时间后我们再次访问“/actuator/health”服务:
可以看到,Hystrix的状态变为了:
"hystrix":
{
"status":"CIRCUIT_OPEN",
"details":{
"openCircuitBreakers":["MovieController::findUserById"]
}
}
可以看到,Hystrix目前的状态为“CIRCUIT_OPEN”,即“电路开放”的意思,其实可以理解为断路器开启,然后在details显示了具体开启了断路器的服务是哪一个,这里可以看到是我们的“MovieController”类下的“findUserById”方法。
那么我们为什么能通过health服务获取到Hystrix的状态信息呢?这是因为Hystrix利用了spring-boot的actuator组件将Hystrix的相关数据暴露给了health端点。
Spring boot的健康信息都是从ApplicationContext中的各种HealthIndicator
Beans中收集到的,Spring boot框架中包含了大量的HealthIndicators的实现类,当然你也可以实现自己认为的健康状态。自定义的健康状态检查信息,可以通过实现HealthIndicator的接口来实现。
很显然,Hystrix组件一定编写了一个实现了HealthIndicator接口的服务来暴露自己的健康信息。
所以我们通过spring-boot的actuator组件提供的health服务,就可以轻松的查看系统断路器开启的情况,以便于我们及时排查相关服务端点的异常。
二、Metrics Stream指标流
Hystrix的Metrics流,实际上是从来监控当前API的情况(例如各个服务的情况、线程情况、断路器情况)。要使用Metrics服务,同样需要引入spring-boot-start-actuator的依赖,并设置management.endpoints.web.exposure.include: hystrix.stream。我们在启动的工程中访问/actuator/hystrix.stream即可看到当前系统的API情况(拿Movie工程举例):
这里我们可以注意到,网页在不停的实时打出新的API监控信息。我们分析一下API监控信息的具体数据:
{
"type": "HystrixCommand",
"name": "findUserById",
"group": "MovieController",
"currentTime": 1569925167121,
"isCircuitBreakerOpen": false,
"errorPercentage": 0,
"errorCount": 0,
"requestCount": 0,
"rollingCountBadRequests": 0,
"rollingCountCollapsedRequests": 0,
"rollingCountEmit": 0,
"rollingCountExceptionsThrown": 0,
"rollingCountFailure": 0,
"rollingCountFallbackEmit": 0,
"rollingCountFallbackFailure": 0,
"rollingCountFallbackMissing": 0,
"rollingCountFallbackRejection": 0,
"rollingCountFallbackSuccess": 0,
"rollingCountResponsesFromCache": 0,
"rollingCountSemaphoreRejected": 0,
"rollingCountShortCircuited": 0,
"rollingCountSuccess": 0,
"rollingCountThreadPoolRejected": 0,
"rollingCountTimeout": 0,
"currentConcurrentExecutionCount": 0,
"rollingMaxConcurrentExecutionCount": 0,
"latencyExecute_mean": 0,
"latencyExecute": {
"0": 0,
"25": 0,
"50": 0,
"75": 0,
"90": 0,
"95": 0,
"99": 0,
"99.5": 0,
"100": 0
},
"latencyTotal_mean": 0,
"latencyTotal": {
"0": 0,
"25": 0,
"50": 0,
"75": 0,
"90": 0,
"95": 0,
"99": 0,
"99.5": 0,
"100": 0
},
"propertyValue_circuitBreakerRequestVolumeThreshold": 20,
"propertyValue_circuitBreakerSleepWindowInMilliseconds": 5000,
"propertyValue_circuitBreakerErrorThresholdPercentage": 50,
"propertyValue_circuitBreakerForceOpen": false,
"propertyValue_circuitBreakerForceClosed": false,
"propertyValue_circuitBreakerEnabled": true,
"propertyValue_executionIsolationStrategy": "SEMAPHORE",
"propertyValue_executionIsolationThreadTimeoutInMilliseconds": 5000,
"propertyValue_executionTimeoutInMilliseconds": 5000,
"propertyValue_executionIsolationThreadInterruptOnTimeout": true,
"propertyValue_executionIsolationThreadPoolKeyOverride": null,
"propertyValue_executionIsolationSemaphoreMaxConcurrentRequests": 10,
"propertyValue_fallbackIsolationSemaphoreMaxConcurrentRequests": 10,
"propertyValue_metricsRollingStatisticalWindowInMilliseconds": 10000,
"propertyValue_requestCacheEnabled": true,
"propertyValue_requestLogEnabled": true,
"reportingHosts": 1,
"threadPool": "MovieController"
}
相关字段的解释如下:
对于我们来说,使用/actuator/hystrix.stream端点获取的API监控信息是一堆JSON信息,并不好阅读,而Spring Cloud给我们提供了“Hystrix Dashboard”图形化监控看板,可以让我们更轻松的获取应用的API状态信息,这个我们后面会讲到。
参考:《51CTO学院Spring Cloud高级视频》
https://blog.csdn.net/makyan/article/details/88664949
https://www.jianshu.com/p/1aadc4c85f51
https://my.oschina.net/xiaominmin/blog/1789084
https://blog.csdn.net/ruihin/article/details/77579794
转载请注明出处:https://blog.csdn.net/acmman/article/details/101863588