一、实现读写分离

1.1、引入依赖

  1. <dependency>
  2. <groupId>org.apache.shardingsphere</groupId>
  3. <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
  4. <version>4.1.1</version>
  5. </dependency>

1.2、配置属性

  1. spring:
  2. shardingsphere:
  3. datasource:
  4. names: master,slave1,slave2
  5. master:
  6. type: com.zaxxer.hikari.HikariDataSource
  7. driverClassName: com.mysql.cj.jdbc.Driver
  8. jdbcUrl: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
  9. username: root
  10. password: master
  11. slave1:
  12. type: com.zaxxer.hikari.HikariDataSource
  13. driverClassName: com.mysql.cj.jdbc.Driver
  14. jdbcUrl: jdbc:mysql://localhost:4401/test?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
  15. username: root
  16. password: slave1
  17. slave2:
  18. type: com.zaxxer.hikari.HikariDataSource
  19. driverClassName: com.mysql.cj.jdbc.Driver
  20. jdbcUrl: jdbc:mysql://localhost:4402/test?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
  21. username: root
  22. password: slave2
  23. # 开启 sql 日志打印
  24. props:
  25. sql.show: true
  26. masterslave:
  27. # 负载均衡算法
  28. load-balance-algorithm-type: round_robin
  29. sharding:
  30. master-slave-rules:
  31. master:
  32. master-data-source-name: master
  33. slave-data-source-names:
  34. - slave1
  35. - slave2

1.3、引入MybatisPlus 测试

1.3.1、依赖

  1. <dependency>
  2. <groupId>com.baomidou</groupId>
  3. <artifactId>mybatis-plus-boot-starter</artifactId>
  4. <version>3.5.1</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>mysql</groupId>
  8. <artifactId>mysql-connector-java</artifactId>
  9. <scope>runtime</scope>
  10. </dependency>

1.3.2、配置包扫描

  1. @MapperScan("cn.hdj.ShardingSphere.dao")
  2. @SpringBootApplication
  3. public class ShardingSphereApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(ShardingSphereApplication.class, args);
  6. }
  7. }

1.3.3、代码生成后测试

  • controller ```java @RestController @RequestMapping(value = “/course”) public class CourseController {

    @Autowired private CourseService courseService;

    @PostMapping(value = “/create”) public ResponseEntity create(@RequestBody CoursePO coursePO) {

    1. this.courseService.save(coursePO);
    2. return ResponseEntity.ok().body("ok");

    }

    @GetMapping(value = “/query”) public ResponseEntity query() {

    1. List<CoursePO> list = this.courseService.list();
    2. return ResponseEntity
    3. .ok(list);

    } }

  1. - 测试输出
  2. ```shell
  3. -- 采用 round_robin(轮询调度) 负载均衡读取数据
  4. -- 查询第一次 slave2
  5. Logic SQL: SELECT CNO,CNAME,TNO FROM COURSE
  6. Actual SQL: slave2 ::: SELECT CNO,CNAME,TNO FROM COURSE
  7. -- 查询第二次 slave1
  8. Logic SQL: SELECT CNO,CNAME,TNO FROM COURSE
  9. Actual SQL: slave1 ::: SELECT CNO,CNAME,TNO FROM COURSE

二、水平分表

2.1、分表需求

  • 按照水平分表来创建数据库
  • 创建表 course_1 、 course_2
  • 约定规则:如果添加的课程 id 为偶数添加到 course_1 中,奇数添加到 course_2 中。

2.2、表结构

  1. DROP TABLE IF EXISTS `course_1`;
  2. CREATE TABLE `course_1` (
  3. `ID` bigint NOT NULL,
  4. `CNO` varchar(5) NOT NULL COMMENT '课程号',
  5. `CNAME` varchar(104) DEFAULT NULL COMMENT '课程名称',
  6. `TNO` varchar(10) DEFAULT NULL COMMENT '老师编号',
  7. PRIMARY KEY (`ID`),
  8. UNIQUE KEY `COURSE_un` (`CNO`)
  9. ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='课程表1';
  10. DROP TABLE IF EXISTS `course_2`;
  11. CREATE TABLE `course_2` (
  12. `ID` bigint NOT NULL,
  13. `CNO` varchar(5) NOT NULL COMMENT '课程号',
  14. `CNAME` varchar(104) DEFAULT NULL COMMENT '课程名称',
  15. `TNO` varchar(10) DEFAULT NULL COMMENT '老师编号',
  16. PRIMARY KEY (`ID`),
  17. UNIQUE KEY `COURSE_un` (`CNO`)
  18. ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='课程表2';

2.3、配置 Sharding-JDBC 分片策略

  1. spring:
  2. shardingsphere:
  3. datasource:
  4. # 数据源名称
  5. names: master,slave1,slave2
  6. # 数据源配置
  7. master:
  8. type: com.zaxxer.hikari.HikariDataSource
  9. driverClassName: com.mysql.cj.jdbc.Driver
  10. jdbcUrl: jdbc:mysql://localhost:4400/test?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
  11. username: root
  12. password: master
  13. slave1:
  14. type: com.zaxxer.hikari.HikariDataSource
  15. driverClassName: com.mysql.cj.jdbc.Driver
  16. jdbcUrl: jdbc:mysql://localhost:4401/test?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
  17. username: root
  18. password: slave1
  19. slave2:
  20. type: com.zaxxer.hikari.HikariDataSource
  21. driverClassName: com.mysql.cj.jdbc.Driver
  22. jdbcUrl: jdbc:mysql://localhost:4402/test?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
  23. username: root
  24. password: slave2
  25. # 打开 sql 输出日志
  26. props:
  27. sql.show: true
  28. masterslave:
  29. # 负载均衡算法
  30. load-balance-algorithm-type: round_robin
  31. sharding:
  32. master-slave-rules:
  33. master:
  34. master-data-source-name: master
  35. slave-data-source-names:
  36. - slave1
  37. - slave2
  38. tables:
  39. course:
  40. # 指定course表分布的情况,配置表在哪个数据库里,表的名称都是什么 m1.course_1,m1.course_2
  41. actual-data-nodes: master.course_$->{1..2}
  42. # 指定 course 表里面主键 id 的生成策略 SNOWFLAKE
  43. key-generator:
  44. column: id
  45. type: SNOWFLAKE
  46. # 配置分表策略 约定 id 值偶数添加到 course_1 表,如果 id 是奇数添加到 course_2 表
  47. table-strategy:
  48. inline:
  49. algorithm-expression: course_$->{id % 2 + 1}
  50. sharding-column: id
  51. main:
  52. # 一个实体类对应两张表,覆盖
  53. allow-bean-definition-overriding: true

三、水平分库分表

四、垂直分表

参考