美文网首页
Hystrix之通配服务降级FeignFallback(),此方

Hystrix之通配服务降级FeignFallback(),此方

作者: 拄杖忙学轻声码 | 来源:发表于2021-07-13 01:05 被阅读0次

第一步:
配置yml文件,启动hystrix对断路器的支持,如下:

feign:
  hystrix:
    enabled: true

第二步:
创建降级(兜底)处理类,实现FeignClient接口,示例如下:

@Component
public class PaymentHystrixFallbackService implements PaymentHystrixService{
    @Override
    public CommonResult paymentInfo_OK(Integer id) {
        return new CommonResult(ResultCode.FAIL.getCode(),ResultCode.FAIL.getMsg(),"------PaymentHystrixService fall back-paymentInfo_OK");
    }

    @Override
    public CommonResult paymentInfo_TimeOut(Integer id) {
        return new CommonResult(ResultCode.FAIL.getCode(),ResultCode.FAIL.getMsg(),"------PaymentHystrixService fall back-paymentInfo_TimeOut");
    }
}

第三步:
@FeignClient注解中增加fallback属性,指向兜底处理类fallback = PaymentHystrixFallbackService.class,示例如下:

@FeignClient(name = "cloud-provider-hystrix-payment", fallback = PaymentHystrixFallbackService.class)
public interface PaymentHystrixService {

    @GetMapping("/payment/hystrix/ok/{id}")
    CommonResult paymentInfo_OK(@PathVariable("id") Integer id);

    @GetMapping("/payment/hystrix/timeout/{id}")
    CommonResult paymentInfo_TimeOut(@PathVariable("id") Integer id);

}

相关文章

网友评论

      本文标题:Hystrix之通配服务降级FeignFallback(),此方

      本文链接:https://www.haomeiwen.com/subject/rekjpltx.html