Hystrix 实战 & 理论

整体概念

spring cloud 用的是 hystrix,是一个容错组件。
当自身 依赖的服务不可用时,服务自身不会被拖垮。防止微服务级联异常

Hystrix实现了 超时机制和断路器模式。

Hystrix是Netflix开源的一个类库,用于隔离远程系统、服务或者第三方库,防止级联失败,从而提升系统的可用性与容错性。主要有以下几点功能:

  1. 为系统提供保护机制。在依赖的服务出现高延迟或失败时,为系统提供保护和控制。
  2. 防止雪崩。
  3. 包裹请求:使用HystrixCommand(或HystrixObservableCommand)包裹对依赖的调用逻辑,每个命令在独立线程中运行。
  4. 跳闸机制:当某服务失败率达到一定的阈值时,Hystrix可以自动跳闸,停止请求该服务一段时间。
  5. 资源隔离:Hystrix为每个请求都的依赖都维护了一个小型线程池,如果该线程池已满,发往该依赖的请求就被立即拒绝,而不是排队等候,从而加速失败判定。防止级联失败。
  6. 快速失败:Fail Fast。同时能快速恢复。侧重点是:(不去真正的请求服务,发生异常再返回),而是直接失败。
  7. 监控:Hystrix可以实时监控运行指标和配置的变化,提供近实时的监控、报警、运维控制。
  8. 回退机制:fallback,当请求失败、超时、被拒绝,或当断路器被打开时,执行回退逻辑。回退逻辑我们自定义,提供优雅的服务降级。
  9. 自我修复:断路器打开一段时间后,会自动进入“半开”状态,可以进行打开,关闭,半开状态的转换。前面有介绍。

准备

准备 eureka-service(注册中心,port:7901),eureka-order(服务提供者,port:8081),eureka-shop(服务消费者,port:8080)

eureka-service pom 文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>eureka-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-service</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>2021.0.1</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

eureka-service 配置文件

#是否将自己注册到其他Eureka Server
eureka.client.register-with-eureka=false
#是否从eureka server获取注册信息
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://eureka-service1.com:7901/eureka/
#web端口,服务是由这个端口处理rest请求的
server.port=7901

eureka-order pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>eureka-order</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-order</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <!-- HttpClient 实现 -->
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-httpclient</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.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>
                spring-cloud-starter-netflix-hystrix
            </artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

eureka-order配置文件


#是否将自己注册到其他Eureka Server
eureka.client.register-with-eureka=true
#是否从eureka server获取注册信息
eureka.client.fetch-registry=true
#设置服务注册中心的URL,用于client和server端交流
eureka.client.serviceUrl.defaultZone=http://eureka-service1.com:7901/eureka/
#主机名
eureka.instance.hostname=eureka-order1.com
spring.application.name=order-service
#web端口,服务是由这个端口处理rest请求的
server.port=8081

feign.hystrix.enabled=true

eureka-shop pom 文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>eureka-shop</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka-shop</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <!-- HttpClient 实现 -->
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-httpclient</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-turbine</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>
                spring-cloud-starter-netflix-hystrix
            </artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

eureka-shop配置文件

#是否将自己注册到其他Eureka Server
eureka.client.register-with-eureka=true
#是否从eureka server获取注册信息
eureka.client.fetch-registry=true
#设置服务注册中心的URL,用于client和server端交流
eureka.client.serviceUrl.defaultZone=http://eureka-service1.com:7901/eureka/
spring.application.name=shop-service
#web端口,服务是由这个端口处理rest请求的
server.port=8080

feign.hystrix.enabled=true

启动类添加注解

// 酌情添加,后面都会用到
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
@EnableHystrixDashboard
@EnableCircuitBreaker
@EnableTurbine

启动类添加配置
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/actuator/hystrix.stream");
registrationBean.setName(“HystrixMetricsStreamServlet”);
return registrationBean;
}

与feign结合使用

  1. eureka-order
    增加请求异常方法getError()
@RestController
public class BalancerController {

    @Value("${eureka.instance.hostname}")
    private String hostName;
    @Value("${server.port}")
    private Integer port;

    @GetMapping("/getHostAndPort")
    public String getHostAndPort() {
        return "host:" + hostName + ",port:" + port;
    }

    @GetMapping("/getError")
    public String getError() {
        System.out.println("进来了");
        int i = 1 / 0;
        return "ERROR";
    }

}
  1. eureka-shop 项目结构
    在这里插入图片描述

创建通过feign 远程调用接口

@FeignClient(value = "order-service", fallback = FallBack.class)
public interface OrderService {

    @GetMapping("/getHostAndPort")
    String getHostAndPort();

    @GetMapping("/getError")
    String getError();

}

增加FallBack .class

