JeeSite 快速开发平台
官方网站
    • 平台简介
    • 架构特点
    • 技术选型
    • 功能介绍
    • 安装部署
    • 常见问题
    • 目录结构
    • 更新日志
    • Vue 文档
    • BPM 文档
    • Cloud 文档
V4 演示
Vue 演示
💖联系
  • 我要提意见、文档纠错
  • JeeSite 代码库, 请⭐Star关注
  • JeeSite Vue 代码库, 请⭐关注
  • Spring Cloud 微服务架构
  • JeeSite 手机端/移动端
  • Flowable 中国式工作流
  • OAauth2 统一认证服务器
  • 政务内外网环境中间件
  • 访问 ThinkGem 官方博客
  • 点击进入,下拉查看动态
官方网站
    • 平台简介
    • 架构特点
    • 技术选型
    • 功能介绍
    • 安装部署
    • 常见问题
    • 目录结构
    • 更新日志
    • Vue 文档
    • BPM 文档
    • Cloud 文档
V4 演示
Vue 演示
💖联系
  • 我要提意见、文档纠错
  • JeeSite 代码库, 请⭐Star关注
  • JeeSite Vue 代码库, 请⭐关注
  • Spring Cloud 微服务架构
  • JeeSite 手机端/移动端
  • Flowable 中国式工作流
  • OAauth2 统一认证服务器
  • 政务内外网环境中间件
  • 访问 ThinkGem 官方博客
  • 点击进入,下拉查看动态
  • 快速了解

  • 开发手册

  • Vue 开发手册

  • 扩展功能专题

  • 云服务技术架构

    • SaaS架构、多租户
    • 负载均衡、集群、高可用
    • SpringCloud 分布式、微服务
    • 分布式事务Seata、AT模式
    • 分布式事务LCN、柔性事务
    • 读写分离、分库分表
      • 适应于 v4.3.0+ 版本
        • 读写分离(JeeSite 实现)
        • 1、配置方法
        • 2、扩展数据源
        • ShardingSphere 实现
        • 1、引入依赖模块
        • 2、数据源配置
        • 3、扩展数据源
        • 4、读写分离配置
        • 5、分库分表配置
      • 适应于 v4.2.3 之前版本
        • 引入依赖模块
        • 读写分离配置文件
        • 配置文件规则
  • 技术服务与支持

读写分离、分库分表、分布式数据库解决方案

# 适应于 v4.3.0+ 版本

# 读写分离(JeeSite 实现)

特点:支持复杂SQL语句;支持多数据源或扩展数据源的读写分离;支持负载均衡算法自定义。

# 1、配置方法

# 数据源配置
jdbc:
  
  # Mysql 数据库配置
  type: mysql
  driver: com.mysql.cj.jdbc.Driver
  url: jdbc:mysql://127.0.0.1:3306/jeesite_test?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
  username: root
  password: 123456
  testSql: SELECT 1

  # 读写分离配置(专业版)v4.3.0
  readwriteSplitting:
    # 读库的数据源名称列表(默认数据源)
    readDataSourceNames: ds_read_01, ds_read_02
    # 负载均衡算法(ROUND_ROBIN轮询、RANDOM随机、自定义类名)
    loadBalancerAlgorithm: RANDOM

  # 多数据源名称列表,多个用逗号隔开,使用方法:@MyBatisDao(dataSourceName="ds2")
  dataSourceNames: ds_read_01, ds_read_02

  # 默认数据源的从库01
  ds_read_01:
    type: mysql
    driver: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/jeesite_test1?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
    testSql: SELECT 1
    pool:
      init: 1
      minIdle: 3
      maxActive: 20
  
  # 默认数据源的从库02
  ds_read_02:
    type: mysql
    driver: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/jeesite_test2?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
    testSql: SELECT 1
    pool:
      init: 1
      minIdle: 3
      maxActive: 20

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

# 2、扩展数据源

# 数据源配置
jdbc:

  dataSourceNames: ds2, ds2_read_01, ds2_read_02

  # 多数据源配置:ds2
  ds2:
    type: mysql
    driver: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/jeesite_ds2?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
    testSql: SELECT 1

    # 其它数据源支持密码加密
    encrypt:
      username: false
      password: true

    # 其它数据源支持连接池设置
    pool:
      init: 1
      minIdle: 3
      maxActive: 20

    # 其它数据源支持读写分离(重点)
    readwriteSplitting:
      readDataSourceNames: ds2_read_01, ds2_read_02
      loadBalancerAlgorithm: RANDOM

  # 默认数据源的从库01
  ds2_read_01:
    type: mysql
    driver: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/jeesite_ds21?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
    testSql: SELECT 1
    pool:
      init: 1
      minIdle: 3
      maxActive: 20
  
  # 默认数据源的从库02
  ds2_read_02:
    type: mysql
    driver: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/jeesite_ds22?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
    testSql: SELECT 1
    pool:
      init: 1
      minIdle: 3
      maxActive: 20

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

