SpringBoot如何在运⾏时动态添加数据源
此⽅案适⽤于解决springboot项⽬运⾏时动态添加数据源,⾮静态切换多数据源
⼀、多数据源应⽤场景:
1.配置⽂件配置多数据源,如默认数据源:master,数据源1:,运⾏时动态切换已配置的数据源(master、salve1互相切换),⽆法在运⾏时动态添加配置⽂件中未配置的数据源。
2.配置⼀个默认数据源,运⾏时动态添加新数据源使⽤(本博客适⽤于此场景)
⼆、解决⽅案:
Spring提供了AbstractRoutingDataSource⽤于动态路由数据源,第⼀种场景继承AbstractRoutingDataSource类并覆写其protected abstract Object determineCurrentLookupKey()即可;
⽽第⼆种场景我们直接覆写protected DataSource determineTargetDataSource⽅法即可。原理可看下AbstractRoutingDataSource对应源码,⽐较简单,不做赘述。
直接上⼲货:
import com.fizz.utils.spring.SpringUtils;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import javax.sql.DataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
private static final ThreadLocal<DataSource> dataSource = ThreadLocal.withInitial(() -> (DataSource) Bean("defaultDataSource"));
public static void setDataSource(DataSource dataSource) {
DynamicDataSource.dataSource.set(dataSource);
}
public static DataSource getDataSource() {
return ();
}
@Override
protected Object determineCurrentLookupKey() {
return null;
}
@Override
protected DataSource determineTargetDataSource() {
return getDataSource();
}
public static void clear() {
ve();
}
}
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.t.properties.ConfigurationProperties;
import t.annotation.Bean;
import t.annotation.Configuration;
import t.annotation.Primary;
import javax.sql.DataSource;
import java.util.HashMap;
@Configuration
public class DataSourceConfig {
@Bean
@ConfigurationProperties("spring.datasource.druid")
public DataSource defaultDataSource() {
ate().build();
}
@Bean
@Primary
public DynamicDataSource dataSource() {
DynamicDataSource dynamicDataSource = new DynamicDataSource();
dynamicDataSource.setTargetDataSources(new HashMap<>());
return dynamicDataSource;
}
springboot原理pdf}
使⽤时直接调⽤DynamicDataSource.setDataSource(DataSource dataSource)⽅法即可,使⽤完后调⽤DynamicDataSource.clear()防⽌内存泄漏并重置默认数据源。
附上详细使⽤⽅法:
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setUrl("jdbc:mysql://localhost:3306/sys?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&useAffectedRows=true");    druidDataSource.setUsername("root");
druidDataSource.setPassword("root");
DynamicDataSource.setDataSource(druidDataSource);
此时数据源已切换到druidDataSource ,调⽤⾃⼰的业务⽅法即可。
使⽤完后调⽤DynamicDataSource.clear();重置为默认数据源。
附上⼯具类SpringUtils :
import lombok.Getter;
import org.springframework.beans.BeansException;
import t.ApplicationContext;
import t.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public final class SpringUtils implements ApplicationContextAware {
@Getter
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (SpringUtils.applicationContext == null) {
SpringUtils.applicationContext = applicationContext;
}
}
public static <T> T getBean(Class<T> clazz) {
return Bean(clazz);
}
public static Object getBean(String name) {
return Bean(name);
}
public static String getProperty(String key) {
return Environment().getProperty(key);
}
}
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。