feign整合hystrix

feign整合hystrix

feign 默认是支持hystrix的, 但是在Spring - cloud Dalston 版本之后就默认关闭了, 因为不一定业务需求要用的到
开启feign 对hystrix的支持
feign:
  hystrix:
    enabled: true #开启feign当中的hystrix
    
#自定义ribbon的超时时间 设置的要比hystrix-timeoutInMilliseconds超时时间大
ribbon:
  ReadTimeout: 20000
  #处理请求的超时时间,默认为5秒
  ConnectTimeout: 20000
  
hystrix:
  threadpool:
    default:
      coreSize: 5

方式1-fallback
1.创建一个类实现服务FeignClient接口
@Component
public class GoodsFeignFallback implements GoodsFeignClient{
    @Override
    public Object getGoods() {
        return ResponseResult.error("GoodsFeignFallback服务器正在维护,请稍后重试--");
    }
}

2.在服务FeignClient接口上配置实现类
在这里插入图片描述
3.在控制器当中调用
在这里插入图片描述


方式2-fallbackFactory
1.创建一个类实现FallbackFactory
@Component
public class GoodsFeignFallBackFactory implements FallbackFactory<GoodsFeignClient> {
    @Override
    public GoodsFeignClient create(Throwable throwable) {
        return new GoodsFeignClient() {
            @Override
            public Object getGoods() {
                String message = throwable.getMessage();
                return ResponseResult.error(message);
            }
        };
    }
}

2.在服务FeignClient接口上配置实现类
同方式1一致
3.在控制器当中调用
同方式1一致

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值