SpringCloud實戰之路 | 應用篇(四)服務熔斷器Hystrix Dashboard及 Hystrix Turbine聚合監控
Hystrix Dashboard斷路監控儀表盤
1.新建一個監控服務工程引入依賴
<!--hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--hystrix 儀表盤-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.啓動類添加@EnableHystrixDashboard
激活儀表盤
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
@EnableHystrixDashboard // 開啓hystrix dashboard
public class HystrixDashboardApplication9000 {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardApplication.class, args);
}
}
server:
port: 9000
spring:
application:
name: cloud-hystrix-dashboard
eureka:
client:
serviceUrl: # eureka server的路徑
defaultZone: http://EurekaServerA:8761/eureka/,http://EurekaServerB:8762/eureka/ #把 eureka 集羣中的所有 url 都填寫了進來,也可以只寫一臺,因爲各個 eureka server 可以同步註冊表
instance:
#使用ip註冊,否則會使用主機名註冊了(此處考慮到對老版本的兼容,新版本經過實驗都是ip)
prefer-ip-address: true
#自定義實例顯示格式,加上版本號,便於多版本管理,注意是ip-address,早期版本是ipAddress
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
3.在被監測的微服務中註冊監控servlet
/**
* 在被監控的微服務中註冊一個serlvet,後期我們就是通過訪問這個servlet來獲取該服務的Hystrix監控數據的
* 前提:被監控的微服務需要引入springboot的actuator功能
* @return
*/
@Bean
public ServletRegistrationBean getServlet(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/actuator/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
訪問http://localhost:9000/hystrix
輸入監控的微服務地址http://localhost:8090/actuator/hystrix.stream
Hystrix Turbine聚合監控
Hystrix Dashboard是針對一個微服務實例的Hystrix數據查詢分析,在微服務架構下,一個微服務的實例往往是多個,我們可用通過Hystrix Turbine實現聚合監控
1.Turbine服務搭建引入依賴
<dependencies>
<!--hystrix turbine聚合監控-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eurekaclient</artifactId>
</dependency>
</dependencies>
server:
port: 9001
Spring:
application:
name: cloud-hystrix-turbine
eureka:
client:
serviceUrl: # eureka server的路徑
defaultZone: http://EurekaServerA:8761/eureka/,http://EurekaServerB:8762/eureka/ #把 eureka 集羣中的所有 url 都填寫了進來,也可以只寫一臺,因爲各個 eureka server 可以同步註冊表
instance:
#使用ip註冊,否則會使用主機名註冊了(此處考慮到對老版本的兼容,新版本經過實驗都是ip)
prefer-ip-address: true
#自定義實例顯示格式,加上版本號,便於多版本管理,注意是ip-address,早期版本是ipAddress
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@
#turbine配置
turbine:
# appCofing配置需要聚合的服務名稱,比如這裏聚合自動投遞微服務的hystrix監控數據
# 如果要聚合多個微服務的監控數據,那麼可以使用英文逗號拼接,比如 a,b,c
appConfig: cloud-service-user
clusterNameExpression: "'default'" # 集羣默認名稱
2.啓動類添加@EnableTurbine
開啓Turbine聚合功能
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
@SpringBootApplication
@EnableDiscoveryClient
@EnableTurbine // 開啓Turbine聚合功能
public class HystrixTurbineApplication9001 {
public static void main(String[] args) {
SpringApplication.run(HystrixTurbineApplication9001.class,args);
}
}
訪問http://localhost:9000/hystrix
輸入聚合監控的地址http://localhost:9001/turbine.stream
就可以看到數據了