Netflix:Hystrix
图
Hystrix主要用于处理分布式系统中的故障容错和延迟容错。它提供了一系列功能,包括:熔断器、回退、线程池隔离、请求缓存

Hystrix

在复杂的分布式体系中,服务之间的依赖关系比较多,并形成链路,如果其中某个出现问题,就会导致雪崩效应

Hystrix主要用于处理分布式系统中的故障容错和延迟容错。它提供了一系列功能,包括:

  1. 熔断器(circuit breaker):Hystrix会监控应用程序与服务之间的通信,并在出现故障时自动打开熔断器。这可以防止故障扩散到整个系统中,从而保证系统的可靠性
  2. 回退(fallback):当依赖服务无法提供服务时,Hystrix可以使用回退逻辑返回默认值或执行替代操作,从而保持应用程序的可用性
  3. 线程池隔离(thread pooling):Hystrix使用线程池将请求隔离开来,以避免单个请求对应用程序的响应时间产生过大的影响
  4. 请求缓存(request caching):Hystrix可以通过缓存请求结果来减少对依赖服务的调用次数,从而降低延迟并提高系统的吞吐量

服务熔断

添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
    <version>1.4.7.RELEASE</version>
</dependency>

编写配置

@RestController
public class Hello {

    @RequestMapping("/hello")
    // 方法调用失败,设置回调方法
    @HystrixCommand(fallbackMethod = "hystrix_hello")
    public User hello() {
        User user = new User();
        user.setName("张三");
        System.out.println("访问了提供者8001");
        return user;
    }

    // 备用方法
    public User hystrix_hello() {
        return new User();
    }

}

开启使用

@SpringBootApplication
@EnableEurekaClient
// 开启熔断机制
@EnableCircuitBreaker
public class Pro_8001 {
    public static void main(String[] args) {
        SpringApplication.run(Pro_8001.class, args);
    }
}