zoukankan      html  css  js  c++  java
  • Spring Cloud进阶之路 | 十一:断路器监控(Hystrix Dashboard)

    转载请注明作者及出处:

    作者:银河架构师

    原文链接:https://www.cnblogs.com/luas/p/12201599.html

    前言

    Spring Cloud进阶之路 | 六:断路器(Hystrix)一文中,介绍了服务熔断降级及断路器组件。可是,这些数据终归看不到摸不着,不好监控。

    所以,需要一个能针对断路器进行监控的组件,即本文要介绍的Hystrix Dashboard断路器监控组件,该组件针对各断路器的数据,提供友好的图形化界面。

    准备工作

    复用Spring Cloud进阶之路 | 六:断路器(Hystrix)文章中的所有工程:xmall-product、xmall-product-clients-ribbon、xmall-product-clients-feign。

    ribbon方式断路器监控


    改造xmall-product-clients-ribbon工程,添加相关依赖及配置。

    依赖改造

    添加依赖spring-boot-starter-actuator、spring-cloud-starter-netflix-hystrix-dashboard。修改后的pom文件如下。

    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion><parent>
            <groupId>com.luas.cloud</groupId>
            <artifactId>java-boot-parent-2.1</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <relativePath>../../java-boot-parent-2.1</relativePath>
        </parent><groupId>com.luas.xmall</groupId>
        <artifactId>xmall-product-clients-ribbon</artifactId>
        <version>0.0.1-SNAPSHOT</version><name>xmall-product-clients-ribbon</name>
        <description>product service clients by ribbon</description><properties>
            <java.version>1.8</java.version>
        </properties><dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency><!-- nacos cloud -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            </dependency><dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            </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-actuator</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-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies><build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build></project>

    注意:一定要添加spring-boot-starter-actuator依赖,不然不会创建/actuator/hystrix.stream端点。

    开启断路器监控

    启动类添加@EnableHystrixDashboard注解,开启断路器监控组件。

    package com.luas.xmall.product.clients;
    ​
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.cloud.netflix.hystrix.EnableHystrix;
    import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;
    ​
    @EnableHystrix
    @EnableHystrixDashboard
    @SpringBootApplication
    public class XmallProductClientsRibbonApplication {
    ​
        public static void main(String[] args) {
            SpringApplication.run(XmallProductClientsRibbonApplication.class, args);
        }
    ​
        @Bean
        @LoadBalanced
        public RestTemplate restTemplate() {
            return new RestTemplate();
        }
    ​
    }

    监控

    启动工程xmall-product、xmall-product-clients-ribbon,端口分别为8080、8082。

    如配置无误,控制台则会打印/actuator/hystrix.stream端点注册成功信息。

    访问http://localhost:8082/actuator/hystrix.stream,如果配置正确,会出现一连串的ping。

    访问http://localhost:8082/hystrix,输入相关参数如下:

    点击Monitor Stream,即可开启断路器监控界面。

    可以看到,两处监控均出现loading。不慌,这是因为没有数据。

    访问http://localhost:8082/sku/1122,正常出现商品信息。

    此时,再观察hystrix.stream,已经出现了相关数据。

    hystrix监控界面已正常展示图形化界面。

    控制台也显示了连接成功信息。

    feign方式断路器监控

    依赖改造

    添加依赖spring-boot-starter-actuator、spring-cloud-starter-netflix-hystrix-dashboard。修改后的pom文件如下。

    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion><parent>
            <groupId>com.luas.cloud</groupId>
            <artifactId>java-boot-parent-2.1</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <relativePath>../../java-boot-parent-2.1</relativePath>
        </parent><groupId>com.luas.xmall</groupId>
        <artifactId>xmall-product-clients-feign</artifactId>
        <version>0.0.1-SNAPSHOT</version><name>xmall-product-clients-feign</name>
        <description>product service clients by ribbon</description><properties>
            <java.version>1.8</java.version>
        </properties><dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency><!-- nacos cloud -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            </dependency><dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            </dependency><dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency><dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</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-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies><build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build></project>

    注意:一定要添加spring-boot-starter-actuator依赖,不然不会创建/actuator/hystrix.stream端点。

    开启断路器监控

    启动类添加注解@EnableCircuitBreaker、@EnableHystrixDashboard。

    package com.luas.xmall.product.clients;
    ​
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
    import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    ​
    @EnableCircuitBreaker
    @EnableHystrixDashboard
    @EnableFeignClients("com.luas.xmall.product.clients.clients")
    @SpringBootApplication
    public class XmallProductClientsFeignApplication {
    ​
        public static void main(String[] args) {
            SpringApplication.run(XmallProductClientsFeignApplication.class, args);
        }
    ​
    }

    注意:虽然feign集成了hystrix,可以手动通过配置开启断路器,但是,想要开启断路器相关监控功能,还需添加@EnableCircuitBreaker注解。

    监控

    启动工程xmall-product、xmall-product-clients-feign,端口分别为8080、8083。

    如配置无误,控制台则会打印/actuator/hystrix.stream端点注册成功信息。

    访问http://localhost:8083/actuator/hystrix.stream,如果配置正确,依然会出现一连串的ping。

    访问http://localhost:8083/hystrix,将监控地址变更为http://localhost:8083/actuator/hystrix.stream,点击Monitor Stream,开启新的断路器监控界面。

    可以看到,依然是两处监控均出现loading,还是因为没有数据。

    访问http://localhost:8083/sku/1122,正常出现商品信息。然后,再观察hystrix.stream,也已经出现了相关数据。

    此时,hystrix监控界面已正常展示图形化界面。

    控制台也显示了连接成功信息。

    到此,断路器监控组件集成完成。

    监控指标

    断路器监控指标含义如下图所示。

    此图是基于ribbon方式的断路器监控,feign方式的与此一模一样,唯一不同的,就是短路点名称格式。ribbon方式直接为方法名,而feign方式既有服务名,又有参数类型,比较详细。

    源码


    github

    https://github.com/liuminglei/SpringCloudLearning/tree/master/11/

    gitee

    https://gitee.com/xbd521/SpringCloudLearning/tree/master/11/

    微信搜索【银河架构师】,发现更多精彩内容。

    技术资料领取方法:关注公众号,回复微服务,领取微服务相关电子书;回复MK精讲,领取MK精讲系列电子书;回复JAVA 进阶,领取JAVA进阶知识相关电子书;回复JAVA面试,领取JAVA面试相关电子书,回复JAVA WEB领取JAVA WEB相关电子书。

  • 相关阅读:
    数据结构和算法(Golang实现)(13)常见数据结构-可变长数组
    数据结构和算法(Golang实现)(5)简单入门Golang-接口
    数据结构和算法(Golang实现)(3)简单入门Golang-流程控制语句
    数据结构和算法(Golang实现)(4)简单入门Golang-结构体和方法
    分库分布的几件小事(一)数据库如何拆分
    分布式的几件小事(十二)分布式事务
    分布式的几件小事(十一)分布式session如何实现
    分布式的几件小事(十)分布式锁是啥
    分布式的几件小事(九)zookeeper都有哪些使用场景
    分布式的几件小事(八)分布式服务接口请求的顺序性如何保证
  • 原文地址:https://www.cnblogs.com/luas/p/12201599.html
Copyright © 2011-2022 走看看