1.代码
在cloud-payment-provider8001的PaymentController里添加
// 用于测试openFeign的超时控制 @GetMapping("/payment/timeout") public String paymentOpenFeignTimeOut() { try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); } return serverPort; }
单独测试这个,等待三秒成功访问。
在cloud-consumer-feign-order80里写接口等:
@Component @FeignClient(value = "CLOUD-PAYMENT-SERVICE") public interface PaymentFeignService { @GetMapping(value="/payment/get/{id}") public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id); @GetMapping("/payment/timeout") public String paymentOpenFeignTimeOut(); }
@RestController public class PaymentFeignController { @Resource private PaymentFeignService paymentFeignService; @GetMapping("/consumer/payment/get/{id}") public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) { return paymentFeignService.getPaymentById(id); } @GetMapping("/consumer/payment/timeout") public String paymentOpenFeignTimeOut() { return paymentFeignService.paymentOpenFeignTimeOut(); } }
再测试:
因为feign默认等待一秒。
openFeign默认支持Ribbon, 在yml里开启客户端超时控制:
ribbon: ReadTimeout: 5000 ConnectTimeout: 5000
测试超时控制ok: