1. 程式人生 > >springcloud-知識點總結(三):Hystrix & Dashboard & turbine & Zuul & SpringCloud Config

springcloud-知識點總結(三):Hystrix & Dashboard & turbine & Zuul & SpringCloud Config

blank 分布式 新項目 ride 情況下 processor ctu 構圖 -i

1.Hystrix斷路器簡介

Hystrix斷路器簡介

  hystrix對應的中文名字是“豪豬”,豪豬周身長滿了刺,能保護自己不受天敵的傷害,代表了一種防禦機制,這與hystrix本身的功能不謀而合,因此Netflix團隊將該框架命名為Hystrix,並使用了對應的卡通形象做作為logo。

  在一個分布式系統裏,許多依賴不可避免的會調用失敗,比如超時、異常等,如何能夠保證在一個依賴出問題的情況下,不會導致整體服務失敗,這個就是Hystrix需要做的事情。Hystrix提供了熔斷、隔離、Fallback、cache、監控等功能,能夠在一個、或多個依賴同時出現問題時保證系統依然可用。

2.服務雪崩效應

服務雪崩效應

當一個請求依賴多個服務的時候:

正常情況下的訪問

技術分享圖片

但是,當請求的服務中出現無法訪問、異常、超時等問題時(圖中的I),那麽用戶的請求將會被阻塞。

技術分享圖片

如果多個用戶的請求中,都存在無法訪問的服務,那麽他們都將陷入阻塞的狀態中。

技術分享圖片

Hystrix的引入,可以通過服務熔斷和服務降級來解決這個問題。

3.Hystrix服務熔斷服務降級@HystrixCommand fallbackMethod

Hystrix服務熔斷服務降級@HystrixCommand fallbackMethod

熔斷機制是應對雪崩效應的一種微服務鏈路保護機制。

當某個服務不可用或者響應時間超時,會進行服務降級,進而熔斷該節點的服務調用,快速返回自定義的錯誤影響頁面信息。

我們寫個項目來測試下;

我們寫一個新的帶服務熔斷的服務提供者項目 microservice-student-provider-hystrix-1004

把 配置和 代碼 都復制一份到這個項目裏;

然後修改;

1,pom.xml加下 hystrix支持

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-hystrix</artifactId>

</dependency>

2,application.yml修改下端口和實例名稱

port: 1004

instance-id: microservice-student-hystrix:1004 #客戶端實例名稱

3,啟動類名稱改成StudentProviderHystrixApplication_1004

以及加下註解支持 @EnableCircuitBreaker

4,我們在( microservice-student-provider-hystrix-1004)新增方法getInfo

技術分享圖片
 1  /**
 2  * 獲取信息
 3  * @return
 4  * @throws InterruptedException 
 5  */
 6 @ResponseBody
 7 @GetMapping(value="/getInfo")
 8 @HystrixCommand(fallbackMethod="getInfoFallback")
 9 public Map<String,Object> getInfo() throws InterruptedException{
10     Thread.sleep(2000);
11     Map<String,Object> map=new HashMap<String,Object>();
12     map.put("code", 200);
13     map.put("info", "業務數據xxxxx");
14     return map;
15 }
16  
17 public Map<String,Object> getInfoFallback() throws InterruptedException{
18     Map<String,Object> map=new HashMap<String,Object>();
19     map.put("code", 500);
20     map.put("info", "系統出錯,稍後重試");
21     return map;
22 }
View Code

這裏我正常訪問 返回的是 200 業務數據xxxxx

但是我們這裏Thread.sleep(2000) 模擬超時;

這裏的話 我們加上@HystrixCommand註解 以及 fallbackMethod

表明這個方法我們再 沒有異常以及沒有超時(hystrix默認1秒算超時)的情況,才返回正常的業務數據;

否則,進入我們fallback指定的本地方法,我們搞的是500 系統出錯,稍後重試,有效的解決雪崩效應,以及返回給用戶界面

很好的報錯提示信息;

============================

microservice-student-consumer-80項目也要對應的加個方法

技術分享圖片
 1 /**
 2  * 熔斷器測試方法
 3  * @return
 4  */
 5 @SuppressWarnings("unchecked")
 6 @GetMapping(value="/getInfo")
 7 @ResponseBody
 8 public Map<String,Object> getInfo(){
 9     return restTemplate.getForObject(PRE_HOST+"/student/getInfo/", Map.class);
10 }
View Code

然後我們來測試下;

先啟動三個eureka,再啟動帶hystrix的provider,最後啟動普通的consumer;

