Commit c863c6fd authored by suhuiguang's avatar suhuiguang

feat(statis):性能

1.大屏接口优化
parent 0ec38bc0
...@@ -36,7 +36,9 @@ public class ElasticSearchClientConfig { ...@@ -36,7 +36,9 @@ public class ElasticSearchClientConfig {
RestClientBuilder builder = RestClient.builder(httpHosts); RestClientBuilder builder = RestClient.builder(httpHosts);
builder.setHttpClientConfigCallback(httpClientBuilder -> { builder.setHttpClientConfigCallback(httpClientBuilder -> {
httpClientBuilder.disableAuthCaching(); httpClientBuilder.disableAuthCaching();
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)
.setMaxConnTotal(200)
.setMaxConnPerRoute(60);
}); });
// 该方法接收一个RequestConfig.Builder对象,对该对象进行修改后然后返回。 // 该方法接收一个RequestConfig.Builder对象,对该对象进行修改后然后返回。
builder.setRequestConfigCallback(requestConfigBuilder -> { builder.setRequestConfigCallback(requestConfigBuilder -> {
......
package com.yeejoin.amos.boot.module.statistcs.biz.config; package com.yeejoin.amos.boot.module.statistcs.biz.config;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.client.loadbalancer.LoadBalanced;
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.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
/** /**
...@@ -15,7 +19,18 @@ public class RestTemplateConfig { ...@@ -15,7 +19,18 @@ public class RestTemplateConfig {
@Bean @Bean
@LoadBalanced @LoadBalanced
public RestTemplate getRestTemplate() { public RestTemplate loadBalancedRestTemplate() {
return new RestTemplate(); PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager();
manager.setMaxTotal(200);
manager.setDefaultMaxPerRoute(50);
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(manager)
.build();
// 2. 创建自定义工厂(最终会被 InterceptingClientHttpRequestFactory 包装)
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
factory.setConnectTimeout(5000);
factory.setReadTimeout(60000);
return new RestTemplate(factory);
} }
} }
\ No newline at end of file
...@@ -54,8 +54,7 @@ public class DPSubServiceImpl { ...@@ -54,8 +54,7 @@ public class DPSubServiceImpl {
private static final String GATEWAY_SERVER_NAME = "AMOS-SERVER-GATEWAY"; private static final String GATEWAY_SERVER_NAME = "AMOS-SERVER-GATEWAY";
@Autowired @Autowired
@LoadBalanced private RestTemplate loadBalancedRestTemplate;
private RestTemplate restTemplate;
@Autowired @Autowired
DataDictionaryServiceImpl iDataDictionaryService; DataDictionaryServiceImpl iDataDictionaryService;
...@@ -857,6 +856,7 @@ public class DPSubServiceImpl { ...@@ -857,6 +856,7 @@ public class DPSubServiceImpl {
} }
public ResponseModel getApiResult(JSONObject apiObj, String resultConvert, JSONObject detailResult) { public ResponseModel getApiResult(JSONObject apiObj, String resultConvert, JSONObject detailResult) {
log.info("restTemplate 请求的客户端是:{}", loadBalancedRestTemplate.getRequestFactory().getClass());;
String url = apiObj.getString("apiPath"); String url = apiObj.getString("apiPath");
if(ValidationUtil.isEmpty(url)){ if(ValidationUtil.isEmpty(url)){
return null; return null;
...@@ -893,19 +893,19 @@ public class DPSubServiceImpl { ...@@ -893,19 +893,19 @@ public class DPSubServiceImpl {
URI reqUri = new URI(reqUrl); URI reqUri = new URI(reqUrl);
if (StringUtils.contrastLowerStr("GET", reqType)) { if (StringUtils.contrastLowerStr("GET", reqType)) {
responseEntity = reqUrl.contains(GATEWAY_SERVER_NAME) responseEntity = reqUrl.contains(GATEWAY_SERVER_NAME)
? RestTemplateUtils.get(restTemplate, reqUri, httpHeaders, body, String.class, new HashMap<>()) ? RestTemplateUtils.get(loadBalancedRestTemplate, reqUri, httpHeaders, body, String.class, new HashMap<>())
: RestTemplateUtils.get(reqUri, httpHeaders, body, String.class, new HashMap<>()); : RestTemplateUtils.get(reqUri, httpHeaders, body, String.class, new HashMap<>());
} else if (StringUtils.contrastLowerStr("POST", reqType)) { } else if (StringUtils.contrastLowerStr("POST", reqType)) {
responseEntity = reqUrl.contains(GATEWAY_SERVER_NAME) responseEntity = reqUrl.contains(GATEWAY_SERVER_NAME)
? RestTemplateUtils.post(restTemplate, reqUri, httpHeaders, body, String.class, new HashMap<>()) ? RestTemplateUtils.post(loadBalancedRestTemplate, reqUri, httpHeaders, body, String.class, new HashMap<>())
: RestTemplateUtils.post(reqUri, httpHeaders, body, String.class, new HashMap<>()); : RestTemplateUtils.post(reqUri, httpHeaders, body, String.class, new HashMap<>());
} else if (StringUtils.contrastLowerStr("PUT", reqType)) { } else if (StringUtils.contrastLowerStr("PUT", reqType)) {
responseEntity = reqUrl.contains(GATEWAY_SERVER_NAME) responseEntity = reqUrl.contains(GATEWAY_SERVER_NAME)
? RestTemplateUtils.put(restTemplate, reqUri, httpHeaders, body, String.class) ? RestTemplateUtils.put(loadBalancedRestTemplate, reqUri, httpHeaders, body, String.class)
: RestTemplateUtils.put(reqUri, httpHeaders, body, String.class); : RestTemplateUtils.put(reqUri, httpHeaders, body, String.class);
} else if (StringUtils.contrastLowerStr("DELETE", reqType)) { } else if (StringUtils.contrastLowerStr("DELETE", reqType)) {
responseEntity = reqUrl.contains(GATEWAY_SERVER_NAME) responseEntity = reqUrl.contains(GATEWAY_SERVER_NAME)
? RestTemplateUtils.delete(restTemplate, reqUri, httpHeaders, body, String.class) ? RestTemplateUtils.delete(loadBalancedRestTemplate, reqUri, httpHeaders, body, String.class)
: RestTemplateUtils.delete(reqUri, httpHeaders, body, String.class); : RestTemplateUtils.delete(reqUri, httpHeaders, body, String.class);
} }
} catch (Exception e) { } catch (Exception e) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment