com.github.javaclub.cdl.client.atom.SAtomDataSource Maven / Gradle / Ivy
package com.github.javaclub.cdl.client.atom;
import java.sql.SQLException;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.druid.pool.DruidDataSource;
import com.github.javaclub.cdl.client.config.DbConfigManager;
import com.github.javaclub.cdl.client.util.LogUtils;
/**
* 对应一个数据库实例,持有数据源
*/
public class SAtomDataSource extends DruidDataSource {
/**
*
*/
private static final long serialVersionUID = 7801719289867678817L;
private static final Logger logger = LoggerFactory.getLogger(SAtomDataSource.class);
private String dsKey; // 如:db0,db1...
private String groupName; // 所属组
private String ip;
private int port;
private String dbName;
private int maxActive = 20; // 连接数
private int initialSize = 1;
private long maxWait = 10000l;
private int minIdle = 8;
private boolean testOnBorrow = false;
private boolean testWhileIdle = true;
private boolean testOnReturn = false;
private long timeBetweenEvictionRunsMillis = 60000l;
private long minEvictableIdleTimeMillis = 300000l;
private final String driverClassName = "com.mysql.jdbc.Driver";
private final String JDBC_CFG = "jdbc_cfg";
private String JDBC_CFG_URL = "";
private DbConfigManager dbConfigManager;
public void setUp() throws SQLException {
Map confMap = dbConfigManager.getAtomConf(groupName, dsKey, this);
if (confMap != null) {
if (StringUtils.isNotBlank(confMap.get("username"))) {
super.setUsername(confMap.get("username").trim());
}
if (StringUtils.isNotBlank(confMap.get("password"))) {
super.setPassword(confMap.get("password").trim());
}
if (StringUtils.isNotBlank(confMap.get("ip"))) {
this.ip = confMap.get("ip").trim();
}
if("dev".equals(dbConfigManager.getEnv()) && StringUtils.isNotBlank(confMap.get("devIp"))){
this.ip = confMap.get("devIp").trim();
}
if (StringUtils.isNotBlank(confMap.get("port"))) {
this.port = Integer.parseInt(confMap.get("port"));
}
if (StringUtils.isNotBlank(confMap.get("dbName"))) {
this.dbName = confMap.get("dbName").trim();
}
if (StringUtils.isNotBlank(confMap.get("maxActive"))) {
this.maxActive = Integer.parseInt(confMap.get("maxActive"));
}
if (StringUtils.isNotBlank(confMap.get(JDBC_CFG))) {
this.JDBC_CFG_URL = confMap.get(JDBC_CFG);
}
} else {
throw new RuntimeException("not found db conf for " + dsKey + "," + groupName);
}
super.setUrl(wrapUrl(ip, port, dbName));
super.setDriverClassName(driverClassName);
super.setMaxActive(maxActive);
super.setInitialSize(initialSize);
super.setMaxWait(maxWait);
super.setMinIdle(minIdle);
super.setTestOnBorrow(testOnBorrow);
super.setTestWhileIdle(testWhileIdle);
super.setTestOnReturn(testOnReturn);
super.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
super.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
super.setValidationQuery("select 'x'");
super.init();
logger.warn("inited ds: {}", this.toString());
LogUtils.printCdlLog(String.format("inited ds: %s", this.toString()));
}
/**
* DB配置变更
*
* @param confMap
*/
public void updateConf(Map confMap) {
if (confMap != null) { // 目前只支持密码、最大连接数修改
if (StringUtils.isNotBlank(confMap.get("password"))) {
super.setPassword(confMap.get("password").trim());
}
if (StringUtils.isNotBlank(confMap.get("maxActive"))) {
super.setMaxActive(Integer.parseInt(confMap.get("maxActive")));
}
}
}
private String wrapUrl(String ip, int port, String dbName) {
StringBuilder sb = new StringBuilder();
sb.append("jdbc:mysql://");
sb.append(ip);
sb.append(":");
sb.append(port);
sb.append("/");
sb.append(dbName);
sb.append("?");
if(StringUtils.isNotBlank(JDBC_CFG_URL)){
sb.append(JDBC_CFG_URL);
}else{
sb.append("useUnicode=true&autoReconnect=true&failOverReadOnly=false&zeroDateTimeBehavior=convertToNull");
}
logger.warn("jdbc[dskey={}, url={}]", dsKey, sb.toString());
LogUtils.printCdlLog(String.format("jdbc[dskey=%s, url=%s]", dsKey, sb.toString()));
return sb.toString();
}
public void close() {
super.close();
}
public String getDsKey() {
return dsKey;
}
public void setDsKey(String dsKey) {
this.dsKey = dsKey;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDbName() {
return dbName;
}
public void setDbName(String dbName) {
this.dbName = dbName;
}
public int getMaxActive() {
return maxActive;
}
public void setMaxActive(int maxActive) {
this.maxActive = maxActive;
}
public int getInitialSize() {
return initialSize;
}
public void setInitialSize(int initialSize) {
this.initialSize = initialSize;
}
public long getMaxWait() {
return maxWait;
}
public void setMaxWait(long maxWait) {
this.maxWait = maxWait;
}
public int getMinIdle() {
return minIdle;
}
public void setMinIdle(int minIdle) {
this.minIdle = minIdle;
}
public boolean isTestOnBorrow() {
return testOnBorrow;
}
public void setTestOnBorrow(boolean testOnBorrow) {
this.testOnBorrow = testOnBorrow;
}
public boolean isTestWhileIdle() {
return testWhileIdle;
}
public void setTestWhileIdle(boolean testWhileIdle) {
this.testWhileIdle = testWhileIdle;
}
public boolean isTestOnReturn() {
return testOnReturn;
}
public void setTestOnReturn(boolean testOnReturn) {
this.testOnReturn = testOnReturn;
}
public long getTimeBetweenEvictionRunsMillis() {
return timeBetweenEvictionRunsMillis;
}
public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) {
this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
}
public long getMinEvictableIdleTimeMillis() {
return minEvictableIdleTimeMillis;
}
public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {
this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
}
public DbConfigManager getDbConfigManager() {
return dbConfigManager;
}
public void setDbConfigManager(DbConfigManager dbConfigManager) {
this.dbConfigManager = dbConfigManager;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
@Override
public String toString() {
return "SAtomDataSource [dsKey=" + dsKey + ", ip=" + ip + ", port=" + port + ", username=" + username + ", dbName=" + dbName + "]";
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy