实现环境:采用JDK8,SpringBoot2.x实现
设置了三个枚举常量,名字见名知意的,然后采用了阿里的druid组件,启了三个线程池。用了一个map,添加了三个key,根据ip地址去选择不通的key
实现关键点1:SpringBoot中properties设置
#dev开发环境
multiple.datasource.dev.driver-class-name=oracle.jdbc.OracleDriver
multiple.datasource.dev.url=jdbc:mysql://localhost:3306/db_master?useSSL=false&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&allowMultiQueries=true
multiple.datasource.dev.username=root
multiple.datasource.dev.password=root
#blue蓝色环境
multiple.datasource.blue.driver-class-name=oracle.jdbc.OracleDriver
multiple.datasource.blue.url=jdbc:oracle:thin:@192.168.51.xx:3555:orcl
multiple.datasource.blue.username=root
multiple.datasource.blue.password=root
#green绿色环境
multiple.datasource.green.driver-class-name=oracle.jdbc.OracleDriver
multiple.datasource.green.url=jdbc:oracle:thin:@18.4.18.xx:3555:orcl
multiple.datasource.green.username=root
multiple.datasource.green.password=root
实现关键点2:获取主机的ip地址,在DynamicDataSourceContextHolder中根据ip地址绑定现有的数据源,并根据不同的DataSourceKey 的key值获取数据源,ip地址写在常量类中(如果返回error错误就重启server端)
/**
* 获取主机ip地址(linux或windows),查找不到返回error
* @return
*/
public static String getCurrentIp(){
try{
//枚举本机所有的网络接口
Enumeration<NetworkInterface> networkInterfaces =NetworkInterface.getNetworkInterfaces();
while(networkInterfaces.hasMoreElements()){
NetworkInterface ni =(NetworkInterface)networkInterfaces.nextElement();
Enumeration<InetAddress> nias =ni.getInetAddresses();
while(nias.hasMoreElements()){
InetAddress ia=(InetAddress)nias.nextElement();
//!本地连接地址&&!回送地址&&Inet4Address类型
if(!ia.isLinkLocalAddress()&&!ia.isLoopbackAddress()&&ia instanceof Inet4Address){
//转为String类型
String IPStr= ia.toString();
int indexOf = IPStr.indexOf('/');
return IPStr.substring(indexOf+1);
}
}
}
}catch(SocketException e){
logger.error(e.getMessage(),e);
}
return "error";
}
实现关键点3:设置枚举量,并作为数据库的key值
/**
* @author jakie
* @version 1.0
*/
public enum DataSourceKey {
DB_BLUE,//蓝色环境
DB_GREEN,//绿色环境
DB_DEV,//开发环境
}
启动druid数据库连接池,并设置key所在的数据库连接
DynamicDataSourceConfiguration类中设置数据库连接池,并管理
/**
* 开发环境
* @return
*/
@Bean
@ConfigurationProperties(prefix = "multiple.datasource.dev")
public DataSource dbDev() {
return DruidDataSourceBuilder.create().build();
}
/**
* 蓝色环境
* @return
*/
@Bean
@ConfigurationProperties(prefix = "multiple.datasource.blue")
public DataSource dbBlue() {
return DruidDataSourceBuilder.create().build();
}
/**
* 绿色环境
* @return
*/
@Bean
@ConfigurationProperties(prefix = "multiple.datasource.green")
public DataSource dbGreen() {
return DruidDataSourceBuilder.create().build();
}
/**
* 核心动态数据源
*
* @return 数据源实例
*/
@Bean
public DataSource dynamicDataSource() {
DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource();
dataSource.setDefaultTargetDataSource(dbBlue());
//设置数据源Map
Map<Object, Object> dataSourceMap = new HashMap<>(4);
dataSourceMap.put(DataSourceKey.DB_DEV, dbDev());
dataSourceMap.put(DataSourceKey.DB_GREEN, dbBlue());
dataSourceMap.put(DataSourceKey.DB_BLUE, dbGreen());
dataSource.setTargetDataSources(dataSourceMap);
return dataSource;
}