上文我們介紹了如何使用Hystrix進行單節(jié)點的實時監(jiān)控,但是對于使用了微服務(wù)的系統(tǒng),會存在很多節(jié)點,并且整個服務(wù)治理體系也會存在很多服務(wù)節(jié)點,這就需要有個統(tǒng)一聚合監(jiān)控工具,Turbine就是這樣的一個存在,它可以方便的聚合所有被監(jiān)控的節(jié)點。
在springcloud下可以直接將數(shù)據(jù)通過Hystrix Stream將數(shù)據(jù)聚合到Turbine上,但是使用Hystrix Stream來實現(xiàn)會有一個問題就是,監(jiān)控服務(wù)器和被監(jiān)控服務(wù)器是在同一個局域網(wǎng),并且網(wǎng)絡(luò)是完全暢通的,在測試環(huán)境還可以這樣配置,但是在生產(chǎn)環(huán)境這樣的網(wǎng)絡(luò)策略是非常不安全的,但是我們可以通過中間件來將數(shù)據(jù)聚合到dashboard上,springcloud支持通過RabbitMq和Kafka來作為消息中間件來實效被監(jiān)控服務(wù)器向監(jiān)控server發(fā)送監(jiān)控數(shù)據(jù),今天我們就講下如何使用RabbitMq來實現(xiàn)聚合監(jiān)控。 RabbitMQ的安裝可以參考我的另一篇文章 http://www.itdecent.cn/p/6a4c11b22513
1. 被監(jiān)控Client端
1)增加pom依賴
在被監(jiān)控端需要增加hystrix、hystrix-stream和springcloud-stream的rabbitmq依賴,增加了這幾個依賴就可以保證服務(wù)啟動后會作為生產(chǎn)者向rabbitmq的queue中發(fā)送監(jiān)控消息。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<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>
</dependency>
2)application.yml配置rabbitmq
host表示rabbitmq的ip地址,rabbitmq端口默認(rèn)為5672,username和password分別為創(chuàng)建的rabbitmq的用戶名和密碼。
spring:
application:
name: hystrix-mq
rabbitmq:
host: 10.132.XX.XX
port: 5672
username: admin
password: admin
3)controller增加HystrixCommand注解
@RestController
public class TestController {
@HystrixCommand(fallbackMethod = "error", commandProperties = {
@HystrixProperty(name="execution.isolation.strategy", value = "THREAD"),
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "4000"),
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "40")
}, threadPoolProperties = {
@HystrixProperty(name = "coreSize", value = "1"),
// @HystrixProperty(name = "maximumSize", value = "5"),
@HystrixProperty(name = "maxQueueSize", value = "10"),
@HystrixProperty(name = "keepAliveTimeMinutes", value = "1000"),
@HystrixProperty(name = "queueSizeRejectionThreshold", value = "8"),
@HystrixProperty(name = "metrics.rollingStats.numBuckets", value = "12"),
@HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "1440")
})
@RequestMapping(value = "/hello", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
public String hello(){
return "hello hystrix!";
}
public String error() {
return "hello error!";
}
}
2.Turbine服務(wù)端
在監(jiān)控server端需要通過MQ接收Metrics,并在Turbine dashboard展示。
1)pom引入依賴
<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>
2)application.yml
server:
port: 8031
spring:
application:
name: turbine
rabbitmq:
host: 10.132.XX.XX
port: 5672
username: admin
password: admin
eureka:
client:
service-url:
defaultZone: http://10.132.33.43:8761/eureka/
instance:
prefer-ip-address: true
3)Application
在啟動的Application中增加 @EnableTurbineStream 注解,即可在啟動后自動從queue中搜集監(jiān)控信息。
@SpringBootApplication
@EnableTurbineStream
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
3.啟動rabbitmq
登陸到安裝rabbitmq的機器,在命令行輸入 service rabbitmq-server start 啟動服務(wù)。
mq啟動好后,再將剛才創(chuàng)建的被監(jiān)控的client端和監(jiān)控端Turbine都啟動起來,就可以在瀏覽器輸入client端的hystrix dashboard地址進行查看,http://127.0.0.1:8081/hystrix;
并在輸入欄中輸入turbine的地址 http://127.0.0.1:8031/turbine.stream,啟動后一旦有接口調(diào)用就會顯示dashboard儀表盤監(jiān)控信息了,啟動多個被監(jiān)控client端,在turbine的儀表盤就可以查看多個節(jié)點信息。
