文章首發(fā)于微信公眾號(hào)《程序員果果》
地址:https://mp.weixin.qq.com/s/2RNK2XkjTvd1sUM7oR-Jfw
本篇源碼:https://github.com/gf-huanchupk/SpringCloudLearning
簡(jiǎn)介
上一篇介紹了 Hystrix Dashboard 監(jiān)控單體應(yīng)用的例子递礼,在生產(chǎn)環(huán)境中,監(jiān)控的應(yīng)用往往是一個(gè)集群召娜,我們需要將每個(gè)實(shí)例的監(jiān)控信息聚合起來分析狈蚤,這就用到了 Turbine 工具。Turbine有一個(gè)重要的功能就是匯聚監(jiān)控信息,并將匯聚到的監(jiān)控信息提供給Hystrix Dashboard來集中展示和監(jiān)控草冈。
流程
實(shí)驗(yàn)
工程說明
工程名 | 端口 | 作用 |
---|---|---|
eureka-server | 8761 | 注冊(cè)中心 |
service-hi | 8762 | 服務(wù)提供者 |
service-consumer | 8763 | 服務(wù)消費(fèi)者 |
service-turbine | 8765 | Turbine服務(wù) |
核心代碼
eureka-server 、service-hi瓮增、service-consumer 工程代碼與上一節(jié) 微服務(wù)熔斷限流Hystrix之Dashboard 相同怎棱,下面是 service-turbine 工程的核心代碼。
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
application.yml
server:
port: 8765
spring:
application:
name: service-turbine
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
turbine:
app-config: service-consumer
cluster-name-expression: new String("default")
combine-host-port: true
參數(shù)說明:
- turbine.app-config:指定要監(jiān)控的應(yīng)用名
- turbine.cluster-name-expression:指定集群的名字
- turbine.combine-host-port:表示同一主機(jī)上的服務(wù)通過host和port的組合來進(jìn)行區(qū)分绷跑,默認(rèn)情況下是使用host來區(qū)分拳恋,這樣會(huì)使本地調(diào)試有問題
啟動(dòng)類
@SpringBootApplication
@EnableEurekaClient
@EnableHystrixDashboard
@EnableTurbine
public class ServiceTurbineApplication {
public static void main(String[] args) {
SpringApplication.run( ServiceTurbineApplication.class, args );
}
}
模擬多實(shí)例
啟動(dòng)多個(gè) service-consumer 工程,來模擬多實(shí)例砸捏,可以通過命令java -jar service-consumer.jar --server.port=XXXX 來實(shí)現(xiàn)谬运。
為了方便,在編輯器中實(shí)現(xiàn)啟動(dòng)工程垦藏。但 idea 不支持單個(gè)應(yīng)用的多次啟動(dòng)梆暖, 需要開啟并行啟動(dòng):
選擇 “Edit Configurations...”
勾選 “Allow running in parallel”
測(cè)試
啟動(dòng)工程,訪問 http//localhost:8763/hi , http//localhost:8764/hi , http//localhost:8763/oh , http//localhost:8764/oh掂骏,來產(chǎn)生測(cè)試數(shù)據(jù)式廷。
訪問 http://localhost:8765/hystrix ,
輸入監(jiān)控流地址 http://localhost:8765/turbine.stream 芭挽,點(diǎn)擊 Monitor Stream 進(jìn)入監(jiān)控頁面
可以看到聚合了兩個(gè)實(shí)例的 Hystrix dashbord 數(shù)據(jù)。
源碼
https://github.com/gf-huanchupk/SpringCloudLearning/tree/master/chapter18
歡迎掃碼或微信搜索公眾號(hào)《程序員果果》關(guān)注我蝗肪,關(guān)注有驚喜~