1.整合redis
@Configuration
@PropertySource(value = "classpath:redis.properties")
public class RedisSpringConfig {
??? @Value("${redis.maxTotal}")
??? private Integer redisMaxTotal;
??? @Value("${redis.node1.host}")
??? private String redisNode1Host;
??? @Value("${redis.node1.port}")
??? private Integer redisNode1Port;
??? private JedisPoolConfig jedisPoolConfig() {
??????? JedisPoolConfigjedisPoolConfig = newJedisPoolConfig();
??????? jedisPoolConfig.setMaxTotal(redisMaxTotal);
??????? return jedisPoolConfig;
??? }
??? @Bean
??? public ShardedJedisPool shardedJedisPool() {
??????? ListjedisShardInfos = newArrayList<JedisShardInfo>();
??????? jedisShardInfos.add(new JedisShardInfo(redisNode1Host, redisNode1Port));
??????? return newShardedJedisPool(jedisPoolConfig(), jedisShardInfos);
??? }
}
2.整合httpclient
@Configuration
@PropertySource(value = "classpath:httpclient.properties")
public class HttpclientSpringConfig {
??? @Value("${http.maxTotal}")
??? private Integer httpMaxTotal;
??? @Value("${http.defaultMaxPerRoute}")
??? private Integer httpDefaultMaxPerRoute;
??? @Value("${http.connectTimeout}")
??? private Integer httpConnectTimeout;
??? @Value("${http.connectionRequestTimeout}")
??? private Integer httpConnectionRequestTimeout;
??? @Value("${http.socketTimeout}")
??? private Integer httpSocketTimeout;
??? @Value("${http.staleConnectionCheckEnabled}")
??? private Boolean httpStaleConnectionCheckEnabled;
??? @Autowired
??? privatePoolingHttpClientConnectionManager manager;
??? @Bean
??? publicPoolingHttpClientConnectionManager poolingHttpClientConnectionManager() {
??????? PoolingHttpClientConnectionManager? poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
??????? // 最大連接數(shù)
??????? poolingHttpClientConnectionManager.setMaxTotal(httpMaxTotal);
??????? // 每個(gè)主機(jī)的最大并發(fā)數(shù)
??????? poolingHttpClientConnectionManager.setDefaultMaxPerRoute(httpDefaultMaxPerRoute);
??????? return poolingHttpClientConnectionManager;
??? }
??? // 定期關(guān)閉無效連接
??? @Bean
??? public IdleConnectionEvictor idleConnectionEvictor() {
??????? return new IdleConnectionEvictor(manager);
??? }
??? // 定義Httpclient對
??? @Bean
??? @Scope("prototype")
??? public CloseableHttpClient closeableHttpClient() {
??????? return?HttpClients.custom().setConnectionManager(this.manager).build();
??? }
??? // 請求配置
??? @Bean
??? public RequestConfig requestConfig(){
??????? returnRequestConfig.custom().setConnectTimeout(httpConnectTimeout) // 創(chuàng)建連接的最長時(shí)間
??????????????? .setConnectionRequestTimeout(httpConnectionRequestTimeout) // 從連接池中獲取到連接的最長時(shí)間
??????????????? .setSocketTimeout(httpSocketTimeout) // 數(shù)據(jù)傳輸?shù)淖铋L時(shí)間
??????????????? .setStaleConnectionCheckEnabled(httpStaleConnectionCheckEnabled) // 提交請求前測試連接是否可用
??????????????? .build();
??? }
}