SpringCloud 实战十二:Feign的超时控制

  作者:记性不好的阁主

默认OpenFeign超时时间是1秒钟


1、8001服务提供者创建模拟超时服务


@GetMapping("/payment/feign/timeout")
public String paymentFeignTimeout(){
try {
TimeUnit.SECONDS.sleep(3);
}catch (InterruptedException e){
e.printStackTrace();
}
return serverPort;
}


2、80消费端创建服务调用Feign


@GetMapping("/payment/feign/timeout")
public String paymentFeignTimeout();



3、80消费端创建测试接口



@GetMapping("/consummer/payment/feign/timeout")
public String paymentFeignTimeout(){
//openFeign-ribbon,客户端一般默认等待1秒钟
return paymentService.paymentFeignTimeout();
}


4、访问http://localhost:8001/payment/feign/timeout


此时会等待3秒后返回端口




5、访问http://localhost/consumer/payment/feign/timeout


此时会因为超时而报错





6、可以通过配置控制超时时间


ribbon:
ReadTimeout: 5000
ConnectTimeout: 5000



7、再次访问http://localhost/consumer/payment/feign/timeout 成功








相关推荐

评论 抢沙发

表情

分类选择