一、前言
随着项目业务的日益复杂,单个数据库有时无法满足项目的需求,比如:数据库的读写分离,集成多个数据库。而多数据源配置的出现就能够很好的解决我们这一难题。
二、Spring实现多数据源配置
1、在pom.xml文件中添加所需依赖
<properties>
<log4j.version>2.11.1</log4j.version>
<spring.version>5.1.5.RELEASE</spring.version>
</properties>
<dependencies>
<!-- slf4j的相关依赖包 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<!-- 桥接:告诉Slf4j使用Log4j2 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${slf4j.log4j.version}</version>
</dependency>
<!-- log4j2的相关依赖包 -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- Apache工具组件 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
<!-- MySql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- Druid连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<scope>provided</scope>
</dependency>
<!-- javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- junit单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<!-- 打包资源文件 -->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
2、在src/main/resources资源目录下,创建一个properties目录,并创建和编写jdbc.properties配置文件
#============================================================================
# 数据源01,数据库为data_source1
#============================================================================
jdbc.one.driver=com.mysql.jdbc.Driver
jdbc.one.url=jdbc:mysql://localhost:3306/data_source1?rewriteBatchedStatements=true&allowMultiQueries=true&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&&useSSL=false
jdbc.one.username=root
jdbc.one.password=123456
#============================================================================
# 数据源02,数据库为data_source2
#============================================================================
jdbc.two.driver=com.mysql.jdbc.Driver
jdbc.two.url=jdbc:mysql://localhost:3306/data_source2?rewriteBatchedStatements=true&allowMultiQueries=true&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&&useSSL=false
jdbc.two.username=root
jdbc.two.password=123456
#============================================================================
# 数据源03,数据库为data_source3
#============================================================================
jdbc.three.driver=com.mysql.jdbc.Driver
jdbc.three.url=jdbc:mysql://localhost:3306/data_source3?rewriteBatchedStatements=true&allowMultiQueries=true&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&&useSSL=false
jdbc.three.username=root
jdbc.three.password=123456
#============================================================================
# 通用配置
#============================================================================
jdbc.common.initialSize=5
jdbc.common.minIdle=5
jdbc.common.maxActive=100
jdbc.common.maxWait=100000
jdbc.common.defaultAutoCommit=false
jdbc.common.removeAbandoned=true
jdbc.common.removeAbandonedTimeout=600
jdbc.common.testWhileIdle=true
jdbc.common.timeBetweenEvictionRunsMillis=60000
jdbc.common.minEvictableIdleTimeMillis=300000
3、在src/main/resources资源目录下,创建一个spring目录,并创建和编写applicationContext-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<!-- 配置包扫描器,扫描注解的类,注意:扫描注解时,也需要扫描到AOP注解 -->
<context:component-scan base-package="com.multiple"/>
<!-- 声明自动为spring容器中那些配置@aspectJ切面的bean创建代理 -->
<aop:aspectj-autoproxy />
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:properties/jdbc.properties" />
<!-- 数据源dataSourceOne -->
<bean id="dataSourceOne" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="url" value="${jdbc.one.url}" />
<property name="username" value="${jdbc.one.username}" />
<property name="password" value="${jdbc.one.password}" />
<property name="driverClassName" value="${jdbc.one.driver}"/>
<property name="initialSize" value="${jdbc.common.initialSize}"/>
<property name="minIdle" value="${jdbc.common.minIdle}"/>
<property name="maxActive" value="${jdbc.common.maxActive}"/>
<property name="maxWait" value="${jdbc.common.maxWait}"/>
<property name="defaultAutoCommit" value="${jdbc.common.defaultAutoCommit}"/>
<property name="removeAbandoned" value="${jdbc.common.removeAbandoned}"/>
<property name="removeAbandonedTimeout" value="${jdbc.common.removeAbandonedTimeout}"/>
<property name="testWhileIdle" value="${jdbc.common.testWhileIdle}"/>
<property name="timeBetweenEvictionRunsMillis" value="${jdbc.common.timeBetweenEvictionRunsMillis}"/>
<property name="minEvictableIdleTimeMillis" value="${jdbc.common.minEvictableIdleTimeMillis}"/>
</bean>
<!-- 数据源dataSourceTwo -->
<bean id="dataSourceTwo" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="url" value="${jdbc.two.url}" />
<property name="username" value="${jdbc.two.username}" />
<property name="password" value="${jdbc.two.password}" />
<property name="driverClassName" value="${jdbc.two.driver}"/>
<property name="initialSize" value="${jdbc.common.initialSize}"/>
<property name="minIdle" value="${jdbc.common.minIdle}"/>
<property name="maxActive" value="${jdbc.common.maxActive}"/>
<property name="maxWait" value="${jdbc.common.maxWait}"/>
<property name="defaultAutoCommit" value="${jdbc.common.defaultAutoCommit}"/>
<property name="removeAbandoned" value="${jdbc.common.removeAbandoned}"/>
<property name="removeAbandonedTimeout" value="${jdbc.common.removeAbandonedTimeout}"/>
<property name="testWhileIdle" value="${jdbc.common.testWhileIdle}"/>
<property name="timeBetweenEvictionRunsMillis" value="${jdbc.common.timeBetweenEvictionRunsMillis}"/>
<property name="minEvictableIdleTimeMillis" value="${jdbc.common.minEvictableIdleTimeMillis}"/>
</bean>
<!-- 数据源dataSourceThree -->
<bean id="dataSourceThree" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="url" value="${jdbc.three.url}" />
<property name="username" value="${jdbc.three.username}" />
<property name="password" value="${jdbc.three.password}" />
<property name="driverClassName" value="${jdbc.three.driver}"/>
<property name="initialSize" value="${jdbc.common.initialSize}"/>
<property name="minIdle" value="${jdbc.common.minIdle}"/>
<property name="maxActive" value="${jdbc.common.maxActive}"/>
<property name="maxWait" value="${jdbc.common.maxWait}"/>
<property name="defaultAutoCommit" value="${jdbc.common.defaultAutoCommit}"/>
<property name="removeAbandoned" value="${jdbc.common.removeAbandoned}"/>
<property name="removeAbandonedTimeout" value="${jdbc.common.removeAbandonedTimeout}"/>
<property name="testWhileIdle" value="${jdbc.common.testWhileIdle}"/>
<property name="timeBetweenEvictionRunsMillis" value="${jdbc.common.timeBetweenEvictionRunsMillis}"/>
<property name="minEvictableIdleTimeMillis" value="${jdbc.common.minEvictableIdleTimeMillis}"/>
</bean>
<!-- 注入自己实现的 多数据源管理类 multipleDataSource -->
<bean id="multipleDataSource" class="com.multiple.pool.MultipleDataSource">
<!-- 设置默认的数据源 dataSourceOne -->
<property name="defaultTargetDataSource" ref="dataSourceOne"/>
<property name="targetDataSources">
<map>
<!-- 这个key是对应数据源的别称,通过这个key可以找到对应的数据源,value-ref就是上面数据源的id -->
<entry key="dataSourceOneKey" value-ref="dataSourceOne"/>
<entry key="dataSourceTwoKey" value-ref="dataSourceTwo"/>
<entry key="dataSourceThreeKey" value-ref="dataSourceThree"/>
</map>
</property>
</bean>
<!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="multipleDataSource" />
</bean>
<!-- MapperScannerConfigurer 自动扫描将Mapper接口生成代理注入到spring
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
basePackage指定要扫描的包,在此包之下的映射器都会被搜索到。可指定多个包,包与包之间用逗号或分号分隔
<property name="basePackage" value="com.multiple.mapper" />
</bean> -->
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源 -->
<property name="dataSource" ref="multipleDataSource" />
</bean>
<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 传播行为 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.multiple.service.*.*(..))" />
</aop:config>
</beans>
4、在src/main/resources目录下,创建并编写一个名为log4j2.xml的日志配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!--日志级别以及优先级排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
<!--status="WARN": 用于设置log4j2自身内部日志的信息输出级别,默认是OFF-->
<!--monitorInterval="1800": 间隔秒数,自动检测配置文件的变更和重新配置本身-->
<configuration status="WARN" monitorInterval="1800">
<!--Appenders: 定义输出内容,输出格式,输出方式,日志保存策略等,常用其下三种标签[console,File,RollingRandomAccessFile]-->
<Appenders>
<!--Console: 控制台输出的配置-->
<console name="Console" target="SYSTEM_OUT" follow="true">
<!--PatternLayout: 用于定义输出日志的格式-->
<PatternLayout charset="UTF-8" pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36}.%M() line:%L - %msg%xEx%n"/>
</console>
</Appenders>
<!--然后定义Logger,只有定义了Logger并引入的Appender,Appender才会生效-->
<Loggers>
<!-- Root节点用来指定项目的根日志,如果没有单独指定Logger,那么就会默认使用该Root日志输出 -->
<Root level="info">
<appender-ref ref="Console"/>
</Root>
</Loggers>
</configuration>
5、创建自己的多数据源管理类 MultipleDataSource
package com.multiple.pool;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class MultipleDataSource extends AbstractRoutingDataSource {
private static final ThreadLocal<String> dataSourceKey = new InheritableThreadLocal<String>();
public static void setDataSourceKey(String dataSource) {
dataSourceKey.set(dataSource);
}
public static void clearDataSourceKey() {
dataSourceKey.remove();
}
@Override
protected Object determineCurrentLookupKey() {
return dataSourceKey.get();
}
}
这里重点说一下AbstractRoutingDataSource
类,这个类是实现多数据源的关键,它的作用就是动态切换数据源,实质:把项目中配置的多个数据源存储到该类的targetDataSources
属性中 (targetDataSources
属性 是一个Map集合,其中value为每个数据源,key则存储每个数据源的键),然后根据我们重写该类的determineCurrentLookupKey()
方法来获取当前数据源所对应的key
,并通过获得的key
来获取数据源dataSource
,如果数据源dataSource
为null
,则将其设置为默认的数据源,如果当前数据源为null
且默认数据源也为null
则抛出异常,部分关键源码解析如下所示:
public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBean {
// 多数据源 Map集合
@Nullable
private Map<Object, Object> targetDataSources;
// 默认数据源
@Nullable
private Object defaultTargetDataSource;
// 默认为true
private boolean lenientFallback = true;
// 设置多数据源
public void setTargetDataSources(Map<Object, Object> targetDataSources) {
this.targetDataSources = targetDataSources;
}
// 设置默认数据源
public void setDefaultTargetDataSource(Object defaultTargetDataSource) {
this.defaultTargetDataSource = defaultTargetDataSource;
}
// 动态获取当前数据源
protected DataSource determineTargetDataSource() {
Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");
// 调用我们自己实现的determineCurrentLookupKey()方法,获取当前数据源在map中所对应的lookupKey
Object lookupKey = determineCurrentLookupKey();
// 通过获取到的lookupKey,来获取对应的数据源
DataSource dataSource = this.resolvedDataSources.get(lookupKey);
/**
* 如果当前数据源dataSource为null,并且对应的lookupKey也为null,则设置为默认数据源。
* 其实只要数据源dataSource为null,则会被设置为默认数据源。因为lenientFallback默认为true
*/
if (dataSource == null && (this.lenientFallback || lookupKey == null)) {
dataSource = this.resolvedDefaultDataSource;
}
// 如果默认数据源也为null,则抛出异常
if (dataSource == null) {
throw new IllegalStateException("Cannot determine target DataSource for lookup key [" + lookupKey + "]");
}
// 返回数据源
return dataSource;
}
// 获取当前数据源在map中所对应的key,需要我们自己实现
@Nullable
protected abstract Object determineCurrentLookupKey();
}
6、定义多数据切换规则,并编写AOP类,这里将讲述两种方式实现动态切换数据源
方式一:通过注解方式 实现动态切换数据源
1、自定义注解DataSourceChange,用于动态切换数据源
package com.multiple.annotation;
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
@Documented
public @interface DataSourceChange {
// 设置默认数据库为dataSourceOneKey
String dataSource() default "dataSourceOneKey";
}
对上面的代码的注解进行如下解释:
@Retention
:指定当前注解保留到运行时;@Target
:指定当前自定义注解可以使用在哪些地方,这里仅仅让它使用在方法上和类、接口(包括注解类型) 或enum枚举的声明上;@Documented
:表示该注解是否可以生成到 API文档中;
2、编写AOP类,根据注解中指定的数据源Key,切换不同的数据库
package com.multiple.aspect;
import com.multiple.annotation.DataSourceChange;
import com.multiple.pool.MultipleDataSource;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
@Component
@Aspect
@Order(1)
public class MultipleDataSourceAspectAdvice {
private static final Logger logger = LoggerFactory.getLogger(MultipleDataSourceAspectAdvice.class);
/**
* 定义切面
*/
@Pointcut("execution(* com.multiple.service.*.*(..))")
public void pointCut() {
}
/**
* 设置数据源
**/
@Before("pointCut()")
public void dataSourceChange(JoinPoint joinPoint) throws NoSuchMethodException {
// 获取当前目标的方法签名
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature)signature;
// 通过方法签名 获取方法对象(该方法对象为代理方法对象)
Method method = methodSignature.getMethod();
DataSourceChange data = null;
// 数据源Key
String dataSource;
Method realMethod = null;
if(method != null){
// 获取真正的目标方法对象
realMethod = joinPoint.getTarget().getClass().getDeclaredMethod(signature.getName(), method.getParameterTypes());
data = realMethod.getAnnotation(DataSourceChange.class);
if (data !=null){
// 获得数据源key
dataSource = data.dataSource();
if (StringUtils.isNotBlank(dataSource)){
logger.info("使用的数据源key为:"+dataSource);
MultipleDataSource.setDataSourceKey(dataSource);
}
}
}
}
/**
* 清除数据源
**/
@After("pointCut()")
public void dataSourceClear(JoinPoint joinPoint) {
MultipleDataSource.clearDataSourceKey();
}
}
对上面的代码的注解进行如下解释:
@Component
:表明一个类会作为组件类,并告知Spring要为这个类创建bean;@Aspect
:把当前类标识为一个切面;@Order(1)
:定义Spring IOC容器中Bean的执行顺序的优先级,而不是定义Bean的加载顺序
,数值越小优先级越高,优先级设置到最高,是因为在所有service方法调用dao前都必须把数据源确定好
;
踩坑环节:
使用aop代理通过 Method类的 public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
方法获取到的Annotation注解对象为null问题,问题代码如下所示:
// 获取当前目标的方法签名
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature)signature;
// 通过方法签名 获取方法对象(该方法对象为代理方法对象)
Method method = methodSignature.getMethod();
//此处method是代理对象(由代理模式生成的),结果为null
DataSourceChange data = method.getAnnotation(DataSourceChange.class);
有人就会说了,为什么获取的注解对象会为null呢?其实这是代理对象和目标对象的问题,因为通过代理模式生成的代理方法对象上是不会有注解的,这就导致我们通过代理方法对象获取注解对象为null的问题。
那该怎么获取注解对象呢?其实也很简单,通过反射获取到目标对象,而目标对象上是有我们添加的注解,解决方案代码如下所示:
// 获取当前目标的方法签名
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature)signature;
// 通过方法签名 获取方法对象(该方法对象为代理对象)
Method method = methodSignature.getMethod();
// 通过反射获取真正的方法目标对象
Method realMethod = joinPoint.getTarget().getClass().getDeclaredMethod(signature.getName(), method.getParameterTypes());
// 结果不为null
DataSourceChange data = realMethod.getAnnotation(DataSourceChange.class);
3、创建Service接口—UserService
package com.multiple.service;
public interface UserService {
String getUserMessageOne();
String getUserMessageTwo();
String getUserMessageThree();
}
4、创建Service实现类—UserServiceImpl,为了方便,这里就不再给出dao层
package com.multiple.service.impl;
import com.multiple.annotation.DataSourceChange;
import com.multiple.service.UserService;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
/**
* 通过自定义注解@DataSourceChange,使用默认数据源dataSourceOne
**/
@DataSourceChange
public String getUserMessageOne() {
return "通过数据源dataSourceOne,获取用户名为张三的用户信息";
}
/**
* 通过自定义注解@DataSourceChange,指定使用数据源dataSourceTwo
**/
@DataSourceChange(dataSource = "dataSourceTwoKey")
public String getUserMessageTwo() {
return "通过数据源dataSourceTwo,获取用户名为李四的用户信息";
}
/**
* 通过自定义注解@DataSourceChange,指定使用数据源dataSourceThree
**/
@DataSourceChange(dataSource = "dataSourceThreeKey")
public String getUserMessageThree() {
return "通过数据源dataSourceThree,获取用户名为王五的用户信息";
}
}
5、编写执行类—MainApplication
package com.multiple;
import com.multiple.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApplication {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationController-spring.xml");
UserService userService = (UserService) context.getBean("userServiceImpl");
// 使用默认数据源dataSourceOne
String userMessageOne = userService.getUserMessageOne();
System.out.println(userMessageOne);
// 使用了数据源dataSourceTwo
String userMessageTwo = userService.getUserMessageTwo();
System.out.println(userMessageTwo);
//使用了数据源dataSourceThree
String userMessageThree = userService.getUserMessageThree();
System.out.println(userMessageThree);
}
}
执行结果如下所示:
2019-09-21 16:19:47.001 INFO com.multiple.aspect.MultipleDataSourceAspectAdvice.dataSourceChange() line:70 - 使用的数据源key为:dataSourceOneKey
通过数据源dataSourceOne,获取用户名为张三的用户信息
2019-09-21 16:19:47.016 INFO com.multiple.aspect.MultipleDataSourceAspectAdvice.dataSourceChange() line:70 - 使用的数据源key为:dataSourceTwoKey
通过数据源dataSourceTwo,获取用户名为李四的用户信息
2019-09-21 16:19:47.031 INFO com.multiple.aspect.MultipleDataSourceAspectAdvice.dataSourceChange() line:70 - 使用的数据源key为:dataSourceThreeKey
通过数据源dataSourceThree,获取用户名为王五的用户信息
方式二:通过接口方式 实现动态切换数据源
1、创建三个接口,分别代表不同的数据源
package com.multiple.source;
// 表示使用数据源 dataSourceOne
public interface DataSourceOne {
}
package com.multiple.source;
// 表示使用数据源 dataSourceTwo
public interface DataSourceTwo {
}
package com.multiple.source;
// 表示使用数据源 dataSourceThree
public interface DataSourceThree {
}
2、编写AOP类,根据其实现接口的不同,指定不同的数据源Key,切换不同的数据库
package com.multiple.aspect;
import com.multiple.pool.MultipleDataSource;
import com.multiple.source.DataSourceOne;
import com.multiple.source.DataSourceThree;
import com.multiple.source.DataSourceTwo;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Aspect
@Order(1)
public class MultipleDataSourceAspectAdvice {
private static final Logger logger = LoggerFactory.getLogger(MultipleDataSourceAspectAdvice.class);
/**
* 定义切面
*/
@Pointcut("execution(* com.multiple.service.*.*(..))")
public void pointCut() {
}
@Before("pointCut()")
public void dataSourceChange(JoinPoint joinPoint) {
if (joinPoint.getTarget() instanceof DataSourceOne) {
logger.info("使用数据源:DataSourceOne");
MultipleDataSource.setDataSourceKey("dataSourceOneKey");
} else if (joinPoint.getTarget() instanceof DataSourceTwo) {
logger.info("使用数据源:DataSourceTwo");
MultipleDataSource.setDataSourceKey("dataSourceTwoKey");
} else if (joinPoint.getTarget() instanceof DataSourceThree) {
logger.info("使用数据源:DataSourceThree");
MultipleDataSource.setDataSourceKey("dataSourceThreeKey");
} else {
logger.info("默认使用数据源:dataSourceOneKey");
MultipleDataSource.setDataSourceKey("dataSourceOneKey");
}
}
}
3、创建Service接口—UserService
package com.multiple.service;
public interface UserService {
String getUserMessage();
}
4、创建Service实现类—UserServiceImpl,并实现DatasourceOne接口
package com.multiple.service.impl;
import com.multiple.service.UserService;
import com.multiple.source.DataSourceOne;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService, DataSourceOne {
public String getUserMessage() {
return "获取用户名为张三的用户信息";
}
}
5、编写执行类—MainApplication
package com.multiple;
import com.multiple.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApplication {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationController-spring.xml");
UserService userService = (UserService) context.getBean("userServiceImpl");
// 使用了数据源dataSourceOne
String userMessage = userService.getUserMessage();
System.out.println(userMessage);
}
}
执行结果如下所示:
2019-09-21 16:46:25.192 INFO com.multiple.aspect.MultipleDataSourceAspectAdvice.dataSourceChange() line:45 - 使用数据源:DataSourceOne
获取用户名为张三的用户信息
如果需要更换数据源,只需要把UserServiceImpl实现的DatasourceOne接口,更改为其他接口即可实现数据源的切换。
三、SpringBoot实现多数据源配置
1、在pom.xml文件中添加所需依赖
<properties>
<druid.version>1.1.10</druid.version>
</properties>
<dependencies>
<!-- springboot起步依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 排除自带的Logback日志工具依赖,为了项目使用Log4j2日志打印工具 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--
1、log4j2的依赖包
2、spring-boot-starter-log4j2 中自动依赖了 slf4j-api 和 log4j-slf4j-impl
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- log4j2异步输出的依赖包 -->
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.4.2</version>
</dependency>
<!-- mysql连接驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- jdbc依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- Druid连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- mybatis依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<!-- lombok依赖 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- Apache工具组件 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!-- springboot设置开启传统的xml或properties配置,默认使用yml中的配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- aop依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
2、在src/main/resources资源目录下,编写application.properties配置文件
#============================================================================
# 数据源dataSourcePrimary,数据库为data_source1
#============================================================================
spring.datasource.primary.driverClassName=com.mysql.jdbc.Driver
spring.datasource.primary.username=root
spring.datasource.primary.password=vms123
spring.datasource.primary.url=jdbc:mysql://localhost:3316/data_source1?rewriteBatchedStatements=true&allowMultiQueries=true&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&&useSSL=false
spring.datasource.primary.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.primary.initialSize=5
spring.datasource.primary.minIdle=5
spring.datasource.primary.maxActive=100
spring.datasource.primary.maxWait=100000
spring.datasource.primary.defaultAutoCommit=false
spring.datasource.primary.removeAbandoned=true
spring.datasource.primary.removeAbandonedTimeoutMillis=600
spring.datasource.primary.testWhileIdle=true
spring.datasource.primary.timeBetweenEvictionRunsMillis=60000
spring.datasource.primary.minEvictableIdleTimeMillis=300000
#============================================================================
# 数据源dataSourceLocal,数据库为data_source2
#============================================================================
spring.datasource.local.driverClassName=com.mysql.jdbc.Driver
spring.datasource.local.username=root
spring.datasource.local.password=vms123
spring.datasource.local.url=jdbc:mysql://localhost:3316/data_source2?rewriteBatchedStatements=true&allowMultiQueries=true&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&&useSSL=false
spring.datasource.local.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.local.initialSize=5
spring.datasource.local.minIdle=5
spring.datasource.local.maxActive=100
spring.datasource.local.maxWait=100000
spring.datasource.local.defaultAutoCommit=false
spring.datasource.local.removeAbandoned=true
spring.datasource.local.removeAbandonedTimeoutMillis=600
spring.datasource.local.testWhileIdle=true
spring.datasource.local.timeBetweenEvictionRunsMillis=60000
spring.datasource.local.minEvictableIdleTimeMillis=300000
#============================================================================
# 数据源dataSourceProduce,数据库为data_source3
#============================================================================
spring.datasource.produce.driverClassName=com.mysql.jdbc.Driver
spring.datasource.produce.username=root
spring.datasource.produce.password=vms123
spring.datasource.produce.url=jdbc:mysql://localhost:3316/data_source3?rewriteBatchedStatements=true&allowMultiQueries=true&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&&useSSL=false
spring.datasource.produce.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.produce.initialSize=5
spring.datasource.produce.minIdle=5
spring.datasource.produce.maxActive=100
spring.datasource.produce.maxWait=100000
spring.datasource.produce.defaultAutoCommit=false
spring.datasource.produce.removeAbandoned=true
spring.datasource.produce.removeAbandonedTimeoutMillis=600
spring.datasource.produce.testWhileIdle=true
spring.datasource.produce.timeBetweenEvictionRunsMillis=60000
spring.datasource.produce.minEvictableIdleTimeMillis=300000
#指定mapper.xml文件的所在路径
mybatis.mapper-locations=chasspath*:com/multiple/mapper/*.xml
3、在src/main/resources目录下,创建并编写一个名为log4j2.xml的日志配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!--日志级别以及优先级排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
<!--status="WARN": 用于设置log4j2自身内部日志的信息输出级别,默认是OFF-->
<!--monitorInterval="1800": 间隔秒数,自动检测配置文件的变更和重新配置本身-->
<configuration status="WARN" monitorInterval="1800">
<!--Appenders: 定义输出内容,输出格式,输出方式,日志保存策略等,常用其下三种标签[console,File,RollingRandomAccessFile]-->
<Appenders>
<!--Console: 控制台输出的配置-->
<console name="Console" target="SYSTEM_OUT" follow="true">
<!--PatternLayout: 用于定义输出日志的格式-->
<PatternLayout charset="UTF-8" pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36}.%M() line:%L - %msg%xEx%n"/>
</console>
</Appenders>
<!--然后定义Logger,只有定义了Logger并引入的Appender,Appender才会生效-->
<Loggers>
<!-- Root节点用来指定项目的根日志,如果没有单独指定Logger,那么就会默认使用该Root日志输出 -->
<Root level="info">
<appender-ref ref="Console"/>
</Root>
</Loggers>
</configuration>
4、创建配置类MutiplyDataSource
package com.multiple.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.multiple.pool.DynamicDataSource;
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 org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
import java.util.HashMap;
@Configuration
public class MutiplyDataSource {
/**
* @ConfigurationProperties: 将配置文件的值映射到bean上,需要保证prefix属性指定前缀后面的属性字段名称,
* 要与映射到bean上的属性字段名称一一对应
**/
@Bean(name = "dataSourcePrimary")
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource(){
return new DruidDataSource();
}
@Bean(name = "dataSourceLocal")
@ConfigurationProperties(prefix = "spring.datasource.local")
public DataSource localDataSource(){
return new DruidDataSource();
}
@Bean(name = "dataSourceProduce")
@ConfigurationProperties(prefix = "spring.datasource.produce")
public DataSource produceDataSource(){
return new DruidDataSource();
}
/**
* @Primary: 该注解表示在同一个接口有多个实现类可以注入的时候,优先选择使用@Primary注解的bean,
* 避免@autowire注解自动装配时的歧义性而导致报错
**/
@Primary
@Bean(name = "dynamicDataSource")
public DataSource dynamicDataSource(){
DynamicDataSource dynamicDataSource = new DynamicDataSource();
// 配置默认数据源为 dataSourcePrimary
dynamicDataSource.setDefaultTargetDataSource(primaryDataSource());
// 配置多数据源
HashMap<Object,Object> dataSourceMap = new HashMap<>(3);
dataSourceMap.put("primary",primaryDataSource());
dataSourceMap.put("local",localDataSource());
dataSourceMap.put("produce",produceDataSource());
dynamicDataSource.setTargetDataSources(dataSourceMap);
return dynamicDataSource;
}
/**
* 配置@Transactional注解事务
**/
@Bean
public PlatformTransactionManager transactionManager(){
return new DataSourceTransactionManager(dynamicDataSource());
}
}
5、创建自己的多数据源管理类 DynamicDataSource
package com.multiple.pool;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
private static final ThreadLocal<String> dataSourceKey = new InheritableThreadLocal<String>();
public static void setDataSourceKey(String dataSource) {
dataSourceKey.set(dataSource);
}
public static void clearDataSourceKey() {
dataSourceKey.remove();
}
@Override
protected Object determineCurrentLookupKey() {
return dataSourceKey.get();
}
}
6、定义多数据切换规则,并编写AOP类,这里只演示通过注解方式 实现动态切换数据源
1、自定义注解DataSourceChange,用于动态切换数据源
package com.multiple.annotation;
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
@Documented
public @interface DataSourceChange {
String dataSource() default "primary";
}
2、编写AOP类,根据注解中指定的数据源Key,切换不同的数据库
package com.multiple.aspect;
import com.multiple.annotation.DataSourceChange;
import com.multiple.pool.DynamicDataSource;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
@Component
@Aspect
@Order(1)
public class MultipleDataSourceAspectAdvice {
private static final Logger logger = LoggerFactory.getLogger(MultipleDataSourceAspectAdvice.class);
/**
* 定义切面
*/
@Pointcut("execution(* com.multiple.service.*.*(..))")
public void pointCut() {
}
/**
* 设置数据源
**/
@Before("pointCut()")
public void dataSourceChange(JoinPoint joinPoint) throws Throwable {
// 获取当前目标的方法签名
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
// 通过方法签名 获取方法对象(该方法对象为代理对象)
Method method = methodSignature.getMethod();
DataSourceChange data = null;
// 数据源Key
String dataSource;
Method realMethod = null;
if (method != null) {
// 获取真正的方法目标对象
realMethod = joinPoint.getTarget().getClass().getDeclaredMethod(signature.getName(), method.getParameterTypes());
data = realMethod.getAnnotation(DataSourceChange.class);
if (data != null) {
// 获得数据源key
dataSource = data.dataSource();
if (StringUtils.isNotBlank(dataSource)) {
logger.info("使用的数据源key为:" + dataSource);
DynamicDataSource.setDataSourceKey(dataSource);
}
}
}
}
/**
* 清除数据源
**/
@After("pointCut()")
public void dataSourceClear(JoinPoint joinPoint) {
DynamicDataSource.clearDataSourceKey();
}
}
3、创建Service接口—UserService
package com.multiple.service;
public interface UserService {
String getUserMessagePrimary();
String getUserMessageLocal();
String getUserMessageProduce();
}
4、创建Service实现类—UserServiceImpl,为了方便,这里就不再给出dao层
package com.multiple.service.impl;
import com.multiple.annotation.DataSourceChange;
import com.multiple.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class UserServiceImpl implements UserService{
/**
* 通过自定义注解@DataSourceChange,使用默认数据源primary
**/
@Override
@Transactional(rollbackFor = Exception.class)
@DataSourceChange
public String getUserMessagePrimary() {
return "通过数据源primary,获取用户名为张三的用户信息";
}
/**
* 通过自定义注解@DataSourceChange,使用数据源local
**/
@Override
@Transactional(rollbackFor = Exception.class)
@DataSourceChange(dataSource = "local")
public String getUserMessageLocal() {
return "通过数据源local,获取用户名为李四的用户信息";
}
/**
* 通过自定义注解@DataSourceChange,使用数据源produce
**/
@Override
@Transactional(rollbackFor = Exception.class)
@DataSourceChange(dataSource = "produce")
public String getUserMessageProduce() {
return "通过数据源produce,获取用户名为王五的用户信息";
}
}
5、创建UserController
package com.multiple.controller;
import com.multiple.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/primary")
@ResponseBody
public String primary() {
String messagePrimary = userService.getUserMessagePrimary();
return messagePrimary;
}
@RequestMapping("/local")
@ResponseBody
public String local() {
String messageLocal = userService.getUserMessageLocal();
return messageLocal;
}
@RequestMapping("/produce")
@ResponseBody
public String produce() {
String messageProduce = userService.getUserMessageProduce();
return messageProduce;
}
}
6、编写SpringMdsourceApplication启动类
package com.multiple;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
/**
* 排除ataSourceAutoConfiguration自动配置类,否则会默认自动配置,不会使用我们自定义的DataSource
**/
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class SpringMdsourceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringMdsourceApplication.class, args);
}
}