摘要

Hystrix Dashboard 是Spring Cloud中查看Hystrix实例执行情况的一种仪表盘组件,支持查看单个实例和查看集群实例。Hystrix提供了Hystrix Dashboard来实时监控HystrixCommand方法的执行情况。 Hystrix Dashboard可以有效地反映出每个Hystrix实例的运行情况,帮助我们快速发现系统中的问题,从而采取对应措施。

1. 创建一个hystrix-dashboard 服务

springboothelper插件 激活码 springboard 插件_spring cloud

 

springboothelper插件 激活码 springboard 插件_Cloud_02

 

springboothelper插件 激活码 springboard 插件_java_03

1.1 pom.xml新增 dashboard 依赖

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- 包含了web -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
</dependencies>

 1.2 启动类新增 @EnableCircuitBreaker 和 @EnableDiscoveryClient

package com.zqh.www;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.core.env.Environment;

/**
 * 开启服务发现客户端
 *
 * @EnableEurekaClient只适用于Eureka作为注册中心,@EnableDiscoveryClient 可以是其他注册中心。
 */
@EnableDiscoveryClient
/**
 * 开启监控
 */
@EnableHystrixDashboard
@SpringBootApplication
public class HystrixDashboardApplication {

    private final static Logger logger = LoggerFactory.getLogger(HystrixDashboardApplication.class);

    public static void main(String[] args) {
        Environment env = SpringApplication.run(HystrixDashboardApplication.class, args).getEnvironment();
        logger.info(
                "\n----------------------------------------------------------\n\t"
                        + "Application '{}' is running! Access URLs:\n\t"
                        + "Local: \t\thttp://localhost:{}{}"
                        + "\n----------------------------------------------------------",
                env.getProperty("spring.application.name"), env.getProperty("server.port"),
                env.getProperty("server.servlet.context-path") != null ? env.getProperty("server.servlet.context-path") : "");
    }
}

1.3 yml配置

server:
  port: 8089
spring:
  application:
    name: hystrix-dashboard
eureka:
  client:
    service-url:
      #注册地址
      defaultZone: http://root:root@localhost:8081/eureka/
  #显示服务器IP加端口
  instance:
    hostname: localhost
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}

# is not in the allowed list of proxy host names.  If it should be allowed add it to hystrix.dashboard.proxyStreamAllowList.
hystrix:
  dashboard:
    proxy-stream-allow-list: "localhost"

1.4 启动服务

访问:点击访问Dashboard服务:http://localhost:8089/hystrix

注意:因为我们使用的不是https.所以我们填写的时候是填http

springboothelper插件 激活码 springboard 插件_Cloud_04

1.5 在hystrix-service服务中的HystrixServiceApplication添加监听

注意:是在hystrix-service服务

package com.zqh.www;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

/**
 * 开启断路器功能
 */
@EnableCircuitBreaker
/**
 * 开启服务发现客户端
 * @EnableEurekaClient只适用于Eureka作为注册中心,@EnableDiscoveryClient 可以是其他注册中心。
 */
@EnableDiscoveryClient
@SpringBootApplication
/**
 * 可以取代上面三个
 */
@SpringCloudApplication
public class HystrixServiceApplication {

    private final static Logger logger = LoggerFactory.getLogger(HystrixServiceApplication.class);

    public static void main(String[] args) {
        Environment env = SpringApplication.run(HystrixServiceApplication.class, args).getEnvironment();
        logger.info(
                "\n----------------------------------------------------------\n\t"
                        + "Application '{}' is running! Access URLs:\n\t"
                        + "Local: \t\thttp://localhost:{}{}"
                        + "\n----------------------------------------------------------",
                env.getProperty("spring.application.name"), env.getProperty("server.port"),
                env.getProperty("server.servlet.context-path") != null ? env.getProperty("server.servlet.context-path") : "");
    }

    /**
     * 解决问题:Dashboard显示Unable to connect to Command Metric Stream。
     *
     * @return
     */
    @Bean
    public ServletRegistrationBean getServlet() {
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/actuator/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}

1.6 填写Hystrix Dashboard页面内容

注意:
第一行参数是填写需要监听的服务链接

第二行参数是填写服务名,可以随便填,用来只是一个名字,建议于需要监听的服务名称相同

springboothelper插件 激活码 springboard 插件_spring cloud_05

 

springboothelper插件 激活码 springboard 插件_spring_06

2.Hystrix Dashboard 图表解析

springboothelper插件 激活码 springboard 插件_java_07

springboothelper插件 激活码 springboard 插件_java_08

3.Hystrix 集群实例监控

如果服务集群了怎么办呢?Turbine可以聚合hystrix-service服务的监控信息,然后我们的hystrix-dashboard服务就可以从Turbine获取聚合好的监控信息。

3.1 创建一个turbine-service模块

springboothelper插件 激活码 springboard 插件_spring cloud

 

springboothelper插件 激活码 springboard 插件_spring_10

 

springboothelper插件 激活码 springboard 插件_spring cloud_11

3.2 pom.xml新增 turbine依赖

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

 3.3 启动类新增 @EnableTurbine和 @EnableDiscoveryClient

package com.zqh.www;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
import org.springframework.core.env.Environment;

/**
 * 开启服务发现客户端
 *
 * @EnableEurekaClient只适用于Eureka作为注册中心,@EnableDiscoveryClient 可以是其他注册中心。
 */
@EnableDiscoveryClient
/**
 * 开启监控
 */
@EnableTurbine
@SpringBootApplication
public class TurbineHystrixApplication {

    private final static Logger logger = LoggerFactory.getLogger(TurbineHystrixApplication.class);

    public static void main(String[] args) {
        Environment env = SpringApplication.run(TurbineHystrixApplication.class, args).getEnvironment();
        logger.info(
                "\n----------------------------------------------------------\n\t"
                        + "Application '{}' is running! Access URLs:\n\t"
                        + "Local: \t\thttp://localhost:{}{}"
                        + "\n----------------------------------------------------------",
                env.getProperty("spring.application.name"), env.getProperty("server.port"),
                env.getProperty("server.servlet.context-path") != null ? env.getProperty("server.servlet.context-path") : "");
    }
}

3.4 yml配置

server:
  port: 8090
spring:
  application:
    name: turbine-service
eureka:
  client:
    service-url:
      #注册地址
      defaultZone: http://root:root@localhost:8081/eureka/
  #显示服务器IP加端口
  instance:
    hostname: localhost
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}

turbine:
  # 指定需要收集信息的服务名称
  app-config: hystrix-service
  # 指定服务所属集群
  cluster-name-expression: new String('default')
  # 以主机名和端口号区分服务
  combine-host-port: true

3.5 启动服务

注意:既然是服务集群,需要相同的服务开启多个,我们监控的服务是hystrix-service,所以我们需要开启2个或多个hystrix-service

springboothelper插件 激活码 springboard 插件_spring cloud_12

 

springboothelper插件 激活码 springboard 插件_java_13

访问:点击访问Dashboard服务:http://localhost:8089/hystrix 

重新填写第一行参数:注意是填写我们刚刚创建的turbine-service访问地址:http://localhost:8090/turbine.stream

springboothelper插件 激活码 springboard 插件_spring_14


springboothelper插件 激活码 springboard 插件_微服务_15

集群中的数量就会变成2,原本我们是第一行参数是直接指向hystrix-service的,后面是我们加了一个turbine-service的服务来收集指定服务的信息,第一行参数改为turbine-service

4.总结

  1. 使用 hystrix-dashboard 对 hystrix进行监控
  2. 使用 turbine 对 dashboard 信息收集,然后统一进行管理