最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot基于AbstractRoutingDataSource實(shí)現(xiàn)多數(shù)據(jù)源動(dòng)態(tài)切換

 更新時(shí)間:2022年05月26日 10:38:26   作者:未來(lái)設(shè)計(jì)師  
本文主要介紹了SpringBoot基于AbstractRoutingDataSource實(shí)現(xiàn)多數(shù)據(jù)源動(dòng)態(tài)切換,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

一、場(chǎng)景

在生產(chǎn)業(yè)務(wù)中,有一些任務(wù)執(zhí)行了耗時(shí)較長(zhǎng)的查詢操作,在實(shí)時(shí)性要求不高的時(shí)候,我們希望將這些查詢sql分離出來(lái),去從庫(kù)查詢,以減少應(yīng)用對(duì)主數(shù)據(jù)庫(kù)的壓力。

一種方案是在配置文件中配置多個(gè)數(shù)據(jù)源,然后通過(guò)配置類來(lái)獲取數(shù)據(jù)源以及mapper相關(guān)的掃描配置,不同的數(shù)據(jù)源配置不佟的mapper掃描位置,然后需要哪一個(gè)數(shù)據(jù)源就注入哪一個(gè)mapper接口即可,這種方法比較簡(jiǎn)單。特征是通過(guò)mapper掃描位置區(qū)分?jǐn)?shù)據(jù)源。

第二種方案是配置一個(gè)默認(rèn)使用的數(shù)據(jù)源,然后定義多個(gè)其他的數(shù)據(jù)源,使用aop形成注解式選擇數(shù)據(jù)源。此種方案實(shí)現(xiàn)的核心是對(duì)AbstractRoutingDataSource 類的繼承。這是本文的重點(diǎn)。

二、原理

AbstractRoutingDataSource的多數(shù)據(jù)源動(dòng)態(tài)切換的核心邏輯是:在程序運(yùn)行時(shí),把數(shù)據(jù)源數(shù)據(jù)源通過(guò) AbstractRoutingDataSource 動(dòng)態(tài)織入到程序中,靈活的進(jìn)行數(shù)據(jù)源切換。
基于AbstractRoutingDataSource的多數(shù)據(jù)源動(dòng)態(tài)切換,可以實(shí)現(xiàn)讀寫分離。邏輯如下:

/**
     * Retrieve the current target DataSource. Determines the
     * {@link #determineCurrentLookupKey() current lookup key}, performs
     * a lookup in the {@link #setTargetDataSources targetDataSources} map,
     * falls back to the specified
     * {@link #setDefaultTargetDataSource default target DataSource} if necessary.
     * @see #determineCurrentLookupKey()
     */
    protected DataSource determineTargetDataSource() {
        Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");
        Object lookupKey = determineCurrentLookupKey();
        DataSource dataSource = this.resolvedDataSources.get(lookupKey);
        if (dataSource == null && (this.lenientFallback || lookupKey == null)) {
            dataSource = this.resolvedDefaultDataSource;
        }
        if (dataSource == null) {
            throw new IllegalStateException("Cannot determine target DataSource for lookup key [" + lookupKey + "]");
        }
        return dataSource;
    }
/**
     * Determine the current lookup key. This will typically be
     * implemented to check a thread-bound transaction context.
     * <p>Allows for arbitrary keys. The returned key needs
     * to match the stored lookup key type, as resolved by the
     * {@link #resolveSpecifiedLookupKey} method.
     */
    @Nullable
    protected abstract Object determineCurrentLookupKey();

通過(guò)實(shí)現(xiàn)抽象方法determineCurrentLookupKey指定需要切換的數(shù)據(jù)源

三、代碼示例

示例中主要依賴 

com.alibaba.druid;tk.mybatis

定義一個(gè)類用于關(guān)聯(lián)數(shù)據(jù)源。通過(guò) TheadLocal 來(lái)保存每個(gè)線程選擇哪個(gè)數(shù)據(jù)源的標(biāo)志(key)

@Slf4j
public class DynamicDataSourceContextHolder {
 
 
    private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
    public static List<String> dataSourceIds = new ArrayList<String>();
 
    public static void setDataSourceType(String dataSourceType) {
        log.info("設(shè)置當(dāng)前數(shù)據(jù)源為{}",dataSourceType);
        contextHolder.set(dataSourceType);
    }
 
    public static String getDataSourceType() {
        return contextHolder.get() ;
    }
 
    public static void clearDataSourceType() {
        contextHolder.remove();
    }
 
