1.maven依赖
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.6</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
2.连接池配置
#最大连接数 http: maxTotal: 100 #并发数 defaultMaxPerRoute: 20 #创建连接的最长时间 connectTimeout: 1000 #从连接池中获取到连接的最长时间 connectionRequestTimeout: 500 #数据传输的最长时间 socketTimeout: 10000 #提交请求前测试连接是否可用 staleConnectionCheckEnabled: true #可用空闲连接过期时间,重用空闲连接时会先检查是否空闲时间超过这个时间,如果超过,释放socket重新建立 validateAfterInactivity: 3000000
3.连接池初始化
package cn.likui.study.config; 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.beans.factory.annotation.Value; 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; /** * @Description: https://www.cnblogs.com/yuexiaoyun/articles/13034028.html * @Author: ldg * @Date: 2020/10/9 */ @Configuration public class RestTemplateConfig { @Value("${http.maxTotal}") private Integer maxTotal; @Value("${http.defaultMaxPerRoute}") private Integer defaultMaxPerRoute; @Value("${http.connectTimeout}") private Integer connectTimeout; @Value("${http.connectionRequestTimeout}") private Integer connectionRequestTimeout; @Value("${http.socketTimeout}") private Integer socketTimeout; @Value("${http.staleConnectionCheckEnabled}") private boolean staleConnectionCheckEnabled; @Value("${http.validateAfterInactivity}") private Integer validateAfterInactivity; @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 connectionManager = new PoolingHttpClientConnectionManager(registry); // 最大连接数 connectionManager.setMaxTotal(maxTotal); //单个路由最大连接数 connectionManager.setDefaultMaxPerRoute(defaultMaxPerRoute); // 最大空间时间 connectionManager.setValidateAfterInactivity(validateAfterInactivity); RequestConfig requestConfig = RequestConfig.custom() //服务器返回数据(response)的时间,超过抛出read timeout .setSocketTimeout(socketTimeout) //连接上服务器(握手成功)的时间,超出抛出connect timeout .setConnectTimeout(connectTimeout) // 提交前检测是否可用 .setStaleConnectionCheckEnabled(staleConnectionCheckEnabled) //从连接池中获取连接的超时时间,超时间未拿到可用连接,会抛出org.apache.http.conn.ConnectionPoolTimeoutException: Timeout waiting for connection from pool .setConnectionRequestTimeout(connectionRequestTimeout) .build(); return HttpClientBuilder.create() .setDefaultRequestConfig(requestConfig) .setConnectionManager(connectionManager) .build(); } }
4.示例
@RequestMapping("/testRestTemplate") @ResponseBody public Object testRestTemplate() throws IOException { ResponseEntity result= restTemplate.getForEntity("http://192.168.1.100:8080/User/getAllUser",ResponseEntity.class); return result.getBody(); }
感谢:https://www.cnblogs.com/yuexiaoyun/articles/13034028.html