上文我們介紹了如何使用Hystrix進行單節點的實時監控,但是對于使用了微服務的系統,會存在很多節點,并且整個服務治理體系也會存在很多服務節點,這就需要有個統一聚合監控工具,Turbine就是這樣的一個存在,它可以方便的聚合所有被監控的節點。
在springcloud下可以直接將數據通過Hystrix Stream將數據聚合到Turbine上,但是使用Hystrix Stream來實現會有一個問題就是,監控服務器和被監控服務器是在同一個局域網,并且網絡是完全暢通的,在測試環境還可以這樣配置,但是在生產環境這樣的網絡策略是非常不安全的,但是我們可以通過中間件來將數據聚合到dashboard上,springcloud支持通過RabbitMq和Kafka來作為消息中間件來實效被監控服務器向監控server發送監控數據,今天我們就講下如何使用RabbitMq來實現聚合監控。 RabbitMQ的安裝可以參考我的另一篇文章 http://www.lxweimin.com/p/6a4c11b22513
1. 被監控Client端
1)增加pom依賴
在被監控端需要增加hystrix、hystrix-stream和springcloud-stream的rabbitmq依賴,增加了這幾個依賴就可以保證服務啟動后會作為生產者向rabbitmq的queue中發送監控消息。
<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端口默認為5672,username和password分別為創建的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服務端
在監控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中搜集監控信息。
@SpringBootApplication
@EnableTurbineStream
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
3.啟動rabbitmq
登陸到安裝rabbitmq的機器,在命令行輸入 service rabbitmq-server start 啟動服務。
mq啟動好后,再將剛才創建的被監控的client端和監控端Turbine都啟動起來,就可以在瀏覽器輸入client端的hystrix dashboard地址進行查看,http://127.0.0.1:8081/hystrix;
并在輸入欄中輸入turbine的地址 http://127.0.0.1:8031/turbine.stream,啟動后一旦有接口調用就會顯示dashboard儀表盤監控信息了,啟動多個被監控client端,在turbine的儀表盤就可以查看多個節點信息。