目录
一、多套数据源
1、Spring Boot 的默认配置文件是 application.properties ,由于有两个数据库配置,独立配置数据库是好的实践,因此添加配置文件 jbdc.properties ,添加以下自定义的主从数据库配置 #双数据库
datasource:
database1:
jdbc-url: jdbc:mysql://localhost:3306/zkyq_efe?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false&allowMultiQueries=true
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
database2:
jdbc-url: jdbc:mysql://localhost:3306/zkyq_obd?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false&allowMultiQueries=true
username: root
password: 123456
2、 多套数据源配置
有了数据源连接信息,需要把数据源注入到 Spring 中。由于每个数据库使用独立的一套数据库连接,数据库连接使用的 SqlSession 进行会话连接,SqlSession 是由SqlSessionFactory 生成。因此,需要分别配置SqlSessionFactory 。以下操作均在 config 目录 下:
(1)添加 DatabaseConfig1 配置文件,注入主数据源
package com.zkyq.efe.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* 数据源1相关配置(作为主数据库,项目启动默认连接此数据库)
*
* 主数据库都有 @Primary注解,从数据库都没有
*
* 需要额外注意的位置
* 1:扫描Dao层包路径,basePackages = "com.zkyq.efe.dao"
* 2:关联yml中的数据源,@ConfigurationProperties(prefix = "spring.datasource.database1")
* 3:扫描相关xml文件的所在路径,"classpath:mapper/*.xml"
*
* @author kt
*/
@Configuration
@MapperScan(basePackages = "com.zkyq.efe.dao", sqlSessionTemplateRef = "sqlSessionTemplate1")
public class DatabaseConfig1 {
@Bean(name = "dataSource1")
@ConfigurationProperties(prefix = "spring.datasource.database1")
@Primary
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "sqlSessionFactory1")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource1") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml"));
return bean.getObject();
}
@Bean(name = "transactionManager1")
@Primary
public DataSourceTransactionManager transactionManager(@Qualifier("dataSource1") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "sqlSessionTemplate1")
@Primary
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory1") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
(2)添加 DatabaseConfig2 配置文件,注入从数据源
package com.zkyq.efe.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* 数据源2相关配置(作为从数据库)
*
* 主数据库都有 @Primary注解,从数据库都没有
*
* 需要额外注意的位置
* 1:扫描Dao层包路径,basePackages = "com.kd.opt.dao2"
* 2:关联yml中的数据源,@ConfigurationProperties(prefix = "spring.datasource.database2")
* 3:扫描相关xml文件的所在路径,"classpath:mapper2/*.xml"
*
* @author kt
*/
@Configuration
@MapperScan(basePackages = "com.zkyq.efe.dao2", sqlSessionTemplateRef = "sqlSessionTemplate2")
public class DatabaseConfig2 {
@Bean(name = "dataSource2")
@ConfigurationProperties(prefix = "spring.datasource.database2")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "sqlSessionFactory2")
public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource2") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper2/*.xml"));
return bean.getObject();
}
@Bean(name = "transactionManager2")
public DataSourceTransactionManager transactionManager(@Qualifier("dataSource2") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "sqlSessionTemplate2")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory2") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
3、优缺点
(1)优点
简单、直接:一个库对应一套处理方式,很好理解。
符合开闭原则( OCP ):开发的设计模式告诉我们,对扩展开放,对修改关闭,添加多一个数据库,原来的那一套不需要改动,只添加即可。
(2)缺点
资源浪费:针对每一个数据源写一套操作,连接数据库的资源也是独立的,分别占用同样多的资源。SqlSessionFactory 是一个工厂,建议是使用单例,完全可以重用,不需要建立多个,只需要更改数据源即可,跟多线程,使用线程池减少资源消耗是同一道理。
代码冗余:在前面的多数据源配置中可以看出,其实 db01 和 db02 的很多操作是一样的,只是改个名称而已,因此会造成代码冗余。
缺乏灵活:所有需要使用的地方都需要引入对应的mapper,对于很多操作,只是选择数据源的不一样,代码逻辑是一致的。另外,对于是主从数据库的配置,一主多从的情况,若需要对多个从库进行负载均衡,相对比较麻烦。
正因为有上述的缺点,所以还有改进的空间。于是就有了动态数据源,至于动态数据源如何实现,下回分解。
二、动态数据源
Spring Boot 的动态数据源,本质上是把多个数据源存储在一个 Map 中,当需要使用某个数据源时,从 Map 中获取此数据源进行处理。而在 Spring 中,已提供了抽象类 AbstractRoutingDataSource 来实现此功能。因此,我们在实现动态数据源的,只需要继承它,实现自己的获取数据源逻辑即可。
1、添加动态数据源的配置
(1)配置相关
db1
spring.datasource.db1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.db1.jdbc-url=jdbc:mysql://localhost:3306/accs_sl?useSSL=false&serverTimezone=GMT%2B8&characterEncoding=UTF-8
spring.datasource.db1.username=root
spring.datasource.db1.password=root
# db2
spring.datasource.db2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.db2.jdbc-url=jdbc:mysql://localhost:3306/acss_ls?useSSL=false&serverTimezone=GMT%2B8&characterEncoding=UTF-8
spring.datasource.db2.username=root
spring.datasource.db2.password=root
(2)把数据源常量写在 DataSourceConstants 类中
public class DataSourceConstants {
public static final String DS_KEY_DB1 = "db1";
public static final String DS_KEY_DB2 = "db2";
}
根据连接信息,把数据源注入到 Spring 中,添加 DataSourceConfig 文件,配置如下:
此处使用 PropertySource 指定配置文件,ConfigurationProperties 指定数据源配置前缀
使用 MapperScan 指定包,自动注入相应的 mapper 类。
从此配置可以看到,已经把 SqlSessionFactory 这个配置从代码中擦除,直接使用 Spring Boot 自动配置的 SqlSessionFactory 即可,无需我们自己配置。
(3)在 DataSource 方法中使用 Map 保存多个数据源,并设置到动态数据源对象中。设置默认的数据源是 db1 数据源,使用注解 Primary 优先从动态数据源中获取
@Configuration
@PropertySource("classpath:jdbc.properties")
@MapperScan(basePackages = "com.zkyq.efe.mapper.db1")
public class DynamicDataSourceConfig {
@Bean(DataSourceConstants.DS_KEY_DB1)
@ConfigurationProperties(prefix = "spring.datasource.db1")
public DataSource db1DataSource(){
return DataSourceBuilder.create().build();
}
@Bean(DataSourceConstants.DS_KEY_DB2)
@ConfigurationProperties(prefix = "spring.datasource.db2")
public DataSource db2DataSource(){
return DataSourceBuilder.create().build();
}
@Bean
@Primary
public DataSource KtDataSource(){
Map<Object, Object> datasourceMap = new HashMap<>();
datasourceMap.put(DataSourceConstants.DS_KEY_DB1, db1DataSource());
datasourceMap.put(DataSourceConstants.DS_KEY_DB2, db2DataSource());
DynamicDataSource dynamicDataSource = new DynamicDataSource();
dynamicDataSource.setTargetDataSources(datasourceMap);
dynamicDataSource.setDefaultTargetDataSource(db01DataSource());
return dynamicDataSource;
}
}
2、动态数据源设置
前面的配置已把多个数据源注入到 Spring 中,接着对动态数据源进行配置。
(1)数据源 key 的上下文
为了可以动态切换路由策略,需要有一个动态获取数据源 key 的地方(我们称为上下文),对于 web 应用,访问以线程为单位,使用 ThreadLocal 就比较合适
public class KtDataSourceContextHolder {
/**
* 动态数据源名称上下文
*/
private static final ThreadLocal<String> DATA_SOURCE_CONTEXT_KEY_HOLDER = new ThreadLocal<>();
/**
* 设置/切换数据源
*/
public static void setContextKey(String key){
DATA_SOURCE_CONTEXT_KEY_HOLDER.set(key);
}
/**
* 获取数据源名称
*/
public static String getContextKey(){
String key = DATA_SOURCE_CONTEXT_KEY_HOLDER.get();
return key == null?DataSourceConstants.DS_KEY_DB1:key;
}
/**
* 删除当前数据源名称
*/
public static void removeContextKey(){
DATA_SOURCE_CONTEXT_KEY_HOLDER.remove();
}
}
(2)添加动态数据源类
继承抽象类 AbstractRoutingDataSource ,需要实现方法 determineCurrentLookupKey,即路由策略。该路由策略从上面的 KtDataSourceContextHolder 中获取。
public class KtDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return KtDataSourceContextHolder.getContextKey();
}
}
3、验证
默认是使用 db1 数据源查询,使用上下文的 setContextKey 来切换数据源,使用完后使用 removeContextKey 进行恢复
@GetMapping("/2")
public String test2(){
Map<String, String> map = new HashMap<>();
OrderInfo orderInfo = orderInfoMapper.selectByPrimaryKey(1);
map.put("orderInfo", JSON.toJSONString(orderInfo));
DynamicDataSourceContextHolder.setContextKey(DataSourceConstants.DS_KEY_DB2);
XxlJobInfo xxlJobInfo = xxlJobInfoMapper.selectByPrimaryKey(2);
map.put("xxlJobInfo", JSON.toJSONString(xxlJobInfo));
DynamicDataSourceContextHolder.removeContextKey();
return JSON.toJSONString(map);
}
}
4、使用 AOP 选择数据源
经过上面的动态数据源配置,可以实现动态数据源切换,但我们会发现,在进行数据源切换时,都需要做 setContextKey 和 removeContextKey 操作,如果需要切换的方法比多,就会发现很多重复的代码,如何消除这些重复的代码,就需要用到动态代理了
(1)定义数据源注解
在annotation包中,添加数据源注解 DSS,此注解可以写在类中,也可以写在方法定义中。
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface DSS {
String value() default DataSourceConstants.DS_KEY_DB1;
}
(2)定义切面
注解 Pointcut 使用 annotation 指定注解,注解 Around 使用环绕通知处理,使用上下文进行对使用注解 DSS 的值进行数据源切换,处理完后,恢复数据源。
@Aspect
@Component
public class KtDatasourceAspect {
@Pointcut(value = "@annotation(com.zkqy.efe.DSS)")
public void pointCut(){
}
@Around("pointCut()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
String dsKey = getDsAnnotation(joinPoint).value();
try{
DynamicDataSourceContextHolder.setContextKey(dsKey);
return joinPoint.proceed();
}finally {
DynamicDataSourceContextHolder.removeContextKey();
}
}
private DS getDsAnnotation(ProceedingJoinPoint joinPoint){
Class<?> targetClass = joinPoint.getTarget().getClass();
DS annotation = targetClass.getAnnotation(DS.class);
if(Objects.nonNull(annotation)){
return annotation;
}else {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
return signature.getMethod().getAnnotation(DS.class);
}
}
}
(3)使用 AOP 进行数据源切换
在service层,定义一个 Service ,里面有三个方法,分别使用默认值和设置值从 db1 和 db2 中获取数据,使用了注解DS
@Service
public class TestService {
@Autowired
private OrderInfoMapper1 orderInfoMapper;
@Autowired
private XxlJobInfoMapper1 xxlJobInfoMapper;
@DS
public OrderInfo getOrderInfo(int key){
return orderInfoMapper.selectByPrimaryKey(key);
}
@DS(DataSourceConstants.DS_KEY_DB1)
public OrderInfo getOrderInfo2(int key){
return orderInfoMapper.selectByPrimaryKey(key);
}
@DS(DataSourceConstants.DS_KEY_DB2)
public XxlJobInfo getXxljobInfo(int key){
return xxlJobInfoMapper.selectByPrimaryKey(key);
}
}