springCloud Finchley 实战入门(基于springBoot 2.0.3)【六 Hystrix 仪表盘】
Hystrix儀表盤
通過上一篇我們已經成功的實現了spring cloud對Hystrix的整合了。除此之外,spring cloud還完美的整合了Hystrix的儀表盤組件Hystrix Dashboard。該組件主要是用來實時監控Hystrix的各項指示信息的。通過Hystrix Dashboard反饋的信息,可以幫助我們快速的發現系統中存在的問題,從而即使采取應對方法。
快速入門(單實例監控)
先演示針對單個應用的監控
創建一個module命名為"eureka-hystrix-dashboard",創建時選擇對應的組件
1532502505125.png
創建成功后,我們還需要在pom.xml中添加actuator的依賴:
<?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><groupId>com.example</groupId><artifactId>eureka-hystrix-dashboard</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>eureka-hystrix-dashboard</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Finchley.RELEASE</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</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: hystrix-dashboard server:port: 2001 eureka:client:serviceUrl:defaultZone: http://peer1:8762/eureka/,http://peer2:8763/eureka/在主類"EurekaHystrixDashboardApplication",添加@EnableHystrixDashboard注解,表示開啟Hystrix儀表盤監控
@SpringBootApplication @EnableDiscoveryClient @EnableHystrixDashboard public class EurekaHystrixDashboardApplication {public static void main(String[] args) {SpringApplication.run(EurekaHystrixDashboardApplication.class, args);} }配置完成啟動項目后,訪問地址:http://localhost:2001/hystrix
如果看也下面的頁面就說明項目正常的:
通過 Hystrix Dashboard 主頁面的文字介紹,我們可以知道,Hystrix Dashboard 共支持三種不同的監控方式:
默認的集群監控:通過 URL:http://turbine-hostname:port/turbine.stream 開啟,實現對默認集群的監控。
指定的集群監控:通過 URL:http://turbine-hostname:port/turbine.stream?cluster=[clusterName] 開啟,實現對 clusterName 集群的監控。
單體應用的監控: 通過 URL:http://hystrix-app:port/hystrix.stream 開啟 ,實現對具體某個服務實例的監控。(現在這里的 URL 應該為 http://hystrix-app:port/actuator/hystrix.stream,Actuator 2.x 以后endpoints 全部在/actuator下,可以通過management.endpoints.web.base-path修改)。
前兩者都對集群的監控,需要整合 Turbine 才能實現。
因為我們目前先做實現對單體應用的監控,所以這里的單體應用就用使用 Hystrix 實現的服務提供者"eureka-bussniss-service-user-client-ribbon"
頁面上的另外兩個參數:
Delay:控制服務器上輪詢監控信息的延遲時間,默認為 2000 毫秒,可以通過配置該屬性來降低客戶端的網絡和 CPU 消耗。
Title:該參數可以展示合適的標題。
配置服務提供者
Hystrix Dashboard 監控單實例節點需要通過訪問實例的/actuator/hystrix.stream接口來實現,所以我們需要為服務提供者做一下配置。 在"eureka-bussniss-service-user-client-ribbon"項目的pom文件添加autuator和hystrix的依賴。
<!--添加Hystrix依賴 斷路器容錯保護--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency><!--監控中心--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency>修改application.yml配置
添加management.endpoints.web.exposure.include=hystrix.stream屬性,表示暴露hystrix.stream的監控點地址。
接著在主類名"EurekaBussnissServiceUserClientRibbonApplication"添加@EnableCircuitBreaker 表示啟動Hystrix儀表盤 服務熔斷(ribbon 單服務實例監控)
@SpringBootApplication @EnableEurekaClient @EnableHystrix @EnableCircuitBreaker //Hystrix儀表盤 服務熔斷(ribbon 單服務實例監控) public class EurekaBussnissServiceUserClientRibbonApplication {public static void main(String[] args) {SpringApplication.run(EurekaBussnissServiceUserClientRibbonApplication.class, args);}@Bean@LoadBalanced //開啟客戶端負載均衡RestTemplate restTemplate() {return new RestTemplate();} }重啟"eureka-bussniss-service-user-client-ribbon"項目。
1532504332432.png
在2001項目頁面中輸入"http://localhost:8901/actuator/hystrix.stream",點擊"Monitor Stream ",注意:(spring boot 2.x)監控地址需要加上actuator的路徑。看到下圖,顯示loding...
一直顯示loding...這是因為還沒有檢測到請求,我們訪問一下地址:http://localhost:8901/listUsersByRibbon,然后就可以看到下面的信息了。
頁面上面的信息就是顯示服務的具體情況,具體的信息可以去spring cloud的官網深入了解。這里就不過多解釋了。
到這里我們就已經實現了Hystrix dashboard對單個應用的儀表監控了。
快速入門 (集群監控)
創建一個新的module命名為"eureka-hystrix-dashboard-turbine"。創建時選擇對應的組件依賴
1532505439252.png
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><groupId>com.example</groupId><artifactId>eureka-hystrix-dashboard-turbine</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>eureka-hystrix-dashboard-turbine</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Finchley.RELEASE</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-turbine</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</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: 8989 management:server:port: 8990 eureka:client:serviceUrl:defaultZone: http://peer1:8762/eureka/,http://peer2:8763/eureka/ turbine:appConfig: service-user-ribbonclusterNameExpression: new String("default")combineHostPort: trueturbine.appConfig表示監控的服務實例(通過serviceId;多個服務實例以逗號分隔)
項目的主類名EurekaHystrixDashboardTurbineApplication添加@EnableTurbine注解
@SpringBootApplication @EnableTurbine @EnableEurekaClient public class EurekaHystrixDashboardTurbineApplication {public static void main(String[] args) {SpringApplication.run(EurekaHystrixDashboardTurbineApplication.class, args);} }然后service-user項目pom添加hystrix依賴以及主類添加@EnableHystrix@EnableCircuitBreaker注解。
@SpringBootApplication @EnableEurekaClient @EnableHystrix @EnableCircuitBreaker //Hystrix儀表盤 服務熔斷(ribbon 單服務實例監控) public class EurekaBussnissServiceUserApplication {public static void main(String[] args) {SpringApplication.run(EurekaBussnissServiceUserApplication.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><groupId>com.example</groupId><artifactId>eureka-bussniss-service-user</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>eureka-bussniss-service-user</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Finchley.RELEASE</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency><!--添加Hystrix依賴 斷路器容錯保護--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</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>重啟相關的服務。同樣是在2001頁面輸入對應的地址http://localhost:8989/turbine.stream 點擊Monitor Stream 我們也同樣是可以實現對服務實例的監控。
1532507184545.png
如果aplication.yml中的appConfig是配置了多個的,就會對顯示相應的儀表盤數據。
github 項目源碼
到這里Hystrix儀表盤監控服務實例就基本實現了。接下來我們會用Fegin組件以聲明式服務調用的方式實現Ribbon+Hystrix(負載均衡+服務容錯保護)的功能。
總結
以上是生活随笔為你收集整理的springCloud Finchley 实战入门(基于springBoot 2.0.3)【六 Hystrix 仪表盘】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VS扩展开发 一 导航
- 下一篇: 移动端自动化==什么是Appium