    public static boolean containsDataSource(String dataSourceId){
        log.info("list = {},dataId={}", JSON.toJSON(dataSourceIds),dataSourceId);
        return dataSourceIds.contains(dataSourceId);
    }
}

繼承

AbstractRoutingDataSource

public class DynamicDataSource  extends AbstractRoutingDataSource {
 
    @Override
    protected Object determineCurrentLookupKey() {
 
        return  DynamicDataSourceContextHolder.getDataSourceType();
    }
}

配置主數(shù)據(jù)庫(kù)master 與從數(shù)據(jù)庫(kù)slave(略)。數(shù)據(jù)源配置可以從簡(jiǎn)

@Configuration
@tk.mybatis.spring.annotation.MapperScan(value = {"com.server.dal.dao"})
@ConditionalOnProperty(name = "java.druid.datasource.master.url")
public class JavaDruidDataSourceConfiguration {
 
    private static final Logger logger = LoggerFactory.getLogger(JavaDruidDataSourceConfiguration.class);
 
    @Resource
    private JavaDruidDataSourceProperties druidDataSourceProperties;
    @Primary
    @Bean(name = "masterDataSource", initMethod = "init", destroyMethod = "close")
    @ConditionalOnMissingBean(name = "masterDataSource")
    public DruidDataSource javaReadDruidDataSource() {
 
        DruidDataSource result = new DruidDataSource();
 
        try {
//            result.setName(druidDataSourceProperties.getName());
            result.setUrl(druidDataSourceProperties.getUrl());
            result.setUsername(druidDataSourceProperties.getUsername());
            result.setPassword(druidDataSourceProperties.getPassword());
            result.setConnectionProperties(
                    "config.decrypt=false;config.decrypt.key=" + druidDataSourceProperties.getPwdPublicKey());
            result.setFilters("config");
            result.setMaxActive(druidDataSourceProperties.getMaxActive());
            result.setInitialSize(druidDataSourceProperties.getInitialSize());
            result.setMaxWait(druidDataSourceProperties.getMaxWait());
            result.setMinIdle(druidDataSourceProperties.getMinIdle());
            result.setTimeBetweenEvictionRunsMillis(druidDataSourceProperties.getTimeBetweenEvictionRunsMillis());
            result.setMinEvictableIdleTimeMillis(druidDataSourceProperties.getMinEvictableIdleTimeMillis());
            result.setValidationQuery(druidDataSourceProperties.getValidationQuery());
            result.setTestWhileIdle(druidDataSourceProperties.isTestWhileIdle());
            result.setTestOnBorrow(druidDataSourceProperties.isTestOnBorrow());
            result.setTestOnReturn(druidDataSourceProperties.isTestOnReturn());
            result.setPoolPreparedStatements(druidDataSourceProperties.isPoolPreparedStatements());
            result.setMaxOpenPreparedStatements(druidDataSourceProperties.getMaxOpenPreparedStatements());
 
            if (druidDataSourceProperties.isEnableMonitor()) {
                StatFilter filter = new StatFilter();
                filter.setLogSlowSql(druidDataSourceProperties.isLogSlowSql());
                filter.setMergeSql(druidDataSourceProperties.isMergeSql());
                filter.setSlowSqlMillis(druidDataSourceProperties.getSlowSqlMillis());
                List<Filter> list = new ArrayList<>();
                list.add(filter);
                result.setProxyFilters(list);
            }
 
        } catch (Exception e) {
 
            logger.error("數(shù)據(jù)源加載失敗:", e);
 
        } finally {
            result.close();
        }
 
 
        return result;
    }
}

注意主從數(shù)據(jù)庫(kù)的bean name

配置DynamicDataSource 

  • targetDataSources 存放數(shù)據(jù)源的k-v對(duì)
  • defaultTargetDataSource 存放默認(rèn)數(shù)據(jù)源

配置事務(wù)管理器和SqlSessionFactoryBean

