Source: http://zhangzhi19861216.cnblogs.com/
spring-boot RestTemplate connection pool
Before we project are based on Apache HttpClient connection pool for web interface calls, and later with a spring-boot, found RestTemplate very good use.
Brief introduction:
What is RestTemplate?
RestTemplate is provided by Spring client for accessing Rest services, RestTemplate offers a variety of convenient methods to access the remote Http services, can greatly improve the efficiency of the preparation of the client.
RestTemplate default constructor call, RestTemplate object creation request by using the HTTP implemented in java.net package at the bottom, an HTTP request can specify a different manner by using ClientHttpRequestFactory.
ClientHttpRequestFactory main interface provides two implementations
One is SimpleClientHttpRequestFactory, Http request to create the underlying connectivity supplied with J2SE (both provided by way of java.net package).
The second way is to use HttpComponentsClientHttpRequestFactory way, the underlying HttpClient use of Http access remote services, you can use HttpClient configuration information such as connection pooling and certificates.
RestTemplate default is to use SimpleClientHttpRequestFactory, internal calling the jdk HttpConnection, the default timeout to -1
Now I will introduce next spring-boot RestTemplate how to configure connection pool ( GitHub ):
import org.apache.http.client.HttpClient; import org.apache.http.client.config.RequestConfig; import org.apache.http.config.Registry; import org.apache.http.config.RegistryBuilder; import org.apache.http.conn.socket.ConnectionSocketFactory; import org.apache.http.conn.socket.PlainConnectionSocketFactory; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; @Configuration public class RestTemplateConfig { @Bean public RestTemplate restTemplate() { return new RestTemplate(httpRequestFactory()); } @Bean public ClientHttpRequestFactory httpRequestFactory() { return new HttpComponentsClientHttpRequestFactory(httpClient()); } @Bean public HttpClient httpClient() { Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() .register ( "HTTP", PlainConnectionSocketFactory.getSocketFactory ()) .register("https", SSLConnectionSocketFactory.getSocketFactory()) .build(); PoolingHttpClientConnectionManager the ConnectionManager = new new PoolingHttpClientConnectionManager (Registry); // set the entire maximum number of connections the connection pool based on your own scene connectionManager.setMaxTotal (200); // The routing is maxTotal subdivision connectionManager.setDefaultMaxPerRoute (100); requestConfig requestConfig = RequestConfig.custom () .setSocketTimeout (10000) // server returns the time data (response), and exceeds the timeout time throws Read .setConnectTimeout (5000) connected to the server // (success handshake) time beyond the time throws connect timeout .setConnectionRequestTimeout (1000) // Get the timeout connection from the connection pool, the time does not get over available connections will be thrown org.apache.http.conn.ConnectionPoolTimeoutException: Waiting for the Timeout connection from the pool .build (); return HttpClientBuilder.create () .setDefaultRequestConfig (requestConfig) .setConnectionManager (ConnectionManager) .build (); } }
Before we project are based on Apache HttpClient connection pool for web interface calls, and later with a spring-boot, found RestTemplate very good use.
Brief introduction:
What is RestTemplate?
RestTemplate is provided by Spring client for accessing Rest services, RestTemplate offers a variety of convenient methods to access the remote Http services, can greatly improve the efficiency of the preparation of the client.
RestTemplate default constructor call, RestTemplate object creation request by using the HTTP implemented in java.net package at the bottom, an HTTP request can specify a different manner by using ClientHttpRequestFactory.
ClientHttpRequestFactory main interface provides two implementations
One is SimpleClientHttpRequestFactory, Http request to create the underlying connectivity supplied with J2SE (both provided by way of java.net package).
The second way is to use HttpComponentsClientHttpRequestFactory way, the underlying HttpClient use of Http access remote services, you can use HttpClient configuration information such as connection pooling and certificates.
RestTemplate default is to use SimpleClientHttpRequestFactory, internal calling the jdk HttpConnection, the default timeout to -1
Now I will introduce next spring-boot RestTemplate how to configure connection pool ( GitHub ):
import org.apache.http.client.HttpClient; import org.apache.http.client.config.RequestConfig; import org.apache.http.config.Registry; import org.apache.http.config.RegistryBuilder; import org.apache.http.conn.socket.ConnectionSocketFactory; import org.apache.http.conn.socket.PlainConnectionSocketFactory; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; @Configuration public class RestTemplateConfig { @Bean public RestTemplate restTemplate() { return new RestTemplate(httpRequestFactory()); } @Bean public ClientHttpRequestFactory httpRequestFactory() { return new HttpComponentsClientHttpRequestFactory(httpClient()); } @Bean public HttpClient httpClient() { Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() .register ( "HTTP", PlainConnectionSocketFactory.getSocketFactory ()) .register("https", SSLConnectionSocketFactory.getSocketFactory()) .build(); PoolingHttpClientConnectionManager the ConnectionManager = new new PoolingHttpClientConnectionManager (Registry); // set the entire maximum number of connections the connection pool based on your own scene connectionManager.setMaxTotal (200); // The routing is maxTotal subdivision connectionManager.setDefaultMaxPerRoute (100); requestConfig requestConfig = RequestConfig.custom () .setSocketTimeout (10000) // server returns the time data (response), and exceeds the timeout time throws Read .setConnectTimeout (5000) connected to the server // (success handshake) time beyond the time throws connect timeout .setConnectionRequestTimeout (1000) // Get the timeout connection from the connection pool does not get over the available connection time will be thrown org.apache.http.conn.ConnectionPoolTimeoutException: Waiting for the Timeout Connection from the pool .build (); return HttpClientBuilder.create () .setDefaultRequestConfig (requestConfig) .setConnectionManager (ConnectionManager) .build (); } }