# ShardingSphere 实现

基于 ShardingSphere-JDBC,该框架定位为轻量级 Java 框架,在 Java 的 JDBC 层提供的额外服务。 它使用客户端直连数据库,可理解为增强版的 JDBC 驱动,完全兼容 JDBC 和各种 ORM 框架。 支持 MySQL,Oracle,SQLServer,PostgreSQL 以及任何遵循 SQL92 标准的数据库。

相关链接:

  1. 项目地址:https://github.com/apache/shardingsphere (opens new window)
  2. 使用帮助 5.1.1 :https://shardingsphere.apache.org/document/5.1.1/cn/concepts/ (opens new window)
  3. 使用帮助 5.0.0-beta :https://shardingsphere.apache.org/document/5.0.0-beta/en/overview/ (opens new window)
  4. SQL支持程度:https://shardingsphere.apache.org/document/5.1.1/cn/features/sharding/use-norms/sql/ (opens new window)

# 1、引入依赖模块

<dependency>
	<groupId>com.jeesite</groupId>
	<artifactId>jeesite-module-sharding</artifactId>
	<version>${project.parent.version}</version>
</dependency>
1
2
3
4
5

注意:此功能是专业版功能,请联系售后技术人员获取下载权限

# 2、数据源配置

# 数据源配置
jdbc:
  
  # Mysql 数据库配置
  type: mysql
  driver: com.mysql.cj.jdbc.Driver
  url: jdbc:mysql://127.0.0.1:3306/jeesite_test?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
  username: root
  password: 123456
  testSql: SELECT 1

  # 开启默认数据源分片
  sharding:
    enabled: true
    
    # 分片数据源列表
    dataSourceNames: ds_read_01,ds_read_02
    
    # 默认数据源的从库01
    ds_read_01:
      type: mysql
      driver: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://127.0.0.1:3306/jeesite_test1?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=Asia/Shanghai
      username: root
      password: 123456
      testSql: SELECT 1
      pool:
        init: 1
        minIdle: 3
        maxActive: 20
      
    # 默认数据源的从库02
    ds_read_02:
      type: mysql
      driver: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://127.0.0.1:3306/jeesite_test2?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=Asia/Shanghai
      username: root
      password: 123456
      testSql: SELECT 1
      pool:
        init: 1
        minIdle: 3
        maxActive: 20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

# 3、扩展数据源

上述列表举例 default 默认数据源,jeesite 也支持扩展数据源的读写分离及分片设置

# 数据源配置
jdbc:

  dataSourceNames: ds2
  
  # 多数据源配置:ds2
  ds2:
    # Mysql 数据库配置
    type: mysql
    driver: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/jeesite_ds2?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
    testSql: SELECT 1
    
    # 开启ds2数据源分片
    sharding:
      enabled: true
      
      # ds2数据源的分片列表
      dataSourceNames: ds2_read_01,ds2_read_02
      
      # ds2数据源的从库01
      ds2_read_01:
        type: mysql
        driver: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/jeesite_ds21?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=Asia/Shanghai
        username: root
        password: 123456
        testSql: SELECT 1
        pool:
          init: 1
          minIdle: 3
          maxActive: 20
        
      # ds2数据源的从库02
      ds2_read_02:
        type: mysql
        driver: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/jeesite_ds22?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=Asia/Shanghai
        username: root
        password: 123456
        testSql: SELECT 1
        pool:
          init: 1
          minIdle: 3
          maxActive: 20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

# 4、读写分离配置

# 名词解释

  • 主库:添加、更新以及删除数据操作所使用的数据库,目前仅支持单主库。
  • 从库:查询数据操作所使用的数据库,可支持多从库。
  • 主从同步:将主库的数据异步的同步到从库的操作。 由于主从同步的异步性,从库与主库的数据会短时间内不一致。
  • 负载均衡策略:通过负载均衡策略将查询请求疏导至不同从库。

# 使用规范

