天天看點

SpringCloud之Hystix(熔斷器)

一、Hystix概述

1.1、簡介

       Hystrix is a latency and fault tolerance library designed to isolate points of access to remote systems, services and 3rd party libraries, stop cascading failure and enable resilience in complex distributed systems where failure is inevitable.

       Hystrix是一個延遲和容錯庫,旨在隔離對遠端系統、服務和第三方庫的通路點,停止級聯故障,并在不可避免的複雜分布式系統中啟用彈性。

GitHub: https://github.com/Netflix/Hystrix/

SpringCloud之Hystix(熔斷器)

1.2、Hystix的作用

(1) Latency and Fault Tolerance(延遲和容錯)

      Stop cascading failures. Fallbacks and graceful degradation. Fail fast and rapid recovery.

Thread and semaphore isolation with circuit breakers.

(2) Realtime Operations(實時操作)

      Realtime monitoring and configuration changes. Watch service and property changes take effect immediately as they spread across a fleet.

Be alerted, make decisions, affect change and see results in seconds.

(3) Concurrency(并發)

      Parallel execution. Concurrency aware request caching. Automated batching through request collapsing.

1.3、熔斷器的工作機制

SpringCloud之Hystix(熔斷器)

1.4、服務雪崩效應

      詳細檢視下面的這篇部落格

      轉載:https://blog.csdn.net/sheinenggaosuwo/article/details/86592893

1.5、Hystrix解決服務雪崩的方法

  •      線程隔離
  •      服務熔斷

二、Hystrix

2.1、引入依賴,服務的消費方作降級處理

<!--引入Hystrix的依賴-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>
           

2.2、在啟動類添加注解

  • @EnableCircuitBreaker
               
  • @EnableHystrix
               

2.3、application.yaml中配置Hystrix

預設不需要配置,可根據具體需求更改

#配置Hystrix的逾時時長
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 3000
           

2.4、Contriller中做失敗降級處理

@GetMapping("{id}")
    //添加注解,對服務進行失敗降級處理
    @HystrixCommand(fallbackMethod = "queryByIdFallback")
    public String queryById(@PathVariable("id")int id){
        String url = "http://user_service/user/"+id;
        String user = restTemplate.getForObject(url, String.class);
        return user;
    }

    public String queryByIdFallback(int id){
        return "不好意思,伺服器繁忙,請稍後再試!";
    }
           

2.5、類上做統一降級處理

@DefaultProperties(defaultFallback = "defaultFallback")
//在方法上加上@HystrixCommand即可啟用預設的降級處理
           

2.6、服務降級時間控制

@HystrixCommand(
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value="4000")
            }
    )
           

2.7、SpringCloudApplication注解說明

package org.springframework.cloud.client;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public @interface SpringCloudApplication {
}
           
看源碼可知:
@EnableCircuitBreaker
@EnableDiscoveryClient
@SpringBootApplication    等于     @SpringCloudApplication
           

繼續閱讀