SpringCloud hystrix服務(wù)降級(jí)學(xué)習(xí)筆記
一、Hystrix簡(jiǎn)介
1、Hystrix是什么
流量高峰時(shí),一個(gè)單節(jié)點(diǎn)的宕機(jī)或延遲,會(huì)迅速導(dǎo)致所有服務(wù)負(fù)載達(dá)到飽和。應(yīng)用中任何一個(gè)可能通過(guò)網(wǎng)絡(luò)訪問(wèn)其他服務(wù)的節(jié)點(diǎn),都有可能成為造成潛在故障的來(lái)源。更嚴(yán)重的是,還可能導(dǎo)致服務(wù)之間的延遲增加,占用隊(duì)列、線程等系統(tǒng)資源,從而導(dǎo)致多系統(tǒng)之間的級(jí)聯(lián)故障。
簡(jiǎn)單的說(shuō),當(dāng)服務(wù)器依次請(qǐng)求A->P->H->I時(shí),如果其中一個(gè)發(fā)生了故障,都有可能導(dǎo)致整個(gè)流程失敗,或者降低整個(gè)流程的效率。
更嚴(yán)重的是,當(dāng)網(wǎng)絡(luò)請(qǐng)求是通過(guò)第三方的一個(gè)黑盒客戶端來(lái)發(fā)起時(shí),實(shí)現(xiàn)細(xì)節(jié)都被隱藏起來(lái)了,而且還可能頻繁變動(dòng),這樣發(fā)生問(wèn)題時(shí)就很難監(jiān)控和改動(dòng)。如果這個(gè)第三方還是通過(guò)傳遞依賴的,主應(yīng)用程序中根本沒(méi)有顯示地寫出調(diào)用的代碼,那就更難了。
網(wǎng)絡(luò)連接失敗或者有延遲,服務(wù)將會(huì)產(chǎn)生故障或者響應(yīng)變慢,最終反應(yīng)成為一個(gè) bug。
所有上述表現(xiàn)出來(lái)的故障或延遲,都需要一套管理機(jī)制,將節(jié)點(diǎn)變得相對(duì)獨(dú)立,這樣任何一個(gè)單節(jié)點(diǎn)故障,都至少不會(huì)拖垮整個(gè)系統(tǒng)的可用性。
所以Hystrix就是為了解決請(qǐng)求序列中請(qǐng)求出現(xiàn)問(wèn)題的情況,核心原則就是丟車保帥,如果一個(gè)請(qǐng)求出現(xiàn)問(wèn)題,常見(jiàn)的有兩種方案:
一種是按照原來(lái)的方案再來(lái)一次,這是有成功的可能的。
一種是只是反饋給用戶這個(gè)請(qǐng)求有問(wèn)題,出故障的請(qǐng)求不會(huì)影響到序列中其他的請(qǐng)求。
2、Hystrix能干什么
1、服務(wù)熔斷
2、服務(wù)降級(jí)
3、數(shù)據(jù)監(jiān)控
二、服務(wù)熔斷
1、服務(wù)熔斷簡(jiǎn)介
熔斷機(jī)制是賭贏雪崩效應(yīng)的一種微服務(wù)鏈路保護(hù)機(jī)制。
當(dāng)扇出鏈路的某個(gè)微服務(wù)不可用或者響應(yīng)時(shí)間太長(zhǎng)時(shí),會(huì)進(jìn)行服務(wù)的降級(jí),進(jìn)而熔斷該節(jié)點(diǎn)微服務(wù)的調(diào)用,快速返回錯(cuò)誤的響應(yīng)信息。檢測(cè)到該節(jié)點(diǎn)微服務(wù)調(diào)用響應(yīng)正常后恢復(fù)調(diào)用鏈路。在SpringCloud框架里熔斷機(jī)制通過(guò)Hystrix實(shí)現(xiàn)。Hystrix會(huì)監(jiān)控微服務(wù)間調(diào)用的狀況,當(dāng)失敗的調(diào)用到一定閥值缺省是5秒內(nèi)20次調(diào)用失敗,就會(huì)啟動(dòng)熔斷機(jī)制。熔斷機(jī)制的注解是:@HystrixCommand。
服務(wù)熔斷解決如下問(wèn)題:
- 當(dāng)所依賴的對(duì)象不穩(wěn)定時(shí),能夠起到快速失敗的目的;
- 快速失敗后,能夠根據(jù)一定的算法動(dòng)態(tài)試探所依賴對(duì)象是否恢復(fù)。
2、配置pom.xml
首先新建一個(gè)module,和springcloud-provider-dept-8001一模一樣(名字除外),修改一下application.yaml,一個(gè)是端口(也可以不改,最好修改一下,避免端口沖突),另一個(gè)是instance-id,這樣在尤里卡中可以分辨出來(lái)。
需要引入hystrix的依賴,和feign一樣,只是把feign換成hystrix
<!--引入hystrix依賴--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> <version>1.4.6.RELEASE</version> </dependency>
3、配置application.yaml
和springcloud-provider-dept-8001一樣,只需要修改一下端口號(hào)和instance-id
#端口號(hào)
server:
port: 8001#配置數(shù)據(jù)庫(kù)
spring:
datasource:
url: jdbc:mysql:///dp80?serverTimezone=UTC
username: root
password: 200201203332
driver-class-name: com.mysql.jdbc.Driver
application:
name: springcloud-provider-dept
debug: true#配置Eureka的配置
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/
instance:
instance-id: springcloud-provider-8001info: #autuator監(jiān)控信息
app.name: rainhey-springcloud
company.name: www.rainhey.com
4、修改Controller
@HystrixCommand注解
用來(lái)處理服務(wù)熔斷和服務(wù)降級(jí),屬性和方法可以點(diǎn)進(jìn)去查看。
@HystrixCommand注解通常放到方法之上,當(dāng)這個(gè)方法拋出異常的時(shí)候,系統(tǒng)不會(huì)立刻停止處理異常,而是先找Hystrix里面綁定的備用方法,然后去執(zhí)行備用方法。
那么@HystrixCommand如何綁定備用方法呢?見(jiàn)下圖
@HystrixCommand注釋通過(guò)內(nèi)部屬性fallbackMethod綁定方法,具體步驟是@HystrixCommand(fallbackMethod = “備用方法名”)
package com.you.Controller; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import com.you.mapper.DeptMapper; import com.you.pojo.Dept; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @ResponseBody @Component public class DeptController { @Autowired DeptMapper deptMapper; @GetMapping("/dept/aDept/{id}") @HystrixCommand(fallbackMethod = "hystrixGetDeptOfId") public Dept getDeptOfId(@PathVariable("id") Long id) { System.out.println("進(jìn)來(lái)了,id是:"+id); Dept dept = deptMapper.finaDeptOfId(id); if(dept==null) { throw new RuntimeException("id=>"+id+"不存在該用戶,或者信息沒(méi)有找到!"); } return dept; } /* 備用方案 */ public Dept hystrixGetDeptOfId(@PathVariable("id") Long id) { Dept dept = new Dept(); dept.setDeptno(id); dept.setDeptname("id=>"+id+"不存在該用戶,或信息沒(méi)有找到!@Hystrix"); dept.setDb_source("no info in datebase"); System.out.println("dept的值是:"+dept); return dept; } }
5、修改啟動(dòng)類
從前面的4篇文章里我們了解到,每次用到新的技術(shù),啟動(dòng)類里都會(huì)增加一個(gè)新的注解,那么?Hystrix的注解是什么呢?@EnableCircuitBreaker,只要把這個(gè)注解加在主啟動(dòng)類上,那么主啟動(dòng)類就可以處理服務(wù)熔斷了。
6、效果圖
不開(kāi)熔斷機(jī)制
開(kāi)了熔斷機(jī)制
三、服務(wù)降級(jí)
1、什么是服務(wù)降級(jí)
服務(wù)降級(jí)是指 當(dāng)服務(wù)器壓力劇增的情況下,根據(jù)實(shí)際業(yè)務(wù)情況及流量,對(duì)一些服務(wù)和頁(yè)面有策略的不處理,或換種簡(jiǎn)單的方式處理,從而釋放服務(wù)器資源以保證核心業(yè)務(wù)正常運(yùn)作或高效運(yùn)作。說(shuō)白了,就是盡可能的把系統(tǒng)資源讓給優(yōu)先級(jí)高的服務(wù)。
例如假設(shè)有三個(gè)服務(wù)器ABC,在某一段時(shí)間,訪問(wèn)A的用戶特別多,導(dǎo)致A服務(wù)器快要崩潰了,這時(shí)候訪問(wèn)B、C的用戶很少,那么我暫時(shí)先把B、C停掉,用來(lái)處理A。
服務(wù)降級(jí)主要用于什么場(chǎng)景呢?當(dāng)整個(gè)微服務(wù)架構(gòu)整體的負(fù)載超出了預(yù)設(shè)的上限閾值或即將到來(lái)的流量預(yù)計(jì)將會(huì)超過(guò)預(yù)設(shè)的閾值時(shí),為了保證重要或基本的服務(wù)能正常運(yùn)行,可以將一些 不重要 或 不緊急 的服務(wù)或任務(wù)進(jìn)行服務(wù)的 延遲使用 或 暫停使用。
降級(jí)的方式可以根據(jù)業(yè)務(wù)來(lái),可以延遲服務(wù),比如延遲給用戶增加積分,只是放到一個(gè)緩存中,等服務(wù)平穩(wěn)之后再執(zhí)行 ;或者在粒度范圍內(nèi)關(guān)閉服務(wù),比如關(guān)閉相關(guān)文章的推薦。
2、DeptClientFailBackFactory類
在springcloud-api模塊下的service包中新建降級(jí)配置,DeptClientServiceFallBackFactory.java。同時(shí)實(shí)現(xiàn)其方法,他的返回類型就是我們寫服務(wù)的接口。
package com.you.service; import com.you.pojo.Dept; import feign.hystrix.FallbackFactory; import org.springframework.stereotype.Component; /*!??!降級(jí)?。。?/ @Component //注冊(cè)到容器里面去 public class DeptClientFailBackFactory implements FallbackFactory { @Override public DeptClientService create(Throwable throwable) { return new DeptClientService() { @Override public Dept getDeptOfId(Long id) { Dept dept = new Dept(); dept.setDeptno(id); dept.setDeptname("id=>"+id+"沒(méi)有對(duì)應(yīng)的信息,客戶端提供了降級(jí)的信息,這個(gè)服務(wù)現(xiàn)在被關(guān)閉了!"); dept.setDb_source("沒(méi)有數(shù)據(jù)"); return dept; } }; } }
3、添加注解
在DeptClientService中指定降級(jí)配置:DeptClientServiceFallBackFactory
package com.you.service; import com.you.pojo.Dept; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @Component /*@FeignClient:value的值即為 服務(wù)的名字,fallbackFactory的值即為 實(shí)現(xiàn)FallbackFactory類的名字 */ @FeignClient(value = "SPRINGCLOUD-PROVIDER-DEPT",fallbackFactory = DeptClientFailBackFactory.class) public interface DeptClientService { @GetMapping("/dept/aDept/{id}") public Dept getDeptOfId(@PathVariable("id") Long id); }
4、修改application.yaml
為的是開(kāi)啟服務(wù),但切記此處修改的application是springcloud-consumer-dept-feign下的application.yaml,添加如下代碼
#開(kāi)啟降級(jí) feign-hystrix
feign:
hystrix:
enabled: true
5、效果圖
依次啟動(dòng)springcloud-eureka-7001、springcloud-consumer-dept-feign、springcloud-provider-dept-8001,訪問(wèn)服務(wù)
正確打開(kāi)服務(wù)
現(xiàn)在,停掉這個(gè)springcloud-provider-dept-8001,模擬服務(wù)降級(jí)中暫時(shí)關(guān)閉的一些端口。
8001端口的服務(wù)被停掉了,這時(shí)候刷新頁(yè)面。
端口是可以訪問(wèn)的,只是顯示了我們?cè)O(shè)計(jì)的文字
四、DashBorder
1、新建一個(gè)module
新建一個(gè)名為springcloud-consumer-hystrix-dashborder的module
2、pom.xml配置
將springcloud-provider-dept-8001的依賴如復(fù)制過(guò)來(lái),并且增加上dashborder的依賴
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.you</groupId> <artifactId>springcloud-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--引入Eureka的依賴--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!--引入ribbon--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!--引入DashBorder--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!--引入hystrix依賴--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> <version>1.4.6.RELEASE</version> </dependency> </dependencies>
3、配置application.yml
只需要配置端口號(hào)
server:
port: 9001
4、配置啟動(dòng)類
創(chuàng)建一個(gè)名為DeptConsumerDashBorder_9001的JAVA類
package com.you; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; @SpringBootApplication @EnableHystrixDashboard public class DeptConsumerDashBorder_9001 { public static void main(String[] args) { SpringApplication.run(DeptConsumerDashBorder_9001.class,args); } }
5、添加被監(jiān)控的類
給springcloud-provider-dept-hystrix-8001模塊下的主啟動(dòng)類添加如下代碼,添加監(jiān)控
package com.you; 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.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient /*對(duì)熔斷的支持*/ @EnableCircuitBreaker public class DeptApplication_Hystrix_8001 { public static void main(String[] args) { SpringApplication.run(DeptApplication_Hystrix_8001.class,args); } @Bean public ServletRegistrationBean hystrixMetricsStreamServlet(){ ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet()); //訪問(wèn)該頁(yè)面就是監(jiān)控頁(yè)面 registrationBean.addUrlMappings("/actuator/hystrix.stream"); return registrationBean; } }
6、效果圖
啟動(dòng)springcloud-eureka-7001、springcloud-consumer-hystrix-dashborder、springcloud-provider-dept-hystrix-8001
在7001尤里卡界面,看到springcloud-provider-dept-hystrix-8001被注冊(cè)進(jìn)來(lái)。
訪問(wèn)8011,輸入http://localhost:8011/dept/aDept/1(我設(shè)置的端口號(hào)是8011),可以看到被成功訪問(wèn)。
訪問(wèn)http://localhost:8011/actuator/hystrix.stream,看到如下效果
訪問(wèn)9001,地址輸入http://localhost:9001/hystrix,看到如下界面
將地址填入,http://localhost:8011/actuator/hystrix.stream
刷新這個(gè)界面,下面的圖像會(huì)隨之變化
到此這篇關(guān)于SpringCloud hystrix服務(wù)降級(jí)學(xué)習(xí)筆記的文章就介紹到這了,更多相關(guān)SpringCloud hystrix內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringCloud Feign隔離與降級(jí)詳細(xì)分析
- SpringCloud?hystrix斷路器與局部降級(jí)全面介紹
- SpringCloud hystrix服務(wù)降級(jí)概念介紹
- SpringCloud降級(jí)規(guī)則使用介紹
- SpringCloud-Alibaba-Sentinel服務(wù)降級(jí),熱點(diǎn)限流,服務(wù)熔斷
- springcloud 服務(wù)降級(jí)的實(shí)現(xiàn)方法
- 詳解springcloud 基于feign的服務(wù)接口的統(tǒng)一hystrix降級(jí)處理
- springcloud使用Hystrix進(jìn)行微服務(wù)降級(jí)管理
- SpringCloud災(zāi)難性雪崩效應(yīng)處理方法之降級(jí)實(shí)現(xiàn)流程詳解
相關(guān)文章
MyBatis學(xué)習(xí)教程(六)-調(diào)用存儲(chǔ)過(guò)程
這篇文章主要介紹了MyBatis學(xué)習(xí)教程(六)-調(diào)用存儲(chǔ)過(guò)程的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看下吧2016-05-05使用IDEA搭建Hadoop開(kāi)發(fā)環(huán)境的操作步驟(Window10為例)
經(jīng)過(guò)三次重裝,查閱無(wú)數(shù)資料后成功完成hadoop在win10上實(shí)現(xiàn)偽分布式集群,以及IDEA開(kāi)發(fā)環(huán)境的搭建。一步一步跟著本文操作可以避免無(wú)數(shù)天坑2021-07-07ElasticSearch之索引模板滾動(dòng)索引實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了ElasticSearch之索引模板滾動(dòng)索引實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04