支持项

  • 提供一主多从的读写分离配置,可独立使用,也可配合数据分片使用;
  • 事务中的数据读写均用主库(类或方法上有@Transactional注解的均用主库);
  • 基于 Hint(分片策略)的强制主库路由。

不支持项

  • 主库和从库的数据同步;
  • 主库和从库的数据同步延迟导致的数据不一致;
  • 主库多写(多个主库提供写操作);
  • 主从库间的事务一致性。主从模型中,事务中的数据读写均用主库。

# 配置参数

帮助文档:https://shardingsphere.apache.org/document/5.1.1/cn/user-manual/shardingsphere-jdbc/spring-boot-starter/rules/sharding/ (opens new window)

spring.shardingsphere:
  
  # 是否开启(重要)
  enabled: true
  
  # 模式配置:https://shardingsphere.apache.org/document/5.1.1/cn/concepts/mode/
  mode:
    # 模式配置(测试:Memory、单机:Standalone、集群:Cluster)
    # https://shardingsphere.apache.org/document/5.1.1/cn/user-manual/shardingsphere-jdbc/spring-boot-starter/mode/
    type: Memory

  # 路由规则
  rules:

    # ===================== 读写分离 =====================
    # 核心概念:https://shardingsphere.apache.org/document/5.1.1/cn/features/readwrite-splitting/concept/
    # 使用规范:https://shardingsphere.apache.org/document/5.1.1/cn/features/readwrite-splitting/use-norms/
    # 配置方法:https://shardingsphere.apache.org/document/5.1.1/cn/user-manual/shardingsphere-jdbc/spring-boot-starter/rules/readwrite-splitting/
    readwrite-splitting:
    
      # 读写分离数据源列表
      data-sources:
      
        # 读写分离的数据源名称(对应 jdbc.dataSourceNames,如果主库设置 default)
        default:
          props:

            # 读库的数据源名称(对应 jdbc.dataSourceNames,如果主库设置 default)
            write-data-source-name: default
            # 从库的数据源名称(对应 jdbc.sharding.dataSourceNames、jdbc.ds2.sharding.dataSourceNames,其中的ds2为多数据源名)
            read-data-source-names: ds_read_01,ds_read_02

          # 读写分离类型(Static、Dynamic)
          type: Static

          # 从库负载均衡算法类型
          load-balancer-name: random

      # 负载均衡算法配置
      load-balancers:
        # 轮询算法
        round_robin:
          type: ROUND_ROBIN
        # 随机访问
        random:
          type: RANDOM
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

# 配置测试

  • logback 配置添加 <logger name="ShardingSphere-SQL" level="DEBUG" />
  • 去掉 TestDataService 上的 @Transactional 注解,关闭类的全局事务
  • 访问菜单 研发工具->代码生成实例->单表/主子表 持续点击几下 查询 按钮
  • 日志里打印 Actual SQL: ds_read_01 或 Actual SQL: ds_read_02 信息
  • Actual SQL 为实际执行 SQL 语句的数据源,如果 01 和 02 切换显示,说明配置成功

# 5、分库分表配置

使用 js_sys_log 表举例,进行分表

1、系统创建两张相同的表 js_sys_log_1、js_sys_log_2

2、配置参数如下:

帮助文档:https://shardingsphere.apache.org/document/5.1.1/cn/user-manual/shardingsphere-jdbc/spring-boot-starter/rules/sharding/ (opens new window)

spring.shardingsphere:
  
  # 是否开启(重要)
  enabled: true
  
  # 模式配置:https://shardingsphere.apache.org/document/5.1.1/cn/concepts/mode/
  mode:
    # 模式配置(测试:Memory、单机:Standalone、集群:Cluster)
    # https://shardingsphere.apache.org/document/5.1.1/cn/user-manual/shardingsphere-jdbc/spring-boot-starter/mode/
    type: Memory

  # 路由规则
  rules:

    # ===================== 分库分表 =====================
    # 核心概念:https://shardingsphere.apache.org/document/5.1.1/cn/features/sharding/concept/table/
    # 使用规范:https://shardingsphere.apache.org/document/5.1.1/cn/features/sharding/use-norms/sql/
    # 配置方法:https://shardingsphere.apache.org/document/5.1.1/cn/user-manual/shardingsphere-jdbc/spring-boot-starter/rules/sharding/
    sharding:

      # 分片表:https://shardingsphere.apache.org/document/5.1.1/cn/features/sharding/concept/table/
      tables:
        # 日志表分片,分别创建 2 张相同表结构的表:js_sys_log_1,js_sys_log_2
        js_sys_log:
          # 行表达式:https://shardingsphere.apache.org/document/5.1.1/cn/features/sharding/concept/inline-expression/
          actual-data-nodes: default.js_sys_log_$->{1..2}
          table-strategy:
            standard:
              # 分片字段指定为创建时间,分片算法取时间戳的奇数偶数进行分片
              sharding-column: create_date
              sharding-algorithm-name: js_sys_log_inline

      # 分片算法:InlineShardingAlgorithm
      sharding-algorithms:
        js_sys_log_inline:
          type: INLINE
          props:
            algorithm-expression: js_sys_log_$->{create_date.getTime() % 2 + 1}

      # 绑定表:https://shardingsphere.apache.org/document/5.1.1/cn/features/sharding/concept/table/#绑定表
      binding-tables:
        - js_sys_log
      
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

