14Ribbon整合断路器监控Hystrix-Dashboard

4次阅读

共计 3903 个字符,预计需要花费 10 分钟才能阅读完成。

公众号:java 乐园

上一篇只是大概介绍了一下断路器 Hystrix Dashboard 监控,如何使用 Hystrix Dashboard 监控微服务的状态呢?这篇看看 Ribbon 如何整合断路器监控 Hystrix Dashboard。今天的项目主要整合 sc-eureka-client-consumer-ribbon-hystrix 项目和 sc-hystrix-dashboard 项目

1、新建项目 sc-ribbon-hystrix-dashboard,对应的 pom.xml 文件

<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>spring-cloud</groupId>
  <artifactId>sc-ribbon-hystrix-dashboard</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <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-ribbon</artifactId> 
                <version>1.4.5.RELEASE</version> 
            </dependency> -->

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>

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


        <!--         
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
            <version>1.4.5.RELEASE</version>
        </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-hystrix-dashboard</artifactId>
            <version>1.4.5.RELEASE</version>
        </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>

    </dependencies>
</project>

可以看到这个 pom.xml 文件是 sc-eureka-client-consumer-ribbon-hstrix 项目和 sc-hystrix-dashboard 项目的 pom.xml 的并集

2、新建 spring boot 启动类 HystrixDashboardApplication.java

package sc.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

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

}

可以看到这个启动类的注解也是 sc-eureka-client-consumer-ribbon-hstrix 项目和 sc-hystrix-dashboard 项目的启动类的并集

3、其他的 java 类就不多说了,项目接口如下

4、分别启动配置中心和 sc-eureka-server 和服务提供者 sc-eureka-client-provider

5、启动项目 sc-ribbon-hystrix-dashboard
方式一:http://127.0.0.1:5600/hystrix 验证是否启动成功

方式二:访问 http://127.0.0.1:5600/hystrix.stream

这个是访问 DashboardServletConfig.java 这个 servlet 对应的地址
上篇说了一下下图这段英文的大概意思

6、演示一下单应用模式下的断路器监控 Hystrix Dashboard
在 url 里输入:http://127.0.0.1:5600/hystrix.stream(IP 和端口对应服务消费者的 IP 和端口)

然后点击 Monitor Stream 按钮

7、使用 postman 访问任何接口,以获取用户列表为例(http://127.0.0.1:5600/cli/user/listUser),多访问几次在查看对应的监控界面

发现监控界面的图出现了动态变化,这个就是对服务调用的监控。图中相关单元的含义可以查看:
https://github.com/Netflix-Sk…

http://127.0.0.1:5600/hystrix.stream 对应的界面也出现了大量数据,这个也是对服务调用监控的数据。

其他接口也自行测试。

正文完
 0