1、简介
Hystrix Dashboard 虽然好用,但是它有一个缺点:一个 Hystrix Dashboard 只能收集一个微服务的 Hystrix 流。也就是说对于每个微服务,我们都需要开启一个 Hystrix Dashboard 来监控其健康情况。可以看到如下 Hystrix Dashboard 只能输入一个 actuator 端点地址。
这能忍?反正我是忍不了。
忍不了我们就可以使用 Turbine;Netfilx 的 Turbine 项目,提供了将多个微服务的 Hystrix 流数据聚合到一个流中,并且通过一个 Hystrix Dashboard 进行展示,这样就可以很 nice 的同时监控多个微服务的健康状况啦!
Turbine 项目的大致架构图如下所示:
没有 Turbine 之前,每个微服务都需要开启一个 Hystrix Dashboard 页面来监控当前微服务的健康情况,有了 Turbine 之后,多个微服务的信息先通过 Turbine 进行聚合,再统一在一个 Hystrix Dashboard 页面展示。
2、正文
2.1 快速搭建
服务列表:我这里一共搭建了六个服务,其中 eureka-server 为注册中心,turbine-server 为 turbine 服务,hystrix-dashboard-server 为 hystrix-dashboard 服务(这些服务如果有需要你也可以在一个服务中启动),user-server、order-server、message-server 三个服务是受 hystrix 保护的服务。
 <modules>  <module>eureka-server</module>  <module>turbine-server</module>  <module>hystrix-dashboard-server</module>  <module>user-server</module>  <module>order-server</module>  <module>message-server</module></modules>
   复制代码
 
服务依赖:所有的依赖和版本均在下面,自行在指定服务中选择需要的依赖
 <parent>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-parent</artifactId>  <version>2.3.4.RELEASE</version></parent>
<properties>  <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version></properties>
<dependencies>  <!--web-->  <dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-web</artifactId>  </dependency>  <!--actuator-->  <dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-actuator</artifactId>  </dependency>  <!--eureka server-->  <dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>  </dependency>  <!--eureka client-->  <dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>  </dependency>  <!--hystrix-->  <dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>  </dependency>  <!--turbine-->  <dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-netflix-turbine</artifactId>  </dependency>  <!--hystrix-dashboard-->  <dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>  </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>
   复制代码
 
eureka-server 服务搭建:application.yml 配置文件
 server:  port: 4010
spring:  application:    name: eureka-server
eureka:  instance:    hostname: localhost  client:    register-with-eureka: false    fetch-registry: false    service-url:      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
   复制代码
 
服务启动类
 @SpringBootApplication@EnableEurekaServerpublic class EurekaServerApp {
    public static void main(String[] args) {        SpringApplication.run(EurekaServerApp.class, args);    }
}
   复制代码
 
user-server、order-server、message-server 服务搭建:user-server 服务 application.yml 配置文件(其他两个相似)
 server:  port: 22222
spring:  application:    name: user-server
eureka:  client:    service-url:      defaultZone: http://localhost:4010/eureka
## 开启hystrix.stream端点management:  endpoints:    web:      exposure:        include: 'hystrix.stream'
   复制代码
 
user-server 服务启动类(其他两个相似)
 @SpringBootApplication@EnableEurekaClient// 开启Hystrix@EnableHystrixpublic class UserServerApp {
    public static void main(String[] args) {        SpringApplication.run(UserServerApp.class, args);    }
}
   复制代码
 
user-server 编写受 hystrix 保护控制器(其他两个相似)
 @RestController@RequestMapping(value = "/user")public class UserController {
    @GetMapping    @HystrixCommand(fallbackMethod = "fallback")    public String userDemo() {        return "user-98";    }
    // 写两个方法是为了演示方法名称不同,hystrix dashboard会创建不同的circuit    @GetMapping("/demo1")    @HystrixCommand(fallbackMethod = "fallback")    public String userDemo1() {        return "user-98";    }
    private String fallback() {        return "user-NaN";    }
}
   复制代码
 
turbine-server 服务搭建:application.yml 配置文件
 server:  port: 11111
spring:  application:    name: turbine-server
eureka:  client:    service-url:      defaultZone: http://localhost:4010/eureka
turbine:  ## eureka中服务名称列表    app-config: order-server,user-server,message-server  ## eureka集群名称,默认为default  cluster-name-expression: "'default'"  ## 开启主机+端口组合聚合服务,默认情况下turbine会将统一主机下的服务聚合成一个服务来统计  combine-host-port: true
   复制代码
 
启动类,添加 @EnableTurbine 启动 turbine
 @SpringBootApplication// 开启turbine@EnableTurbinepublic class TurbineServerApp {
    public static void main(String[] args) {        SpringApplication.run(TurbineServerApp.class, args);    }
}
   复制代码
 
hystrix-dashboard-server 服务搭建:application.yml 配置文件
 server:  port: 55555
eureka:  client:    service-url:      defaultZone: http://localhost:4010/eureka    ## 不注册      register-with-eureka: false
   复制代码
 
启动类,添加 @EnableHystrixDashboard 启动 HystrixDashboard
 @SpringBootApplication@EnableHystrixDashboardpublic class HystrixDashboardServerApp {
    public static void main(String[] args) {        SpringApplication.run(HystrixDashboardServerApp.class, args);    }
}
   复制代码
 2.2 服务启动
服务启动应先启动注册中心 eureka-server 再启动 user-server、order-server、message-server 服务,最后启动 turbine-server 和 hystrix-dashboard-server。启动完成之后,先查看 eureka-server 注册中心上服务是否注册正常
http://localhost:4010/
之后访问 hystrix-dashboard-server 服务的 hystrix 端点,确保看到如下 hystrix dashboard 界面
http://localhost:55555/hystrix
在 hystrix dashboard 中输入 turbine-server 提供的 turbine.stream 端点
http://localhost:11111/turbine.stream
初始进入的时候由于各个受 hystrix 保护的方法并未调用,因此未上报任何数据,所以需要调用各个接口触发数据上报。
之后看到如下界面,说明服务启动成功,整个监控服务整合完毕
2.3 注意事项
hystrix dashboard 中展示的 circuit 数据,会根据方法名来创建,因此不管是不是同一个服务中,只要受 hystrix 保护的方法,如果方法名相同,将会被聚合到一起展示,这里指的注意哦!
此外不要理解成一个 circuit 会对应一个 thread pools
评论