1. 程式人生 > 程式設計 >Springcloud hystrix服務熔斷和dashboard如何實現

Springcloud hystrix服務熔斷和dashboard如何實現

服務在經過一定負荷之後,如果達到一定上限之後會中斷進行報錯,而服務呼叫的方法也會報錯等等,一旦整體服務停下,別的客戶端再來訪問就會無法呼叫。對此需要進行另外一種服務熔斷模式。

不同於現實中的熔斷保險絲,服務熔斷是在系統服務達到一定錯誤之後,自動熔斷降級,採取備用方法,但是在一定時間後客戶端再次呼叫成功後,一定時間內成功率上去,系統的熔斷機制會慢慢的關閉,恢復到正常請求的狀態。

本篇接上一章直接改動。

1.主啟動類加上新的註解。

@EnableCircuitBreaker

2.service寫入新的熔斷控制方法

@Service
public class PaymentHystrixService {
 @HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback",commandProperties = {
      @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),//是否開啟斷路器
      @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),//請求數達到後才計算
      @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),//休眠時間窗
      @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60"),//錯誤率達到多少跳閘
  })
  public String paymentCirtuitBreaker(@PathVariable("id")Integer id){
   if(id<0){
     throw new RuntimeException("****id不能為負數");
   }
   String randomNum= IdUtil.simpleUUID();

   return Thread.currentThread().getName()+"\t"+"呼叫成功,編號"+randomNum;

  }
  public String paymentCircuitBreaker_fallback(@PathVariable("id")Integer id){
    return "id不能為負數,請稍後重試,o(╥﹏╥)o+"+id;
  }

此處hystrixCommand註解即是對熔斷的一些限制,一般是在10秒內進行10次有60%的訪問錯誤率就會進行熔斷,自動啟動備用的方法,預設5秒後有 正確的執行結果就會慢慢恢復正常狀態,關閉斷路器。

3.dashboard

為了能夠更加直觀的看見服務訪問的一些情況,配置下視覺化的網頁觀察熔斷。

新建dashboard工程。

pom檔案依賴

<dependencies>
    <dependency>
      <groupId>com.bai</groupId>
      <artifactId>cloud-api-common</artifactId>
      <version>${project.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
    </dependency>
    <!--監控-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>
    <!--eureka client-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <!--熱部署-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <scope>runtime</scope>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

主啟動類

@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboard9001 {
  public static void main(String[] args) {
    SpringApplication.run(HystrixDashboard9001.class,args);
  }
}

yml配置下埠即可。

訪問地址

http://localhost:9001/hystrix/

Springcloud hystrix服務熔斷和dashboard如何實現

對於被監控的服務需要額外的配置。新版本會有報錯需要在啟動類加上如下配置。

/**
     * 此配置是為了服務監控而配置,與服務容錯本身無關,springcloud升級後的坑
     * ServletRegistrationBean因為SpringBoot的預設路徑不是 “/hystrix.stream"
     * 只要在自己的專案裡配置上下的servlet就可以了
     */
    @Bean
    public ServletRegistrationBean getServlet() {
      HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet() ;
      ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
      registrationBean.setLoadOnStartup(1);
      registrationBean.addUrlMappings("/hystrix.stream");
      registrationBean.setName("HystrixMetricsStreamServlet");
      return registrationBean;
    }

本篇所有程式碼均在GitHub:

https://github.com/MaTsukun/springcloud2020

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。