-
Notifications
You must be signed in to change notification settings - Fork 361
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from kingschan1204/develop
1.x稳定版本合并
- Loading branch information
Showing
49 changed files
with
2,856 additions
and
584 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
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
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
78 changes: 78 additions & 0 deletions
78
src/main/java/io.github.kingschan1204.istock/common/conf/RestClientConfig.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,78 @@ | ||
package io.github.kingschan1204.istock.common.conf; | ||
|
||
import org.apache.http.client.HttpClient; | ||
import org.apache.http.config.Registry; | ||
import org.apache.http.config.RegistryBuilder; | ||
import org.apache.http.conn.socket.ConnectionSocketFactory; | ||
import org.apache.http.conn.socket.PlainConnectionSocketFactory; | ||
import org.apache.http.conn.ssl.NoopHostnameVerifier; | ||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; | ||
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler; | ||
import org.apache.http.impl.client.HttpClientBuilder; | ||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; | ||
import org.apache.http.ssl.SSLContextBuilder; | ||
import org.apache.http.ssl.TrustStrategy; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
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; | ||
import javax.net.ssl.HostnameVerifier; | ||
import javax.net.ssl.SSLContext; | ||
import java.security.KeyManagementException; | ||
import java.security.KeyStoreException; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.cert.CertificateException; | ||
import java.security.cert.X509Certificate; | ||
|
||
/** | ||
* restTemplate 配置 | ||
* @author chenguoxiang | ||
* @create 2018-10-30 15:56 | ||
**/ | ||
@Configuration | ||
public class RestClientConfig { | ||
private static Logger log = LoggerFactory.getLogger(RestClientConfig.class); | ||
|
||
@Bean | ||
public RestTemplate restTemplate() { | ||
RestTemplate restTemplate = new RestTemplate(); | ||
restTemplate.setRequestFactory(clientHttpRequestFactory()); | ||
restTemplate.setErrorHandler(new DefaultResponseErrorHandler()); | ||
return restTemplate; | ||
} | ||
@Bean | ||
public HttpComponentsClientHttpRequestFactory clientHttpRequestFactory() { | ||
try { | ||
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); | ||
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { | ||
public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { | ||
return true; | ||
} | ||
}).build(); | ||
httpClientBuilder.setSSLContext(sslContext); | ||
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE; | ||
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier); | ||
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() | ||
.register("http", PlainConnectionSocketFactory.getSocketFactory()) | ||
.register("https", sslConnectionSocketFactory).build();// 注册http和https请求 | ||
// 开始设置连接池 | ||
PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry); | ||
poolingHttpClientConnectionManager.setMaxTotal(500); // 最大连接数500 | ||
poolingHttpClientConnectionManager.setDefaultMaxPerRoute(100); // 同路由并发数100 | ||
httpClientBuilder.setConnectionManager(poolingHttpClientConnectionManager); | ||
httpClientBuilder.setRetryHandler(new DefaultHttpRequestRetryHandler(3, true)); // 重试次数 | ||
HttpClient httpClient = httpClientBuilder.build(); | ||
HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient); // httpClient连接配置 | ||
clientHttpRequestFactory.setConnectTimeout(20000); // 连接超时 | ||
clientHttpRequestFactory.setReadTimeout(30000); // 数据读取超时时间 | ||
clientHttpRequestFactory.setConnectionRequestTimeout(20000); // 连接不够用的等待时间 | ||
return clientHttpRequestFactory; | ||
} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) { | ||
log.error("初始化HTTP连接池出错{}", e); | ||
} | ||
return null; | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.