试图找出如何在springcloud-feign中限制每个主机的最大可用连接数。
有一个简单的springboot应用程序,它只向自己发送和记录请求。尝试设置下一个配置以限制连接池,但似乎已被忽略:
feign.httpclient.max-connections-per-route=2
feign.httpclient.max-connections=2
有一个并行流,请求一个虚拟服务器,并在收到响应后写入日志。服务器在发送响应之前挂起5秒钟。
查看日志文件,我可以看到同时发出了12个请求。以我的电脑的逻辑核数来计算。
2021-04-02 16:33:39.521 [http-nio-8081-exec-6] INFO o.s.web.servlet.DispatcherServlet - Completed initialization in 1 ms
2021-04-02 16:33:44.580 [ForkJoinPool.commonPool-worker-23] INFO com.example.springdemo.TestClient - made '156' request
2021-04-02 16:33:44.580 [ForkJoinPool.commonPool-worker-19] INFO com.example.springdemo.TestClient - made '327' request
2021-04-02 16:33:44.580 [ForkJoinPool.commonPool-worker-7] INFO com.example.springdemo.TestClient - made '406' request
2021-04-02 16:33:44.580 [ForkJoinPool.commonPool-worker-21] INFO com.example.springdemo.TestClient - made '577' request
2021-04-02 16:33:44.580 [ForkJoinPool.commonPool-worker-13] INFO com.example.springdemo.TestClient - made '827' request
2021-04-02 16:33:44.580 [ForkJoinPool.commonPool-worker-31] INFO com.example.springdemo.TestClient - made '452' request
2021-04-02 16:33:44.582 [ForkJoinPool.commonPool-worker-17] INFO com.example.springdemo.TestClient - made '31' request
2021-04-02 16:33:44.582 [ForkJoinPool.commonPool-worker-5] INFO com.example.springdemo.TestClient - made '906' request
2021-04-02 16:33:44.582 [ForkJoinPool.commonPool-worker-27] INFO com.example.springdemo.TestClient - made '968' request
2021-04-02 16:33:44.582 [ForkJoinPool.commonPool-worker-9] INFO com.example.springdemo.TestClient - made '77' request
2021-04-02 16:33:44.582 [ForkJoinPool.commonPool-worker-3] INFO com.example.springdemo.TestClient - made '15' request
2021-04-02 16:33:44.583 [main] INFO com.example.springdemo.TestClient - made '656' request
我错过什么了吗?
下面是我用来重现问题的应用程序:
使用外国客户机的代码:
public void tryParallelInvocation() {
IntStream.range(0, 1000)
.parallel()
.forEach(value -> {
feignClient.testRequest(String.valueOf(value));
LOG.info("made '" + value + "' request");
});
}
外部客户端接口:
@FeignClient(value = "default", url = "http://localhost:8081/")
public interface TestFeignClient {
@GetMapping("/demo")
String testRequest(@RequestParam("param") String param);
}
休息端点:
@GetMapping
public ResponseEntity<String> demo(@RequestParam(value = "param") String param) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return ResponseEntity.ok(param);
}
依赖项:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
<relativePath/>
</parent>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
应用程序属性:
server.port=8081
feign.httpclient.max-connections-per-route=2
feign.httpclient.max-connections=2
feign.httpclient.enabled=true
暂无答案!
目前还没有任何答案,快来回答吧!