Spring Cloud 之服務監控 turbine(六)
阿新 • • 發佈:2019-02-19
turbine是聚合伺服器傳送事件流資料的一個工具,hystrix的監控中,只能監控單個節點,實際生產中都為叢集,
因此可以通過turbine來監控叢集下hystrix的metrics情況,通過eureka來發現hystrix服務。
新建turbine專案
TurbineApplication.java
package turbine; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.hystrix.EnableHystrix; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.cloud.netflix.turbine.EnableTurbine; /** * Created by sai.luo on 2017/4/26. */ @SpringBootApplication @EnableTurbine @EnableHystrix @EnableHystrixDashboard public class TurbineApplication{ public static voidmain(String[] args) { SpringApplication.run(TurbineApplication.class,args); } }
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>turbine</artifactId> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <!-- hystrix依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> </dependency> <!-- turnbine依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-turbine</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.yml
spring: application: name: turbine server: port: 8000 turbine: app-config: hello,helloClient ##需要監控的服務名 aggregator: clusterConfig: main ##需要監控的服務叢集名 clusterNameExpression: metadata['cluster'] eureka: instance: preferIpAddress: true statusPageUrlPath: /info.html client: serviceUrl: defaultZone: http://localhost:8761/eureka/
啟動服務
helloserviceeureka 專案 appliation.yml 增加叢集配置
更改為
spring: application: name: hello server: port: 9001 eureka: instance: lease-renewal-interval-in-seconds: 3 lease-expiration-duration-in-seconds: 5 metadata-map: cluster: main client: serviceUrl: defaultZone: http://localhost:8761/eureka/ registry-fetch-interval-seconds: 3 logging: level: com: netflix: eureka: OFF discovery: OFF
pom.xml增加hystrix依賴包
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>
同理ribboneureka 專案 application.yml 增加叢集配置
更改後如下
spring: application: name: helloClient server: port: 20000 eureka: instance: lease-renewal-interval-in-seconds: 3 lease-expiration-duration-in-seconds: 5 metadata-map: cluster: main client: serviceUrl: defaultZone: http://localhost:8761/eureka/ registry-fetch-interval-seconds: 3 logging: level: com: netflix: eureka: OFF discovery: OFF
pom.xml增加hystrix依賴包
RibbonEurekaApplication.java 增加註解
@EnableHystrix
啟動專案
訪問 localhost:8000/hystrixx 可以看到頁面
注: turbine只能監控hystrix服務,不是hystrix服務,不能監控,如 hello這個服務雖然配置了叢集,但是沒有使用hystrix,所以不會受監控。