springCloud Finchley 实战入门(基于springBoot 2.0.3)【六 Hystrix 仪表盘】

Hystrix仪表盘

通过上一篇我们已经成功的实现了spring cloud对Hystrix的整合了。除此之外,spring cloud还完美的整合了Hystrix的仪表盘组件Hystrix Dashboard。该组件主要是用来实时监控Hystrix的各项指示信息的。通过Hystrix Dashboard反馈的信息,可以帮助我们快速的发现系统中存在的问题,从而即使采取应对方法。

快速入门(单实例监控)

先演示针对单个应用的监控

创建一个module命名为"eureka-hystrix-dashboard",创建时选择对应的组件


springCloud Finchley 实战入门(基于springBoot 2.0.3)【六 Hystrix 仪表盘】
1532502505125.png

创建成功后,我们还需要在pom.xml中添加actuator的依赖:

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>eureka-hystrix-dashboard</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-hystrix-dashboard</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

application.yml配置:

spring:
  application:
    name: hystrix-dashboard
server:
  port: 2001
eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1:8762/eureka/,http://peer2:8763/eureka/

在主类"EurekaHystrixDashboardApplication",添加@EnableHystrixDashboard注解,表示开启Hystrix仪表盘监控

@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrixDashboard
public class EurekaHystrixDashboardApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaHystrixDashboardApplication.class, args);
    }
}

配置完成启动项目后,访问地址:http://localhost:2001/hystrix
如果看也下面的页面就说明项目正常的:

springCloud Finchley 实战入门(基于springBoot 2.0.3)【六 Hystrix 仪表盘】
1532502951844.png

通过 Hystrix Dashboard 主页面的文字介绍,我们可以知道,Hystrix Dashboard 共支持三种不同的监控方式:

默认的集群监控:通过 URL:http://turbine-hostname:port/turbine.stream 开启,实现对默认集群的监控。
指定的集群监控:通过 URL:http://turbine-hostname:port/turbine.stream?cluster=[clusterName] 开启,实现对 clusterName 集群的监控。
单体应用的监控: 通过 URL:http://hystrix-app:port/hystrix.stream 开启 ,实现对具体某个服务实例的监控。(现在这里的 URL 应该为 http://hystrix-app:port/actuator/hystrix.stream,Actuator 2.x 以后endpoints 全部在/actuator下,可以通过management.endpoints.web.base-path修改)。

前两者都对集群的监控,需要整合 Turbine 才能实现。

因为我们目前先做实现对单体应用的监控,所以这里的单体应用就用使用 Hystrix 实现的服务提供者"eureka-bussniss-service-user-client-ribbon"

页面上的另外两个参数:
Delay:控制服务器上轮询监控信息的延迟时间,默认为 2000 毫秒,可以通过配置该属性来降低客户端的网络和 CPU 消耗。
Title:该参数可以展示合适的标题。

配置服务提供者

Hystrix Dashboard 监控单实例节点需要通过访问实例的/actuator/hystrix.stream接口来实现,所以我们需要为服务提供者做一下配置。 在"eureka-bussniss-service-user-client-ribbon"项目的pom文件添加autuator和hystrix的依赖。

 <!--添加Hystrix依赖 断路器容错保护-->
 <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
 </dependency>
 <!--监控中心-->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
 </dependency>

修改application.yml配置
添加management.endpoints.web.exposure.include=hystrix.stream属性,表示暴露hystrix.stream的监控点地址。

spring:
  application:
    name: service-user-ribbon
server:
  port: 8901
eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1:8762/eureka/,http://peer2:8763/eureka/
management:
  endpoints:
    web:
      exposure:
        include: hystrix.stream

接着在主类名"EurekaBussnissServiceUserClientRibbonApplication"添加@EnableCircuitBreaker 表示启动Hystrix仪表盘 服务熔断(ribbon 单服务实例监控)

