一、应用场景

项目需要从自己的数据库上读取和管理数据外,还有一部分业务涉及到其他多个数据库(例如:读写分离的操作)。

为了能够灵活地指定具体的数据库,本文基于注解和AOP的方法实现多数据源自动切换。在使用过程中,只需要添加注解就可以使用,简单方便。

二、创建多数据库

USE test;
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `age` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
 
 
USE test1;
CREATE TABLE `teacher` (
  `tid` int(11) NOT NULL AUTO_INCREMENT,
  `tname` varchar(255) NOT NULL,
  `tage` int(11) NOT NULL,
  PRIMARY KEY (`tid`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
 
 
 
USE test2;
CREATE TABLE `student` (
  `sid` int(11) NOT NULL AUTO_INCREMENT,
  `sname` varchar(255) NOT NULL,
  `sage` int(11) NOT NULL,
  PRIMARY KEY (`sid`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

三、多数据源思路

  • 启动类注册动态数据源 
  • 配置文件中配置多个数据源 
  • 在需要的方法上使用注解指定数据源

四、编码讲解

1、项目目录结构

springboot的多数据源配置 springboot多数据源配置流程_springboot

2.项目的jar依赖(pom.xml)

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.loafer</groupId>
  <artifactId>springboot06</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>springboot06</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
  </properties>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
  </parent>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.0.1</version>
    </dependency>

    <!--mysql-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
    </dependency>
    <!--aop-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <!--数据库连接池-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.3</version>
    </dependency>
    <!-- 分页插件 -->
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper-spring-boot-starter</artifactId>
      <version>1.2.5</version>
    </dependency>
  </dependencies>
</project>

3.项目启动类(DynamicDataSourceApplication.java)

添加注解 @Import :注册动态多数据源;

添加 @MapperScan :将项目中对应的mapper类的路径加进来

@SpringBootApplication //启动项目的入口
@Import({DynamicDataSourceRegister.class}) // 注册动态多数据源
@MapperScan("com.loafer.mapper")//将项目中对应的mapper类的路径加进来就可以了
public class DynamicDataSourceApplication {
    public static void main(String[] args) {
        SpringApplication.run(DynamicDataSourceApplication.class, args);
    }

}

4.在配置文件配置多数据源(application.yml)

##端口号设置
server:
  port: 8001
  ##访问项目根路径设置
  servlet:
    context-path: /boot
#spring:
#  ##数据源配置
#  datasource:
#    url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
#    username: root
#    password: pass
#    driver-class-name: com.mysql.jdbc.Driver
# 更多数据源
custom:
  datasource:
    defaultname: default #当前的是默认数据源
    names: ds1,ds2  #自定义的多数据源名称,与下面的ds1,ds2对应
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
    username: root
    password: pass
    ds1:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/test1?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
      username: root
      password: pass
    ds2:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/test2?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
      username: root
      password: pass
    filters: stat
    maxActive: 100
    initialSize: 1
    minIdle: 1
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: select 'x'
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxOpenPreparedStatements: 100

##mybatis配置
mybatis:
  type-aliases-package: com.loafer.entity
##配置打印sql
logging:
  level:
    com.loafer.mapper: DEBUG
##pagehelper分页插件配置
pagehelper:
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true
  params: count=countSql

5.动态数据源核心代码

  • DynamicDataSource:动态数据源切换;
  • DynamicDataSourceAspect:利用AOP切面实现数据源的动态切换;
  • DynamicDataSourceContextHolder:动态切换数据源;
  • DynamicDataSourceRegister:动态数据源注册;
  • TargetDataSource:在方法上使用,用于指定使用哪个数据源。
/**
 * @ClassName DynamicDataSource
 * @Description [动态数据源]
 **/
public class DynamicDataSource extends AbstractRoutingDataSource {
    @Override
    protected Object determineCurrentLookupKey() {
        return DynamicDataSourceContextHolder.getDataSourceType();
    }
}

 

/**
 * @ClassName DynamicDataSourceAspect
 * @Description [利用AOP切面实现数据源的动态切换]
 **/
@Aspect
@Order(-1)// 保证该AOP在@Transactional之前执行
@Component
public class DynamicDataSourceAspect {
    private static final Logger logger = LoggerFactory.getLogger(DynamicDataSourceAspect.class);

    @Before("@annotation(ds)")
    public void changeDataSource(JoinPoint point, TargetDataSource ds) throws Throwable {
        String dsId = ds.name();
        if (!DynamicDataSourceContextHolder.containsDataSource(dsId)) {
            logger.error("数据源[{}]不存在,使用默认数据源 > {}", ds.name(), point.getSignature());
        }else {
            logger.debug("Use DataSource : {} > {}", dsId, point.getSignature());
            DynamicDataSourceContextHolder.setDataSourceType(dsId);
        }

    }
    @After("@annotation(ds)")
    public void restoreDataSource(JoinPoint point, TargetDataSource ds) {
        logger.debug("Revert DataSource : {} > {}", ds.name(), point.getSignature());
        DynamicDataSourceContextHolder.clearDataSourceType();
    }
}

 

/**
 * @ClassName DynamicDataSourceContextHolder
 * @Description [动态切换数据源]
 **/
public class DynamicDataSourceContextHolder {

    private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
    public static List<String> dataSourceIds = new ArrayList<>();

    public static void setDataSourceType(String dataSourceType) {
        contextHolder.set(dataSourceType);
    }

    public static String getDataSourceType() {
        return contextHolder.get();
    }

    public static void clearDataSourceType() {
        contextHolder.remove();
    }

    /**
     * 判断指定DataSrouce当前是否存在
     */
    public static boolean containsDataSource(String dataSourceId){
        return dataSourceIds.contains(dataSourceId);
    }
}

 

/**
 *  动态数据源注册
 *  启动动态数据源请在启动类中 添加 @Import(DynamicDataSourceRegister.class)
 */
public class DynamicDataSourceRegister implements ImportBeanDefinitionRegistrar, EnvironmentAware {
    private static final Logger logger = LoggerFactory.getLogger(DynamicDataSourceRegister.class);

    // 如配置文件中未指定数据源类型,使用该默认值
    private static final Object DATASOURCE_TYPE_DEFAULT = "com.alibaba.druid.pool.DruidDataSource";

    // 数据源
    private DataSource defaultDataSource;
    private Map<String, DataSource> customDataSources = new HashMap<>();

    private static String DB_NAME = "names";
    private static String DB_DEFAULT_VALUE = "custom.datasource";
    @Value("${custom.datasource.defaultname}")
    private String defaultDbname;

    /**
     * 加载多数据源配置
     */
    @Override
    public void setEnvironment(Environment env) {
        initDefaultDataSource(env);
        initCustomDataSources(env);
    }

    /**
     * 2.0.4 初始化主数据源
     */
    private void initDefaultDataSource(Environment env) {
        // 读取主数据源
        Map<String, Object> dsMap = new HashMap<>();
        dsMap.put("type", env.getProperty(DB_DEFAULT_VALUE + "." + "type"));
        dsMap.put("driver-class-name", env.getProperty(DB_DEFAULT_VALUE + "." + "driver-class-name"));
        dsMap.put("url", env.getProperty(DB_DEFAULT_VALUE + "." + "url"));
        dsMap.put("username", env.getProperty(DB_DEFAULT_VALUE + "." + "username"));
        dsMap.put("password", env.getProperty(DB_DEFAULT_VALUE + "." + "password"));
        defaultDataSource = buildDataSource(dsMap);
    }



    // 初始化更多数据源
    private void initCustomDataSources(Environment env) {
        // 读取配置文件获取更多数据源,也可以通过defaultDataSource读取数据库获取更多数据源
        String dsPrefixs = env.getProperty(DB_DEFAULT_VALUE + "." + DB_NAME);
        for (String dsPrefix : dsPrefixs.split(",")) {// 多个数据源
            Map<String, Object> dsMap = new HashMap<>();

            dsMap.put("type", env.getProperty(DB_DEFAULT_VALUE + "." +  dsPrefix + ".type"));
            dsMap.put("driver-class-name", env.getProperty(DB_DEFAULT_VALUE + "."  +  dsPrefix + ".driver-class-name"));
            dsMap.put("url", env.getProperty(DB_DEFAULT_VALUE + "."  +  dsPrefix + ".url"));
            dsMap.put("username", env.getProperty(DB_DEFAULT_VALUE + "."  +  dsPrefix + ".username"));
            dsMap.put("password", env.getProperty(DB_DEFAULT_VALUE + "."  +  dsPrefix + ".password"));


            DataSource ds = buildDataSource(dsMap);
            customDataSources.put(dsPrefix, ds);
        }
    }



    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        Map<Object, Object> targetDataSources = new HashMap<Object, Object>();
        // 将主数据源添加到更多数据源中
        targetDataSources.put("dataSource", defaultDataSource);
        DynamicDataSourceContextHolder.dataSourceIds.add("dataSource");
        // 添加更多数据源
        targetDataSources.putAll(customDataSources);
        for (String key : customDataSources.keySet()) {
            DynamicDataSourceContextHolder.dataSourceIds.add(key);
        }
        // 创建DynamicDataSource
        GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
        beanDefinition.setBeanClass(DynamicDataSource.class);
        beanDefinition.setSynthetic(true);
        MutablePropertyValues mpv = beanDefinition.getPropertyValues();
        mpv.addPropertyValue("defaultTargetDataSource", defaultDataSource);
        mpv.addPropertyValue("targetDataSources", targetDataSources);
        registry.registerBeanDefinition("dataSource", beanDefinition);

        logger.info("Dynamic DataSource Registry");
    }

    // 创建DataSource
    @SuppressWarnings("unchecked")
    public DataSource buildDataSource(Map<String, Object> dsMap) {
        try {
            Object type = dsMap.get("type");
            if (type == null)
                type = DATASOURCE_TYPE_DEFAULT;// 默认DataSource

            Class<? extends DataSource> dataSourceType;
            dataSourceType = (Class<? extends DataSource>) Class.forName((String) type);

            String driverClassName = dsMap.get("driver-class-name").toString();
            String url = dsMap.get("url").toString();
            String username = dsMap.get("username").toString();
            String password = dsMap.get("password").toString();

            DataSourceBuilder factory = DataSourceBuilder.create().driverClassName(driverClassName).url(url)
                    .username(username).password(password).type(dataSourceType);
            return factory.build();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}

 

/**
 * 在方法上使用,用于指定使用哪个数据源
 */
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface TargetDataSource {
    String name();
}

6.实体类和mapper对象

public class Student {

    private Integer sid;

    private String sname;

    private Integer sage;

    /*************getBean setBean省略**************/

}


public class Teacher {

    private Integer tid;

    private String tname;

    private Integer tage;

    /*************getBean setBean省略**************/
}


public class User {

    private Integer id;

    private String name;

    private Integer age;

    /*************getBean setBean省略**************/

}
/**
 * 映射SQL语句的接口,无逻辑实现
 */
public interface StudentMapper {

    // 注解 @TargetDataSource 不可以在这里使用
    @Select({"select * from student where sid=#{id}"})
    Student getById(int id);
    @Select({"select * from student"})
    List<Student> getStudentList();
}


/**
 * 映射SQL语句的接口,无逻辑实现
 */
public interface TeacherMapper {
    
    // 注解 @TargetDataSource 不可以在这里使用
    @Select({"select * from teacher where tid=#{id}"})
    Teacher getById(int id);
    @Select({"select * from teacher"})
    List<Teacher> getTeacherList();
}


/**
 * 映射SQL语句的接口,无逻辑实现
 */
public interface UserMapper {
    
    // 注解 @TargetDataSource 不可以在这里使用
    @Select({"select * from user where id=#{id}"})
    User getById(int id);
    @Select({"select * from user"})
    List<User> getUserList();
}

7.Service层代码

注解@TargetDataSource 不能直接在接口类Mapper上使用,所以在Service上使用。

@Service
public class DynamicDataSourceService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private TeacherMapper teacherMapper;
    @Resource
    private StudentMapper studentMapper;


    //不指定数据源使用默认数据源
    public User getUserData(Integer id) {
        return userMapper.getById(id);
    }

    //指定数据源-ds1
    @TargetDataSource(name="ds1")
    public Teacher getTeacherData(Integer id) {
        return teacherMapper.getById(id);
    }

    //指定数据源-ds2
    @TargetDataSource(name="ds2")
    public Student getStudentData(Integer id) {
        return studentMapper.getById(id);
    }
    //不指定数据源使用默认数据源
    public List<User> getUserList() {
        return userMapper.getUserList();
    }
    //指定数据源-ds1
    @TargetDataSource(name="ds1")
    public List<Teacher> getTeacherList() {
        return  teacherMapper.getTeacherList();
    }
    //指定数据源-ds2
    @TargetDataSource(name="ds2")
    public List<Student> getStudentList() {
        return studentMapper.getStudentList();
    }
}

8.控制器 Controller层

@RestController
@RequestMapping(value = "/dds")
public class DynamicDataSourceController {

    @Autowired
    private DynamicDataSourceService service;

    /**
     * @description: 查询所有库test、test1、test2的user、student、teacher表信息
     * @author wangdong
     */
    @RequestMapping(value = "list")
    public Map<String,Object> list(){
        Map<String,Object> map = new HashMap<String,Object>();
        List<User> userList = service.getUserList();
        List<Teacher> teacherList = service.getTeacherList();
        List<Student> studentList = service.getStudentList();
        map.put("user",userList);
        map.put("teacher",teacherList);
        map.put("student",studentList);
        return map;
    }

    @RequestMapping(value = "/user/{id}")
    public User getAllUserData(@PathVariable Integer id){
        return service.getUserData(id);
    }

    @RequestMapping(value = "/teacher/{id}")
    public Teacher getAllTeacherData(@PathVariable Integer id) {
        return service.getTeacherData(id);
    }

    @RequestMapping(value = "/student/{id}")
    public Student getAllStudentData(@PathVariable Integer id) {
        return service.getStudentData(id);
    }
}

五、测试:

http://localhost:8001/boot/dds/user/4

springboot的多数据源配置 springboot多数据源配置流程_springboot_02

http://localhost:8001/boot/dds/teacher/4

springboot的多数据源配置 springboot多数据源配置流程_微服务_03

http://localhost:8001/boot/dds/student/4

springboot的多数据源配置 springboot多数据源配置流程_多数据源_04

http://localhost:8001/boot/dds/list

springboot的多数据源配置 springboot多数据源配置流程_springboot的多数据源配置_05

可以发现一个项目访问多个数据源,访问成功!!!

源码地址下载:https://github.com/loafer7423/springboot/tree/master/springboot06