視覺化監控微服務 Hystrix Dashboard、Turbine stream
視覺化監控微服務
一、使用 Hystrix Dashboard 監控單個微服務
1、新增依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
2、配置 application.yml
hystrix: dashboard: proxy-stream-allow-list: "*"
3、開啟 @EnableHystrixDashboard 註解
4、執行 application,然後開啟相應url,檢視微服務執行資訊。
注:被監控的微服務需在 actuator 上暴露 hystrix.stream 介面
二、使用 Turbine 聚合監控資料
1、假設已經建立好了兩個以上的 Hystrix 專案,並且在 actuator 中暴露了 hystrix.stream 介面
2、建立 turbine 專案
3、加入依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-turbine</artifactId> </dependency>
4、在啟動器處開啟 @EnableTurbine 註解
5、配置 application.yml
server: port: 8793 spring: application: name: hystrix-turbine-server eureka: client: service-url: defaultZone: http://zolmk:zolmk@${client.desktop}:8761/eureka/,http://zolmk:zolmk@${client.desktop}:8762/eureka/ register-with-eureka: true fetch-registry: true instance: prefer-ip-address: true turbine: app-config: user-register-server # 這裡是需要進行聚合監控的微服務名稱 cluster-name-expression: "'default'" client: desktop: 192.168.1.7 notebook: eureka-node-a
6、啟動該 application,通過 hystrix dashboard 訪問 http://localhost:8793/turbine.stream 即可看到監控資料
三、使用訊息中介軟體收集資料
在一些場景下,微服務與 Turbine 網路不通,此時,可以藉助訊息中介軟體實現資料收集。各個微服務將 Hystrix Command 的監控資料傳送至訊息中介軟體,Turbine 消費訊息中介軟體中的資料。
1、建立被監控的工程
1)、建立被監控的工程,在 pom.xml 檔案中新增
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
<!-- hystrix stream -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-hystrix-stream</artifactId>
<version>2.0.4.BUILD-SNAPSHOT</version>
</dependency>
<!-- 訊息中介軟體支援 RabbitMQ -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
2)、配置 application.yml
server:
port: 8781
spring:
application:
name: user-register-server
rabbitmq:
host: 192.168.1.3
password: zolmk
username: zolmk
port: 5672
eureka:
client:
service-url:
defaultZone: http://zolmk:zolmk@${client.desktop}:8761/eureka/,http://zolmk:zolmk@${client.desktop}:8762/eureka/
register-with-eureka: true
fetch-registry: true
instance:
prefer-ip-address: true
management:
endpoints:
web:
exposure:
include: ["hystrix.stream","health"]
client:
desktop: 192.168.1.7
notebook: eureka-node-a
3)、假設專案已有 HystrixCommand ,也就是已完成熔斷器部分。
4)、啟動專案
2、建立監控 turbine-stream-server
1)、新增 pom 依賴
<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-stream</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
2)、配置 application.yml
server:
port: 8794
spring:
application:
name: hystrix-turbine-stream-server
rabbitmq:
port: 5672
username: zolmk
password: zolmk
host: 192.168.1.3
eureka:
client:
service-url:
defaultZone: http://zolmk:zolmk@${client.desktop}:8761/eureka/,http://zolmk:zolmk@${client.desktop}:8762/eureka/
register-with-eureka: true
fetch-registry: true
instance:
prefer-ip-address: true
client:
desktop: 192.168.1.7
notebook: eureka-node-a
3)、開啟註解 @EnableTurbineStream
4)、啟動 application
5)、需要將 hystrixStreamOutput 和 hystrixStreamOutput 兩個交換器進行繫結。
繫結方法,在交換器中點選 hystrixStreamOutput ,選擇 Bindings ,繫結到交換器,輸入交換器名稱 hystrixStreamOutput ,路由鍵填寫 #,繫結之後就可以在 Hystrix dashboard 中看到資料了。