1.restTemplate
@Autowired
private TestImplProperties testImplProperties;
@Bean
public RestTemplate testTemplate() {
HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
// 指定连接池配置,否则线上容易出现请求排队导致接口超时问题
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(300);
//单host(可以理解为单域名)最大并发数,在依赖的外部系统数量较少时,可以配置和maxTotal一致。
connectionManager.setDefaultMaxPerRoute(100);
httpRequestFactory.setHttpClient(HttpClientBuilder.create().setConnectionManager(connectionManager).build());
httpRequestFactory.setConnectionRequestTimeout(30 * 1000);
httpRequestFactory.setConnectTimeout(30 * 1000);
httpRequestFactory.setReadTimeout(30 * 3000);
RestTemplate restTemplate = new RestTemplate(httpRequestFactory);
restTemplate.getMessageConverters().add(new FastJsonHttpMessageConverter());
QueryParamsUrlTemplateHandler handler = new QueryParamsUrlTemplateHandler();
handler.setFileCenterProperties(testImplProperties);
restTemplate.setUriTemplateHandler(handler);
return restTemplate;
}
2.TestImplProperties
@Data
@ConfigurationProperties(prefix = "xx.pe")
public class TestImplProperties implements TestProperties{
private String xx;
private String xx;
private String xx;
}
3.TestProperties
public interface TestProperties{
String getXx();
String getXx();
String getXx();
}