• springboot 多数据源配置


    1、配置类

    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;
    import java.util.Map;
    
    /**
     * 配置多数据源
     */
    @Configuration
    public class DynamicDataSourceConfig {
    
      @Bean
      @ConfigurationProperties("spring.datasource.druid.report")
      public DataSource reportDataSource() {
        return DruidDataSourceBuilder.create().build();
      }
      
      @Bean
      @ConfigurationProperties("spring.datasource.druid.etlengine-v4")
      public DataSource etlengineV4DataSource() {
          return DruidDataSourceBuilder.create().build();
      }
      
    
      @Bean
      @Primary
      public DynamicDataSource dataSource(DataSource reportDataSource, DataSource etlengineV4DataSource) {
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put(DataSourceNames.REPORT, reportDataSource);
        targetDataSources.put(DataSourceNames.ETLENGINE_V4, etlengineV4DataSource);
        return new DynamicDataSource(reportDataSource, targetDataSources);
      }
    }

    2、动态数据源

    import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
    
    import javax.sql.DataSource;
    import java.util.Map;
    
    /**
     * 动态数据源
     */
    public class DynamicDataSource extends AbstractRoutingDataSource {
      private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
    
      public DynamicDataSource(DataSource defaultTargetDataSource,
          Map<Object, Object> targetDataSources) {
        super.setDefaultTargetDataSource(defaultTargetDataSource);
        super.setTargetDataSources(targetDataSources);
        super.afterPropertiesSet();
      }
    
      @Override
      protected Object determineCurrentLookupKey() {
        return getDataSource();
      }
    
      public static void setDataSource(String dataSource) {
        contextHolder.set(dataSource);
      }
    
      public static String getDataSource() {
        return contextHolder.get();
      }
    
      public static void clearDataSource() {
        contextHolder.remove();
      }
    
    }

    3、自定义一个注解,并通过aop自定义切面去动态注入不同的数据源

    @Target({ElementType.TYPE, ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface DataSource {
      String name() default "";
    }
    @Aspect
    @Component
    public class DataSourceAspect implements PriorityOrdered {
      protected Logger logger = LoggerFactory.getLogger(getClass());
    
      @Pointcut("@annotation(com.patrick.datasources.annotation.DataSource)")
      public void dataSourcePointCut() {
    
      }
    
      @Around("dataSourcePointCut()")
      public Object around(ProceedingJoinPoint point) throws Throwable {
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
    
        DataSource ds = method.getAnnotation(DataSource.class);
        if (ds == null) {
          DynamicDataSource.setDataSource(DataSourceNames.REPORT);
          logger.debug("set datasource is " + DataSourceNames.REPORT);
        } else {
          DynamicDataSource.setDataSource(ds.name());
          logger.debug("set datasource is " + ds.name());
        }
    
        try {
          return point.proceed();
        } finally {
          DynamicDataSource.clearDataSource();
          logger.debug("clean datasource");
        }
      }
    
      @Override
      public int getOrder() {
        return 1;
      }
    }
  • 相关阅读:
    【NumPy】 之常见运算(np.around、np.floor、np.ceil、np.where)(转)
    python3中collections模块(转)
    numpy.random模块用法小结
    生成模型和判别模型(《统计学习方法》截图)
    linux(Ubuntu)下机器学习/深度学习环境配置
    sklearn中predict_proba的用法例子(转)
    机器学习案例实战之信用卡欺诈检测——收获的技术
    分析长期治疗数据显示TNF拮抗剂似乎并未增加严重感染和肿瘤发生风险
    超声在鉴别诊断RA与PsA中的应用价值
    结合超声计数炎症关节的改良版DAS28的临床应用
  • 原文地址:https://www.cnblogs.com/patrick-king/p/12098401.html
Copyright © 2020-2023  润新知