Spring boot 有没有一种方法可以为Rest模板提供动态超时?
我正在使用SpringREST模板和apache的PoolightTPClientConnectionManager进行API调用。我正在处理的项目需要为我通过rest模板发出的每个HTTP请求设置自定义超时。为了实现这一点,我使用CompletableFuture和一个单独的ExecutorService,并调用get(Timeout)方法Spring boot 有没有一种方法可以为Rest模板提供动态超时?,spring-boot,connection-pooling,resttemplate,completable-future,spring-webclient,Spring Boot,Connection Pooling,Resttemplate,Completable Future,Spring Webclient,我正在使用SpringREST模板和apache的PoolightTPClientConnectionManager进行API调用。我正在处理的项目需要为我通过rest模板发出的每个HTTP请求设置自定义超时。为了实现这一点,我使用CompletableFuture和一个单独的ExecutorService,并调用get(Timeout)方法 试试看{ CompletableFuture=CompletableFuture.SupplySync(()->bidderService.getBid(
试试看{
CompletableFuture=CompletableFuture.SupplySync(()->bidderService.getBid(),executorService);
bidResponse=future.get(bidderTimeout,TimeUnit.millizes);
}捕获(InterruptedException | TimeoutException | ExecutionException e){
bidResponse=getTimeoutBidResponse();
}
不幸的是,这种方法的问题是,在超时的情况下,底层线程将继续工作,直到rest模板完成其调用。因此,我在某种程度上丢失了线程池中的一个线程,以及HTTP连接池中的一个连接。
有没有办法在收到超时异常后立即关闭HTTP连接,并将HTTP连接返回到池中
p、 我还尝试使用SpringWebClient和Mono.timeout。实际上,它会立即关闭HTTP连接,但不会将其返回HTTP池
@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder)
{
return restTemplateBuilder
.setConnectTimeout(...)
.setReadTimeout(...)
.build();
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder)
{
return restTemplateBuilder
.setConnectTimeout(...)
.setReadTimeout(...)
.build();
}