您好,登錄后才能下訂單哦!
SpringBoot中怎么在運(yùn)行時(shí)動(dòng)態(tài)添加數(shù)據(jù)源,相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
一、多數(shù)據(jù)源應(yīng)用場(chǎng)景:
1.配置文件配置多數(shù)據(jù)源,如默認(rèn)數(shù)據(jù)源:master,數(shù)據(jù)源1:salve1...,運(yùn)行時(shí)動(dòng)態(tài)切換已配置的數(shù)據(jù)源(master、salve1互相切換),無(wú)法在運(yùn)行時(shí)動(dòng)態(tài)添加配置文件中未配置的數(shù)據(jù)源。
2.配置一個(gè)默認(rèn)數(shù)據(jù)源,運(yùn)行時(shí)動(dòng)態(tài)添加新數(shù)據(jù)源使用(本博客適用于此場(chǎng)景)
二、解決方案:
Spring提供了AbstractRoutingDataSource用于動(dòng)態(tài)路由數(shù)據(jù)源,第一種場(chǎng)景繼承AbstractRoutingDataSource類并覆寫其protected abstract Object determineCurrentLookupKey()即可;
而第二種場(chǎng)景我們直接覆寫protected DataSource determineTargetDataSource方法即可。原理可看下AbstractRoutingDataSource對(duì)應(yīng)源碼,比較簡(jiǎn)單,不做贅述。
直接上干貨:
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) SpringUtils.getBean("defaultDataSource")); public static void setDataSource(DataSource dataSource) { DynamicDataSource.dataSource.set(dataSource); } public static DataSource getDataSource() { return DynamicDataSource.dataSource.get(); } @Override protected Object determineCurrentLookupKey() { return null; } @Override protected DataSource determineTargetDataSource() { return getDataSource(); } public static void clear() { DynamicDataSource.dataSource.remove(); }}
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import javax.sql.DataSource;import java.util.HashMap;@Configurationpublic class DataSourceConfig { @Bean @ConfigurationProperties("spring.datasource.druid") public DataSource defaultDataSource() { return DruidDataSourceBuilder.create().build(); } @Bean @Primary public DynamicDataSource dataSource() { DynamicDataSource dynamicDataSource = new DynamicDataSource(); dynamicDataSource.setTargetDataSources(new HashMap<>()); return dynamicDataSource; }}
使用時(shí)直接調(diào)用DynamicDataSource.setDataSource(DataSource dataSource)方法即可,使用完后調(diào)用DynamicDataSource.clear()防止內(nèi)存泄漏并重置默認(rèn)數(shù)據(jù)源。
附上詳細(xì)使用方法:
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); 此時(shí)數(shù)據(jù)源已切換到druidDataSource ,調(diào)用自己的業(yè)務(wù)方法即可。 使用完后調(diào)用DynamicDataSource.clear();重置為默認(rèn)數(shù)據(jù)源。
附上工具類SpringUtils :
import lombok.Getter;import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.stereotype.Component;@Componentpublic 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 SpringUtils.applicationContext.getBean(clazz); } public static Object getBean(String name) { return SpringUtils.applicationContext.getBean(name); } public static String getProperty(String key) { return SpringUtils.applicationContext.getEnvironment().getProperty(key); }}
看完上述內(nèi)容,你們掌握SpringBoot中怎么在運(yùn)行時(shí)動(dòng)態(tài)添加數(shù)據(jù)源的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。