您好,登录后才能下订单哦!
这篇文章运用了实例代码展示springboot动态数据源的使用方法,代码非常详细,可供感兴趣的小伙伴们参考借鉴,希望对大家有所帮助。
我们的数据库A为主库,其他数据库配置在主库中,从库B,C,D的数量是不固定的,会根据业务的需要动态的把配置写入到主库中并动态在创建新的数据库,也就是说在项目中我们只需要配置主库的数据源,其他从库都需要从主库中读出配置并动态创建数据源,动态的注入到Spring容器中,在使用的时候动态的切换数据源以实现相应的功能逻辑
Springboot:2.0.4
Mybatis-plus:3.0.7.1
JDK:1.8
在启动类添加@Import({DynamicDataSourceRegister.class})注解用于代替默认的数据源配置
代码结构如下:
@Component
public class ApplicationContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ApplicationContextUtil.applicationContext = applicationContext;
}
/**
* 取得存储在静态变量中的ApplicationContext.
*/
public static ApplicationContext getApplicationContext() {
checkApplicationContext();
return applicationContext;
}
/**
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
public static <T> T getBean(String name) {
checkApplicationContext();
if (applicationContext.containsBean(name)) {
return (T) applicationContext.getBean(name);
}
return null;
}
/**
* 从静态变量ApplicationContext中取得Bean, 自动转型为所赋值对象的类型.
*/
public static <T> T getBean(Class<T> clazz) {
checkApplicationContext();
return (T) applicationContext.getBeansOfType(clazz);
}
private static void checkApplicationContext() {
if (applicationContext == null)
throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextUtil");
}
public synchronized static void registerSingletonBean(String beanName,Class clzz,Map<String,Object> original) {
checkApplicationContext();
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) ApplicationContextUtil.getApplicationContext().getAutowireCapableBeanFactory();
if(beanFactory.containsBean(beanName)){
removeBean(beanName);
}
GenericBeanDefinition definition = new GenericBeanDefinition();
//类class
definition.setBeanClass(clzz);
//属性赋值
definition.setPropertyValues(new MutablePropertyValues(original));
//注册到spring上下文
beanFactory.registerBeanDefinition(beanName, definition);
}
public synchronized static void registerSingletonBean(String beanName, Object obj, Map<String,Object> original) {
checkApplicationContext();
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) ApplicationContextUtil.getApplicationContext().getAutowireCapableBeanFactory();
if(beanFactory.containsBean(beanName)){
removeBean(beanName);
}
GenericBeanDefinition definition = new GenericBeanDefinition();
//类class
definition.setBeanClass(obj.getClass());
//属性赋值
definition.setPropertyValues(new MutablePropertyValues(original));
//注册到spring上下文
beanFactory.registerBeanDefinition(beanName, definition);
}
public synchronized static void registerSingletonBean(String beanName,Object obj) {
registerSingletonBean(beanName,obj,BeanUtils.transBean2Map(obj));
}
/**
* 删除spring中管理的bean
* @param beanName
*/
public static void removeBean(String beanName){
ApplicationContext ctx = ApplicationContextUtil.getApplicationContext();
DefaultListableBeanFactory acf = (DefaultListableBeanFactory) ctx.getAutowireCapableBeanFactory();
if(acf.containsBean(beanName)) {
acf.removeBeanDefinition(beanName);
}
}
}
public class BeanUtils {
public static Map<String, Object> transBean2Map(Object obj) {
if(obj == null){
return null;
}
Map<String, Object> map = new HashMap<>();
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
// 过滤class属性
if (!key.equals("class")) {
// 得到property对应的getter方法
Method getter = property.getReadMethod();
Object value = getter.invoke(obj);
map.put(key, value);
}
}
} catch (IntrospectionException | InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
}
return map;
}
}
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DynamicDataSourceContextHolder.getDataSourceType();
}
public void updateTargetDataSource(Map<String,DataSource> customDataSources){
Map<Object,Object> customDS=new HashMap<Object, Object>();
customDS.putAll(customDataSources);
setTargetDataSources(customDS);
afterPropertiesSet();
}
}
public class DynamicDataSourceContextHolder {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
public static List<String> dataSourceIds = new ArrayList<>();
public static String getDataSourceType() {
return contextHolder.get();
}
public static void setDataSourceType(String dataSourceType) {
if(!containsDataSource(dataSourceType)){
DynamicDataSourceRegister.addSlaveDataSource(dataSourceType);
}
contextHolder.set(dataSourceType);
}
public static void clearDataSourceType() {
contextHolder.remove();
}
/**
* 判断指定DataSrouce当前是否存在
*/
public static boolean containsDataSource(String dataSourceId) {
return dataSourceIds.contains(dataSourceId);
}
}
public class DynamicDataSourceRegister implements ImportBeanDefinitionRegistrar, EnvironmentAware {
private static final Logger logger = LoggerFactory.getLogger(DynamicDataSourceRegister.class);
//默认数据源
public static DataSource defaultDataSource;
//用户自定义数据源
public static Map<String, DataSource> slaveDataSources = new HashMap<>();
public static BeanDefinitionRegistry beanDefinitionRegistry=null;
public static String driverName;
public static String userName;
public static String password;
public static String type;
public static String url;
@Override
public void setEnvironment(Environment environment) {
initDefaultDataSource(environment);
}
private void initDefaultDataSource(Environment env) {
// 读取主数据源
driverName=env.getProperty("spring.datasource.driver-class-name");
userName=env.getProperty("spring.datasource.username");
password=env.getProperty("spring.datasource.password");
type=env.getProperty("spring.datasource.type");
url=env.getProperty("spring.datasource.url");
Constant.defaultDbName="a";
Map<String, Object> dsMap = new HashMap<>();
dsMap.put("driver",driverName);
dsMap.put("url",url);
dsMap.put("username",userName);
dsMap.put("password",password);
dsMap.put("type",type);
defaultDataSource = buildDataSource(dsMap);
}
@Override
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
DynamicDataSourceContextHolder.dataSourceIds.add("dataSource");
Map<Object, Object> targetDataSources = new HashMap<Object, Object>();
//添加默认数据源
targetDataSources.put("dataSource", this.defaultDataSource);
this.beanDefinitionRegistry=beanDefinitionRegistry;
beanDefinitionRegistry(defaultDataSource,targetDataSources);
logger.info("Dynamic DataSource Registry");
}
public static void addSlaveDataSource(String dataSourceType){
BeanDefinition beanDefinition=beanDefinitionRegistry.getBeanDefinition("dataSource");
PropertyValue propertyValue=beanDefinition.getPropertyValues().getPropertyValue("targetDataSources");
Map<String,DataSource> oldTargetDataSource=(Map<String,DataSource>) propertyValue.getValue();
String newUrl=firstStr+dataSourceType+secondStr;
Map<String, Object> dsMap = new HashMap<>();
dsMap.put("driver",driverName);
dsMap.put("url",newUrl);
dsMap.put("username",userName);
dsMap.put("password",password);
dsMap.put("type",type);
DataSource ds = buildDataSource(dsMap);
oldTargetDataSource.put(dataSourceType,ds);
DynamicDataSource dynamicDataSource =ApplicationContextUtil.getBean("dataSource");
dynamicDataSource.updateTargetDataSource(oldTargetDataSource);
DynamicDataSourceContextHolder.dataSourceIds.add(dataSourceType);
}
public void beanDefinitionRegistry(DataSource defaultDataSource,Map<Object,Object> targetDataSources){
//创建DynamicDataSource
GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
beanDefinition.setBeanClass(DynamicDataSource.class);
beanDefinition.setSynthetic(true);
MutablePropertyValues mpv = beanDefinition.getPropertyValues();
mpv.addPropertyValue("defaultTargetDataSource", defaultDataSource);
if(targetDataSources.size()>0){
mpv.addPropertyValue("targetDataSources", targetDataSources);
}
//注册 - BeanDefinitionRegistry
beanDefinitionRegistry.registerBeanDefinition("dataSource", beanDefinition);
}
public static DataSource buildDataSource(Map<String, Object> dataSourceMap) {
try {
Object type = dataSourceMap.get("type");
Class<? extends DataSource> dataSourceType;
dataSourceType = (Class<? extends DataSource>) Class.forName((String) type);
String driverClassName = dataSourceMap.get("driver").toString();
String url = dataSourceMap.get("url").toString();
String username = dataSourceMap.get("username").toString();
String password = dataSourceMap.get("password").toString();
// 自定义DataSource配置
DataSourceBuilder factory = DataSourceBuilder.create().driverClassName(driverClassName).url(url)
.username(username).password(password).type(dataSourceType);
return factory.build();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
}
DynamicDataSourceContextHolder.setDataSourceType("B");
Integer lProductUv=dataVisitCollectionMapper.getProductUv(dDate);
DynamicDataSourceContextHolder.setDataSourceType(Constant.defaultDbName);
在setDataSourceType的时候判断是否存在此数据源,如果存在就直接切换,不存在就动态创建并加入到Spring容器中,从而实现动态创建数据源的目的
关于springboot动态数据源的使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。