
在微服務(wù)架構(gòu)中,hystrix處理容錯外,還有實時監(jiān)控功能,在服務(wù)發(fā)生調(diào)用時,會將每秒請求數(shù)、成功請求數(shù)等運行指標(biāo)記錄下來。
本文示例代碼:springcloud-demo
其中本文相關(guān)的項目有:
- 服務(wù)發(fā)現(xiàn) Eureka Server: discovery
- 鏈路追蹤 sleuth+zipkin:trace
- 服務(wù)提供者:hello
- 服務(wù)提供者: world
- 服務(wù)消費者: helloworld
- 服務(wù)消費者: helloworld-feign
- 服務(wù)調(diào)用監(jiān)控: hystrix-dashboard
- 監(jiān)控聚合: hystrix-turbine
- 監(jiān)控聚合(消息中間件): hystrix-turbine-mq
通過接口的監(jiān)控
啟動helloworld項目后,訪問http://localhost:8020/message后,再次訪問http://localhost:8020/hystrix.stream可以看到界面不斷地輸出監(jiān)控日志,監(jiān)控日志里包含了各種指標(biāo)(各種指標(biāo)信息請參考官方文檔)。
按照同樣的步驟操作helloworld-feign項目后,卻發(fā)現(xiàn)找不到該頁面,這是因為需要做一些如下配置:
pom.xml引入hystrix依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
在啟動類上加上@EnableCircuitBreaker注解,再次執(zhí)行上述操作,界面不斷輸出監(jiān)控日志。

使用hystrix-board對監(jiān)控進(jìn)行圖形化展示
上面的日志信息不夠直觀,借助hystrix-dashboard可對監(jiān)控進(jìn)行圖形化展示。
- 在service創(chuàng)建hystrix-dashboard項目
- 引入hystrix-dashboard依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
- 啟動類配置
@EnableHystrixDashboard注解(同時配置@EnableDiscoveryClient向注冊中心注冊,方便管理) - 配置文件設(shè)置端口
server:
port: 8087
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${server.port}
spring:
application:
name: hystrix-dashboard
- 測試結(jié)果
啟動hystrix-dashboard后,輸入http://localhost:8087/hystrix地址,出現(xiàn)文章第一張圖所示的界面。在第一個文本框輸入http://localhost:8020/hystrix.stream或者http://localhost:8030/hystrix.stream后點擊Monitor Stream則會顯示如下監(jiān)控結(jié)果:

以上的hystrix-dashboard每次只能輸入一個監(jiān)控地址,在微服務(wù)架構(gòu)中往往有很多無法需要監(jiān)控,該怎么辦呢?
可以使用Turbine聚合監(jiān)控數(shù)據(jù),讓hystrix-dashboard顯示這個聚合數(shù)據(jù)后的地址。
Turbine聚合監(jiān)控數(shù)據(jù)
- 創(chuàng)建一個hystrix-turbine項目,引入如下maven依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>
- 啟動類上配置@EnableTurbine注解
- 配置文件
application.yml指明要從收集哪些微服務(wù)的監(jiān)控數(shù)據(jù)
server:
port: 8088
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${server.port}
prefer-ip-address: true
spring:
application:
name: hystrix-turbine
turbine:
app-config: helloworld,helloworldfeign
cluster-name-expression: "'default'"
注意turbine的配置,這里收集helloworld和hellowordfeign日志
- 啟動項目hystrix-turbine后,在hystrix-dashboard中輸入
http://localhost:8089/turbine.stream,則展示如下聚合結(jié)果:

以上Turbine聚合微服務(wù)的監(jiān)控數(shù)據(jù),然后在hystrix-dashboard展示多個微服務(wù)的實時監(jiān)控數(shù)據(jù)。 但是Turbine也有它的局限性,比如服務(wù)之間無法通信,服務(wù)不在Eureka Server上注冊,則Turbine無法收集到微服務(wù)的日志。那么這種情況下,需要借助消息中間件來解決。
通過消息中間件RabbitMQ收集監(jiān)控數(shù)據(jù)
微服務(wù)在發(fā)生調(diào)用時,將監(jiān)控數(shù)據(jù)存儲到RabbitMQ,監(jiān)控從RabbitMQ獲取數(shù)據(jù)進(jìn)行實時展示,這樣有利于微服務(wù)和監(jiān)控端進(jìn)行解耦,可以解決不在服務(wù)中心注冊的服務(wù)的監(jiān)控數(shù)據(jù)也可以被收集到。
- 安裝RabbitMQ
使用如下docker命令安裝RabbitMQ,并暴露相應(yīng)的端口(本人開發(fā)機為linux環(huán)境,docker的使用可以極大提高軟件安裝和項目部署的效率,推薦使用docker)
docker run -d --name rabbitmq -p 5673:5672 -p 15673:15672 docker.io/rabbitmq:3-management
這樣向外暴露5673的連接端口和15673的web端口,默認(rèn)用戶名密碼是guest。

- 微服務(wù)端修改,以便向RabbitMQ傳輸監(jiān)控日志
本文以helloworld為例改造微服務(wù),pom中引入依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-hystrix-stream</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
配置文件中加上連接RabbitMQ的配置
spring:
rabbitmq:
host: localhost
port: 5673
username: guest
password: guest
重啟helloworld,并在瀏覽器輸入http://localhost:8020/message形成監(jiān)控數(shù)據(jù)。
- 新建hystrix-turbine-mq項目
項目中引入turbine和rabbitmq依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-turbine-stream</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
啟動類上配置@EnableTurbineStream注解
配置文件:
server:
port: 8089
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${server.port}
prefer-ip-address: true
spring:
application:
name: hystrix-turbine-mq
rabbitmq:
host: localhost
port: 5673
username: guest
password: guest
這樣指明了監(jiān)控的數(shù)據(jù)來源。
- 測試結(jié)果
啟動hystrix-dashboard后,輸入http://localhost:8089即可展示如下的監(jiān)控結(jié)果:
RabbitMQ收集監(jiān)控數(shù)據(jù).png
本文參考了《Spring cloud 與Docker 微服務(wù)實戰(zhàn)》這本書。
