Spring Cloud Feign Hystrix 断路器 支持

执行流程:请求失败,先执行多次重试,重试还不行就进入断路器熔断

使用回退模式,当远程服务调用失败时,服务消费者将执行替代代码路径,以尝试通过另一种方式执行操作,而不是生成异常。

步骤一 配置开启

feign:
  client:
    config:
      default:
        connectTimeout: 3000
        readTimeout: 3000
        loggerLevel: BASIC
  hystrix:
    enabled: true # 开启断路器
hystrix:
  command:
    myusers-service:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 6000  #应大于上面的feign超时,否则上面的超时不起作用,直接走断路器了

步骤二 实现在服务失败时调用的回退方法

@Component
public class JSONPlaceHolderFallback implements JSONPlaceHolderClient {

    @Override
    public List<Post> getPosts() {
        return Collections.emptyList();
    }

    @Override
    public Post getPostById(Long postId) {
        return null;
    }
}

步骤三 让 Feign 知道已经提供了回退方法,我们还需要在@FeignClient*注释中设置我们的回退类*

@FeignClient(value = "jplaceholder", url = "https://jsonplaceholder.typicode.com/", fallback = JSONPlaceHolderFallback.class)
public interface JSONPlaceHolderClient {

    @RequestMapping(method = RequestMethod.GET, value = "/posts")
    List<Post> getPosts();

    @RequestMapping(method = RequestMethod.GET, value = "/posts/{postId}", produces = "application/json")
    Post getPostById(@PathVariable("postId") Long postId);
}

效果
重试

回退结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lakernote

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值