因為 Hystrix默認1算超時,所有 sleep了2秒 所以進入自定義fallback方法,防止服務雪崩;

我們這裏改sleep修改成100毫秒;

4.Hystrix默認超時時間設置

Hystrix默認超時時間設置

Hystrix默認超時時間是1秒,我們可以通過hystrix源碼看到,

找到 hystrix-core.jar com.netflix.hystrix包下的HystrixCommandProperties類

default_executionTimeoutInMilliseconds屬性局勢默認的超時時間

技術分享圖片

默認1000毫秒 1秒

我們系統裏假如要自定義設置hystrix的默認時間的話;

application.yml配置文件加上

hystrix:

command:

default:

execution:

isolation:

thread:

timeoutInMilliseconds: 3000

修改成3秒 然後 我們代碼裏sleep修改成2秒測試;

5.Feign Hystrix整合&服務熔斷服務降級徹底解耦

Feign Hystrix整合&服務熔斷服務降級徹底解耦

前面的代碼,用@HystrixCommand fallbackMethod是很不好的,因為和業務代碼耦合度太高,不利於維護,所以需要解耦,這我們講下Feign Hystrix整合。

第一,microservice-student-provider-hystrix-1004項目修改

我們不用原先那套。按照正常的邏輯來寫;

StudentService加新的接口方法:

/**

* 獲取信息 * @return */ public Map<String,Object> getInfo(); StudentServiceImpl寫具體實現: @Override public Map<String, Object> getInfo() { Map<String,Object> map=new HashMap<String,Object>(); map.put("code", 200); map.put("info", "業務數據xxxxx"); return map; } StudentProviderController正常調用service方法: /** * 獲取信息 * @return * @throws InterruptedException */ @ResponseBody @GetMapping(value="/getInfo") public Map<String,Object> getInfo() throws InterruptedException{ Thread.sleep(900); return studentService.getInfo(); }

第二步:microservice-common項目新建FallbackFactory類,解耦服務熔斷服務降級

StudentClientService接口,新增getInfo方法;

/**

* 獲取信息 * @return */ @GetMapping(value="/student/getInfo") public Map<String,Object> getInfo(); 新建 StudentClientFallbackFactory 類,實現FallbackFactory<StudentClientService>接口; 技術分享圖片
 1 @Component
 2 public class StudentClientFallbackFactory implements FallbackFactory<StudentClientService>{
 3  
 4     @Override
 5     public StudentClientService create(Throwable cause) {
 6         // TODO Auto-generated method stub
 7         return new StudentClientService() {
 8              
 9             @Override
10             public boolean save(Student student) {
11                 // TODO Auto-generated method stub
12                 return false;
13             }
14              
15             @Override
16             public List<Student> list() {
17                 // TODO Auto-generated method stub
18                 return null;
19             }
20              
21             @Override
22             public Map<String, Object> getInfo() {
23                 Map<String,Object> map=new HashMap<String,Object>();
24                 map.put("code", 500);
25                 map.put("info", "系統出錯,稍後重試");
26                 return map;
27             }
28              
29             @Override
30             public Student get(Integer id) {
31                 // TODO Auto-generated method stub
32                 return null;
33             }
34              
35             @Override
36             public boolean delete(Integer id) {
37                 // TODO Auto-generated method stub
38                 return false;
39             }
40         };
41     }
42  
43 }
View Code

StudentClientService接口的@FeignClient註解加下 fallbackFactory屬性

@FeignClient(value="MICROSERVICE-STUDENT",fallbackFactory=StudentClientFallbackFactory.class)

這類我們實現了 降級處理方法實現;

最後測試即可。

6.Hystrix服務監控Dashboard

Hystrix服務監控Dashboard儀表盤

Hystrix提供了 準實時的服務調用監控項目Dashboard,能夠實時記錄通過Hystrix發起的請求執行情況,

可以通過圖表的形式展現給用戶看。

我們新建項目:microservice-student-consumer-hystrix-dashboard-90

加依賴:

技術分享圖片
 1 <dependency>
 2     <groupId>org.springframework.cloud</groupId>
 3     <artifactId>spring-cloud-starter-hystrix</artifactId>
 4 </dependency>
 5 <dependency>
 6     <groupId>org.springframework.cloud</groupId>
 7     <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
 8 </dependency>
 9 <dependency>
10     <groupId>org.springframework.boot</groupId>
11     <artifactId>spring-boot-starter-actuator</artifactId>
12 </dependency>
View Code

application.yml配置

server:

port: 90

context-path: /

新建啟動類:StudentConsumerDashBoardApplication_90

加註解:@EnableHystrixDashboard

