init: 导入项目
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package com.ag.config;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.SQLFeatureNotSupportedException;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* @description: 动态数据源代理类
|
||||
* @author: WangN
|
||||
* @create: 2019-08-16
|
||||
*/
|
||||
public abstract class AbstractDataSourceProxy implements DataSource, Closeable {
|
||||
|
||||
public abstract DataSource getMainDataSource();
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
((Closeable)getMainDataSource()).close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection() throws SQLException {
|
||||
return getMainDataSource().getConnection();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection(String username, String password) throws SQLException {
|
||||
return getMainDataSource().getConnection(username, password);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T unwrap(Class<T> iface) throws SQLException {
|
||||
return getMainDataSource().unwrap(iface);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isWrapperFor(Class<?> iface) throws SQLException {
|
||||
return getMainDataSource().isWrapperFor(iface);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PrintWriter getLogWriter() throws SQLException {
|
||||
return getMainDataSource().getLogWriter();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLogWriter(PrintWriter out) throws SQLException {
|
||||
getMainDataSource().setLogWriter(out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLoginTimeout(int seconds) throws SQLException {
|
||||
getMainDataSource().setLoginTimeout(seconds);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLoginTimeout() throws SQLException {
|
||||
return getMainDataSource().getLoginTimeout();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
|
||||
return getMainDataSource().getParentLogger();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
23
commonapi/src/main/java/com/ag/config/BaseDataSourceCfg.java
Normal file
23
commonapi/src/main/java/com/ag/config/BaseDataSourceCfg.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package com.ag.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* @description: 主工程基础数据源
|
||||
* @author: WangN
|
||||
* @create: 2019-08-19
|
||||
*/
|
||||
//@Repository
|
||||
public class BaseDataSourceCfg extends AbstractDataSourceProxy {
|
||||
|
||||
//web工程配置的数据源beanName=dataSource
|
||||
@Autowired
|
||||
private DataSource dataSource;
|
||||
@Override
|
||||
public DataSource getMainDataSource() {
|
||||
return this.dataSource;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.ag.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.connection.RedisConnection;
|
||||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
|
||||
/**
|
||||
* @description: jedis连接工厂代理
|
||||
* @author: WangN
|
||||
* @create: 2019-09-18
|
||||
*/
|
||||
//@Repository
|
||||
public class JedisConnectionFactoryProxy extends JedisConnectionFactory {
|
||||
|
||||
@Autowired
|
||||
private JedisConnectionFactory jedisConnectionFactory;
|
||||
|
||||
@Override
|
||||
public RedisConnection getConnection() {
|
||||
return jedisConnectionFactory.getConnection();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHostName() {
|
||||
return jedisConnectionFactory.getHostName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHostName(String hostName) {
|
||||
jedisConnectionFactory.setHostName(hostName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPassword() {
|
||||
return jedisConnectionFactory.getPassword();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPassword(String password) {
|
||||
jedisConnectionFactory.setPassword(password);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPort() {
|
||||
return jedisConnectionFactory.getPort();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPort(int port) {
|
||||
jedisConnectionFactory.setPort(port);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JedisPoolConfig getPoolConfig() {
|
||||
return jedisConnectionFactory.getPoolConfig();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPoolConfig(JedisPoolConfig poolConfig) {
|
||||
jedisConnectionFactory.setPoolConfig(poolConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getDatabase() {
|
||||
return jedisConnectionFactory.getDatabase();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDatabase(int index) {
|
||||
jedisConnectionFactory.setDatabase(index);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package com.ag.config;
|
||||
|
||||
import org.apache.http.Header;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
|
||||
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.message.BasicHeader;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||
import org.springframework.web.client.DefaultResponseErrorHandler;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @description: 调用http
|
||||
* @author: WangN
|
||||
* @create: 2019-11-13
|
||||
*/
|
||||
@Configuration
|
||||
public class RestTemplateConfig {
|
||||
|
||||
/**
|
||||
* 返回RestTemplate
|
||||
* @param factory
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public RestTemplate restTemplate(ClientHttpRequestFactory factory){
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
restTemplate.setRequestFactory(clientHttpRequestFactory());
|
||||
restTemplate.setErrorHandler(new DefaultResponseErrorHandler());
|
||||
return restTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* ClientHttpRequestFactory接口的另一种实现方式(推荐使用),即:
|
||||
* HttpComponentsClientHttpRequestFactory:底层使用Httpclient连接池的方式创建Http连接请求
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public HttpComponentsClientHttpRequestFactory clientHttpRequestFactory(){
|
||||
//Httpclient连接池,长连接保持30秒
|
||||
PoolingHttpClientConnectionManager connectionManager =
|
||||
new PoolingHttpClientConnectionManager(30, TimeUnit.SECONDS);
|
||||
|
||||
//设置总连接数
|
||||
connectionManager.setMaxTotal(1000);
|
||||
//设置同路由的并发数
|
||||
connectionManager.setDefaultMaxPerRoute(1000);
|
||||
|
||||
//设置header
|
||||
List<Header> headers = new ArrayList<Header>();
|
||||
headers.add(new BasicHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.04"));
|
||||
headers.add(new BasicHeader("Accept-Encoding", "gzip, deflate"));
|
||||
headers.add(new BasicHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3"));
|
||||
headers.add(new BasicHeader("Connection", "keep-alive"));
|
||||
|
||||
//创建HttpClient
|
||||
HttpClient httpClient = HttpClientBuilder.create()
|
||||
.setConnectionManager(connectionManager)
|
||||
.setDefaultHeaders(headers)
|
||||
.setRetryHandler(new DefaultHttpRequestRetryHandler(3, true)) //设置重试次数
|
||||
.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy()) //设置保持长连接
|
||||
.build();
|
||||
|
||||
//创建HttpComponentsClientHttpRequestFactory实例
|
||||
HttpComponentsClientHttpRequestFactory requestFactory =
|
||||
new HttpComponentsClientHttpRequestFactory(httpClient);
|
||||
|
||||
//设置客户端和服务端建立连接的超时时间
|
||||
requestFactory.setConnectTimeout(12000);
|
||||
//设置客户端从服务端读取数据的超时时间
|
||||
requestFactory.setReadTimeout(12000);
|
||||
//设置从连接池获取连接的超时时间,不宜过长
|
||||
requestFactory.setConnectionRequestTimeout(200);
|
||||
//缓冲请求数据,默认为true。通过POST或者PUT大量发送数据时,建议将此更改为false,以免耗尽内存
|
||||
requestFactory.setBufferRequestBody(true);
|
||||
return requestFactory;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user