我再尝试使用webclient,因为restTemplate已经进入维护,但是有些细节不太了解,我写了一个简单的例子:
参考链接: https://spring.io/guides/gs/reactive-rest-service/
代码结构:
有一个test的入口 WebClientTestController
@RestController
@RequestMapping("/test")
public class WebClientTestController {
private final WebClient webClient;
public WebClientTestController(WebClient webClient) {
this.webClient = webClient;
}
@GetMapping
public Mono<Object> main() {
new SimpleAsyncTaskExecutor()
.execute(
() -> {
for (int i = 0; i <= 65536; ++i) {
System.out.println(
webClient
.get()
.uri("http://127.0.0.1:8080/hello")
.retrieve()
.bodyToMono(String.class)
.block()
+ i);
}
});
return Mono.empty();
}
}
appConfig
@Configuration
public class AppConfig {
@Bean
public WebClient webClient(WebClient.Builder builder) throws SSLException {
final SslContext sslContext =
SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build();
HttpClient httpClient =
HttpClient.create().secure(sslContextSpec -> sslContextSpec.sslContext(sslContext));
return builder.clientConnector(new ReactorClientHttpConnector(httpClient)).build();
}
}
我再window上启动并访问: curl http://127.0.0.1:8080/test
可以得到正常输出:
一样的代码,我再linux上测试,得到错误:
请求成功的数量 ≈ ulimit -n
我想知道webclient的使用注意事项以及原理细节,为什么在linux上,这样用会每次请求打开一个sockt直到请求失败?
期望能得到回复,感谢~
System.out.println( webClient .get() .uri("http://127.0.0.1:8080/hello") .retrieve() .bodyToMono(String.class) .block() + i);
改成
试试