前言
本篇主要介紹的是SpringCloud中的斷路器(Hystrix)和斷路器名額看闆(Dashboard)的相關使用知識。
SpringCloud Hystrix
Hystrix 介紹
Netflix建立了一個名為Hystrix的庫,它實作了斷路器模式。主要的目的是為了解決服務雪崩效應的一個元件,是保護服務高可用的最後一道防線。
開發準備
開發環境
- JDK:1.8
- SpringBoot:2.1.1.RELEASE
- SpringCloud:Finchley
注:不一定非要用上述的版本,可以根據情況進行相應的調整。需要注意的是SpringBoot2.x以後,jdk的版本必須是1.8以上!
确認了開發環境之後,我們再來添加相關的pom依賴。
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<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>
</dependencies>
注: 實際上這裡是不需要Hystrix依賴的,Fegin已經添加了Hystrix依賴。
SpringCloud Hystrix 示例
由于Hystrix機制是在微服務項目上進行的,并且Fegin中包含了該機制。是以這裡我們可以把之前的
springcloud-feign
的項目進行簡單的改造就行了。
服務端
首先是服務端這塊,為了進行區分,建立一個
springcloud-hystrix-eureka
的項目,用于做注冊中心。 代碼和配置和之前的基本一樣。
application.properties
配置資訊:
spring.application.name=springcloud-hystrix-eureka-server
server.port=8002
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:8002/eureka/
配置說明:
- spring.application.name: 這個是指定服務名稱。
- server.port:服務指定的端口。
- eureka.client.register-with-eureka:表示是否将自己注冊到Eureka Server,預設是true。
- eureka.client.fetch-registry:表示是否從Eureka Server擷取注冊資訊,預設為true。
- eureka.client.serviceUrl.defaultZone: 這個是設定與Eureka Server互動的位址,用戶端的查詢服務和注冊服務都需要依賴這個位址。
服務端這邊隻需要在SpringBoot啟動類添加
@EnableEurekaServer
注解就可以了,該注解表示此服務是一個服務注冊中心服務。
代碼示例:
@EnableEurekaServer
@SpringBootApplication
public class HystrixEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixEurekaApplication.class, args);
System.out.println("hystrix注冊中心服務啟動...");
}
}
用戶端
這裡我們把之前的
springcloud-fegin-consumer
項目稍微改造下,項目名改為
springcloud-hystrix-consumer
。然後在
application.properties
配置檔案新增如下配置,
feign.hystrix.enabled
配置表示是否啟用熔斷機制。
feign.hystrix.enabled=true
增加了配置之後,我們在來把之前的fegin進行定義轉發服務的
@FeignClient
注解進行添加一個回調方法
fallback
。代碼改造後的實作如下:
@FeignClient(name= "springcloud-hystrix-consumer2",fallback = HelloRemoteHystrix.class)
public interface HelloRemote {
@RequestMapping(value = "/hello")
public String hello(@RequestParam(value = "name") String name);
}
最後新增一個回調類,用于處理斷路的情況。這裡我們就簡單的處理下,傳回錯誤資訊即可!
@Component
public class HelloRemoteHystrix implements HelloRemote{
@Override
public String hello(@RequestParam(value = "name") String name) {
return name+", 請求另一個服務失敗!";
}
}
其餘的代碼就不展示了,和之前的
springcloud-fegin-consumer
項目中的一緻,詳細的可以在這篇博文SpringCloud學習系列之二 ----- 服務消費者(Feign)和負載均衡(Ribbon)進行檢視。
然後在把之前的
springcloud-feign-consumer2
進行簡單的改造下,項目名稱改為
springcloud-hystrix-consumer2
。然後更改下配置的端口。
功能測試
完成如上的工程開發之後,我們依次啟動服務端和用戶端的
springcloud-hystrix-eureka
、
springcloud-hystrix-consumer
和
springcloud-hystrix-consumer2
這三個程式,然後在浏覽器界面輸入:
http://localhost:8002/
,即可檢視注冊中心的資訊。
首先在浏覽器輸入:
http://localhost:9004/hello/pancm
控制台列印:
接受到請求參數:pancm,進行轉發到其他服務
浏覽器傳回:
pancm,Hello World
然後再輸入:
http://localhost:9005/hello?name=pancm
pancm,Hello World
說明程式運作正常,fegin的調用也ok。這時我們在進行斷路測試。
停止
springcloud-hystrix-consumer2
這個服務,然後在到浏覽器輸入:
http://localhost:9004/hello/pancm
進行檢視資訊。
接受到請求參數:pancm,進行轉發到其他服務
pancm, 請求另一個服務失敗!
出現以上結果說明斷路器的功能已經實作了。
示例圖:

SpringCloud Hystrix-Dashboard
Hystrix-Dashboard 介紹
Hystrix-dashboard是一款針對Hystrix進行實時監控的工具,通過Hystrix Dashboard我們可以在直覺地看到各Hystrix Command的請求響應時間, 請求成功率等資料。
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</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-web</artifactId>
</dependency>
</dependencies>
注:
spring-boot-starter-actuator
這個必須需要要,該jar可以得到SpringBoot項目的各種資訊,關于該jar包的使用,可以在springboot-actuator這個項目中進行檢視。
SpringCloud Hystrix-Dashboard 示例
這裡我們把上面的
springcloud-hystrix-consumer
項目進行改造下,項目名稱改為
springcloud-hystrix-dashboard-consumer
。然後在到啟動類新增如下配置。
- EnableCircuitBreaker:表示啟用hystrix功能。
- EnableHystrixDashboard:啟用 HystrixDashboard 斷路器看闆 相關配置。
完整的啟動類配置如下:
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrixDashboard
@EnableCircuitBreaker
@EnableFeignClients
public class HystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardApplication.class, args);
System.out.println("hystrix dashboard 服務啟動...");
}
}
然後在到
application.properties
配置檔案中新增如下配置:
management.endpoints.web.exposure.include=hystrix.stream
management.endpoints.web.base-path=/
該配置的意思是指定hystrixDashboard的通路路徑,SpringBoot2.x以上必須指定,不然是無法進行通路的,通路會出現
Unable to connect to Command Metric Stream
錯誤。
如果不想使用配置的話,也可以使用代碼進行實作。
實作的代碼如下:
@Component
public class HystrixServlet extends Servlet{
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
完成如上的工程改造之後,我們啟動該程式。
然後在浏覽器輸入:
http://localhost:9010/hystrix
會出現以下的界面:
可以通過該界面監控使用了hystrix dashboard的項目。這裡我們依照提示在中間的輸入框輸入如下的位址:
http://localhost:9010/hystrix.stream
該界面就是Hystrix Dashboard監控的界面了,通過這個界面我們可以很詳細的看到程式的資訊。關于這些資訊中說明可以用網上找到的一張來加以說明。
注: 如果界面一直提示loading,那麼是因為沒有進行請求通路,隻需在到浏覽器上輸入:
http://localhost:9010/hello/pancm
,然後重新整理該界面就可以進行檢視了。
其他
springcloud系列部落格:
- SpringCloud學習系列之一 ----- 搭建一個高可用的注冊中心(Eureka)
- SpringCloud學習系列之二 ----- 服務消費者(Feign)和負載均衡(Ribbon)
項目位址
基于SpringBoot2.x、SpringCloud的Finchley版本開發的位址:https://github.com/xuwujing/springcloud-study
基于SpringBoot1.x、SpringCloud 的Dalston版本開發的位址: https://github.com/xuwujing/springcloud-study-old
如果感覺項目不錯,希望能給個star,謝謝!