Skip to content

Commit

Permalink
perf($Starter): abstract rest template configuration
Browse files Browse the repository at this point in the history
[skip ci]
  • Loading branch information
johnnymillergh committed Jan 29, 2021
1 parent d343369 commit 07dfbff
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
RedisConfiguration.class,
Swagger2Configuration.class,
SftpConfiguration.class,
WebSecurityConfiguration.class
WebSecurityConfiguration.class,
RestTemplateConfiguration.class
})
public class MafAutoConfiguration {
@PostConstruct
Expand Down Expand Up @@ -161,11 +162,4 @@ public JwtConfiguration jwtConfiguration(MafProjectProperty mafProjectProperty)
log.warn("Initial bean: '{}'", JwtConfiguration.class.getSimpleName());
return new JwtConfiguration(mafProjectProperty);
}

@Bean
@LoadBalanced
public RestTemplate restTemplate() {
log.warn("Initial bean: '{}'", RestTemplate.class.getSimpleName());
return new RestTemplate();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.jmsoftware.maf.springbootstarter.configuration;

import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestTemplate;

/**
* Description: RestTemplateConfiguration, change description here.
*
* @author 钟俊(zhongjun), email: zhongjun@toguide.cn, date: 1/29/2021 4:29 PM
**/
@Slf4j
@Configuration
public class RestTemplateConfiguration {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
val poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
poolingHttpClientConnectionManager.setMaxTotal(1000);
poolingHttpClientConnectionManager.setDefaultMaxPerRoute(1000);
val httpClientBuilder = HttpClients.custom();
httpClientBuilder.setConnectionManager(poolingHttpClientConnectionManager);
val httpClient = httpClientBuilder.build();
val httpComponentsClientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
httpComponentsClientHttpRequestFactory.setConnectTimeout(6000);
httpComponentsClientHttpRequestFactory.setReadTimeout(6000);
httpComponentsClientHttpRequestFactory.setConnectionRequestTimeout(200);
val restTemplate = new RestTemplate();
restTemplate.setRequestFactory(httpComponentsClientHttpRequestFactory);
restTemplate.setErrorHandler(new DefaultResponseErrorHandler());
log.warn("Initial bean: '{}'", restTemplate.getClass().getSimpleName());
return restTemplate;
}
}

0 comments on commit 07dfbff

Please sign in to comment.