Hystrix Dashboard visual monitoring

Reprinted Disclaimer: The source of the article is to carry sacks of teenager

Write at the beginning

  Continued from the previous article: Spring Cloud integrates Hystrix to achieve service degradation, service fuse, and service current limit . In addition 服务降级, 服务熔断, 服务限流and other outside services, Hystrix also provided 准实时的调用监控(Hystrix Dashboard), Hystrix record will continue to perform all the information requested by Hystrix initiated and presented to the user in the form of statistical reports and graphs, including how many requests per second to perform, how successful, how many Failure etc.

  Netflix has realized the monitoring of the above indicators through the hystrix-metrics-event-stream project.同时 Spring Cloud 也提供了对 Hystrix Dashboard 的整合,将监控的内容信息转化成可视化界面。

1. Use of Hystrix Dashboard

1.1 Operating Hystrix Dashboard module

  When using Hystrix Dashboard, it needs to be run as a separate project. The following is the usage process of Hystrix Dashboard.

Ⅰ. Create project

  Use maven to create a module and name it:, cloud-consumer-hystrix-dashboard9001to complete the deployment of Hystrix Dashboard.

Ⅱ. pom.xml dependency

<!--引入 hystrix dashboard 依赖-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

 
  
  

Ⅲ. Modify application.yml configuration

# 仅定义一个端口号即可,并不需要将其注册到 Eureka 等
server:
  port: 9001

 
  
  

Ⅳ. Add @EnableHystrixDashboard annotation to the startup class

@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboardMain9001 {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(HystrixDashboardMain9001.class,args);
    }
}

 
  
  

Ⅴ. The monitored service providers need to add actuator dependencies

  The general situation is dependent with spring-boot-starter-webuse, the module is mainly used to complete 服务的健康监控. In a production environment, the availability of services needs to be monitored in real time or regularly. The actuator (health monitoring) function of Spring Boot provides a lot of interfaces required for monitoring, and can configure and view the application system and statistics on related functions.

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

 
  
  

Ⅵ. Start Hystrix Dashboard module

  Start the Hystrix Dahsboard module project, we visit http://localhost:9001/hystrix , you can see 豪猪哥the monitoring page of Hystrix Dashboard .
Insert picture description here

1.2 Operation 服务提供module

  The following is the need 服务提供模块to be configured.

Ⅰ. Configure Hystrix to specify the monitoring path

  注意:In the new version of Hystrix 需要在主启动类中指定监控路径, if there is no such operation, after the project is launched, Hystrix Dashboard will report: Unable to connect to Command Metric Streamsuch an error. The configuration content is as follows:

@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class PaymentHystrixMain8001 {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(PaymentHystrixMain8001.class,args);
    }
    //在主启动类中指定监控路径
    @Bean
    public ServletRegistrationBean getServlet(){
    
    
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}

 
  
  

Ⅱ. Start monitoring

  The 服务注册中心(7001端口), 服务提供方(8001端口), Hystrix Dashboard(9001端口)module start. Then use Hystrix Dashboard 9001 to monitor server 8001. You need to fill in the Hystrix Dashboard 监控地址: http://localhost:8001/hystrix.stream and complete the four steps in the picture:
Insert picture description here
turn on monitoring, so if the services are all started ok, you will see the following interface. Here, the configuration is OK
Insert picture description here

Ⅲ. Monitoring test

  Service http://localhost:8001/payment/circuit/{id}Interface: . When id> 0, the normal operation process is performed. When id <0, reported abnormal into the 服务降级method.

  此处配置的服务熔断规则为:Within 10s, if the failure rate of 10 requests is> 60%, the circuit breaker is turned on and the entire service is unavailable. As the request accuracy rate increases, the service will gradually resume.

//服务熔断
@HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback",commandProperties = {
    
    
        @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),  //是否开启断路器
        @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),   //请求次数
        @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),  //时间范围
        @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60"), //失败率达到多少后跳闸,此处为60%(上面配置意思是:10秒钟内,10次请求,失败率60%就不让用了)
        // 这些参数的配置,在 HystrixCommandProperties.java 类中
})
public String paymentCircuitBreaker(@PathVariable("id") Integer id){
    
    
    if (id < 0){
    
    
        throw new RuntimeException("*****id 不能负数");
    }
    String serialNumber = IdUtil.simpleUUID();
    return Thread.currentThread().getName()+"\t"+"调用成功,流水号:"+serialNumber;
}
public String paymentCircuitBreaker_fallback(@PathVariable("id") Integer id){
    
    
    return "id 不能负数,请稍候再试,(┬_┬)/~~     id: " +id;
}

 
  
  

  When we enter id> 0, the request is normal ( 断路器状态:Closed). When the input id <0, continuous transmission request 满足 10s 内,10次请求如果失败率 > 60%,此时断路器开关打开(断路器状态:Closed → Open). Now enter again id> 0, since the entire service is not available, or will enter into the 服务降级method. Send requests with id> 0 several times in a row, 此时正确率逐渐提高,服务也会逐渐恢复(断路器状态:Opend → Close)and the monitoring is shown in the figure below.
Insert picture description here

2. Hystrix Dashboard diagram

  Looking at all the content in the picture above, is there a look of bewilderment. Haha, let’s briefly introduce the contents of the monitoring page. Monitoring page can be divided 7色into: 1圈, 1线, .
Insert picture description here
Insert picture description here

The code download address of this article: The use of Hystrix Dashboard (extraction code: b07u)
 
Next article: Spring Cloud Gateway microservice new generation gateway

Guess you like

Origin blog.csdn.net/m0_37989980/article/details/108508173