1.Spring Cloud提供的Turbine就是將監控信息進行聚合,並將聚合的監控信息提供給Hystrix Dashboard進行集中的展示和監控的組件,使用它可以實現監控整個集羣的效果。
新建項目:ms-turbine-consumer
1.pom文件:
<!-- 註冊eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- 打印日誌 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- feigin的依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<!-- hystrix 的依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--hystrix command -->
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
<version>1.5.12</version>
</dependency>
<!-- dashboard -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<!-- turbine-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>
2.application.yml文件:
下面的turbine的幾個參數的解釋:
(1)turbine.app-config指定要監控的應用名字,多個可以使用逗號隔開。上面我們監控的是“ms-hystrix-consumer”服務。
(2)turbine.cluster-name-expression=default,表示集羣的名字爲default
(3)turbine.combine-host-port=true表示同一主機上的服務通過host和port的組合來進行區分,默認情況下是使用host來區分,這樣會使本地調試有問題。
server:
port: 8007
spring:
application:
name: ms-turbine-consumer
eureka:
client:
healthcheck:
enabled: true
serviceUrl:
defaultZone: http://ljf:123@localhost:8761/eureka
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
turbine:
aggregator:
clusterConfig: default
appConfig: ms-hystrix-consumer
clusterNameExpression: "'default'"
#設置查看指標
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: "*"
exclude: "-"
3.在啓動類:添加turbine註解:
package com.ljf.weifuwu.springcloud.turbine;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
/**
* Hello world!
*
*/
@SpringBootApplication
@EnableTurbine
public class TurbineApp
{
public static void main( String[] args )
{
SpringApplication.run(TurbineApp.class, args);
System.out.println( "turbine啓動起來了!" );
}
}
4.啓動服務:ms-eureka-center(8761)、ms-eureaka-provider(7901)、ms-hystrix-consumer(8004)、ms-hystrix-consumer(8005)、ms-hystrix-dashboard(8006)、ms-turbine-consumer(8007)
5.訪問:
http://localhost:8004//hystrix-consumer/1
http://localhost:8005//hystrix-consumer/1
http://localhost:8007/turbine.stream:
http://localhost:8006/hystrix:
點擊monitor stream:
拼命刷新:http://localhost:8004//hystrix-consumer/1 ,http://localhost:8005//hystrix-consumer/1 之後的結果:
查看集羣的狀態 :http://localhost:8007/turbine.stream?cluster=default
二修改 集羣的名稱,指定監控的服務
1.修改項目ms-turbine-consumer的配置文件的turbine的配置:
turbine: aggregator: clusterConfig: ljf-turbine #自定義 app-config: ms-hystrix-consumer cluster-name-expression: metadata['cluster'] combine-host-port: true
2.修改監控項目的元信息:在項目ms-hystrix-consumer修改配置文件:
3.重新啓動這些服務:ms-eureka-center(8761)、ms-eureaka-provider(7901)、ms-hystrix-consumer(8004)、ms-hystrix-consumer(8005)、ms-hystrix-dashboard(8006)、ms-turbine-consumer(8007)
#查看這個集羣的聚合信息,進行監控:http://localhost:8007/turbine.stream?cluster=ljf-turbine
放到dashboard查看:
最後,有一個疑問:爲何http://localhost:8007/clusters,查詢不到可用的clusters的信息呢????