@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
@EnableCircuitBreaker //Hystrix仪表盘 服务熔断(ribbon 单服务实例监控)
public class EurekaBussnissServiceUserClientRibbonApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaBussnissServiceUserClientRibbonApplication.class, args);
    }

    @Bean
    @LoadBalanced //开启客户端负载均衡
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

重启"eureka-bussniss-service-user-client-ribbon"项目。


springCloud Finchley 实战入门(基于springBoot 2.0.3)【六 Hystrix 仪表盘】
1532504332432.png

在2001项目页面中输入"http://localhost:8901/actuator/hystrix.stream",点击"Monitor Stream ",注意:(spring boot 2.x)监控地址需要加上actuator的路径。看到下图,显示loding...

springCloud Finchley 实战入门(基于springBoot 2.0.3)【六 Hystrix 仪表盘】
1532504462251.png

一直显示loding...这是因为还没有检测到请求,我们访问一下地址:http://localhost:8901/listUsersByRibbon,然后就可以看到下面的信息了。

springCloud Finchley 实战入门(基于springBoot 2.0.3)【六 Hystrix 仪表盘】
1532504537311.png

页面上面的信息就是显示服务的具体情况,具体的信息可以去spring cloud的官网深入了解。这里就不过多解释了。
到这里我们就已经实现了Hystrix dashboard对单个应用的仪表监控了。

快速入门 (集群监控)

创建一个新的module命名为"eureka-hystrix-dashboard-turbine"。创建时选择对应的组件依赖


springCloud Finchley 实战入门(基于springBoot 2.0.3)【六 Hystrix 仪表盘】
1532505439252.png

pom.xml如下:

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>eureka-hystrix-dashboard-turbine</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-hystrix-dashboard-turbine</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

application.yml配置如下:

spring:
  application:
    name: turbine
server:
  port: 8989
management:
  server:
    port: 8990
eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1:8762/eureka/,http://peer2:8763/eureka/
turbine:
  appConfig: service-user-ribbon
  clusterNameExpression: new String("default")
  combineHostPort: true

turbine.appConfig表示监控的服务实例(通过serviceId;多个服务实例以逗号分隔)

项目的主类名EurekaHystrixDashboardTurbineApplication添加@EnableTurbine注解

@SpringBootApplication
@EnableTurbine
@EnableEurekaClient
public class EurekaHystrixDashboardTurbineApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaHystrixDashboardTurbineApplication.class, args);
    }
}

然后service-user项目pom添加hystrix依赖以及主类添加@EnableHystrix@EnableCircuitBreaker注解。

@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
@EnableCircuitBreaker //Hystrix仪表盘 服务熔断(ribbon 单服务实例监控)
public class EurekaBussnissServiceUserApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaBussnissServiceUserApplication.class, args);
    }
}

pom.xml

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>eureka-bussniss-service-user</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-bussniss-service-user</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

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

        <!--添加Hystrix依赖 断路器容错保护-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>    <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

重启相关的服务。同样是在2001页面输入对应的地址http://localhost:8989/turbine.stream 点击Monitor Stream 我们也同样是可以实现对服务实例的监控。

springCloud Finchley 实战入门(基于springBoot 2.0.3)【六 Hystrix 仪表盘】
1532507114614.png

springCloud Finchley 实战入门(基于springBoot 2.0.3)【六 Hystrix 仪表盘】
1532507184545.png

如果aplication.yml中的appConfig是配置了多个的,就会对显示相应的仪表盘数据。

github 项目源码

到这里Hystrix仪表盘监控服务实例就基本实现了。接下来我们会用Fegin组件以声明式服务调用的方式实现Ribbon+Hystrix(负载均衡+服务容错保护)的功能。

上一篇:springCloud Finchley 实战入门(基于springBoot 2.0.3)【二 服务治理 Eureka】


下一篇:springCloud Finchley 实战入门(基于springBoot 2.0.3)【七 Fegin 声明式服务调用】