六、Hystrix详解三:Hystrix的健康监测

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/makyan/article/details/88664949

5.3. Hystrix的健康监测


1、添加依赖


<!--添加认证监控-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2、浏览器访问 http://192.168.215.2:8908/actuator/health
就能显示Hystrix开启状态信息。
有人说,Spring boot 2.x 使用了endpoint,导致/actuator/health,/actuator/hystrix.stream都无法访问,报404错误。
我用的Spring Cloud 版本是Finchley.RELEASE,但是我没遇上,如果有人遇上,可以试试以下三种方法。而我将以下三种方法注释掉,一样能显示Hystrix的/actuator/health,/actuator/hystrix.stream信息


设置让endpoint允许显示/actuator/health,/actuator/hystrix.stream信息的方法如下:
方式一:在application.yml中添加如下配置:


management:
  endpoint:
      health:    
        show-details: always   #允许显示/actuator/health信息

management:
  endpoints:
    web:
      exposure:
        include: ["hystrix-stream"]   #允许显示/actuator/hystrix.stream信息

management:
  endpoints:
    web:
      exposure:
        include: '*'   #允许显示所有信息,包括/actuator/health,/actuator/hystrix.stream

解决方式二:在Hystrix监控项目的启动类中添加方法,设置允许显示的信息


 /**
 * 配置Hystrix ,使其显示健康监测信息
 * @return
 */
@Bean
public ServletRegistrationBean getServlet() {
    HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
    ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
    registrationBean.setLoadOnStartup(1);
    registrationBean.addUrlMappings("/actuator/health","/actuator/hystrix.stream");
    registrationBean.setName("HystrixMetricsStreamServlet");
    return registrationBean;
}

解决方式三:在application.yml中打开Feign 的Hystrix

*************************************************************
#开启hystrix配置
feign:
  hystrix:
    enabled: true

5.4. Hystrix-DashBoard的使用
即前面说的/hystrix.stream 详细信息的图形化。


创建一个项目futurecloud-hystrix-dashboard
1、引入依赖


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>futurecloud-master</artifactId>
        <groupId>com.futurecloud</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>futurecloud-hystrix-dashboard</artifactId>
    <name>futurecloud-hystrix-dashboard</name>
    <dependencies>
        <!--将此项目变为web项目-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
        <!--添加hystrix dashboard依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>
    </dependencies>
</project>

2、在主函数上添加配置@EnableHystrixDashboard,开启Hystrix-DashBoard


package com.futurecloud.hystrix.dashboard;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
@EnableHystrixDashboard //开启Hystrix-DashBoard
public class FuturecloudHystrixDashBoardApplication
{
    public static void main( String[] args )
    {
        SpringApplication.run(FuturecloudHystrixDashBoardApplication.class,args);
    }
}

3、配置application.yml,只需要给项目一个端口即可


server:
  port: 9999

4、输入:http://localhost:9999/hystrix
5、在Hystrix-DashBoard界面输入要监控的地址,
地址格式:http://host + port + /actuator/hystrix.stream,
例如:http://localhost:8908/actuator/hystrix.stream,实现对该地址的监控。
在这里插入图片描述


猜你喜欢

转载自blog.csdn.net/makyan/article/details/88664949