@Configuration
public class DynamicDataSourceConfig {
? ? private static final String MAPPER_LOCATION = "classpath*:sqlmap/dao/*Mapper.xml";
?
? ? @Bean(name = "dynamicDataSource")
? ? public DynamicDataSource dynamicDataSource(@Qualifier("masterDataSource") DruidDataSource masterDataSource,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?@Qualifier("slaveDataSource") DruidDataSource slaveDataSource) {
? ? ? ? Map<Object, Object> targetDataSource = new HashMap<>();
? ? ? ? DynamicDataSourceContextHolder.dataSourceIds.add("masterDataSource");
? ? ? ? targetDataSource.put("masterDataSource", masterDataSource);
? ? ? ? DynamicDataSourceContextHolder.dataSourceIds.add("slaveDataSource");
? ? ? ? targetDataSource.put("slaveDataSource", slaveDataSource);
? ? ? ? DynamicDataSource dataSource = new DynamicDataSource();
? ? ? ? dataSource.setTargetDataSources(targetDataSource);
? ? ? ? dataSource.setDefaultTargetDataSource(masterDataSource);
? ? ? ? return dataSource;
? ? }
?
? ? @Primary
? ? @Bean(name = "javaTransactionManager")
? ? @ConditionalOnMissingBean(name = "javaTransactionManager")
? ? public DataSourceTransactionManager transactionManager(@Qualifier("dynamicDataSource") DynamicDataSource druidDataSource) {
? ? ? ? return new DataSourceTransactionManager(druidDataSource);
? ? }
?
? ? @Bean(name = "sqlSessionFactoryBean")
? ? public SqlSessionFactoryBean myGetSqlSessionFactory(@Qualifier("dynamicDataSource") DynamicDataSource ?dataSource) {
? ? ? ? SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
? ? ? ? ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
? ? ? ? try {
? ? ? ? ? ? sqlSessionFactoryBean.setMapperLocations(resolver.getResources(MAPPER_LOCATION));
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? ? ? sqlSessionFactoryBean.setDataSource(dataSource);
? ? ? ? return sqlSessionFactoryBean;
? ? }
}

定義一個(gè)注解用于指定數(shù)據(jù)源

@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface TargetDataSource {
    String value();
}

切面的業(yè)務(wù)邏輯。注意指定order,以確保在開啟事務(wù)之前執(zhí)行 。

@Aspect
@Slf4j
@Order(-1)
@Component
public class DataSourceAop {
 
    @Before("@annotation(targetDataSource)")
    public void changeDataSource(JoinPoint point, TargetDataSource targetDataSource) {
        String dsId = targetDataSource.value();
        if (!DynamicDataSourceContextHolder.containsDataSource(dsId)) {
            log.error("數(shù)據(jù)源[{}]不存在,使用默認(rèn)數(shù)據(jù)源 > {}" + targetDataSource.value() + point.getSignature());
        } else {
            log.info("UseDataSource : {} > {}" + targetDataSource.value() + point.getSignature());
            DynamicDataSourceContextHolder.setDataSourceType(targetDataSource.value());
 
        }
    }
 
    @After("@annotation(targetDataSource)")
    public void restoreDataSource(JoinPoint point, TargetDataSource targetDataSource) {
        log.info("RevertDataSource : {} > {}"+targetDataSource.value()+point.getSignature());
        DynamicDataSourceContextHolder.clearDataSourceType();
    }
}

以上略去了pom.xml和application.yml

使用示例

    @Resource
    private ShopBillDOMapper shopBillDOMapper;
 
//使用默認(rèn)數(shù)據(jù)源
    public ShopBillBO queryTestData(Integer id){
 
        return shopBillDOMapper.getByShopBillId(id);
    }
 
//切換到指定的數(shù)據(jù)源
    @TargetDataSource("slaveDataSource")
    public ShopBill queryTestData2(Integer id){
        return shopBillDOMapper.getByShopBillId(id);
    }

如果返回不同的結(jié)果就成功了!

到此這篇關(guān)于SpringBoot基于AbstractRoutingDataSource實(shí)現(xiàn)多數(shù)據(jù)源動(dòng)態(tài)切換的文章就介紹到這了,更多相關(guān)SpringBoot 多數(shù)據(jù)源動(dòng)態(tài)切換內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Idea設(shè)置spring boot應(yīng)用配置參數(shù)的兩種方式

    Idea設(shè)置spring boot應(yīng)用配置參數(shù)的兩種方式

    本文通過(guò)兩個(gè)方式介紹Idea設(shè)置spring boot應(yīng)用配置參數(shù),一種是配置VM options的參數(shù)時(shí)要以:-DparamName的格式設(shè)置參數(shù),第二種可以參考下本文詳細(xì)設(shè)置,感興趣的朋友跟隨小編一起看看吧
    2023-11-11
  • Kotlin 語(yǔ)言中調(diào)用 JavaScript 方法實(shí)例詳解

    Kotlin 語(yǔ)言中調(diào)用 JavaScript 方法實(shí)例詳解

