|
|
package com.yoho.datasync.consumer.handler.config;
|
|
|
package com.yoho.datasync.consumer.starter.config;
|
|
|
|
|
|
import com.alibaba.druid.pool.DruidDataSource;
|
|
|
import lombok.Data;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.context.annotation.Primary;
|
|
|
|
|
|
import javax.sql.DataSource;
|
|
|
|
|
|
@Data
|
|
|
@Configuration
|
|
|
@ConfigurationProperties(prefix="spring.datasource")
|
|
|
public class DataSourceConfig {
|
|
|
|
|
|
@Value("${spring.datasource.primary.url}")
|
|
|
private String url;
|
|
|
|
|
|
@Value("${spring.datasource.primary.username}")
|
|
|
private String username;
|
|
|
|
|
|
@Value("${spring.datasource.primary.password}")
|
|
|
private String password;
|
|
|
|
|
|
@Value("${spring.datasource.yhGrass.url}")
|
|
|
private String yhGrassUrl;
|
|
|
|
|
|
@Value("${spring.datasource.yhGrass.username}")
|
|
|
private String yhGrassUsername;
|
|
|
|
|
|
@Value("${spring.datasource.yhGrass.password}")
|
|
|
private String yhGrassPassword;
|
|
|
|
|
|
|
|
|
public static final String text = "1qaz2wsx3edc4rfv";
|
|
|
|
|
|
@Bean
|
|
|
public DataSource getDataSource() throws Exception{
|
|
|
/**
|
|
|
* 连接池配置
|
|
|
*/
|
|
|
@Bean(name = "primaryDataSource")
|
|
|
@Qualifier("primaryDataSource")
|
|
|
@Primary
|
|
|
public DataSource primaryDataSource() throws Exception {
|
|
|
return getDataSource(url, username, password);
|
|
|
}
|
|
|
|
|
|
@Bean(name = "yhGrassDataSource")
|
|
|
public DataSource yhGrassDataSource() throws Exception {
|
|
|
return getDataSource(yhGrassUrl, yhGrassUsername, yhGrassPassword);
|
|
|
}
|
|
|
|
|
|
|
|
|
private DataSource getDataSource(String url, String userName, String passWord) throws Exception {
|
|
|
/*
|
|
|
// * 连接池配置
|
|
|
// */
|
|
|
int initialSize = 5; //初始化连接
|
|
|
int maxTotal = 30; //最大活动连接
|
|
|
int maxIdle = 15; //最大空闲连接
|
|
|
int minIdle = 10; //最小空闲连接
|
|
|
int defaultQueryTimeout = 1000; //查询超时时间
|
|
|
|
|
|
/**
|
|
|
/*
|
|
|
* 定期检测连接是否有效
|
|
|
*/
|
|
|
boolean testWhileIdle = true;
|
...
|
...
|
@@ -49,15 +75,15 @@ public class DataSourceConfig { |
|
|
//池中的连接空闲120秒后被回收
|
|
|
int minEvictableIdleTimeMillis = 120000;
|
|
|
DruidDataSource dataSource = new DruidDataSource();
|
|
|
password = StringUtils.trim(password);
|
|
|
passWord = StringUtils.trim(passWord);
|
|
|
//对密码进行解密
|
|
|
password = AES.decrypt(text, password);
|
|
|
passWord = AES.decrypt(text, passWord);
|
|
|
dataSource.setUrl(url);
|
|
|
dataSource.setUsername(username);// 用户名
|
|
|
dataSource.setPassword(password);// 密码
|
|
|
dataSource.setUsername(userName);// 用户名
|
|
|
dataSource.setPassword(passWord);// 密码
|
|
|
dataSource.setInitialSize(initialSize);
|
|
|
dataSource.setMaxActive(maxTotal);
|
|
|
dataSource.setMaxIdle(maxIdle);
|
|
|
// dataSource.setMaxIdle(maxIdle);
|
|
|
dataSource.setMinIdle(minIdle);
|
|
|
dataSource.setQueryTimeout(defaultQueryTimeout);
|
|
|
dataSource.setTestWhileIdle(testWhileIdle);
|
...
|
...
|
@@ -71,5 +97,4 @@ public class DataSourceConfig { |
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} |
...
|
...
|
|