第一步:
配置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);
}
网友评论