天天看点

Turbine

1、简介

Hystrix Dashboard虽然好用,但是它有一个缺点:一个Hystrix Dashboard只能收集一个微服务的Hystrix流。也就是说对于每个微服务,我们都需要开启一个Hystrix Dashboard来监控其健康情况。可以看到如下Hystrix Dashboard只能输入一个actuator端点地址。

Turbine

这能忍?反正我是忍不了。

Turbine

忍不了我们就可以使用Turbine;Netfilx的Turbine项目,提供了将多个微服务的Hystrix流数据聚合到一个流中,并且通过一个Hystrix Dashboard进行展示,这样就可以很nice的同时监控多个微服务的健康状况啦!

Turbine项目的大致架构图如下所示:

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-->

    <artifactId>spring-boot-starter-actuator</artifactId>

  <!--eureka server-->

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>

  <!--eureka client-->

    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

  <!--hystrix-->

    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>

  <!--turbine-->

    <artifactId>spring-cloud-starter-netflix-turbine</artifactId>

  <!--hystrix-dashboard-->

    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>

</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配置文件(其他两个相似)

  port: 22222

    name: user-server

      defaultZone: http://localhost:4010/eureka

## 开启hystrix.stream端点

management:

  endpoints:

    web:

      exposure:

        include: 'hystrix.stream'

user-server服务启动类(其他两个相似)

@EnableEurekaClient

// 开启Hystrix

@EnableHystrix

public class UserServerApp {

        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")

    public String userDemo1() {

    private String fallback() {

        return "user-NaN";

turbine-server服务搭建:

  port: 11111

    name: turbine-server

turbine:

  ## eureka中服务名称列表 

  app-config: order-server,user-server,message-server

  ## eureka集群名称,默认为default

  cluster-name-expression: "'default'"

  ## 开启主机+端口组合聚合服务,默认情况下turbine会将统一主机下的服务聚合成一个服务来统计

  combine-host-port: true

启动类,添加@EnableTurbine启动turbine

// 开启turbine

@EnableTurbine

public class TurbineServerApp {

        SpringApplication.run(TurbineServerApp.class, args);

hystrix-dashboard-server服务搭建:

  port: 55555

    ## 不注册  

启动类,添加@EnableHystrixDashboard启动HystrixDashboard

@EnableHystrixDashboard

public class HystrixDashboardServerApp {

        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/
Turbine

之后访问hystrix-dashboard-server服务的hystrix端点,确保看到如下hystrix dashboard界面

http://localhost:55555/hystrix
Turbine

在hystrix dashboard中输入turbine-server提供的turbine.stream端点

http://localhost:11111/turbine.stream
Turbine

初始进入的时候由于各个受hystrix保护的方法并未调用,因此未上报任何数据,所以需要调用各个接口触发数据上报。

Turbine

之后看到如下界面,说明服务启动成功,整个监控服务整合完毕

Turbine

2.3 注意事项

hystrix dashboard中展示的circuit数据,会根据方法名来创建,因此不管是不是同一个服务中,只要受hystrix保护的方法,如果方法名相同,将会被聚合到一起展示,这里指的注意哦!

Turbine

此外不要理解成一个circuit会对应一个thread pools

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