Spring Cloud进阶之路 | 十二:断路器聚合监控(Turbine)

转载请注明作者及出处:

作者:银河架构师

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

前言

在上一篇文章Spring Cloud进阶之路 | 十一:断路器监控(Hystrix Dashboard)中,阐述了断路器监控组件的使用,可以有好的展示图形化的界面。

但是,微服务下拆分的服务那么多,还都是集群部署,这么多的断路器监控,一个一个查看显然是不可取的。另外,如果这些服务均部署在内网的话,外网想看也看不到。

要想解决这个问题,势必要聚合每个服务的Hystrix Dashboard数据,最好也能有好的以图形化界面展示。

而断路器聚合监控组件Hystrix Turbine将每个服务的Hystrix Dashboard数据进行整合,且如果单个服务的监控界面一样,提供友好的图形化界面。

准备工作

复用前一篇文章Spring Cloud进阶之路 | 十一:断路器监控(Hystrix Dashboard)的所有工程xmall-product、xmall-product-clients-ribbon、xmall-product-clients-feign。

创建聚合监控工程

依赖

引入依赖spring-cloud-starter-netflix-turbine,完整的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-turbine</artifactId>
    <version>0.0.1-SNAPSHOT</version><name>xmall-turbine</name>
    <description>xmall hystrix turbine dashboard</description><properties>
    </properties><dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency><dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
                </exclusion>
            </exclusions>
        </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.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>

开启聚合监控

启动类添加注解@EnableTurbine。

package com.luas.xmall.turbine;
​
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
​
@EnableTurbine
@SpringBootApplication
public class XmallTurbineApplication {
​
    public static void main(String[] args) {
        SpringApplication.run(XmallTurbineApplication.class, args);
    }
​
}

配置

在application.yml中,配置所要监控的服务列表、聚合监控配置。

server:
  port: 8888
​
turbine:
  app-config: xmall-product-clients-feign, xmall-product-clients-ribbon
  aggregator:
    cluster-config: default
  cluster-name-expression: "'default'"
  combine-host: true
#  instanceUrlSuffix:
#    default: actuator/hystrix.stream
​
​
management:
  endpoints:
    web:
      exposure:
        include: '*'
      cors:
        allowed-origins: '*'
        allowed-methods: '*'

注意:

  • 关于aggregator,使用默认配置。

  • 关于cluster-name-expression,也使用默认配置。特别注意,必须使用双引号为单引号转义。

  • 关于instanceUrlSuffix,由于G版本框架默认前缀即为actuator/hystrix.stream,所以无需特别定义。

instanceUrlSuffix默认取值逻辑见下图:

监控

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

启动工程xmall-turbine,端口为8888,注意查看控制台。

分别从服务xmall-product-clients-ribbon、xmall-product-clients-feign拉取监控信息成功,上线了这两个服务的监控信息。

访问turbine.stream,如同hystrix.stream一般,依然是一连串的ping。

访问turbine监控界面http://localhost:8888/hystrix,可以看到,同样是loading。

同样的套路,分别访问http://localhost:8082/sku/1122,http://localhost:8083/sku/1122,再次观察监控界面。

此时,界面已正常展示获取的监控数据。

再查看turbine.stream,也有了数据。

查看控制台,发现turbine框架会定时从注册中心拉取服务信息,并刷新数据。

断路器聚合监控集成成功。

源码


github

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

gitee

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

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

猜你喜欢

转载自www.cnblogs.com/luas/p/12201630.html