繁体   English   中英

RestTemplate与自定义ConnectionPool?

[英]RestTemplate with custom ConnectionPool?

我正在使用以下配置来创建RestTemplate bean。

@Bean
@Primary
public RestTemplate restTemplate(RestTemplateBuilder builder) {
    HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();

    return builder.requestFactory(() -> new BufferingClientHttpRequestFactory(factory))
            .build();
}

问题:默认情况下, HttpClient实例化如下:

org.apache.http.impl.client.HttpClientBuilder:

    String s = System.getProperty("http.keepAlive", "true");
    if ("true".equalsIgnoreCase(s)) {
        s = System.getProperty("http.maxConnections", "5");
        int max = Integer.parseInt(s);
        poolingmgr.setDefaultMaxPerRoute(max);
        poolingmgr.setMaxTotal(2 * max);
    }

因此,默认情况下,该休息模板上最多有10个并发url连接。

问题:如何在使用spring-boot时最好地配置最大总数? 我没有找到任何application.properties条目将其设置为自定义值。

Sidequestion: 每条路线的财产是什么意思? 是一个路由localhost:8080/myfirst ,另一个路由是localhost:8080/mysnd 或者都是相同的路由localhost:8080

对不起,我很想念你的问题。

它很简单:在application.properties您可以创建自己的配置。 例如:

## Connection pool max size to appache http client
myProjectId.http.maxConnections=100

然后在你的Bean / Service / Something中你可以通过简单的动作注入它

@Bean
public class HttpClient  {

    @Value( "${myProjectId.http.maxConnections}" )
    private int maxConnections;

    // some code below

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM