使用httpcomponents作为webclient的底层客户端连接,配置最大连接数为1,从池中获取连接的超时时间为0.2秒。
创建一个接口sleep(30000),并用webclient连续调用两次,发现第二次调用并不会因获取不到连接而立即返回,而是等第一次结束后(30s后)才返回 Caused by: org.apache.hc.core5.util.DeadlineTimeoutException。
如果直接用httpClient会因0.2秒内未从池中获取到空闲连接而抛出 ConnectionRequestTimeoutException。请问webclient为什么和httpClient表现不一样,是我漏掉了什么配置,还是因为用httpClient作为底层连接的兼容性问题吗?
webclient调用方式如下:
@Slf4j
@RestController
@Tag(name = "测试WebClient", description = "测试WebClient")
@RequestMapping(path = "/testWebClient", consumes = {MediaType.ALL_VALUE}, produces = {MediaType.APPLICATION_JSON_VALUE})
public class TestWebClientController {
@Resource
private WebClient webClient;
@GetMapping("/testGet")
@Operation(summary = "WebClient的get请求", description = "WebClient的get请求")
public R<Object> get() {
CountDownLatch countDownLatch = new CountDownLatch(2);
Mono<R> resp = webClient.get()
.uri(uriBuilder -> uriBuilder
.scheme("http")
.host("localhost")
.port(60001)
.path("/baseservice/user/get")
.queryParam("id", 1)
.build())
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(R.class);
return R.success(resp.block());
}
}