Fork me on GitHub

熔断器-Feign使用Hystrix

目录

Feign默认已经整合了Hystrix,本节详细探讨Feign使用Hystrix的具体细节。

服务降级

  • 加配置,默认Feign是不启用Hystrix的,需要添加如下配置启用Hystrix,这样所有的Feign Client都会受到Hystrix保护!
1
2
3
feign:
hystrix:
enabled: true
  • 提供Fallback:
1
2
3
4
5
6
7
8
9
10
11
12
@FeignClient(name = "microservice-provider-user", fallback = UserFeignClientFallback.class)
public interface UserFeignClient {
@GetMapping("/users/{id}")
User findById(@PathVariable("id") Long id);
}
@Component
class UserFeignClientFallback implements UserFeignClient {
@Override
public User findById(Long id) {
return new User(id, "默认用户", "aaaaaa", "13899988898");
}
}

测试

  • 启动microservice-discovery-eureka
  • 启动microservice-provider
  • 启动microservice-consumer-feign-with-hystrix
  • 访问http://localhost:8010/feign/user/1 ,能正常返回结果
  • 关闭microservice-provider ,再次访问http://localhost:8010/feign/user/1 ,可返回类似如下结果,说明当服务提供者时,服务消费者进入了回退方法
1
2
3
4
5
6
{
"userId": 1,
"userName": "默认用户",
"password": "aaaaaa",
"phone": "13908888888"
}
  • 访问http://localhost:8010/actuator/health ,可获得类似如下结果:
1
2
3
4
5
6
7
8
9
10
11
{
"status": "UP",
"details": {
"diskSpace": ...,
"refreshScope": ...,
"discoveryComposite": ...,
"hystrix": {
"status": "UP"
}
}
}
  • 由结果不难发现,此时断路器并未打开!这是为什么呢? 原因是:此时只请求了一次,没有达到Hystrix的阈值——Hystrix设计来保护高并发应用的,它要求10秒(可用hystrix.command.default.metrics.rollingStats.timeInMilliseconds 自定义)以内API错误次数超过20次(用circuitBreaker.requestVolumeThreshold 自定义),此时才可能触发断路器。
  • 持续不断地访问http://localhost:8010/feign/user/1 多次(至少20次)
  • 再次访问http://localhost:8010/actuator/health ,可获得类似如下结果:
1
2
3
4
5
6
7
8
9
10
{
"hystrix": {
"status": "CIRCUIT_OPEN",
"details": {
"openCircuitBreakers": [
"microservice-provider::UserFeignClient#findById(Integer)"
]
}
}
}
  • 由结果可知,此时断路器已经打开,并且列出了是哪个API的断路器被打开了。

获得造成fallback的原因

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@FeignClient(name = "microservice-provider", fallbackFactory = UserFeignClientFallbackFactory.class)
public interface UserFeignClient {
@GetMapping("/user/{id}")
User findById(@PathVariable("id") Long id);
}
@Component
@Slf4j
class UserFeignClientFallbackFactory implements FallbackFactory<UserFeignClient> {
@Override
public UserFeignClient create(Throwable throwable) {
return new UserFeignClient() {
@Override
public User findById(Long id) {
log.error("进入回退逻辑", throwable);
return new User(id, "默认用户", "aaaaaa", "13899988898");
}
};
}
}

Feign启用/禁用Hystrix

全局启用

1
feign.hystrix.enabled: true

全局禁用

1
feign.hystrix.enabled: false

或直接省略不写。

局部启用

利用Feign配置的自定义,为指定Feign Client指定如下配置类即可

1
2
3
4
5
6
7
public class FeignDisableHystrixConfiguration {
@Bean
@Scope("prototype")
public HystrixFeign.Builder feignBuilder() {
return HystrixFeign.builder();
}
}

局部禁用

1
2
3
4
5
6
7
public class FeignDisableHystrixConfiguration {
@Bean
@Scope("prototype")
public Feign.Builder feignBuilder() {
return Feign.builder();
}
}

代码示例

服务降级:

microservice-consumer-feign-with-hystrix

获得造成fallback的原因:

microservice-consumer-feign-hystrix-fallback-factory

相关文章