前言

默认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);}@Componentclass 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@Slf4jclass 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));      }    };  }}