@Component
public class FallBack implements OrderService {

    @Override
    public String getHostAndPort() {
        System.out.println("is fall back");
        return "is fall back";
    }

    @Override
    public String getError() {
        System.out.println("is fall back");
        return "is fall back";
    }
}

调用controller

@RestController
public class FeignController {
    @Resource
    private OrderService orderService;
    
    @GetMapping("/getError")
    public String getError(){
        return orderService.getError();
    }

    @GetMapping("/getErrorByLocal")
    public String getErrorByLocal(){
        try {
            int i = 1 / 0 ;
        } catch (Exception e) {
            // TODO: handle exception
            throw new BusinessException("业务代码异常");
        }
        return orderService.getError();
    }
}
  1. 访问 http://localhost:8080/getError
    在这里插入图片描述

针对不同的异常返回不同的信息

  1. 修改OrderService.class 注解
@FeignClient(value = "order-service", fallbackFactory = OrderFallBack.class)
  1. 增加OrderFallBack.class
@Component
public class OrderFallBack implements FallbackFactory<OrderService> {

    @Override
    public OrderService create(Throwable cause) {
        return new OrderService() {
            @Override
            public String getHostAndPort() {
                return null;
            }

            @Override
            public String getError() {
                System.out.println(cause);
                if(cause instanceof FeignException.InternalServerError) {
                    System.out.println("InternalServerError");
                    return "远程服务报错";
                }else if(cause instanceof RuntimeException) {
                    return "请求时异常:" + cause;
                }else {
                    return "其他异常";
                }
            }

        };
    }
}
  • 访问 http://localhost:8080/getError
    在这里插入图片描述

忽略异常

服务提供者正常,消费者发生业务异常,不走熔断的备用方法

  • 第一种方式:继承HystrixBadRequestException
// 自定义异常,继承HystrixBadRequestException,当发生此异常时,不走备用方法。

public class BusinessException extends HystrixBadRequestException {
	
	private String message;
	
	public BusinessException(String message) {
		super(message);
		this.message = message;
	}
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
}
@GetMapping("/getOrderForFeign")
    public String getOrderForFeign(){
    	// 远程调用正常
        String str = orderService.getHostAndPort();
        // 本服务业务发生异常
        try {
            int i = 1/0;
        } catch (Exception e) {
            throw new BusinessException("熔断忽略的异常");
        }

        return str;
    }

请求 http://localhost:8080/getOrderForFeign
在这里插入图片描述

  • 第二种方式:Hystrix属性配置。
配置属性:
@HystrixCommand(fallbackMethod = "sendFail",
   ignoreExceptions = {HystrixIgnoreException.class})
自定义异常:

public class HystrixIgnoreException extends RuntimeException {
	
	private String message;
	
	public HystrixIgnoreException(String message) {
		this.message = message;
	}	
}

强制熔断

service 层

@Service
public class ForceFallService {

    @HystrixCommand(fallbackMethod = "fall", commandProperties = {
                    @HystrixProperty(name = "fallback.enabled", value = "true"),
                    @HystrixProperty(name = "circuitBreaker.forceOpen", value = "true")

            })
    public String forceFall() {
        return "正常执行";
    }

    private String fall() {
        return "强制熔断了";
    }

}

controller 层

    @GetMapping("/forceFall")
    public String forceFall(){
        return forceFallService.forceFall();
    }

访问 http://localhost:8080/forceFall
在这里插入图片描述

配置

Hystrix完整配置列表

监控

  1. 原始
    在服务消费端 eureka-shop 添加
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

请求一个熔断的方法 http://localhost:8080/getError
访问 http://localhost:8080/actuator/hystrix.stream,可看到页面 ping 的返回结果 ,isCircuitBreakerOpen 代表是否熔断
在这里插入图片描述

  1. 可视化
    在服务消费端 eureka-shop 添加
	<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
		</dependency>

启动类添加

@EnableHystrixDashboard

访问 http://localhost:8080/hystrix
输入上面的地址 http://localhost:8080/actuator/hystrix.stream
在这里插入图片描述
可看到较为友好的面板信息
在这里插入图片描述
3. 集中可视化
创建项目 study-hystrix-turbine
pom 文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>study-hystrix-turbine</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>study-hystrix-turbine</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
        </dependency>

        <!-- HttpClient 实现 -->
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-httpclient</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.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>
                spring-cloud-starter-netflix-hystrix
            </artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

配置文件

#是否将自己注册到其他Eureka Server,默认为true 需要
eureka.client.register-with-eureka=true
#是否从eureka server获取注册信息, 需要
eureka.client.fetch-registry=true
#设置服务注册中心的URL,用于client和server端交流
eureka.client.serviceUrl.defaultZone=http://eureka-service1.com:7901/eureka/

