|
|
@ -1,19 +1,27 @@ |
|
|
package com.inspect.agg.config; |
|
|
package com.inspect.agg.config; |
|
|
|
|
|
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
import java.nio.charset.StandardCharsets; |
|
|
import java.time.Duration; |
|
|
|
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
|
|
|
|
import org.apache.http.client.config.RequestConfig; |
|
|
|
|
|
import org.apache.http.impl.client.CloseableHttpClient; |
|
|
|
|
|
import org.apache.http.impl.client.HttpClients; |
|
|
|
|
|
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; |
|
|
import org.springframework.boot.web.client.RestTemplateBuilder; |
|
|
import org.springframework.boot.web.client.RestTemplateBuilder; |
|
|
import org.springframework.context.annotation.Bean; |
|
|
import org.springframework.context.annotation.Bean; |
|
|
import org.springframework.context.annotation.Configuration; |
|
|
import org.springframework.context.annotation.Configuration; |
|
|
|
|
|
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; |
|
|
import org.springframework.http.converter.HttpMessageConverter; |
|
|
import org.springframework.http.converter.HttpMessageConverter; |
|
|
import org.springframework.http.converter.StringHttpMessageConverter; |
|
|
import org.springframework.http.converter.StringHttpMessageConverter; |
|
|
import org.springframework.web.client.RestTemplate; |
|
|
import org.springframework.web.client.RestTemplate; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* HTTP 客户端配置:RestTemplate + 超时 + UTF-8(站点接口返回中文,必须 UTF-8 解码)。 |
|
|
|
|
|
|
|
|
* HTTP 客户端配置:连接池 + 超时 + UTF-8(站点接口返回中文,必须 UTF-8 解码)。 |
|
|
|
|
|
* |
|
|
|
|
|
* <p>聚合层向站端发起的起流/停流请求在打开多画面时会同时出现十几个,串行新建 TCP |
|
|
|
|
|
* 连接会明显放大延迟并让部分请求打满页面 8 秒超时;这里改为复用连接的连接池。</p> |
|
|
*/ |
|
|
*/ |
|
|
@Configuration |
|
|
@Configuration |
|
|
public class RestTemplateConfig { |
|
|
public class RestTemplateConfig { |
|
|
@ -27,10 +35,41 @@ public class RestTemplateConfig { |
|
|
|
|
|
|
|
|
@Bean |
|
|
@Bean |
|
|
public RestTemplate restTemplate(RestTemplateBuilder builder) { |
|
|
public RestTemplate restTemplate(RestTemplateBuilder builder) { |
|
|
|
|
|
AggProperties.Http http = aggProperties.getHttp(); |
|
|
|
|
|
|
|
|
|
|
|
// 连接池:总连接数与单站端连接数都要覆盖“同时在飞的站端请求”。 |
|
|
|
|
|
PoolingHttpClientConnectionManager connectionManager = |
|
|
|
|
|
new PoolingHttpClientConnectionManager(); |
|
|
|
|
|
connectionManager.setMaxTotal(Math.max(1, http.getPoolMaxTotal())); |
|
|
|
|
|
connectionManager.setDefaultMaxPerRoute(Math.max(1, http.getPoolMaxPerRoute())); |
|
|
|
|
|
|
|
|
|
|
|
// 超时与池等待:取连接不能无限等,否则池满时请求会一直挂到页面超时。 |
|
|
|
|
|
RequestConfig requestConfig = RequestConfig.custom() |
|
|
|
|
|
.setConnectTimeout(http.getConnectTimeoutMs()) |
|
|
|
|
|
.setSocketTimeout(http.getReadTimeoutMs()) |
|
|
|
|
|
.setConnectionRequestTimeout(http.getConnectionRequestTimeoutMs()) |
|
|
|
|
|
.build(); |
|
|
|
|
|
|
|
|
|
|
|
CloseableHttpClient httpClient = HttpClients.custom() |
|
|
|
|
|
.setConnectionManager(connectionManager) |
|
|
|
|
|
.setDefaultRequestConfig(requestConfig) |
|
|
|
|
|
// 定期清理失效空闲连接,避免站端重启后一直复用坏连接。 |
|
|
|
|
|
.evictExpiredConnections() |
|
|
|
|
|
.evictIdleConnections(30L, TimeUnit.SECONDS) |
|
|
|
|
|
.build(); |
|
|
|
|
|
|
|
|
|
|
|
// 只在这里指定请求工厂;连接与读取超时统一由上面的 RequestConfig 控制, |
|
|
|
|
|
// 避免与 RestTemplateBuilder 的超时设置叠加产生歧义。 |
|
|
RestTemplate restTemplate = builder |
|
|
RestTemplate restTemplate = builder |
|
|
.setConnectTimeout(Duration.ofMillis(aggProperties.getHttp().getConnectTimeoutMs())) |
|
|
|
|
|
.setReadTimeout(Duration.ofMillis(aggProperties.getHttp().getReadTimeoutMs())) |
|
|
|
|
|
|
|
|
.requestFactory(() -> { |
|
|
|
|
|
HttpComponentsClientHttpRequestFactory factory = |
|
|
|
|
|
new HttpComponentsClientHttpRequestFactory(httpClient); |
|
|
|
|
|
factory.setConnectTimeout(http.getConnectTimeoutMs()); |
|
|
|
|
|
factory.setReadTimeout(http.getReadTimeoutMs()); |
|
|
|
|
|
return factory; |
|
|
|
|
|
}) |
|
|
.build(); |
|
|
.build(); |
|
|
|
|
|
|
|
|
// 默认 StringHttpMessageConverter 用 ISO-8859-1,会破坏站点返回的中文 |
|
|
// 默认 StringHttpMessageConverter 用 ISO-8859-1,会破坏站点返回的中文 |
|
|
List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters(); |
|
|
List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters(); |
|
|
for (HttpMessageConverter<?> converter : converters) { |
|
|
for (HttpMessageConverter<?> converter : converters) { |
|
|
@ -41,4 +80,4 @@ public class RestTemplateConfig { |
|
|
} |
|
|
} |
|
|
return restTemplate; |
|
|
return restTemplate; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |