java基础

SpringBoot的多数据源配置

2019-04-15  本文已影响0人  大男孩IU

demo地址:https://gitee.com/fhmj520/springboot-datasource.git

1.导入相对应的pom文件

  <!--aop-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>

    <!--mybatis plus-->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.0.1</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>

    <!--druid-->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.1.10</version>
    </dependency>

2.配置数据源链接

双数据源.png

3.新建DynamicDataSourceConfig

DynamicDataSourceConfig.png

4.新建DynamicDataSource继承AbstractRoutingDataSource,重写determineCurrentLookupKey方法实现动态数据源

需要理解ThreadLocal,线程的局部变量,相当于private static,供本线程内存储内容


ThreadLocal.png

在DynamicDataSource中重写构造方法,在决定数据源前把数据源信息初始化完成并配置好


DynamicDataSource的重构方法.png

5.设定多数据源的Names

新建DataSourceNames

public interface DataSourceNames {
  String FIRST = "first";
  String SECOND = "second";
}

6.需要启动时初始化数据源信息,完成后面事务的初始化需要的依赖数据源Bean

所以在DynamicDataSourceConfig中进行配置

/**
 * 由于数据源的信息需要在初始化的时候加载启动
 * @param firstDataSource
 * @param secondDataSource
 * @return
 */
@Bean
@Primary //优先考虑,优先考虑被注解的对象的注入
public DynamicDataSource dataSource(DataSource firstDataSource,
                                    DataSource secondDataSource){
    Map<Object,Object> targetDataSources = new HashMap<>();
    targetDataSources.put(DataSourceNames.FIRST,firstDataSource);
    targetDataSources.put(DataSourceNames.SECOND,secondDataSource);
    return new DynamicDataSource(firstDataSource,targetDataSources);
}

7.编写多数据源的切面处理类和注解

AOP类

/**
 * 多数据源,切面处理类
 */
@Aspect
@Component
public class DataSourceAspect implements Ordered {
protected Logger logger = LoggerFactory.getLogger(getClass());

@Pointcut("@annotation(com.example.datasource.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.FIRST);
        logger.debug("set datasource is " + DataSourceNames.FIRST);
    }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;
}

}

DataSource注解


DataSource注解.png

8.使用

在业务实现中进行注解调用的数据库,进行多数据源的切换,不涉及多事务


使用.png
上一篇下一篇

猜你喜欢

热点阅读