3

Spring Cloud中Hystrix仪表盘学习(笔记)

 2 years ago
source link: https://segmentfault.com/a/1190000041362060
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Spring Cloud中Hystrix仪表盘学习(笔记)

发布于 2 月 2 日

先简单介绍一下Hystrix:
Hystrix是由Netflflix开源的一个延迟和容错库,由于隔离访问远程系统、服务或者第三方库,防止级联失败,从而提升系统的可用性与容错性。
Hystrix主要通过以下几点实现延迟和容错。

包裹请求:使用HystrixCommand包裹对依赖的调用逻辑。 自动投递微服务⽅法(@HystrixCommand 添加Hystrix控制) ——调用微服务跳闸机制:当某服务的错误率超过一定的阈值时,Hystrix可以跳闸,停止请求该服务一段时间。

资源隔离:Hystrix为每个依赖都维护了一个小型的线程池(舱壁模式)(或者信号量)。如果该线程池已满, 发往该依赖的请求就被立即拒绝,而不是排队等待,从而加速失败判定。

监控:Hystrix可以近乎实时地监控运行指标和配置的变化,例如成功、失败、超时、以及被拒绝 的请求等。

回退机制:当请求失败、超时、被拒绝,或当断路器打开时,执行回退逻辑。回退逻辑由开发人员自行提供,例如返回一个缺省值。

自我修复:断路器打开一段时间后,会自动进入“半开”状态。

Hystrix Dashboard(仪表盘)是一款针对Hystrix进行实时监控的工具,通过Hystrix Dashboard可以直观地看到各Hystrix Command的请求响应时间,请求成功率等数据。在实际工作中我还没实践过,先记录一下学习笔记吧。

单体应用监控项目实践:

1、首先创建一个新的springboot工程,然后添加依赖:
<dependency>

<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
<version>2.2.7.RELEASE</version>

</dependency>

2、在启动类上添加注解:@EnableHystrixDashboard

3、项目启动后,在浏览器访问,我这边项目地址是:http://localhost:8084/hystrix,然后就可以看到Dashboard的主页了:

上面可以看到仪表盘工程已经创建好了,现在还需要有一个服务,让这个服务提供一个路径为/actuator/hystrix.stream接口,然后就可以使用Hystrix仪表盘来对该服务进行监控了。

4、创建一个新的工程项目,让其能提供/actuator/hystrix.stream接口,这里写个测试例子:
(1)创建新项目后,添加hystrix依赖:
<!--Spring Cloud 熔断器依赖-->
<dependency>

<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

</dependency>
<!--Spring Boot 服务监控检查监控依赖-->
<dependency>

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

(2)修改配置文件
同时,项目的访问入口也需要配置,配置springboot监控端点的访问权限:

这个是用来暴露endpoints的,由于endpoints中会包含很多敏感信息,除了health和info两个支持直接访问外,其他的默认不能直接访问,可以指定(hystrix.stream)或者让它都能访问(*):

SpringBoot的监控端点访问权限-指定访问

management:
endpoints:

web:
  exposure:
    exclude: hystrix.stream

(3)修改启动类
为被监控的服务的启动类添加一个Bean
@Bean
public ServletRegistrationBean hystrixMetricsStreamServlet() {

ServletRegistrationBean registration = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
registration.addUrlMappings("/actuator/hystrix.stream");
return registration;

(4)调用熔断接口:
这里我随便写了一个远程调用接口,然后设置超时导致触发熔断:
远程服务提供者代码如下:
@RequestMapping("/service/hello")
public String hello() {

try {
    Thread.sleep(4000);
} catch (InterruptedException e) {
    e.printStackTrace();
}
//进行业务处理(省略)
return "服务提供者";

本地消费者代码如下:
/**

  • hystrix超时时间是3.5秒
  • @return
    */

@RequestMapping("/web/hystrix")
@HystrixCommand(fallbackMethod="error", ignoreExceptions= RuntimeException.class, commandProperties={@HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value="3500")})
public String hystrix () {

return restTemplate.getForEntity("http://01-SPRINGCLOUD-SERVICE-PROVIDER/service/hello", String.class).getBody();

}
public String error(Throwable throwable){

System.out.println(throwable.getMessage());
return "error";

(5)访问入口:
通过路径的访问入口:http://localhost:8082/actuato...

【注意】:这里直接访问/hystrix.stream接口,会输出一连串的ping:,处理方式是先调用一下这个项目工程里的触发熔断的接口。
结果如下:

通过Dashboard的主页输入完上面3个数据后,点击Monitor Stream按钮,可以看到出现Loading

这种情况跟上面的一样,这是因为熔断服务一直没有被访问,所以没有监控的数据,也是需要调用一下这个项目工程里的触发熔断的接口。结果如下:

具体相关详细的描述:

【采坑注意】
如果访问之后,页面出现下面的提示:

需要检查控制里的信息:
(1)如果出现404,检查一下监控地址填写是否正确;
(2)如果提示is not in the allowed,表示你没有配置运行访问熔断监控页面的地址:
需要在hystrix-dashBoard配置文件中添加如下配置即可:
hystrix:
dashboard:

proxy-stream-allow-list: localhost

大致内容就这些,而且是针对单体应用的监控,后续有其它问题再补充吧。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK