前些天發現了一個巨牛的人工智能學習網站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉到教程。
前面有用過 Hystrix 熔斷,在多服務運行時。可以通過?Hystrix 的監控面板來實時觀察各個服務的運行健康、效率和請求量等。
首先從《Spring Cloud微服務實戰》作者的博客中借用一圖:
此圖出自:http://blog.didispace.com/spring-cloud-starter-dalston-5-1/
這圖是怎么來的呢? 下面一步一步來實現。?
1. 新建一個 springboot 工程?hystrix-dashboard,實現 hystrix?的監控面板。( 工程詳細新建方式在前面幾篇博客中均有說明 )
1.1 工程結構:
?1.2? pom 依賴:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com</groupId><artifactId>hystrix-dashboard</artifactId><version>0.0.1-SNAPSHOT</version><name>hystrix-dashboard</name><description>服務監控-面板</description><properties><java.version>1.8</java.version></properties><parent><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-parent</artifactId><version>Dalston.SR1</version><relativePath /></parent><dependencies><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><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><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
1.3?application.properties :
# 端口
server.port= 8888# 工程名
spring.application.name= hystrix-dashboard# 修改 Hystrix 默認超時時間
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds= 2000
1.4 主啟動類:
package com.hystrixdashboard;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;// 開啟監控面板
@EnableHystrixDashboard@SpringBootApplication
public class HystrixDashboardApplication {public static void main(String[] args) {SpringApplication.run(HystrixDashboardApplication.class, args);}}
此時,啟動工程,瀏覽器訪問:http://localhost:8888/hystrix.stream
面板主頁面已經有了,但是無任何實際監控信息。要查看具體內容需在第一個輸入框中輸入監控對象 相關服務的 URL。
如頁面上提示的信息所示:有 3 種 URL 方式可以查看監控內容。
前 2 者都是監控集群服務時要輸入的 UR,最后一種為監控單個應用服務的 URL :
默認集群 :http://turbine-hostname:port/turbine.stream?指定集群:http://turbine-hostname:port/turbine.stream?cluster=[clusterName]指定單個服務:http://hystrix-app:port/hystrix.stream
2. 改造已有工程 ribbon?,把其服務納入 Hystrix 的監控中。
2.1 pom 中加入依賴,開啟 hystrix 監控:
<!-- 開啟 hystrix 監控 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency>
此時 ribbon?完整 pom 依賴為:
<?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.ribbon</groupId><artifactId>service-ribbon</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>service-ribbon</name><description>服務消費 ribbon 方式</description><parent><groupId>com.base</groupId><artifactId>base-config</artifactId><version>0.0.1-SNAPSHOT</version></parent><dependencies><!-- 開啟 hystrix 監控 --> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</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-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId></dependency></dependencies><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories></project>
2.2 確認啟動類有注解: Hystrix @EnableHystrix,以 開啟斷路器。同時加上以下代碼,以保證 Hystrix 映射正常運行:
@Beanpublic ServletRegistrationBean hystrixMetricsStreamServlet() {ServletRegistrationBean registration = new ServletRegistrationBean(new HystrixMetricsStreamServlet());registration.addUrlMappings("/hystrix.stream");return registration;}
此時 ribbon?完整啟動類為:
package com.ribbon.serviceribbon;import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;@SpringBootApplication// 開啟斷路器: Hystrix
@EnableHystrix// 標明自已為服務
@EnableDiscoveryClient
public class ServiceRibbonApplication {public static void main(String[] args) {SpringApplication.run(ServiceRibbonApplication.class, args);}/*** 向 ioc 注入 bean : restTemplate;* 注解 @LoadBalanced :此 bean 開啟負載均衡。* @return*/@Bean@LoadBalancedRestTemplate restTemplate() {return new RestTemplate();}@Beanpublic ServletRegistrationBean hystrixMetricsStreamServlet() {ServletRegistrationBean registration = new ServletRegistrationBean(new HystrixMetricsStreamServlet());registration.addUrlMappings("/hystrix.stream");return registration;}
}
2.3? 配置文件 application.properties 中增加:?management.endpoints.web.exposure.include= hystrix.stream,以配置 actuator 的?endpoint,完整配置為:
# 注冊中心 - 端口: 1234、工程名: eureka (見 eureka 工程中配置)。
eureka.client.serviceUrl.defaultZone= http://localhost:1234/eureka/# 端口
server.port= 8701# 工程名
spring.application.name= ribbon# 也可配置為'*'
management.endpoints.web.exposure.include= hystrix.stream
3.依次啟動工程:注冊中心 eureka 、服務應用 ribbon 、監控面板 hystrix 、服務應用 seeParam 。
關于?ribbon 和 seeParam 詳見文章:springCloud - 第5篇 - 斷路器 Hystrix ( Feign 、Ribbon )
3.1 注冊中心可見 ,2 個服務都已注冊:
3.2? 此時查看單個服務 ribbon 的運行狀況,在 Hystrix 界面第一個輸入框中輸入:?http://localhost:8701/hystrix.stream
可以看到 :
3.3? 當 seeParam 工程運行正常時,熔斷未觸發,此時 ribbon 服務也運行正常。
多次刷新請求?http://localhost:8701/seeParam?param=99?,這樣才會在 hystrix 中有統計數據,2分鐘內請求如下:
如文初說明,線條的高低起伏變化表示請求量。
而圓球則主要表示2層意思:
1.圓球的顏色可能變化為:綠色、黃色、橙色、紅色,分別對應服務的健康程度,綠色健康程度最高,紅色最差。2.球體大小變化:圓越大則代表請求量越大,同理,圓越小則代表請求量越小。
3.4 停掉工程?seeParam 服務時,熔斷觸發,此時 ribbon 服務運行正常,但請求seeParam 失敗。
多次刷新請求?http://localhost:8701/seeParam?param=99?,這時2分鐘內,在 hystrix 中統計數據如下:
如上圖 紅框中所示,seeParam 工程服務請求超時率為 100% 。
到此單個服務的監控就實現了。
PS:遇到報錯:
com.sun.jersey.api.client.ClientHandlerException:java.net.ConnectException: Connection refused: connect
解決方式:(其實文中 已經有解決方法了)在被監控服務 ribbon 中加上 2 點:
1) 配置文件:
# 也可配置為'*'
management.endpoints.web.exposure.include= hystrix.stream
2)啟動類:
@Beanpublic ServletRegistrationBean hystrixMetricsStreamServlet() {ServletRegistrationBean registration = new ServletRegistrationBean(new HystrixMetricsStreamServlet());registration.addUrlMappings("/hystrix.stream");return registration;}
此解決方法出自:?https://blog.csdn.net/WYA1993/article/details/82419131
-------------------------------------------------------------
下一篇: springCloud - 第13篇 - 服務監控 集群模式 Hystrix-turbine
源碼見:
https://gitee.com/FJ_WoMenDeShiJie/springcloud-seeParam
https://gitee.com/FJ_WoMenDeShiJie/springcloud-ribbon
https://gitee.com/FJ_WoMenDeShiJie/springcloud-hystrix-dashboard
-------------------------------------------------------------
PS:這個系列不定時更新,只是個人的學習分享,
內容全程參考書目:
《Spring Cloud 與 Docker 微服務架構空實戰?》、
《Spring Cloud 微服務實戰》及此書作者博客:http://blog.didispace.com/spring-cloud-learning/
《深入理解 Spring Cloud 與微服務構建》及此書作者博客:https://blog.csdn.net/forezp/article/details/70148833
----------------------------------------------------------------