spring.application.name=turbine
#web端口,服务是由这个端口处理rest请求的
server.port=7900

eureka.instance.hostname=turbine.com

feign.hystrix.enabled=true

turbine.combine-host-port=true
turbine.app-config=order-service,shop-service
turbine.cluster-name-expression=new String("default")
turbine.aggregator.cluster-config=default

启动类

@EnableTurbine
@EnableHystrixDashboard

访问 http://localhost:7900/hystrix
填上 http://localhost:7900/turbine.stream
在这里插入图片描述

相关概念

熔断

如果对某个微服务请求有大量超时(说明该服务不可用),再让新的请求访问该服务就没有意义,只会无谓的消耗资源。例如设置了超时时间1s,如果短时间内有大量的请求无法在1s内响应,就没有必要去请求依赖的服务了。

熔断的转换
关闭状态:正常情况下,断路器关闭,可以正常请求依赖的服务。

打开状态:当一段时间内,请求失败率达到一定阈值,断路器就会打开。服务请求不会去请求依赖的服务。调用方直接返回。不发生真正的调用。重置时间过后,进入半开模式。

半开状态:断路器打开一段时间后,会自动进入“半开模式”,此时,断路器允许一个服务请求访问依赖的服务。如果此请求成功(或者成功达到一定比例),则关闭断路器,恢复正常访问。否则,则继续保持打开状态。

断路器的打开,能保证服务调用者在调用异常服务时,快速返回结果,避免大量的同步等待,减少服务调用者的资源消耗。并且断路器能在打开一段时间后继续侦测请求执行结果,判断断路器是否能关闭,恢复服务的正常调用。

降级

为了在整体资源不够的时候,适当放弃部分服务,将主要的资源投放到核心服务中,待渡过难关之后,再重启已关闭的服务,保证了系统核心服务的稳定。当服务停掉后,自动进入fallback替换主方法。

用fallback方法代替主方法执行并返回结果,对失败的服务进行降级。当调用服务失败次数在一段时间内超过了断路器的阈值时,断路器将打开,不再进行真正的调用,而是快速失败,直接执行fallback逻辑。服务降级保护了服务调用者的逻辑。

熔断和降级:
共同点:
	1、为了防止系统崩溃,保证主要功能的可用性和可靠性。
	2、用户体验到某些功能不能用。
不同点:
	1、熔断由下级故障触发,主动惹祸。
	2、降级由调用方从负荷角度触发,无辜被抛弃。

隔离

避免了单个服务消耗掉所有资源,从而导致其他服务出现故障的场景。
两种线程隔离策略:线程池(THREAD,默认)、信号量(SEMAPHORE)。

  1. 线程池隔离
    使用该方式,HystrixCommand将会在单独的线程上执行,并发请求受线程池中线程数量的限制。不同服务通过使用不同线程池,彼此间将不受影响,达到隔离效果
    在这里插入图片描述
    将调用服务线程与服务访问的执行线程分割开来,调用线程能够空出来去做其他工作,而不至于因为服务调用的执行,阻塞过长时间。

  2. 信号量隔离
    使用该方式,HystrixCommand将会在调用线程上执行,通过信号量限制单个服务提供者的并发量,开销相对较小(因为不用那么多线程池),并发请求受到信号量个数的限制。
    在这里插入图片描述

  3. 两种隔离方式对比

  • 线程池
    优点:可调整失败策略;异步请求,解放worker的线程阻塞;异常隔离

    缺点:需要线程切换,开销较大

  • 信号量
    优点:轻量,相比线程池,信号量不需要线程切换,避免了不必要的开销

    缺点:信号量不支持异步,也不支持超时,也就是说当所请求的服务不可用时,信号量会控制超过限制的请求立即返回,但是已经持有信号量的线程只能等待服务响应或从超时中返回,即可能出现长时间等待

实现流程

  1. 构建HystrixCommand或者HystrixObservableCommand对象,用于封装请求,并在构造方法配置请求被执行需要的参数。

  2. 执行命令,Hystrix提供了4种执行命令的方法。

  3. 检查是否有相同命令执行的缓存,若启用了缓存,且缓存可用,直接使用缓存响应请求。Hystrix支持请求缓存,但需要用户自定义启动。

  4. 检查断路器是否打开,如果打开走 第8步。

  5. 检查线程池或者信号量是否被消耗完,如果已满,走第8步。

  6. 调用HystrixCommand的run 或者 HystrixObservableCommand的construct 执行被封装的调用逻辑,如果执行失败或超时,走第8步。

  7. 计算链路的健康情况

  8. 在命令执行失败时获取fallback逻辑。

  9. 返回响应。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小刘说

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值