我们使用的是baomidou 提供的多数据源整合。
github官网:https://github.com/baomidou/dynamic-datasource-spring-boot-starter
苞米豆官网:https://dynamic-datasource.com/guide/integration/Druid.html#%E5%8F%82%E6%95%B0%E9%85%8D%E7%BD%AE

引入依赖

  1. <!-- druid 数据库连接池-->
  2. <dependency>
  3. <groupId>com.alibaba</groupId>
  4. <artifactId>druid-spring-boot-starter</artifactId>
  5. </dependency>
  6. <!-- Dynamic DataSource 动态数据源 -->
  7. <dependency>
  8. <groupId>com.baomidou</groupId>
  9. <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
  10. </dependency>

添加版本号

由于 druid-spring-boot-starter、dynamic-datasource-spring-boot-starter 都不是 SpringBoot 官网整合依赖,因此需要在父工程中添加版本号

  1. <properties>
  2. <druid.version>1.2.4</druid.version>
  3. <dynamic-ds.version>3.2.1</dynamic-ds.version>
  4. </properties>
  5. <!-- Druid -->
  6. <dependency>
  7. <groupId>com.alibaba</groupId>
  8. <artifactId>druid-spring-boot-starter</artifactId>
  9. <version>${druid.version}</version>
  10. </dependency>
  11. <!-- Dynamic DataSource -->
  12. <dependency>
  13. <groupId>com.baomidou</groupId>
  14. <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
  15. <version>${dynamic-ds.version}</version>
  16. </dependency>

编写配置

编辑 nacos 配置中心上的 ruoyi-system-dev.yml 文件,将原来的数据源配置删除或注释掉。新增多数据源配置

  1. spring:
  2. autoconfigure:
  3. exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
  4. # dynamic-datasource 动态数据源的配置
  5. datasource:
  6. druid:
  7. stat-view-servlet:
  8. enabled: true
  9. loginUsername: admin
  10. loginPassword: 123456
  11. dynamic:
  12. druid:
  13. initial-size: 5
  14. min-idle: 5
  15. maxActive: 20
  16. maxWait: 60000
  17. timeBetweenEvictionRunsMillis: 60000
  18. minEvictableIdleTimeMillis: 300000
  19. validationQuery: SELECT 1 FROM DUAL
  20. testWhileIdle: true
  21. testOnBorrow: false
  22. testOnReturn: false
  23. poolPreparedStatements: true
  24. maxPoolPreparedStatementPerConnectionSize: 20
  25. filters: stat,wall,slf4j
  26. connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
  27. datasource:
  28. master:
  29. driver-class-name: com.mysql.cj.jdbc.Driver
  30. url: jdbc:mysql://localhost:3306/ry-cloud?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
  31. username: root
  32. password: 123456
  33. slave_1:
  34. driver-class-name: com.mysql.cj.jdbc.Driver
  35. url: jdbc:mysql://localhost:3306/ry-cloud-2?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
  36. username: root
  37. password: 123456
  38. # 数据库连接基本信息
  39. # spring:
  40. # datasource:
  41. # url: jdbc:mysql://localhost:3306/ry-cloud?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
  42. # driver-class-name: com.mysql.cj.jdbc.Driver
  43. # username: root
  44. # password: 123456
  45. # mybatis 配置
  46. mybatis:
  47. # 别名配置
  48. type-aliases-package: com.ruoyi.system
  49. # 映射文件位置配置
  50. mapper-locations: classpath*:mapper/**/*.xml

