最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

在項目中直接使用hystrix的流程分析

 更新時間:2022年06月06日 09:38:22   作者:編號94530  
最近由于一些背景原因,需要在項目中需要對接口進行限流。所以就考慮到了直接使用Hystrix,但是呢,又不想直接使用SpringCloud,而是直接引入原生,現(xiàn)在發(fā)現(xiàn)挺好用的,所以記錄下來,需要的朋友參考下吧

什么是Hystrix

Hystrix是Netflix針對微服務(wù)分布式系統(tǒng)的熔斷保護中間件,當我們的客戶端連接遠程的微服務(wù)時,有兩種情況需要考慮:首先,如果遠程系統(tǒng)當機了我們怎么辦?

其次,我們?nèi)绾喂芾韺h程微服務(wù)的調(diào)用性能,以保證每個微服務(wù)以最小延遲最快性能響應(yīng)?

Hystrix是一個有關(guān)延遲和失敗容錯的開源庫包,用來設(shè)計隔離訪問遠程系統(tǒng)端點或微服務(wù)等,防止級聯(lián)爆炸式的失敗,也就是由一個小問題引起接二連三擴大的

瘋狂的錯誤爆炸直至整個系統(tǒng)癱瘓,能夠讓復(fù)雜的分布式系統(tǒng)更加靈活具有彈性。這篇文章給大家介紹下在項目中如何直接使用hystrix?

一、背景

最近由于一些背景原因,需要在項目中需要對接口進行限流。所以就考慮到了直接使用Hystrix。但是呢,又不想直接使用SpringCloud,而是直接引入原生,現(xiàn)在發(fā)現(xiàn)挺好用的,所以記錄下來,分享出來。

二、使用方式

2.1 Jar包引入

<dependency>
  <groupId>com.netflix.hystrix</groupId>
  <artifactId>hystrix-javanica</artifactId>
  <version>1.5.18</version>
</dependency>

<dependency>
  <groupId>com.netflix.hystrix</groupId>
  <artifactId>hystrix-core</artifactId>
  <version>1.5.18</version>
</dependency>

引入兩個包,分別是Hystrix核心包,以及直接原生的Java包

2.2 配置文件

在Resources目錄下面,放上hystrix.properties文件。配置如下。

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=3000
hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests=1000
hystrix.command.default.circuitBreaker.requestVolumeThreshold=20
hystrix.command.default.metrics.rollingStats.numBuckets=10
hystrix.command.default.metrics.rollingStats.timeInMilliseconds=10000
hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=5000
hystrix.command.default.circuitBreaker.errorThresholdPercentage=50
hystrix.command.default.circuitBreaker.forceOpen=false
hystrix.command.default.circuitBreaker.forceClosed=false
hystrix.command.default.requestCache.enabled=false

hystrix.threadpool.default.coreSize=10
hystrix.threadpool.default.maximumSize=10
hystrix.threadpool.default.allowMaximumSizeToDivergeFromCoreSize=true
hystrix.threadpool.default.keepAliveTimeMinutes=1
hystrix.threadpool.default.maxQueueSize=100
hystrix.threadpool.default.queueSizeRejectionThreshold=101
hystrix.threadpool.default.metrics.rollingStats.numBuckets=10
hystrix.threadpool.default.metrics.rollingStats.timeInMilliseconds=10000
#hystrix.timer.threadpool.default.coreSize = 10

這個是一部分配置,如果需要知道更多,可以Click-Github Hystrix Wiki

2.3 設(shè)置配置

設(shè)置Hystrix的配置

/**
 * <p>熔斷器配置</p>
 *
 * @author fattycal@qq.com
 * @since 2022/6/4
 */
@Configuration
public class HystrixConfig implements InitializingBean {

    @Bean
    public HystrixCommandAspect hystrixCommandAspect(){
        // 初始化切面
        return new HystrixCommandAspect();
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        // 初始化熔斷器配置
        // 清除配置
        ConfigurationManager.getConfigInstance().clear();
        // 加載配置文件
        ConfigurationManager.loadCascadedPropertiesFromResources("hystrix");
    }
}

HystrixCommandAspect是jar包帶的切面,通過切面通知,找去需要熔斷的方法,然后進行處理。

@Aspect
public class HystrixCommandAspect {
    //...略

    @Pointcut("@annotation(com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand)")
    public void hystrixCommandAnnotationPointcut() {
    }

  @Pointcut("@annotation(com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser)")
    public void hystrixCollapserAnnotationPointcut() {
    }

    @Around("hystrixCommandAnnotationPointcut() || hystrixCollapserAnnotationPointcut()")
    public Object methodsAnnotatedWithHystrixCommand(final ProceedingJoinPoint joinPoint) throws Throwable {
        // ... 略
    }
}

ConfigurationManager看看這名字,就知道是配置管理的,也不負眾望,的確是用來加載配置的。

2.4 實現(xiàn)代碼

/**
 * <p>熔斷器測試</p>
 *
 * @author fattycal@qq.com
 * @since 2022/6/4
 */
@RestController
public class HystrixTestController {


    @GetMapping("/hystrix")
    @HystrixCommand(commandKey = "hystrixTestController-getHello", threadPoolKey = "hystrixTestController-getHello",
            fallbackMethod = "getHelloFallback")
    public String getHello(){
        try {
            // 執(zhí)行太快不便于測試
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "TEST Hystrix";
    }

    public String getHelloFallback(Throwable error){
        // 打印日志
        System.out.println("TEST Hystrix: " + error.getMessage());
        return "TEST Hystrix: " + error.getMessage();
    }
}

代碼沒有啥花里胡哨的,直接在需要熔斷的方法上面加上HystrixCommond。

commandKeythreadPoolKey是自己設(shè)置的,可以為這個方法定制線程數(shù)、核心線程等配置(在hystrix.properties中添加)。給出示例如下。

#-------------------------------------------------------------------
hystrix.threadpool.hystrixTestController-getHello.coreSize=1
hystrix.threadpool.hystrixTestController-getHello.maximumSize=2
hystrix.threadpool.hystrixTestController-getHello.maxQueueSize=1
hystrix.threadpool.hystrixTestController-getHello.queueSizeRejectionThreshold=2
#-------------------------------------------------------------------

至此,完成了所有的配置和準備,接下來直接測試

三、測試試驗

直接從Jmeter官網(wǎng)下載jmeter,拿到跑測試, 具體下載過程就不一樣展示了,直接貼出測試結(jié)果。

由于為這個方法設(shè)置的核心線程數(shù)、線程數(shù)、隊列數(shù)都不大,很容易測試出結(jié)果。我們可以從console中很明顯的看到熔斷器打開,說明方法被執(zhí)行到。

在從Jmeter中查看一下結(jié)果,也是可以佐證我們的效果。測試圖如下:

四、總結(jié)

自此,整個流程是走完了,可以看到效果著實起來了。 Hystrix知識限流熔斷中的一種方案,大家可以結(jié)合實際情況做出更多的選擇。

相關(guān)文章

最新評論

荔浦县| 克拉玛依市| 洛浦县| 乌什县| 奇台县| 板桥市| 曲靖市| 遂平县| 昭通市| 开封市| 常德市| 康平县| 澎湖县| 全州县| 宿迁市| 龙陵县| 西城区| 吉林省| 五河县| 那坡县| 大同市| 邢台市| 若尔盖县| 邯郸市| 铜鼓县| 黄平县| 盈江县| 紫云| 库尔勒市| 东阳市| 阿拉善盟| 海林市| 长沙市| 梁平县| 新化县| 枣庄市| 徐州市| 谢通门县| 正宁县| 日照市| 永川市|