技術分享圖片
1 @SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
2 @EnableHystrixDashboard
3 public class StudentConsumerDashBoardApplication_90 {
4  
5     public static void main(String[] args) {
6         SpringApplication.run(StudentConsumerDashBoardApplication_90.class, args);
7     }
8 }
View Code

這樣就完事了。

我們啟動這個項目;

然後瀏覽器輸入:http://localhost:90/hystrix

技術分享圖片

出現這個 就說明OK;

然後我們來測試下;

我們啟動三個eureka,然後再啟動microservice-student-provider-hystrix-1004

我們直接請求http://localhost:1004/student/getInfo

返回正常業務

我們監控的話,http://localhost:1004/hystrix.stream 這個路徑即可;

一直是ping,然後data返回數據;

用圖形化的話

技術分享圖片

輸入 ,然後點擊按鈕即可;

技術分享圖片

指標含義:

技術分享圖片

各種情況:

技術分享圖片

7.Hystrix集群監控turbine

前面Dashboard演示的僅僅是單機服務監控,實際項目基本都是集群,所以這裏集群監控用的是turbine。

turbine是基於Dashboard的。

先搞個集群;

再microservice-student-provider-hystrix-1004項目的基礎上再搞一個microservice-student-provider-hystrix-1005

代碼和配置都復制一份,然後修改幾個地方;

第一 yml配置

server:

port: 1005

instance-id: microservice-student-hystrix:1005 #客戶端實例名稱

第二 啟動類改成StudentProviderHystrixApplication_1005

這樣的話 就有了 hystrix集群服務;

我們新建項目microservice-student-consumer-hystrix-turbine-91

pom.xml加下依賴

技術分享圖片
1 <dependency>
2     <groupId>org.springframework.boot</groupId>
3     <artifactId>spring-boot-starter-actuator</artifactId>
4 </dependency>
5 <dependency>
6     <groupId>org.springframework.cloud</groupId>
7     <artifactId>spring-cloud-starter-turbine</artifactId>
8 </dependency>
View Code

8.Zuul API路由網關服務簡介

Zuul API路由網關服務簡介

技術分享圖片

請看上圖,這裏的API 路由網關服務 由Zuul實現,主要就是對外提供服務接口的時候,起到了請求的路由和過濾作用,也因此能夠隱藏內部服務的接口細節,從來有利於保護系統的安全性;

9.SpringCloud Config簡介

SpringCloud Config簡介

Spring Cloud Config 是 Spring Cloud 團隊創建的一個全新項目,用來為分布式系統中的基礎設施和微服務應用提供集中化的外部配置支持,它分為服務端與客戶端兩個部分。其中服務端也稱為分布式配置中心,它是一個獨立的微服務應用,用來連接配置倉庫並為客戶端提供獲取配置信息、加密 / 解密信息等訪問接口;而客戶端則是微服務架構中的各個微服務應用或基礎設施,它們通過指定的配置中心來管理應用資源與業務相關的配置內容,並在啟動的時候從配置中心獲取和加載配置信息。Spring Cloud Config 實現了對服務端和客戶端中環境變量和屬性配置的抽象映射,所以它除了適用於 Spring 構建的應用程序之外,也可以在任何其他語言運行的應用程序中使用。由於 Spring Cloud Config 實現的配置中心默認采用 Git 來存儲配置信息,所以使用 Spring Cloud Config 構建的配置服務器,天然就支持對微服務應用配置信息的版本管理,並且可以通過 Git 客戶端工具來方便的管理和訪問配置內容。當然它也提供了對其他存儲方式的支持,比如:GIT倉庫、SVN 倉庫、本地化文件系統。

技術分享圖片

Config Server端主要和Git/SVN服務器

通俗點,就是統一管理配置,包括方便切換環境配置,以及修改配置無需動代碼,省心省力;

如果用上SpringCloud Bus,能實現無需重啟,自動感知配置變化以及應用新配置;

技術分享圖片

10.Config Server基本使用

Config Server基本使用

根據前面SpringCloud架構圖,首先第一步,要搞個 configServer來聯通遠程GIT倉庫,來讀取遠程配置;

這裏GIT倉庫,我們一般選用GitHub https://github.com/,或者碼雲 https://gitee.com/

我們課程用GitHub演示,首先大夥去GitHub註冊個賬號,

建個倉庫 microservice-config 然後 Git下載本地;

上傳一個配置文件上到git倉庫,application.yml 記住要utf-8編碼,否則亂碼,解析各種問題;

springcloud-知識點總結(三):Hystrix & Dashboard & turbine & Zuul & SpringCloud Config