# 配置测试

  • logback 配置添加 <logger name="ShardingSphere-SQL" level="DEBUG" />
  • 访问菜单 研发工具->代码生成实例->单表/主子表 持续点击几下 查询 按钮
  • 日志里打印 Actual SQL: default ::: INSERT INTO js_sys_log_1 或 Actual SQL: default ::: INSERT INTO js_sys_log_2 信息
  • Actual SQL 为实际的执行 SQL 语句,如果 js_sys_log_1 和 js_sys_log_2 切换显示,说明配置成功

# 适应于 v4.2.3 之前版本

相关链接:

  1. 项目地址:https://github.com/apache/shardingsphere (opens new window)
  2. 使用帮助:https://shardingsphere.apache.org/document/legacy/4.x/document/cn/overview/ (opens new window)

# 引入依赖模块

<dependency>
	<groupId>com.jeesite</groupId>
	<artifactId>jeesite-module-sharding</artifactId>
	<version>${project.parent.version}</version>
</dependency>
1
2
3
4
5

注意:此功能是专业版功能,请联系售后技术人员获取下载权限

# 读写分离配置文件

classpath:config/application.yml

# 数据源配置
jdbc:

  # Mysql 数据库配置
  type: mysql
  driver: com.mysql.cj.jdbc.Driver
  url: jdbc:mysql://192.168.0.5:3306/jeesite_test?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=Asia/Shanghai
  username: root
  password: 123456
  testSql: SELECT 1

  # 分片数据源
  sharding:
    enabled: true
    
    # 分片数据源列表
    dataSourceNames: slave0,slave1
    
    # 从库01
    slave0:
      type: mysql
      driver: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://192.168.0.6:3306/jeesite_test1?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=UTC
      username: root
      password: 123456
      testSql: SELECT 1
      pool:
        init: 1
        minIdle: 3
        maxActive: 20
    
    # 从库02
    slave1:
      type: mysql
      driver: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://192.168.0.7:3306/jeesite_test2?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=UTC
      username: root
      password: 123456
      testSql: SELECT 1
      pool:
        init: 1
        minIdle: 3
        maxActive: 20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

# 配置文件规则

命名规则:classpath:config/sharding-数据源名.yml

该数据源名是 jdbc.dataSourceNames 的数据源名,不是分片 jdbc.sharding.dataSourceNames 的数据源。

配置参数 jdbc 直属下的数据源配置为默认数据源 default,如:sharding-default.yml,扩展数据源可参考,如:sharding-ds2.yml、sharding-ds3.yml

详细文档:https://shardingsphere.apache.org/document/legacy/4.x/document/cn/manual/sharding-jdbc/configuration/config-yaml/ (opens new window)

classpath:config/sharding-default.yml

# 读写分离
shardingRule:  
  masterSlaveRules:
    ms_default:

	  # 主库数据源名
      masterDataSourceName: default

	  # 从库数据源名列表
      slaveDataSourceNames:
        - slave0
        - slave1

      # 从库负载均衡算法类型,可选值:ROUND_ROBIN、RANDOM
      loadBalanceAlgorithmType: ROUND_ROBIN
      
# 属性配置
props:
  # 是否开启SQL显示,默认值: false
  sql.show: true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
进入 JeeSite 源码仓库页面,点击右上角 ⭐ Star 加星关注。

← 分布式事务LCN、柔性事务 版本区别、联系我们→

联系我们:s.jeesite.com  |  微信号:jeesitex  |  邮箱:jeesite@163.com
© 2013-2023 济南卓源软件有限公司 版权所有 | Theme Vdoing

关注 JeeSite 公众号,了解最新动态

  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式