-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf($Starter): abstract rest template configuration
[skip ci]
- Loading branch information
1 parent
d343369
commit 07dfbff
Showing
2 changed files
with
43 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...in/java/com/jmsoftware/maf/springbootstarter/configuration/RestTemplateConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |