SpringCloud微服務 之hystrix(六)
阿新 • • 發佈:2018-11-09
前言
本小結我們來學習一下使用Turbine來聚合各個服務節點的Hystrix.stream資訊。
案例
-
Eureka Server端編寫(參考前例)。
-
Eureka Client端服務提供方編寫(參考:SpringCloud微服務 之hystrix(五))
Mock兩個服務呼叫方且各自都實現了監控各自Hystrix.stream端點的功能。- microservice-deal-broker-cloud-hystrix-dashboard
- microservice-deal-broker-cloud-hystrix-dashboard-2
-
Eureka Client Turbine Dashboard編寫
- 專案結構
- CoreCode
<?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>microservice-deal-broker-cloud-hystrix-dashboard-turbine</artifactId> <packaging>jar</packaging> <name>microservice-deal-broker-cloud-hystrix-dashboard-turbine</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.example</groupId> <artifactId>microservice-deal-parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <!-- Feignf --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!-- springCloud Hystrix依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <!-- springCloud Hystrix依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> <!-- Turbine依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-turbine</artifactId> </dependency> </dependencies> </project>
server: port: 8084 spring: application: name: microservice-deal-broker-cloud-hystrix-dashboard-turbine eureka: client: serviceUrl: #defaultZone: http://localhost:8080/eureka/ defaultZone: http://Dustyone:[email protected]:8080/eureka/ instance: prefer-ip-address: true turbine: # 配置 Eureka 中的 serviceId 列表,指定要監控的服務 app-config: MICROSERVICE-DEAL-BROKER-CLOUD-HYSTRIX-DASHBOARD-2,MICROSERVICE-DEAL-BROKER-CLOUD-HYSTRIX-DASHBOARD aggregator: cluster-config: default cluster-name-expression: "'default'" # 指定叢集名稱 combine-host-port: true #表示同一主機上的服務通過host和port的組合來進行區分,預設情況下是使用host來區分,這樣會使本地除錯有問題 #開放所有的監控節點 management: endpoints: web: exposure: include: '*' #宣告Feign中的Hystrix # 說明:請務必注意,從Spring Cloud Dalston開始,Feign預設是不開啟Hystrix的。 # 因此,如使用Dalston請務必額外設定屬性:feign.hystrix.enabled=true,否則斷路器不會生效。 # 而,Spring Cloud Angel/Brixton/Camden中,Feign預設都是開啟Hystrix的。無需設定該屬性。 feign: hystrix: enabled: true #使用Feign時必須新增以下兩項配置 ribbon: eureka: enabled: true #設定feign的hystrix響應超時時間(必須) hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 5000
@Component public class InitiServlets { /** * 初始化Hystrix 容器 * @return */ @Bean public ServletRegistrationBean<Servlet> getHystrixSerlvet() { // HystrixStreamServlet HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet(); ServletRegistrationBean<Servlet> registrationBean = new ServletRegistrationBean<>(streamServlet); registrationBean.setLoadOnStartup(1); registrationBean.addUrlMappings("/hystrix.stream"); registrationBean.setName("HystrixMricsStreamServlet"); return registrationBean; } }
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients //開啟FeignClient註解 @EnableCircuitBreaker //開啟Hystrix熔斷機制 @EnableHystrixDashboard @EnableTurbine //開啟Hystrix Turbine聚合Dashboard public class MicroserviceDealBrokerHystrixDashBoardTurbineApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceDealBrokerHystrixDashBoardTurbineApplication.class, args); } /** * 1被監控的所有服務節點都要開啟Hystrix.stream監控,不然Turbine會報錯 * * 2訪問Turnbine.stream時需注意 * http://localhost:8084/turbine.stream * unable to connect to command metric stream: * 1)stream地址輸入錯誤,一定要輸入:ip:port/turbine.stream,可能會誤寫成:ip:port/turbine.stream,這樣一來,反覆檢查都不容易檢查出問題,因為本身後者在使用hystrix時是沒錯的,但用turbine時就不對了; * 2)缺少pom依賴或註解 */ }
- 專案結構
-
Eureka Client端服務提消費編寫(單例項) (參考前例)。
-
訪問被Turbine監控的服務節點:
http://localhost:8082/deal/1、http://localhost:8082/dealMock/1、http://localhost:8083/deal/1
、http://localhost:8083/dealMock/1:
-
手動shutdown服務提供方:
-
再次訪問被Turbine監控的服務節點:
http://localhost:8082/deal/1、http://localhost:8082/dealMock/1、http://localhost:8083/deal/1
、http://localhost:8083/dealMock/1:
由此便實現了使用Turbine聚合監控各個服務節點的Hystrix.stream資訊。
小結
- 實現Turbine聚合監控各個服務節點Hystrix.stream資訊,首先需要新增Turbine依賴:spring-cloud-starter-netflix-turbine然後在主類上宣告:@EnableTurbine(Turbine所在的服務節點也必須先實現Hystrix.stream Dashboard監控功能)。
- Turbine監控的所有服務節點必須先實現Hystrix.stream Dashboard監控功能,否則Turbine監控服務會報異常。
- 訪問Turnbine的Hystrix Dashboard時需注意:一定要輸入:ip:port/turbine.stream否則無法進入監控頁面。
- 本小結使用到的案例:microservice-deal-eureka-authentication、microservice-deal-cloud、microservice-deal-broker-cloud-hystrix-dashboard、microservice-deal-broker-cloud-hystrix-dashboard-2、microservice-deal-broker-cloud-hystrix-dashboard-turbine。