ホームページ  >  記事  >  Java  >  SpringBoot が AbstractRoutingDataSource に基づいて複数のデータ ソースの動的な切り替えを実装する方法

SpringBoot が AbstractRoutingDataSource に基づいて複数のデータ ソースの動的な切り替えを実装する方法

WBOY
WBOY転載
2023-05-31 10:43:521053ブラウズ

1. シナリオ

運用ビジネスでは、一部のタスクで長時間のクエリ操作が実行されます。リアルタイム要件が高くない場合、アプリケーションの負荷を軽減するために、これらのクエリ SQL をデータベース クエリから分離したいと考えています。メインデータベース上で。

1 つの解決策は、構成ファイルで複数のデータ ソースを構成し、構成クラスを通じてデータ ソースとマッパー関連のスキャン構成を取得することです。データ ソースが異なれば、マッパーのスキャン位置も異なります。データ ソースは任意のマッパー インターフェイスに挿入でき、この方法は比較的簡単です。この機能は、マッパーのスキャン位置によってデータ ソースを区別することです。

実現可能な解決策は、デフォルトのデータ ソースを事前に設定し、他の複数のデータ ソースを同時に定義し、AOP を使用してデータ ソースを切り替えるアノテーションを実装することです。 AbstractRoutingDataSource クラスの継承が、このソリューションを実装するための鍵です。これがこの記事の焦点です。

2. 原則

AbstractRoutingDataSource の複数のデータ ソースの動的切り替えの中心となるロジックは、プログラムの実行中に、データ ソースが AbstractRoutingDataSource を通じてプログラムに動的に組み込まれ、データは柔軟に処理され、ソースの切り替えが可能になります。
AbstractRoutingDataSource に基づく複数のデータ ソースの動的切り替えにより、読み取りと書き込みの分離を実現できます。ロジックは次のとおりです:

/**
     * 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();

抽象メソッド決定CurrentLookupKey

3を実装して、切り替える必要があるデータ ソースを指定します。コード例

この例は主に # に依存します。

##com.alibaba .druid;tk.mybatis

データ ソースの関連付けに使用するクラスを定義します。 TheadLocal を使用して、各スレッドがどのデータ ソースを選択するかのフラグ (キー) を保存します。

@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("设置当前数据源为{}",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);
    }
}

Inheritance

AbstractRoutingDataSource

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

メイン データベース マスターとスレーブ データベース スレーブを構成します (省略) 。データ ソース構成は簡素化できます

@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("数据源加载失败:", e);
 
        } finally {
            result.close();
        }
 
 
        return result;
    }
}

マスター/スレーブ データベースの Bean 名に注意してください

DynamicDataSource の構成

  • targetDataSources には k-v が格納されます。データ ソースのペア

  • defaultTargetDataSource にはデフォルトのデータ ソースが格納されます

  • #トランザクション マネージャーと 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;
    }
}

データ ソースを指定するためのアノテーション

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

Aspect ビジネス ロジック。トランザクションを開始する前に確実に実行されるように、順序の指定に注意してください。

@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("数据源[{}]不存在,使用默认数据源 > {}" + 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;
 
//使用默认数据源
    public ShopBillBO queryTestData(Integer id){
 
        return shopBillDOMapper.getByShopBillId(id);
    }
 
//切换到指定的数据源
    @TargetDataSource("slaveDataSource")
    public ShopBill queryTestData2(Integer id){
        return shopBillDOMapper.getByShopBillId(id);
    }

異なる結果が返れば成功です。

以上がSpringBoot が AbstractRoutingDataSource に基づいて複数のデータ ソースの動的な切り替えを実装する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はyisu.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。