3.2.3 Hystris - 服務容錯機制

服務相互依賴

在這裏插入圖片描述

服務異常

在這裏插入圖片描述

系統雪崩

在這裏插入圖片描述

構建彈性應用的思路

在這裏插入圖片描述

Hystrix集成示例

在這裏插入圖片描述

Hystrix的核心知識

在這裏插入圖片描述

資源隔離

在這裏插入圖片描述

Hystrix的初始化流程

在這裏插入圖片描述

Hystrix核心執行流程

在這裏插入圖片描述

Hystrix與springcloud集成

在這裏插入圖片描述

Hystrix可視化監視

在這裏插入圖片描述

Hystrix使用

  1. 創建一個類繼承HystrixCommand,重寫fallback方法,詳細。
package com.study.springcloudhystrix.command;

import com.netflix.hystrix.*;
import org.springframework.web.client.RestTemplate;

public class CustomerCommand extends HystrixCommand<Object> {

  private RestTemplate restTemplate;
    public CustomerCommand(RestTemplate restTemplate) {

        super(
       Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("study-hystrix"))
               .andCommandKey(HystrixCommandKey.Factory.asKey("CustomerController"))
                .andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("studyThreadPool"))
                .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                                              .withExecutionTimeoutInMilliseconds(100)
                                               .withCircuitBreakerSleepWindowInMilliseconds(5000))
                .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
                                                 .withCoreSize(1)
                                                 .withMaxQueueSize(2))
        );
        this.restTemplate = restTemplate;
    }

    @Override
    protected Object run() throws Exception {
     //核心實現,調用我們期望調用的方法.
        System.out.println("當前線程是: "+Thread.currentThread().getName());
      Object result = restTemplate.getForObject("http://helloserver",String.class,"");
      return  result;
    }

    @Override
    protected Object getFallback() {
        //核心方法,降級之後會來實現這個
        System.out.println("降級啦。。。");
        return "降級了,返回降級";
    }
}
  1. 配置文件config.properties的方式
# Hystrix 默認加載的配置文件 - 限流、 熔斷示例

# 線程池大小
hystrix.threadpool.default.coreSize=1
# 緩衝區大小, 如果爲-1,則不緩衝,直接進行降級 fallback
hystrix.threadpool.default.maxQueueSize=200
# 緩衝區大小超限的閾值,超限就直接降級
hystrix.threadpool.default.queueSizeRejectionThreshold=2

# 執行策略
# 資源隔離模式,默認thread。 還有一種叫信號量
hystrix.command.default.execution.isolation.strategy=THREAD
# 是否打開超時
hystrix.command.default.execution.timeout.enabled=true
# 超時時間,默認1000毫秒
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=2000
# 超時時中斷線程
hystrix.command.default.execution.isolation.thread.interruptOnTimeout=true
# 取消時候中斷線程
hystrix.command.default.execution.isolation.thread.interruptOnFutureCancel=false
# 信號量模式下,最大併發量
hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests=2

# 降級策略
# 是否開啓服務降級
hystrix.command.default.fallback.enabled=true
# fallback執行併發量
hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests=100

# 熔斷策略
# 啓用/禁用熔斷機制
hystrix.command.default.circuitBreaker.enabled=true
# 強制開啓熔斷
hystrix.command.default.circuitBreaker.forceOpen=false
# 強制關閉熔斷
hystrix.command.default.circuitBreaker.forceClosed=false
# 前提條件,一定時間內發起一定數量的請求。  也就是5秒鐘內(這個5秒對應下面的滾動窗口長度)至少請求3次,熔斷器才發揮起作用。總數  默認20
hystrix.command.default.circuitBreaker.requestVolumeThreshold=3
# 錯誤百分比。達到或超過這個百分比,熔斷器打開。  比如:5秒內有100請求,60個請求超時或者失敗,就會自動開啓熔斷
hystrix.command.default.circuitBreaker.errorThresholdPercentage=50
# 10秒後,進入半打開狀態(熔斷開啓,間隔一段時間後,會讓一部分的命令去請求服務提供者,如果結果依舊是失敗,則又會進入熔斷狀態,如果成功,就關閉熔斷)。 默認5秒
hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds=10000


# 度量策略
# 5秒爲一次統計週期,術語描述:滾動窗口的長度爲5秒
hystrix.command.default.metrics.rollingStats.timeInMilliseconds=5000
# 統計週期內 度量桶的數量,必須被timeInMilliseconds整除。作用:
hystrix.command.default.metrics.rollingStats.numBuckets=10
# 是否收集執行時間,並計算各個時間段的百分比
hystrix.command.default.metrics.rollingPercentile.enabled=true
# 設置執行時間統計週期爲多久,用來計算百分比
hystrix.command.default.metrics.rollingPercentile.timeInMilliseconds=60000
# 執行時間統計週期內,每個度量桶最多統計多少條記錄。設置爲50,有100次請求,則只會統計最近的50次
hystrix.command.default.metrics.rollingPercentile.bucketSize=100
# 數據取樣時間間隔
hystrix.command.default.metrics.healthSnapshot.intervalInMilliseconds=500

# 設置是否緩存請求,request-scope內緩存
hystrix.command.default.requestCache.enabled=false
# 設置HystrixCommand執行和事件是否打印到HystrixRequestLog中
hystrix.command.default.requestLog.enabled=false


######DnUser-ThreadPool特定配置

# hystrix.threadpool.DnUser-ThreadPool.coreSize=20
# hystrix.threadpool.DnUser-ThreadPool.maxQueueSize=1000
# 超過就報錯
# hystrix.threadpool.DnUser-ThreadPool.queueSizeRejectionThreshold=800
  1. 基於註解的方式

在啓動入口添加註解 @EnableCircuitBreaker

@HystrixCommand(fallbackMethod = "callTimeoutFallback",
      threadPoolProperties = {
             @HystrixProperty(name="coreSize",value = "1"),
              @HystrixProperty(name="queueSizeRejectionThreshold",value = "1")
      },
     commandProperties = {
             @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "100")
     })
     @GetMapping("index2")
     public Object getIndex2(){
        return restTemplate.getForObject("http://helloserver",String.class,"");
     }

      public Object callTimeoutFallback(){
          return  "請求index2降級";
       }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章