天天看點

#yyds幹貨盤點#Turbine

1、簡介

Hystrix Dashboard雖然好用,但是它有一個缺點:一個Hystrix Dashboard隻能收集一個微服務的Hystrix流。也就是說對于每個微服務,我們都需要開啟一個Hystrix Dashboard來監控其健康情況。可以看到如下Hystrix Dashboard隻能輸入一個actuator端點位址。

#yyds幹貨盤點#Turbine

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

#yyds幹貨盤點#Turbine

忍不了我們就可以使用Turbine;Netfilx的Turbine項目,提供了将多個微服務的Hystrix流資料聚合到一個流中,并且通過一個Hystrix Dashboard進行展示,這樣就可以很nice的同時監控多個微服務的健康狀況啦!

Turbine項目的大緻架構圖如下所示:

#yyds幹貨盤點#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
@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/​​

#yyds幹貨盤點#Turbine

之後通路hystrix-dashboard-server服務的hystrix端點,確定看到如下hystrix dashboard界面

​​http://localhost:55555/hystrix​​

#yyds幹貨盤點#Turbine

在hystrix dashboard中輸入turbine-server提供的turbine.stream端點

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

#yyds幹貨盤點#Turbine

初始進入的時候由于各個受hystrix保護的方法并未調用,是以未上報任何資料,是以需要調用各個接口觸發資料上報。

#yyds幹貨盤點#Turbine

之後看到如下界面,說明服務啟動成功,整個監控服務整合完畢

#yyds幹貨盤點#Turbine

2.3 注意事項

  • circuit的個數與方法名個數相同
  • thread pools每一個受hystrix保護的方法所在類會建立一個