微服务设计指导-使用turbine结合nacos监控多集群hystrix

首先,你要确保每一个hystrix客户端全部的设置如这一篇中所描述的跑通:微服务设计指导-hystrix的监控。且每一个hystrix的配置都是如这篇所讲的那几步保留不动。

然后开始我们使用一个组件叫turbine的东西,来集成各个集群内的hystrix端。

turbine收集hystrix的原理如下:

其于以上原理,我们一般会在一个系统里制作一个单独的Turbine的Spring Boot Application。

maven-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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.sky.demo.timeout</groupId>
        <artifactId>TimeoutServiceDemo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>TurbineDemo</artifactId>
    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</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.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
        <!-- hystrix必备 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- hystrix必备 -->
        <!-- 使用Turbine进行多个集群的hystrix dashboard的聚合,记得排掉默认用的eureka-client的包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</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>
        <!-- 使用Turbine进行多个集群的hystrix dashboard的聚合,记得排掉默认用的eureka-client的包 -->
        <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-log4j2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
 
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.ow2.asm</groupId>
                    <artifactId>asm</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>
    </dependencies>
</project>

 

可以看到,它和标准的hystrix端所要依赖差不多,它也要actuator,只是它多了:

  • spring-cloud-starter-netflix-turbine这一项,由于turbine默认用的是eurka作服务自动注册与发现的,因此我们在此例中使用的是nacos,应必须把eureka给exclude掉。

application.yml文件

server:
  port: 9992
spring:
  application:
    name: TurbineDemo
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
feign:
  hystrix:
    enabled: true
hystrix:
   dashboard:
      proxy-stream-allow-list: "localhost" #hystrix dashboard用,如果不加这条hystrix启动后也会出现hystrix.dashboard.proxyStreamAllowList
turbine:
   app-config: AdvancedIndex,AdvancedIndex2
   aggregator:
     cluster-config: default
   cluster-name-expression:  new String('default')
   combine-host-port: true
management: #hystrix dashboard用,actuator暴露端口
  endpoints:
    web:
      exposure:
        include: "hystrix.stream,turbine.stream"
      cors:
        allowed-origins: "*"
        allowed-methods: "*"

可以看到,大体上差不多,但是有几项,需要高度注意,这是网上的那些错误的例子所不具备的知识:

  • turbine.app-config,这后面是一个以逗号分隔的需要被并且可以在服务注册中心里的hystrix端的spring.aplication.name;
  • turbine.aggregator.cluster-config,你可以保持为default,该功能是用来聚合多个“服务群”的即指定聚合哪些集群,多个使用","分割,默认为default;
  • turbine.aggregator.cluster-name-expression后的值特别的有意思,如果是在eureka里还真不会碰到问题如果你打一个“default“,但是在nacos里,你必须写成new String('default'),否则直接会报错,这是让turbine保持在服务自动注册发现的“domain namespace”去搜索那些hystrix用的;
  • combine-host-port开成true,可以让同一主机上的服务通过主机名与端口号的组合来进行区分,默认情况下会以host来区分不同的服务,这会使得在本机调试的时候,本机上的不同服务聚合成一个服务来统计;

制作一个spring boot的启动类把turbine启动起来

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
 
@EnableTurbine
@SpringBootApplication
@EnableHystrixDashboard
public class TurbineDemo {
 
    public static void main(String[] args) {
        SpringApplication.run(TurbineDemo.class);
 
    }
 
}

这边一定不能忽略两样东西(网上在这块地方100%竟然都是漏了)

  • @EnableTurbine
  • @EnableHystrixDashboard

特别是@EnableHystrixDashboard,如果不指定,你也就不可能在Turbine访问时打开Turbine的内置hystrix dashboard了。

使用Turbine

把Turbine应用,启动起来。

然后通过浏览器访问:http://turbine应用ip:port/hystrix,可以得到以下界面

 

然后在以下截图的方框内填入:http://turbine应用的ip:端口/turbine.stream,点[Monitor Stream]按钮,然后记得你要访问那些个被监控的hystrix端过一会(nacos里服务自动发现+turbine.stream收集延时)后,你会看到如下效果。

在如下截图内,我们有两个hystrix端,都被turbine收集到了,如果是不同服务比如说3个服务,每个服务分在3个spring boot实例中,你是可以得到3*2(一个熔断被开启一个熔断没被触发)共6个仪表盘的。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TGITCIC

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值