在微服務(wù)架構(gòu)中接奈,hystrix處理容錯(cuò)外,還有實(shí)時(shí)監(jiān)控功能通孽,在服務(wù)發(fā)生調(diào)用時(shí)序宦,會(huì)將每秒請求數(shù)、成功請求數(shù)等運(yùn)行指標(biāo)記錄下來背苦。
本文示例代碼:springcloud-demo
其中本文相關(guān)的項(xiàng)目有:
- 服務(wù)發(fā)現(xiàn) Eureka Server: discovery
- 鏈路追蹤 sleuth+zipkin:trace
- 服務(wù)提供者:hello
- 服務(wù)提供者: world
- 服務(wù)消費(fèi)者: helloworld
- 服務(wù)消費(fèi)者: helloworld-feign
- 服務(wù)調(diào)用監(jiān)控: hystrix-dashboard
- 監(jiān)控聚合: hystrix-turbine
- 監(jiān)控聚合(消息中間件): hystrix-turbine-mq
通過接口的監(jiān)控
啟動(dòng)helloworld
項(xiàng)目后互捌,訪問http://localhost:8020/message
后,再次訪問http://localhost:8020/hystrix.stream
可以看到界面不斷地輸出監(jiān)控日志行剂,監(jiān)控日志里包含了各種指標(biāo)(各種指標(biāo)信息請參考官方文檔)秕噪。
按照同樣的步驟操作helloworld-feign
項(xiàng)目后,卻發(fā)現(xiàn)找不到該頁面厚宰,這是因?yàn)樾枰鲆恍┤缦屡渲茫?br>
pom.xml引入hystrix依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
在啟動(dòng)類上加上@EnableCircuitBreaker
注解腌巾,再次執(zhí)行上述操作,界面不斷輸出監(jiān)控日志铲觉。
使用hystrix-board對監(jiān)控進(jìn)行圖形化展示
上面的日志信息不夠直觀澈蝙,借助hystrix-dashboard可對監(jiān)控進(jìn)行圖形化展示。
- 在service創(chuàng)建hystrix-dashboard項(xiàng)目
- 引入hystrix-dashboard依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
- 啟動(dòng)類配置
@EnableHystrixDashboard
注解(同時(shí)配置@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é)果
啟動(dòng)hystrix-dashboard后灯荧,輸入http://localhost:8087/hystrix
地址,出現(xiàn)文章第一張圖所示的界面盐杂。在第一個(gè)文本框輸入http://localhost:8020/hystrix.stream
或者http://localhost:8030/hystrix.stream
后點(diǎn)擊Monitor Stream
則會(huì)顯示如下監(jiān)控結(jié)果:
以上的hystrix-dashboard每次只能輸入一個(gè)監(jiān)控地址逗载,在微服務(wù)架構(gòu)中往往有很多無法需要監(jiān)控,該怎么辦呢况褪?
可以使用Turbine聚合監(jiān)控?cái)?shù)據(jù)撕贞,讓hystrix-dashboard顯示這個(gè)聚合數(shù)據(jù)后的地址。
Turbine聚合監(jiān)控?cái)?shù)據(jù)
- 創(chuàng)建一個(gè)hystrix-turbine項(xiàng)目测垛,引入如下maven依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>
- 啟動(dòng)類上配置@EnableTurbine注解
- 配置文件
application.yml
指明要從收集哪些微服務(wù)的監(jiān)控?cái)?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
日志
- 啟動(dòng)項(xiàng)目hystrix-turbine后,在hystrix-dashboard中輸入
http://localhost:8089/turbine.stream
,則展示如下聚合結(jié)果:
以上Turbine聚合微服務(wù)的監(jiān)控?cái)?shù)據(jù),然后在hystrix-dashboard展示多個(gè)微服務(wù)的實(shí)時(shí)監(jiān)控?cái)?shù)據(jù)号涯。 但是Turbine也有它的局限性目胡,比如服務(wù)之間無法通信,服務(wù)不在Eureka Server上注冊链快,則Turbine無法收集到微服務(wù)的日志誉己。那么這種情況下,需要借助消息中間件來解決域蜗。
通過消息中間件RabbitMQ收集監(jiān)控?cái)?shù)據(jù)
微服務(wù)在發(fā)生調(diào)用時(shí)巨双,將監(jiān)控?cái)?shù)據(jù)存儲(chǔ)到RabbitMQ
,監(jiān)控從RabbitMQ獲取數(shù)據(jù)進(jìn)行實(shí)時(shí)展示霉祸,這樣有利于微服務(wù)和監(jiān)控端進(jìn)行解耦筑累,可以解決不在服務(wù)中心注冊的服務(wù)的監(jiān)控?cái)?shù)據(jù)也可以被收集到。
- 安裝RabbitMQ
使用如下docker命令安裝RabbitMQ丝蹭,并暴露相應(yīng)的端口(本人開發(fā)機(jī)為linux環(huán)境慢宗,docker的使用可以極大提高軟件安裝和項(xiàng)目部署的效率,推薦使用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)控?cái)?shù)據(jù)缅茉。
- 新建hystrix-turbine-mq項(xiàng)目
項(xiàng)目中引入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>
啟動(dòng)類上配置@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é)果
啟動(dòng)hystrix-dashboard后湘换,輸入http://localhost:8089
即可展示如下的監(jiān)控結(jié)果:
RabbitMQ收集監(jiān)控?cái)?shù)據(jù).png
本文參考了《Spring cloud 與Docker 微服務(wù)實(shí)戰(zhàn)》這本書宾舅。