1:pom
<!-- hystrix --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
2:yml
hystrix: enabled: true
3:service
@Component @FeignClient(value = "CLOUD-PAYMENT-HYSTRIX-SERVICE", fallback = PaymentServiceImpl.class) public interface PaymentService { @GetMapping(value = "/payment/ok") BaseResult paymentInfo_OK(); @RequestMapping("/payment/timeOut") BaseResult paymentInfo_TimeOut(); }
4:serviceImpl
@Component public class PaymentServiceImpl implements PaymentService { @Override public BaseResult paymentInfo_OK() { Object obj = "PaymentServiceImpl paymentInfo_OK fall back"; return new BaseResult(-1, "error", obj); } @Override public BaseResult paymentInfo_TimeOut() { Object obj = "PaymentServiceImpl paymentInfo_TimeOut fall back"; return new BaseResult(-1, "error", obj); } }
5:controller
@RequestMapping("/timeOut") public BaseResult paymentInfo_TimeOut() { return paymentService.paymentInfo_TimeOut(); }
调用timeOut接口
{"code":-1,"message":"error","t":"PaymentServiceImpl paymentInfo_TimeOut fall back"}
一般使用该方式做降级处理