简体   繁体   中英

RestTemplate with custom ConnectionPool?

I'm using the following config to create a RestTemplate bean.

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

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

Problem: by default HttpClient is instantiated as follows:

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);
    }

Thus by default having a maximum of 10 concurrent url connections on that rest template.

Question: how could I best configure the max total when using spring-boot ? I did not find any application.properties entry to set it to a custom value.

Sidequestion: what does the property per route mean? Is a route localhost:8080/myfirst , and another route is localhost:8080/mysnd ? Or are both the same route localhost:8080 ?

Sorry, I'm missunderstood your question.

It's simply: in application.properties you can create you own configuration. For example:

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

And then in you Bean/Service/Something else you can inject it by simple action

@Bean
public class HttpClient  {

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

    // some code below

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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