SpringCloud斷路器Hystrix原理及用法解析
這篇文章主要介紹了SpringCloud斷路器Hystrix原理及用法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
在分布式環(huán)境中,許多服務(wù)依賴項(xiàng)中的一些必然會失敗。Hystrix是一個庫,通過添加延遲容忍和容錯邏輯,幫助你控制這些分布式服務(wù)之間的交互。Hystrix通過隔離服務(wù)之間的訪問點(diǎn)、停止級聯(lián)失敗和提供回退選項(xiàng)來實(shí)現(xiàn)這一點(diǎn),所有這些都可以提高系統(tǒng)的整體彈性
兩個比較重要的類
- HystrixCommand
- HystrixObservableCommand
注解@HystrixCommand(fallbackMethods="methods")methods中可以添加降級策略
除了提供服務(wù)降級
還提供了請求緩存
- @CacheResult
- @CacheRemve
不過添加CacheResult的時候,說
HystrixRequestContext未初始化。
2020-01-13 16:12:10.273 ERROR 15348 --- [nio-8083-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.reflect.UndeclaredThrowableException] with root cause java.lang.IllegalStateException: Request caching is not available. Maybe you need to initialize the HystrixRequestContext? at com.netflix.hystrix.HystrixRequestCache.get(HystrixRequestCache.java:104) ~[hystrix-core-1.5.18.jar:1.5.18] at com.netflix.hystrix.AbstractCommand$7.call(AbstractCommand.java:478) ~[hystrix-core-1.5.18.jar:1.5.18] at com.netflix.hystrix.AbstractCommand$7.call(AbstractCommand.java:454) ~[hystrix-core-1.5.18.jar:1.5.18] at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:46) ~[rxjava-1.3.8.jar:1.3.8] at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35) ~[rxjava-1.3.8.jar:1.3.8]
查看官方文檔https://github.com/Netflix/Hystrix/wiki/How-To-Use
Typically this context will be initialized and shut down via a ServletFilter that wraps a user request or some other lifecycle hook.
在同一用戶請求的上下文中,相同依賴服務(wù)的返回?cái)?shù)據(jù)始終保持一致。在當(dāng)次請求內(nèi)對同一個依賴進(jìn)行重復(fù)調(diào)用,只會真實(shí)調(diào)用一次。在當(dāng)次請求內(nèi)數(shù)據(jù)可以保證一致性。
初始化是在filter中進(jìn)行(官方建議),但是每一次請求都會進(jìn)行初始化 。所以說和一般的緩存還是有去別的,可以解決高并發(fā),保證的資源的線程安全。在某些場景很有用。
請求合并
/**
* 建議: 服務(wù)提供方有較高的延遲??梢钥紤]使用請求合并
* HystrixCollapser 合并請求的時候會創(chuàng)建一個請求處理器。如果每次合并的請求量不大,只有很少的請求還要合并,會造成合并時間窗
* 并發(fā)量增大,時間窗的創(chuàng)建和消耗增大。所以只有在時間窗內(nèi)有很大的并發(fā)量,推薦請求合并。
*
* batchMethod 請求合并后的替換方法com.gitee.munan56.cloud.hystrixconsumer.AService#findALl(java.util.List) 注意客戶端要有這個方法
*HystrixProperty 一個屬性合并時間窗100s 這個時間結(jié)束后會發(fā)起請求,也就是指這個時間是合并處理的時間
* @param id
* @return
*/
@HystrixCollapser(batchMethod = "findALl",collapserProperties = @HystrixProperty(name = "timerDelayInMilliseconds",value = "100"))
public String doBFindOne(String id){
System.out.println("begin do provider service");
return restTemplate.getForEntity("http://service-provider:8081/api/v1/provider/do",String.class).getBody();
}
全部代碼
package com.gitee.munan56.cloud.hystrixconsumer;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import com.netflix.ribbon.proxy.annotation.Hystrix;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.List;
/**
* @author munan
* @version 1.0
* @date 2020/1/13 10:41
*/
@Service
public class AService {
@Autowired
private RestTemplate restTemplate;
public String doAService(){
return "A Service is run";
}
// @Hystrix(fallbackHandler = )
@HystrixCommand(fallbackMethod = "error")
public String doBServiceOne(){
System.out.println("begin do provider service");
return restTemplate.getForEntity("http://service-provider:8081/api/v1/provider/do",String.class).getBody();
}
/**
* CacheResult 請求緩存(針對request的緩存),官方建議在serverlet filter 中初始化HystrixRequestContext
* 在同一用戶請求的上下文中緩存在統(tǒng)一請求的上下文環(huán)境中有效。
* @param id
* @return
*/
@CacheResult(cacheKeyMethod = "getKey")
@HystrixCommand(fallbackMethod = "error")
public String doBServiceTwo(String id){
System.out.println("begin do provider service");
return restTemplate.getForEntity("http://service-provider:8081/api/v1/provider/do",String.class).getBody();
}
/**
* 建議: 服務(wù)提供方有較高的延遲。可以考慮使用請求合并
* HystrixCollapser 合并請求的時候會創(chuàng)建一個請求處理器。如果每次合并的請求量不大,只有很少的請求還要合并,會造成合并時間窗
* 并發(fā)量增大,時間窗的創(chuàng)建和消耗增大。所以只有在時間窗內(nèi)有很大的并發(fā)量,推薦請求合并。
*
* batchMethod 請求合并后的替換方法com.gitee.munan56.cloud.hystrixconsumer.AService#findALl(java.util.List) 注意客戶端要有這個方法
*HystrixProperty 一個屬性合并時間窗100s 這個時間結(jié)束后會發(fā)起請求,也就是指這個時間是合并處理的時間
* @param id
* @return
*/
@HystrixCollapser(batchMethod = "findALl",collapserProperties = @HystrixProperty(name = "timerDelayInMilliseconds",value = "100"))
public String doBFindOne(String id){
System.out.println("begin do provider service");
return restTemplate.getForEntity("http://service-provider:8081/api/v1/provider/do",String.class).getBody();
}
@HystrixCommand()
public String findALl(List<String> ids){
System.out.println("begin do provider service");
return restTemplate.getForEntity("http://service-provider:8081/api/v1/provider/do",String.class).getBody();
}
/**
* 服務(wù)降級
* 指定調(diào)用服務(wù)出錯的回調(diào)方法
* @return
*/
public String error(Throwable e){
return "do provider error this is default result" + "the error is " + e.getMessage();
}
/**
* 服務(wù)降級
* 指定調(diào)用服務(wù)出錯的回調(diào)方法
* @return
*/
public String error(String id ,Throwable e){
return "do provider error this is default result" + "the error is " + e.getMessage();
}
public String getKey(String id){
return id;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java來實(shí)現(xiàn)文本轉(zhuǎn)圖片需求示例
本文主要介紹了Java來實(shí)現(xiàn)文本轉(zhuǎn)圖片需求示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-04-04
Idea配置maven-tomcat-plugin插件實(shí)現(xiàn)項(xiàng)目部署
今天小編就為大家分享一篇關(guān)于Idea配置maven-tomcat-plugin插件實(shí)現(xiàn)項(xiàng)目部署,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-02-02
Spring Boot集成MyBatis-Plus 自定義攔截器實(shí)現(xiàn)動態(tài)表名切換功能
本文介紹了如何在SpringBoot項(xiàng)目中集成MyBatis-Plus,并通過自定義攔截器實(shí)現(xiàn)動態(tài)表名切換,此外,還探討了MyBatis攔截器在其他場景中的應(yīng)用,如SQL日志記錄、多租戶數(shù)據(jù)隔離、數(shù)據(jù)權(quán)限控制等,感興趣的朋友跟隨小編一起看看吧2024-11-11
IntelliJ IDEA Java項(xiàng)目手動添加依賴 jar 包的方法(圖解)
這篇文章主要介紹了IntelliJ IDEA Java項(xiàng)目手動添加依賴 jar 包,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04

