应用场景
多租户,sass,pass,数据库的读写分离等。
动态数据源切换实现流程:
实操:
一、添加依赖包
这里以gradle构建工具为例
dependencies {
//springboot 基础的依赖
compile('org.springframework.boot:spring-boot-starter-web')
//alibaba 开源的数据库连接池
compile('com.alibaba:druid:1.1.9')
//实现spring aop切换需要的依赖
compile group: 'org.aspectj', name: 'aspectjweaver'
//spring 与mybatis整合需要的依赖
compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2')
}
二、数据库连接池配置文件
我这里配置了三个,分别是primary.properties(主库)、follower.properties(备库)、report.properties(报表数据库),同时主库是默认的数据库连接。
driverClassName= com.mysql.jdbc.Driverurl= jdbc:mysql://127.0.0.1:3306/HUNAN?useUnicode=true&characterEncoding=utf8
username= root
password= root
initialSize=1
minIdle=3
maxActive=20
maxWait=60000
timeBetweenEvictionRunsMillis=60000
minEvictableIdleTimeMillis=30000
poolPreparedStatements=true
filters=stat,wall,slf4j
connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
三、自定义动态切换数据源注解
该注解可以同时作用于类和方法上,如果类和方法注解同时存在,则方法的注解优先级高于类上的注解优先级 ,就是说类和方法同时存在该注解的话,则会使用方法的数据源
/**
* description 动态切换数据源注解
* 该注解可以同时作用于类和方法上,如果类和方法注解同时存在,则方法的注解优先级高于类上的注解优先级
* 也就是说类和方法同时存在该注解的话,则会使用方法的数据源
*
* @author lhc
*/@Target({ElementType.METHOD, ElementType.TYPE, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DynamicDataSource {
String value() default "primaryDataSource";
}
四、将数据库连接池对象注册到Spring IOC容器
采用DruidDataSourceFactory工程初始化连接池对象,@Bean 注解配合@Configuration 创建的对象,再次调用primaryDataSource()方法返回的实际是从IOC容器获取到的对象,因为是代理对象调用了primaryDataSource()方法,非原生java方法调用,Spring 采用AOP做了增强,如果没有@Configuration注解类,则会创建新的对象。
package com.ai.nsg.iptv.task.config.dynamicdatasource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.springframework.beans.factory.annotation.Value;
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 javax.sql.DataSource;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* description 数据源配置类
*
* @author lhc
*/
@Configuration
public class DynamicDataSourceConfig{
@Value("${datasourcepath.primary}")
private String primaryDB;
@Value("${datasourcepath.follower}")
private String followerDB;
@Value("${datasourcepath.report}")
private String reportDB;
@Bean(name = "primaryDataSource")
public DataSource primaryDataSource(){
try {
Properties prop = new Properties();
prop.load(new FileInputStream(primaryDB));
return DruidDataSourceFactory.createDataSource(prop);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Bean(name = "followerDataSource")
public DataSource followerDataSource(){
try {
Properties prop = new Properties();
prop.load(new FileInputStream(followerDB));
return DruidDataSourceFactory.createDataSource(prop);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Bean(name = "reportDataSource")
public DataSource reportDataSource(){
try {
Properties prop = new Properties();
prop.load(new FileInputStream(reportDB));
return DruidDataSourceFactory.createDataSource(prop);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Bean
@Primary
public DynamicRoutingDataSource dynamicDataSource(){
DynamicRoutingDataSource dynamicRoutingDataSource = new DynamicRoutingDataSource();
//设置默认的数据源
dynamicRoutingDataSource.setDefaultTargetDataSource(primaryDataSource());
Map<Object, Object> targetDataSources = new HashMap<>(4);
targetDataSources.put("followerDataSource", followerDataSource());
targetDataSources.put("reportDataSource", reportDataSource());
//设置数据源
dynamicRoutingDataSource.setTargetDataSources(targetDataSources);
dynamicRoutingDataSource.afterPropertiesSet();
DynamicDataSourceContextHolder.dataSourceSet.addAll(targetDataSources.keySet());
return dynamicRoutingDataSource;
}
public static void main(String[] args) {
System.out.println(System.getProperty("user.dir"));
}
}
五、配置数据源上下文
我们需要创建一个数据源上下文,用户记录当前线程使用的数据源是什么,以及记录所有注册成功的数据源的集合。对于线程级别的私有变量,我们首先ThreadLocal
来实现。
package com.ai.nsg.iptv.task.config.dynamicdatasource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashSet;
import java.util.Set;
/**
* description 设置当前线程数据源上下文类
*
* @author lhc
*/
public class DynamicDataSourceContextHolder{
private static final Logger logger = LoggerFactory.getLogger(DynamicDataSourceContextHolder.class);
/**
* 存储已经注册的数据源的key
*/
public static Set<Object> dataSourceSet = new HashSet<>();
/**
* 线程级别的私有变量
*/
private static final ThreadLocal<String> HOLDER = new ThreadLocal<>();
public static String getDataSourceRouterKey() {
return HOLDER.get();
}
/**
* 设置数据源之前一定要先移除
*/
public static void removeDataSourceRouterKey() {
HOLDER.remove();
}
/**
* 判断指定dataSource当前是否存在
*
* @param dataSource dataSource
* @return boolean
*/
public static boolean isExistDataSource(Object dataSource) {
return dataSourceSet.contains(dataSource);
}
public static void setDataSourceRouterKey(String dataSourceRouterKey) {
logger.info("当前数据源正在切换中......");
HOLDER.set(dataSourceRouterKey);
}
}
六、动态数据源路由
第五步我们以及新建了数据源上下文,用于存储我们当前线程的数据源,那么怎么通知spring用当前的数据源,spring提供一个接口,名为AbstractRoutingDataSource的抽象类,我们只需要重写determineCurrentLookupKey方法就可以,这个方法看名字就知道,就是返回当前线程的数据源,那我们只需要从我们刚刚的数据源上下文中取出当前数据源即可。
package com.ai.nsg.iptv.task.config.dynamicdatasource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
* description 数据源动态路由
*
* @author lhc
*/
public class DynamicRoutingDataSource extends AbstractRoutingDataSource{
private static final Logger logger = LoggerFactory.getLogger(DynamicRoutingDataSource.class);
@Override
protected Object determineCurrentLookupKey() {
String dataSourceName = DynamicDataSourceContextHolder.getDataSourceRouterKey();
logger.info("当前数据源是:{}", dataSourceName == null?"主库":dataSourceName.equals("followerDataSource")?"备库":"报表库");
return dataSourceName;
}
}
七、动态切换数据源切面
使用Spring 的AOP原理实现数据源切面
package com.ai.nsg.iptv.task.config;
import com.ai.nsg.iptv.task.annotate.DynamicDataSource;
import com.ai.nsg.iptv.task.config.dynamicdatasource.DynamicDataSourceContextHolder;
import org.aspectj.lang.JoinPoint;
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;
/**
* description 动态切换数据源切面
*
* @author lhc
*/
@Aspect
@Component
@Order(-1)
public class DynamicDataSourceAspect {
private static final Logger logger = LoggerFactory.getLogger(DynamicDataSourceAspect.class);
/**
* 同时拦截标注DataSource的注解类上或者方法
*/
@Pointcut(value = "@within(com.ai.nsg.iptv.task.annotate.DynamicDataSource) || @annotation(com.ai.nsg.iptv.task.annotate.DynamicDataSource)")
public void datasource(){
}
@Before("datasource()")
public void changeDataSource(JoinPoint joinPoint){
Class<?> klass = joinPoint.getTarget().getClass();
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
DynamicDataSource dataSource;
if (method.isAnnotationPresent(DynamicDataSource.class)) {
dataSource = method.getAnnotation(DynamicDataSource.class);
} else {
dataSource = klass.getAnnotation(DynamicDataSource.class);
}
//判断注解中的数据源是否在已注册的数据源中
if (DynamicDataSourceContextHolder.isExistDataSource(dataSource.value())) {
DynamicDataSourceContextHolder.setDataSourceRouterKey(dataSource.value());
logger.info("数据源设置为 > {}", dataSource.value());
} else {
throw new RuntimeException("数据源[" + dataSource.value() + "]不存在");
}
}
@After("datasource()")
public void restoreDataSource() {
DynamicDataSourceContextHolder.removeDataSourceRouterKey();
}
}
测试效果:
创建:controller层
package com.ai.nsg.iptv.task.controller;
import com.ai.nsg.iptv.task.service.TestService;
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
@RequestMapping("db")
public class TestController {
@Autowired
private TestService testService;
@RequestMapping("/test")
@ResponseBody
public void test(){
testService.test();
}
}
创建:service层
使用注解@DynamicDataSource("reporDataSource"),标注方法获取数据的数据源是报表库
package com.ai.nsg.iptv.task.service;
import com.ai.nsg.iptv.task.annotate.DynamicDataSource;
import com.ai.nsg.iptv.task.dao.SyncTaskDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class TestService {
@Autowired
private SyncTaskDAO syncTaskDAO;
@DynamicDataSource("reportDataSource")
public String test(){
List list = syncTaskDAO.getSyncTaskList();
return "Success";
}
}
创建:dao层
package com.ai.nsg.iptv.task.dao;
import com.ai.nsg.iptv.task.annotate.DynamicDataSource;
import com.ai.nsg.iptv.task.po.SyncTaskList;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface SyncTaskDAO {
@Select(" select taskKey ,taskExplain, taskStatus from I_SYNCTIME_TASK where taskType = '2'")
List <SyncTaskList> getSyncTaskList();
}