1.通过HttpClient配置连接池
package com.kittlen.cloud.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.cloud.client.loadbalancer.LoadBalanced;
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{
@Value("${restTemplate.maxTotal:200}")
int maxTotal;
@Value("${restTemplate.defaultMaxPerRoute:100}")
int defaultMaxPerRoute;
@Value("${restTemplate.socketTimeout:10000}")
int socketTimeout;
@Value("${restTemplate.connectTimeout:5000}")
int connectTimeout;
@Value("${restTemplate.connectionRequestTimeout:1000}")
int connectionRequestTimeout;
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory());
restTemplate.getInterceptors().add(new HttpInterceptor());
return restTemplate;
}
@Bean
public ClientHttpRequestFactory clientHttpRequestFactory() {
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);
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(socketTimeout)
.setConnectTimeout(connectTimeout)
.setConnectionRequestTimeout(connectionRequestTimeout)
.build();
return HttpClientBuilder.create()
.setDefaultRequestConfig(requestConfig)
.setConnectionManager(connectionManager)
.build();
}
}
2.使用map进行传参并添加headers
@PostMapping("/byMap")
public Result<List<Test>> byMap() {
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("author", "kittlen");
MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add(HttpHeaders.ACCEPT, "application/json");
try {
ResponseEntity<Result<List<Test>>> exchange = restTemplate.exchange(HTTP + "/test/getTestAll", HttpMethod.POST,
new HttpEntity<>(map, headers), new ParameterizedTypeReference<Result<List<Test>>>() {
});
return exchange.getBody();
} catch (Exception e) {
return Result.error();
}
}