Hystrix+Hystrix Dashboard+Turbine使用,它不香么?

「这是我参与11月更文挑战的第20天,活动详情查看:2021最后一次更文挑战

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保护的服务。

扫描二维码关注公众号,回复: 13257053 查看本文章
<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
@EnableEurekaServer
public 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
@EnableHystrix
public 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
@EnableTurbine
public 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
@EnableHystrixDashboard
public 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

  • circuit的个数与方法名个数相同
  • thread pools每一个受hystrix保护的方法所在类会创建一个

猜你喜欢

转载自juejin.im/post/7032470090008231949