OpenFeign超时设置


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:

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM