OpenFign调用超时实现步骤:
1.provider timeout处理 改controller
2.Consumer timeout处理 改service 和controller
3.改application.yml配置
OpenFign调用超时实现步骤:
1.provider timeout处理 改controller
@RestController
@Slf4j
public class PaymentController
{@GetMapping(value = "/payment/feign/timeout")public String paymentFeignTimeout(){// 业务逻辑处理正确,但是需要耗费3秒钟try { TimeUnit.SECONDS.sleep(3); } catch (InterruptedException e) { e.printStackTrace(); }return serverPort;}
}
2.Consumer timeout处理 改service 和controller
service
@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService
{@GetMapping(value = "/payment/feign/timeout")public String paymentFeignTimeout();
}
controller
@RestController
@Slf4j
public class OrderFeignController
{@Resourceprivate PaymentFeignService paymentFeignService;@GetMapping(value = "/consumer/payment/feign/timeout")public String paymentFeignTimeout(){// OpenFeign客户端一般默认等待1秒钟return paymentFeignService.paymentFeignTimeout();}
}
3.application.yml文件
#设置feign客户端超时时间(OpenFeign默认支持ribbon)
ribbon:#指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间ReadTimeout: 5000#指的是建立连接后从服务器读取到可用资源所用的时间ConnectTimeout: 5000