SpringCloud微服務 之hystrix(五)
阿新 • • 發佈:2018-11-09
前言
本小節將學習一下Hystrix的Dashboard。Hystrix Dashboard是Hystrix的儀表盤元件,主要用來實時監控Hystrix的各項指標資訊,通過介面反饋的資訊可以快速發現系統中存在的問題,即服務節點實現Hystrix Metrics Stream,然後通過Hystrix Dashboard將stream的資訊視覺化。下面我們將通過案例來學習一下Hystrix-Dashboard。
案例
-
Eureka Server端編寫(參考前例)。
-
Eureka Client端服務提供方編寫。
-
專案結構
-
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</artifactId> <packaging>jar</packaging> <name>microservice-deal-broker-cloud-hystrix-dashboard</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> <!-- Eureka --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- 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> </dependencies> </project>
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients //開啟FeignClient註解 @EnableCircuitBreaker //開啟Hystrix熔斷機制 @EnableHystrixDashboard public class MicroserviceDealBrokerHystrixDashBoardApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceDealBrokerHystrixDashBoardApplication.class, args); } }
/** * SpringCould 2.0+需要為HystrixMricsStreamServlet手動注入到ServletRegistrationBean中 * @author Administrator * */ @Component public class HystrtixStreamServlet { @Bean public ServletRegistrationBean<Servlet> getSerlvet(){ HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet(); ServletRegistrationBean<Servlet> registrationBean = new ServletRegistrationBean<>(streamServlet); registrationBean.setLoadOnStartup(1); registrationBean.addUrlMappings("/hystrix.stream"); //registrationBean.addUrlMappings("/hystrix"); registrationBean.setName("HystrixMricsStreamServlet"); return registrationBean; } }
@FeignClient(name = "microservice-deal-cloud",fallback = HystrixFallBackMethod.class) public interface FeignConfig { //第一個坑:使用Feign的時候,如果引數中帶有@PathVariable形式的引數,則要用value=""標明對應的引數,否則會丟擲IllegalStateException異常 @GetMapping("/deal/{id}") public Deal findById(@PathVariable(value="id") Long id); @GetMapping("/dealMock/{id}") public Deal findByIdMock(@PathVariable(value="id") Long id); }
@Component public class HystrixFallBackMethod implements FeignConfig { @Override public Deal findById(Long id) { Deal deal = new Deal(); deal.setId(id.intValue()); deal.setName("Dustyone"); return deal; } @Override public Deal findByIdMock(Long id) { Deal deal = new Deal(); deal.setId(id.intValue()); deal.setName("Dustyone"); return deal; } }
@RestController public class BrokerController { @Autowired private FeignConfig feignConfig; @GetMapping("/deal/{id}") public Deal findById(@PathVariable(value="id") Long id) { return this.feignConfig.findById(id); } @GetMapping("/dealMock/{id}") public Deal findByIdMock(@PathVariable(value="id") Long id) { return this.feignConfig.findByIdMock(id); } }
server: port: 8082 spring: application: name: microservice-deal-broker-cloud-hystrix-dashboard eureka: client: serviceUrl: defaultZone: http://localhost:8080/eureka/ instance: prefer-ip-address: true #開放所有的監控節點 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
-
-
Eureka Client端服務提消費編寫(單例項) (參考前例)。
-
持續訪問:http://localhost:8082/deal/2、http://localhost:8082/dealMock/2,然後重新整理Dashboard:
-
一段時間後:
-
從Hystrix-Dashboard上我們可以看見:
- 請求成功次數與佔比率。
- 請求失敗次數與佔比率。
- 請求峰值圖,一段時間內的請求次數總和。
- 該服務節點處理請求頻率。
- 該服務節點的請求處理延遲率。
- 該節點的斷路器狀態。
- 服務提供者相關資訊。
-
手動shutdowm服務提供者:
-
持續訪問:http://localhost:8082/deal/2、http://localhost:8082/dealMock/2,然後重新整理Dashboard:
由此我們便達到了對某個微服務節點實時監控的目的。(實時監控非常消耗系統資源)。
小結
- Dashboard服務是個獨立的結點,不需要配置eureka資訊,需要Hystrix Dashboard 依賴包:spring-cloud-starter-netflix-hystrix-dashboard。
- 啟用Hystrix Dashboard只需在主類上開啟註解:@EnableHystrixDashboard。
- 要確保被監控的服務打開了Actuator,並開啟了斷路器(@EnableCircuitBreaker註解)。同時我們需要對Actuator做訪問時需要對一些節點做宣告,本案例中預設所有監控節點都可以被檢視:
management: endpoints: web:exposure: include: ‘*’。 - SpringCloud V2.0+之後需要手動宣告HystrixMetricsStreamServlet,詳情檢視本小節HystrtixStreamServlet.java。
- 本小節用到的案例:microservice-deal-eureka、microservice-deal-cloud、microservice-deal-broker-cloud-hystrix-dashboard。