SpringCloud Hystrix的使用
簡介
在分布式系統(tǒng)中,服務(wù)與服務(wù)之間依賴錯(cuò)綜復(fù)雜,一種不可避免的情況就是某些服務(wù)將會(huì)出現(xiàn)失敗。Hystrix是一個(gè)庫,它提供了服務(wù)與服務(wù)之間的容錯(cuò)功能,主要體現(xiàn)在延遲容錯(cuò)和容錯(cuò),從而做到控制分布式系統(tǒng)中的聯(lián)動(dòng)故障。Hystrix通過隔離服務(wù)的訪問點(diǎn),阻止聯(lián)動(dòng)故障,并提供故障的解決方案,從而提高了這個(gè)分布式系統(tǒng)的彈性。
面對(duì)的問題: 一個(gè)應(yīng)用一般會(huì)依賴多個(gè)服務(wù),每個(gè)服務(wù)由于網(wǎng)絡(luò)不可靠,機(jī)房的不可靠等等不穩(wěn)定的因素,總會(huì)導(dǎo)致服務(wù)的故障,如果我們不對(duì)這些故障做處理,就會(huì)進(jìn)而導(dǎo)致整個(gè)系統(tǒng)的不可用。
服務(wù)雪崩:


一個(gè)正常的用戶進(jìn)入調(diào)用微服務(wù)A然后調(diào)用B在調(diào)用C然后離開,而當(dāng)其中微服務(wù)C出現(xiàn)故障,導(dǎo)致用戶停留
B,越來越多的用戶進(jìn)入,請(qǐng)求,停留B在最終導(dǎo)致B的資源被耗盡,不可用,進(jìn)而A也慢慢不可用。這一系
列鏈?zhǔn)椒磻?yīng)就像雪崩一樣影響越來越大,稱為"服務(wù)雪崩"
服務(wù)熔斷
參考:www.fzitv.net/article/166784.htm
應(yīng)對(duì)雪崩效應(yīng)的一種微服務(wù)鏈路保護(hù)機(jī)制
當(dāng)調(diào)用鏈路的某個(gè)微服務(wù)不可用或者響應(yīng)時(shí)間太長時(shí),會(huì)進(jìn)行服務(wù)熔斷,不再有該節(jié)點(diǎn)微服務(wù)的調(diào)用,快速返回錯(cuò)誤的響應(yīng)信息。當(dāng)檢測(cè)到該節(jié)點(diǎn)微服務(wù)調(diào)用響應(yīng)正常后,恢復(fù)調(diào)用鏈路。
在Spring Cloud中通過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ī)制。
服務(wù)熔斷解決如下問題:
- 當(dāng)所依賴的對(duì)象不穩(wěn)定時(shí),能夠起到快速失敗的目的;
- 快速失敗后,能夠根據(jù)一定的算法動(dòng)態(tài)試探所依賴對(duì)象是否恢復(fù)
實(shí)踐
項(xiàng)目搭建
根據(jù) 實(shí)驗(yàn)環(huán)境搭建中的項(xiàng)目copy建立一個(gè)新模塊名為springcloud-provider-dept-hystrix。
導(dǎo)入依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
使用
在Controller層,使用@HystrixCommand來實(shí)現(xiàn)熔斷
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface HystrixCommand {
String groupKey() default "";
String commandKey() default "";
String threadPoolKey() default "";
String fallbackMethod() default "";
HystrixProperty[] commandProperties() default {};
HystrixProperty[] threadPoolProperties() default {};
Class<? extends Throwable>[] ignoreExceptions() default {};
ObservableExecutionMode observableExecutionMode() default ObservableExecutionMode.EAGER;
HystrixException[] raiseHystrixExceptions() default {};
String defaultFallback() default "";
}
在@HystrixCommand中有 defaultFallback() 指定默認(rèn)的備用方法(default ""), fallbackMethod() 指定失敗后進(jìn)行的備用方法(default "")
當(dāng)正常的方法調(diào)用失敗后(5秒內(nèi)20次調(diào)用失敗),認(rèn)為是出故障了就進(jìn)行熔斷,快速返回錯(cuò)誤信息(調(diào)用備選方法)。
@RestController
public class DeptController {
@Autowired
private DeptImpl deptimpl;
@RequestMapping("/dev/{id}")
@HystrixCommand(fallbackMethod = "HystrixGet")//指明備用方法
public Dept DeptqueryByID(@PathVariable("id") Long id) {
Dept dept = deptimpl.queryByID(id);
System.out.println(dept);
if (dept==null) {
throw new RuntimeException("id--->" + id + "用戶不存在");
}
return dept;
}
public Dept HystrixGet(@PathVariable("id") Long id) {
Dept dept=new Dept();
dept.setDeptnumber(id.intValue());
dept.setDname("id"+id+"用戶不存在");
dept.setD_source("no~~~~~~");
return dept;
}
}
在啟動(dòng)類上添加@EnableCircuitBreaker開啟熔斷支持
@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker//開啟熔斷支持
public class HApplication {
public static void main(String[] args) {
SpringApplication.run(HApplication.class,args);
}
}
服務(wù)降級(jí)
即在服務(wù)器壓力劇增的情況下,關(guān)閉一些很少被調(diào)用的服務(wù),騰出一些資源,保證正常運(yùn)行。
如淘寶雙十一關(guān)閉退款通道。
實(shí)踐
在原本的FeignClient指明fallbackFactory
@FeignClient(value = "PROVIDER-NAME",fallbackFactory = SerciceFallbackFactory.class)
public interface DeptClientService {
@RequestMapping(value = "/dev/add")
boolean add(Dept dept);
@RequestMapping(value = "/dev/{id}")
Dept queryByID(@PathVariable("id") Long id );
@PostMapping(value = "/dev/list")
List<Dept> queryAll();
}
定義自己的FallbackFactory
報(bào)錯(cuò)注意import feign.hystrix.FallbackFactory;
import feign.hystrix.FallbackFactory;
@Component
public class SerciceFallbackFactory implements FallbackFactory {
public DeptClientService create(Throwable cause) {
return new DeptClientService() {
public boolean add(Dept dept) {
return false;
}
//定義返回的錯(cuò)誤信息
public Dept queryByID(Long id) {
Dept dept = new Dept();
dept.setD_source("服務(wù)降級(jí)");
dept.setDname("fail");
dept.setDeptnumber(-1);
return dept;
}
public List<Dept> queryAll() {
return null;
}
};
}
}
在客戶端的配置文件中添加
#開啟降級(jí)
feign:
hystrix:
enabled: true
結(jié)果:在我們關(guān)閉服務(wù)端后再次訪問服務(wù)時(shí)

