码迷,mamicode.com
首页 > 编程语言 > 详细

springboot 多数据源配置

时间:2019-12-25 18:57:49      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:mes   sse   vax   shm   数据源配置   obj   ash   boot   ble   

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;
  }
}

springboot 多数据源配置

标签:mes   sse   vax   shm   数据源配置   obj   ash   boot   ble   

原文地址:https://www.cnblogs.com/patrick-king/p/12098401.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!