配置文件说明

  1. 排除原生Druid的快速配置类

    1. spring:
    2. autoconfigure:
    3. exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
  2. 配置了两个数据源

    1. datasource:
    2. master:
    3. driver-class-name: com.mysql.cj.jdbc.Driver
    4. url: jdbc:mysql://localhost:3306/ry-cloud?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
    5. username: root
    6. password: 123456
    7. slave_1:
    8. driver-class-name: com.mysql.cj.jdbc.Driver
    9. url: jdbc:mysql://localhost:3306/ry-cloud-2?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
    10. username: root
    11. password: 123456
  3. druid 监控设置

    1. druid:
    2. stat-view-servlet:
    3. enabled: true
    4. loginUsername: admin
    5. loginPassword: 123456

    创建数据库

    创建一个slave_1 数据库名为:ry-cloud-2 , 字符编码 UTF8,脚本为: ```sql SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0;


— Table structure for sys_user


DROP TABLE IF EXISTS sys_user; CREATE TABLE sys_user ( user_id bigint(20) NOT NULL AUTO_INCREMENT COMMENT ‘用户ID’, dept_id bigint(20) NULL DEFAULT NULL COMMENT ‘部门ID’, user_name varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT ‘用户账号’, nick_name varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT ‘用户昵称’, user_type varchar(2) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT ‘00’ COMMENT ‘用户类型(00系统用户)’, email varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT ‘’ COMMENT ‘用户邮箱’, phonenumber varchar(11) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT ‘’ COMMENT ‘手机号码’, sex char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT ‘0’ COMMENT ‘用户性别(0男 1女 2未知)’, avatar varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT ‘’ COMMENT ‘头像地址’, password varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT ‘’ COMMENT ‘密码’, status char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT ‘0’ COMMENT ‘帐号状态(0正常 1停用)’, del_flag char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT ‘0’ COMMENT ‘删除标志(0代表存在 2代表删除)’, login_ip varchar(128) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT ‘’ COMMENT ‘最后登录IP’, login_date datetime(0) NULL DEFAULT NULL COMMENT ‘最后登录时间’, create_by varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT ‘’ COMMENT ‘创建者’, create_time datetime(0) NULL DEFAULT NULL COMMENT ‘创建时间’, update_by varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT ‘’ COMMENT ‘更新者’, update_time datetime(0) NULL DEFAULT NULL COMMENT ‘更新时间’, remark varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT ‘备注’, PRIMARY KEY (user_id) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = ‘用户信息表’ ROW_FORMAT = Compact;


— Records of sys_user


INSERT INTO sys_user VALUES (1, 103, ‘admin’, ‘若依’, ‘00’, ‘ry@163.com’, ‘15888888888’, ‘1’, ‘’, ‘$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2’, ‘0’, ‘0’, ‘127.0.0.1’, ‘2018-03-16 11:33:00’, ‘admin’, ‘2018-03-16 11:33:00’, ‘ry’, ‘2018-03-16 11:33:00’, ‘管理员’); INSERT INTO sys_user VALUES (2, 105, ‘ry’, ‘若依2’, ‘00’, ‘ry@qq.com’, ‘15666666666’, ‘1’, ‘’, ‘$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2’, ‘0’, ‘0’, ‘127.0.0.1’, ‘2018-03-16 11:33:00’, ‘admin’, ‘2018-03-16 11:33:00’, ‘ry’, ‘2018-03-16 11:33:00’, ‘测试员’); INSERT INTO sys_user VALUES (3, 103, ‘lcy’, ‘lcy’, ‘00’, ‘’, ‘13000000001’, ‘0’, ‘’, ‘123456’, ‘0’, ‘2’, ‘’, NULL, ‘ry’, ‘2021-04-10 15:22:05’, ‘’, ‘2021-04-10 15:42:48’, ‘1’); INSERT INTO sys_user VALUES (4, 103, ‘小y’, ‘lcy’, ‘00’, ‘’, ‘13000000000’, ‘0’, ‘’, ‘$2a$10$Dzaub8pDnp5Tuicz2fex7ercsYBwvhFISe8T.uGJbmqtpUN/FH2x6’, ‘0’, ‘2’, ‘’, NULL, ‘ry’, ‘2021-04-10 18:17:28’, ‘’, NULL, ‘1’);

SET FOREIGN_KEY_CHECKS = 1;

  1. <a name="O07Ng"></a>
  2. ## 切换数据库
  3. 将 @DS(**"slave_1"**) 标注在 mapper层或 service 层的类上 或者方法上<br />例如:
  4. ```sql
  5. package com.ruoyi.system.mapper;
  6. import com.baomidou.dynamic.datasource.annotation.DS;
  7. import com.ruoyi.system.api.domain.SysUser;
  8. @DS("slave_1")
  9. public interface SysUserMapper {
  10. public SysUser selectUserByUserName(String userName);
  11. }

测试

运行之前的测试类

  1. @Test
  2. public void selectUserByUserName() {
  3. SysUser user = userMapper.selectUserByUserName("ry");
  4. System.out.println(user);
  5. }

测试结果

image.png

切换说明

默认情况下,连接的是 master 数据源,如需切换到 slave 数据库,那么就需要在 mapper 或 service 类上,添加
@DS(“slave_1”) 注解即可

Druid 监控

监控页面:http://localhost:9201/druid/index.html
由于项目中的端口是 9201, 因此这边的端口也是 9201;登录的账号密码在 ruoyi-system-dev.yml 文件中已经设置过了,我们来看下:

  1. druid:
  2. stat-view-servlet:
  3. enabled: true
  4. loginUsername: admin
  5. loginPassword: 123456