服務(wù)熔斷與服務(wù)降級(jí)的區(qū)別
- 服務(wù)熔斷是在服務(wù)端進(jìn)行的,而服務(wù)降級(jí)是在客戶端進(jìn)行的
- 服務(wù)熔斷的原因:發(fā)生故障,服務(wù)降級(jí):為整體負(fù)荷考慮,保證核心業(yè)務(wù)的運(yùn)行
服務(wù)監(jiān)控 Dashboard
建立項(xiàng)目
導(dǎo)入依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
配置文件
server:
port: 9001
hystrix:
dashboard:
# Hystrix Dashboard會(huì)通過proxyUrl解析到host部分,然后通過配置的proxyStreamAllowList。判定是否允許被訪問
proxy-stream-allow-list: "localhost"
開啟監(jiān)控支持
@SpringBootApplication
@EnableHystrixDashboard//開啟
@EnableDiscoveryClient
public class DashboardApplication {
public static void main(String[] args) {
SpringApplication.run(DashboardApplication.class,args);
}
}
運(yùn)行后訪問:http://localhost:9001/hystrix

根據(jù)提示在spingcloud-provider-dept-hystrix服務(wù)端添加bean
@Bean
public ServletRegistrationBean hystrixMetricsStreamServlet(){
ServletRegistrationBean servlet = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
servlet.addUrlMappings("/actuator/hystrix.stream");
return servlet;
}
運(yùn)行后訪問 http://localhost:8081/actuator/hystrix.stream 可以獲得一些服務(wù)的信息
注意: 需要調(diào)用一次標(biāo)注有 @HystrixCommand 方法才會(huì)有數(shù)據(jù)顯示,只會(huì)監(jiān)控有 @HystrixCommand 的方法

我們也可以通過在http://localhost:9001/hystrix 輸入

按下按鈕開啟對(duì)該服務(wù)的監(jiān)控


以上就是SpringCloud Hystrix的使用的詳細(xì)內(nèi)容,更多關(guān)于SpringCloud Hystrix的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
springBoot2.6.2自動(dòng)裝配之注解源碼解析
對(duì)于springboot個(gè)人認(rèn)為它就是整合了各種組件,然后提供對(duì)應(yīng)的自動(dòng)裝配和啟動(dòng)器(starter),基于這個(gè)流程去實(shí)現(xiàn)一個(gè)定義的裝配組件,下面這篇文章主要給大家介紹了關(guān)于springBoot2.6.2自動(dòng)裝配之注解源碼解析的相關(guān)資料,需要的朋友可以參考下2022-01-01
Java 用反射設(shè)置對(duì)象的屬性值實(shí)例詳解
這篇文章主要介紹了Java 用反射設(shè)置對(duì)象的屬性值實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
基于@MapperScan和@ComponentScan的使用區(qū)別
這篇文章主要介紹了@MapperScan和@ComponentScan的使用區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
解決Unable to start embedded container&nbs
這篇文章主要介紹了解決Unable to start embedded container SpringBoot啟動(dòng)報(bào)錯(cuò)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
spring為類的靜態(tài)屬性實(shí)現(xiàn)注入實(shí)例方法
在本篇文章里小編給大家整理的是關(guān)于spring為類的靜態(tài)屬性實(shí)現(xiàn)注入實(shí)例方法,有需要的朋友們可以參考下。2019-10-10
Spring Cloud Gateway全局異常處理的方法詳解
這篇文章主要給大家介紹了關(guān)于Spring Cloud Gateway全局異常處理的相關(guān)資料,需要的朋友可以參考下2018-10-10
springboot中的Application.properties常用配置
這篇文章主要介紹了springboot中的Application.properties常用配置,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
詳解spring-boot actuator(監(jiān)控)配置和使用
本篇文章主要介紹了spring-boot actuator(監(jiān)控)配置和使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
Java中BigDecimal比較大小的3種方法(??compareTo()、??equals()??和??compar
這篇文章主要給大家介紹了關(guān)于Java中BigDecimal比較大小的3種方法,方法分別是??compareTo()、??equals()??和??compareTo()??,在Java中使用BigDecimal類來進(jìn)行精確的數(shù)值計(jì)算,需要的朋友可以參考下2023-11-11

