Springboot版本为2.2.6.RELEASE,SpringCloud版本为Hoxton.SR3
一、Hystrix相关概念
- Hystrix实现了 超时机制和断路器模式,是Netflix开源的一个类库,用于隔离远程系统、服务或者第三方库,防止级联失败,从而提升系统的可用性与容错性
- 提供保护机制:在调用的服务出现高延迟或失败时,为系统提供保护和控制
- 防止雪崩
- 包裹请求:使用HystrixCommand(或HystrixObservableCommand)对依赖的调用逻辑进行包裹,使每个命令在独立线程中运行
- 跳闸机制:当某个服务提供方的的调用失败率达到一定阈值时,触发Hystrix跳闸机制,停止调用该服务一段时间
- 资源隔离:Hystrix为每个依赖服务的请求维护了一个小型线程池,如果某个依赖服务的线程池已满,发往该依赖的请求会被立即决绝,而不是排队等待,加速失败判定,防止级联失败,即防止影响到其他依赖服务的请求。
- 快速失败:不去真正请求服务,而是直接失败
- 监控:Hystrix可以实时监控运行指标和配置的变化,提供近实时的监控、报警、运维控制
- 回退机制:当请求失败或当断路器被打开时,执行我们自定义的回退逻辑,提供优雅的服务降级
- 自我修复:断路器打开一段时间后,会自动进入“半开”状态,进行随机重试,当发现重试成功时,会重置断路器,请求可以正常进行
二、Hystrix整合RestTemplate
- 引入Maven依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
- 启动类上添加注解
@EnableCircuitBreaker - 创建Service
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class RestService {
private final RestTemplate restTemplate;
public RestService(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
@HystrixCommand(defaultFallback = "back")
public String hello() {
String url = "http://code-service/test";
return restTemplate.getForObject(url, String.class);
}
public String back() {
return "失败啦,熔断啦";
}
}
- Controller调用Service
@Autowired
private RestService restService;
@GetMapping("hello")
public String hello() {
return "hello: " + restService.hello();
}
三、Hystrix整合Feign(一)
- 引入Maven依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
- application.yml添加配置
feign:
hystrix:
enabled: true
- 启动类上添加注解
@EnableCircuitBreaker - 创建
@FeignClient的fallback属性标记的接口实现类,实现降级逻辑
import org.springframework.stereotype.Component;
@Component
public class FeignHystrixBack implements UserServiceFeign {
@Override
public String hello() {
return "hello, Feign and Hystrix!";
}
}
@FeignClient注解指定降级时调用实现类的方法
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(name = "code-service", fallback = FeignHystrixBack.class)
public interface UserServiceFeign {
@GetMapping("test")
String hello();
}
- Controller调用Service
@Autowired
private UserServiceFeign userServiceFeign;
@GetMapping("hello")
public String hello() {
return "hello: " + userServiceFeign.hello();
}
四、Hystrix整合Feign(二)
- 引入Maven依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
- application.yml添加配置
feign:
hystrix:
enabled: true
- 启动类上添加注解
@EnableCircuitBreaker - 创建
@FeignClient的fallbackFactory属性标记的工厂实现类,实现降级逻辑
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;
@Component
public class FeignHystrixBackFactory implements FallbackFactory<UserServiceFeign> {
@Override
public UserServiceFeign create(Throwable throwable) {
return new UserServiceFeign() {
@Override
public String hello() {
return "降级啦!FeignHystrixBackFactory";
}
};
}
}
@FeignClient注解指定降级时调用工厂实现类
package com.it00zyq.user_service.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(name = "code-service", fallbackFactory = FeignHystrixBackFactory.class)
public interface UserServiceFeign {
@GetMapping("test")
String hello();
}
- Controller调用Service
@Autowired
private UserServiceFeign userServiceFeign;
@GetMapping("hello")
public String hello() {
return "hello: " + userServiceFeign.hello();
}