    這篇文章主要介紹了Kotlin 語(yǔ)言中調(diào)用 JavaScript 方法實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • SpringBoot實(shí)現(xiàn)接口校驗(yàn)簽名調(diào)用的項(xiàng)目實(shí)踐

    SpringBoot實(shí)現(xiàn)接口校驗(yàn)簽名調(diào)用的項(xiàng)目實(shí)踐

    在以SpringBoot開發(fā)后臺(tái)API接口時(shí),會(huì)存在哪些接口不安全的因素呢?通常如何去解決的呢?本文主要介紹了SpringBoot實(shí)現(xiàn)接口校驗(yàn)簽名調(diào)用的項(xiàng)目實(shí)踐,感興趣的可以了解一下
    2023-09-09
  • Java基于堆結(jié)構(gòu)實(shí)現(xiàn)優(yōu)先隊(duì)列功能示例

    Java基于堆結(jié)構(gòu)實(shí)現(xiàn)優(yōu)先隊(duì)列功能示例

    這篇文章主要介紹了Java基于堆結(jié)構(gòu)實(shí)現(xiàn)優(yōu)先隊(duì)列功能,結(jié)合實(shí)例形式分析了java優(yōu)先隊(duì)列的簡(jiǎn)單定義與使用方法,需要的朋友可以參考下
    2017-11-11
  • 2018年java技術(shù)面試題整理

    2018年java技術(shù)面試題整理

    小編為大家整理了2018年最新的關(guān)于java技術(shù)相關(guān)的面試題,以及給出了最簡(jiǎn)簡(jiǎn)答方式,學(xué)習(xí)下吧。
    2018-02-02
  • Java中Integer的parseInt和valueOf的區(qū)別詳解

    Java中Integer的parseInt和valueOf的區(qū)別詳解

    這篇文章主要介紹了Java中Integer的parseInt和valueOf的區(qū)別詳解,nteger.parseInt(s)是把字符串解析成int基本類型,Integer.valueOf(s)是把字符串解析成Integer對(duì)象類型,其實(shí)int就是Integer解包裝,Integer就是int的包裝,需要的朋友可以參考下
    2023-11-11
  • IntelliJ IDEA中出現(xiàn)

    IntelliJ IDEA中出現(xiàn)"PSI and index do not match"錯(cuò)誤的解決辦法

    今天小編就為大家分享一篇關(guān)于IntelliJ IDEA中出現(xiàn)"PSI and index do not match"錯(cuò)誤的解決辦法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-10-10
  • Spring @Order注解使用詳解

    Spring @Order注解使用詳解

    注解@Order或者接口Ordered的作用是定義Spring IOC容器中Bean的執(zhí)行順序的優(yōu)先級(jí),而不是定義Bean的加載順序,Bean的加載順序不受@Order或Ordered接口的影響
    2022-08-08
  • Java線程中的ThreadLocal類解讀

    Java線程中的ThreadLocal類解讀

    這篇文章主要介紹了Java線程中的ThreadLocal類解讀,ThreadLocal是一個(gè)泛型類,作用是實(shí)現(xiàn)線程隔離,ThreadLocal類型的變量,在每個(gè)線程中都會(huì)對(duì)應(yīng)一個(gè)具體對(duì)象,對(duì)象類型需要在聲明ThreadLocal變量時(shí)指定,需要的朋友可以參考下
    2023-11-11
  • 從繁瑣到簡(jiǎn)潔的Jenkins?Pipeline腳本優(yōu)化實(shí)踐

    從繁瑣到簡(jiǎn)潔的Jenkins?Pipeline腳本優(yōu)化實(shí)踐

    這篇文章主要為大家介紹了從繁瑣到簡(jiǎn)潔的Jenkins?Pipeline腳本優(yōu)化實(shí)踐示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12

最新評(píng)論

桦川县| 滕州市| 揭西县| 叶城县| 天峨县| 鄯善县| 息烽县| 安龙县| 淄博市| 兴安盟| 济宁市| 慈溪市| 韶山市| 岱山县| 日喀则市| 宁波市| 泽普县| 舒城县| 衡阳县| 田林县| 五家渠市| 仁布县| 永丰县| 应城市| 泽普县| 高要市| 鹿泉市| 临安市| 越西县| 英吉沙县| 上思县| 聊城市| 波密县| 潮州市| 贵定县| 启东市| 西昌市| 苏尼特左旗| 邛崃市| 盖州市| 旌德县|