前言
默认Feign是不启用Hystrix的,需要添加如下配置启用Hystrix,这样所有的Feign Client都会受到Hystrix保护!
新增配置
feign:
hystrix:
enabled: true
提供Fallback
@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, "默认用户", "默认用户", 0, new BigDecimal(1));
}
}
获取原因
@FeignClient(name = "shop-provider-user", fallbackFactory = UserFeignClientFallbackFactory.class)
public interface UserFeignClient {
@GetMapping("/users/{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, "默认用户", "默认用户", 0, new BigDecimal(1));
}
};
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。