可用场景
对于Consumer,fallback方法可以集中起来
实现service接口
@Component
public class PaymentFallbackService implements PaymentHystrixService {
@Override
public String PaymentInfo_OK(Integer id) {
return "------PaymentHystrixFallbackService fall back PaymentInfo_OK , (T_T) ";
}
@Override
public String PaymentInfo_Timeout(Integer id) {
return "------PaymentHystrixFallbackService fall back PaymentInfo_Timeout , (T_T) ";
}
}
服务接口处指定falllback所在类
指定的位置
@FeignClient(value = "CLOUD-PAYMENT-SERVICE",fallback = PaymentFallbackService.class)
完整代码
/**
* @Author: EzerbelCN
* @Date: 2020/7/23 12:30
*/
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE",fallback = PaymentFallbackService.class)
public interface PaymentHystrixService {
@GetMapping("/payment/hystrix/ok/{id}")
public String PaymentInfo_OK(@PathVariable("id") Integer id);
@GetMapping("/payment/hystrix/timeout/{id}")
public String PaymentInfo_Timeout(@PathVariable("id") Integer id);
}
YML增加配置支持
# 通配服务降级fallback时,必须在这里设置true,默认false,会导致fallback失败
feign:
hystrix:
enabled: true