一. Sharding-JDBC 简介

ShardingSphere是一套开源的分布式数据库中间件解决方案组成的生态圈,它由Sharding-JDBC、Sharding-Proxy和Sharding-Sidecar(计划中)这3款相互独立的产品组成。 他们均提供标准化的数据分片、分布式事务和数据库治理功能,可适用于如Java同构、异构语言、容器、云原生等各种多样化的应用场景。

ShardingSphere定位为关系型数据库中间件,旨在充分合理地在分布式的场景下利用关系型数据库的计算和存储能力,而并非实现一个全新的关系型数据库。 它与NoSQL和NewSQL是并存而非互斥的关系。NoSQL和NewSQL作为新技术探索的前沿,放眼未来,拥抱变化,是非常值得推荐的。反之,也可以用另一种思路看待问题,放眼未来,关注不变的东西,进而抓住事物本质。 关系型数据库当今依然占有巨大市场,是各个公司核心业务的基石,未来也难于撼动,我们目前阶段更加关注在原有基础上的增量,而非颠覆。

定位为轻量级Java框架,在Java的JDBC层提供的额外服务。 它使用客户端直连数据库,以jar包形式提供服务,无需额外部署和依赖,可理解为增强版的JDBC驱动,完全兼容JDBC和各种ORM框架。

  • 适用于任何基于Java的ORM框架,如:JPA, Hibernate, Mybatis, Spring JDBC Template或直接使用JDBC。
  • 基于任何第三方的数据库连接池,如:DBCP, C3P0, BoneCP, Druid, HikariCP等。
  • 支持任意实现JDBC规范的数据库。目前支持MySQL,Oracle,SQLServer和PostgreSQL。

sharding-jdbc-brief

二. 入门准备

文章中会放上源码,所以代码构建方面不做详细的介绍。本文着重于Sharding-JDBCSpring Boot项目中配置项,主要也是这一块配置较为繁琐。我在整理时也是查了许多相关的资料,踩过很多坑才完整整合。数据库主从这块使用docker compose构建也非常方便,文章中也会把相关的配置放置出来。本章提供的源码还集成了许多其他的东西,编写这个示例主要原因有一下几条:

  1. 使用gradle构建多模块化项目,来一次全面学习gradle。毕竟以前一直都是maven构建。

  2. 项目整体以微服务架构,基本模型已成。(gateway + nacos + 配置中心等等)

  3. 使用nacos配置中心,完成项目配置文件统一管理维护。

    本项目仅供学习参考及个人使用!如有更好的方案和idea,欢迎互相交流!如您觉得该项目对您有所帮助,欢迎点击右上方的Star标记,给予支持!!!谢谢 ~ ~

    后续也会整理更多的知识分享给大家,敬请期待~~

1. 相关版本

Java:1.8

Gradle: 6.7

Spring Boot:2.3.3.RELEASE

Sharding-JDBC:4.1.1

MybatisPlus:3.3.2

2. Gradle 引入依赖

1
2
3
implementation 'org.springframework.boot:spring-boot-starter-parent:2.3.3.RELEASE'
implementation 'org.apache.shardingsphere:sharding-jdbc-spring-boot-starter:4.1.1'
implementation 'com.baomidou:mybatis-plus-boot-starter:3.3.2'

三. 构建项目

项目具体构建和相关Java业务逻辑代码就不做详细展示了,Mybatis Plus写代码还是很方便快捷的。配置文件做了拆分,通过active来具体引用。

Sharding-JDBC官方文档

application.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 服务端口
server:
port: 8300

# Spring 配置
spring:
application:
name: micros-user
servlet:
multipart:
max-file-size: 10MB
max-request-size: 10MB
profiles:
# sdt: 读写分离 + 分表分库
# sms: 读写分离
# sin: 单库
active: sdt

1. 读写分离 + 分表分库

application-sdt.yml

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
spring:
cloud:
# Nacos 注册中心
nacos:
discovery:
enabled: true
register-enabled: true
server-addr: 192.168.30.180:8848
namespace: a03fbe75-bfcf-4d9e-b848-5da01fc74201
# sharding jdbc
shardingsphere:
enabled: true
# 数据源
datasource:
# 名称
names: master0,m0-slave1,m0-slave2,master1,m1-slave1,m1-slave2
master0:
type: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://192.168.30.180:3201/cluster_db1?allowMultiQueries=true&useSSL=false&characterEncoding=utf8
username: db_cluster
password: db123
m0-slave1:
type: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://192.168.30.180:3401/cluster_db1?allowMultiQueries=true&useSSL=false&characterEncoding=utf8
username: db_cluster
password: db123
m0-slave2:
type: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://192.168.30.180:3402/cluster_db1?allowMultiQueries=true&useSSL=false&characterEncoding=utf8
username: db_cluster
password: db123
master1:
type: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://192.168.30.180:3201/cluster_db2?allowMultiQueries=true&useSSL=false&characterEncoding=utf8
username: db_cluster
password: db123
m1-slave1:
type: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://192.168.30.180:3401/cluster_db2?allowMultiQueries=true&useSSL=false&characterEncoding=utf8
username: db_cluster
password: db123
m1-slave2:
type: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://192.168.30.180:3402/cluster_db2?allowMultiQueries=true&useSSL=false&characterEncoding=utf8
username: db_cluster
password: db123
sharding:
# 绑定表规则列表
binding-tables: tb_user,tb_logs
# 读写分离配置
master-slave-rules:
ds0:
master-data-source-name: master0
slave-data-source-names: m0-slave1, m0-slave2
ds1:
master-data-source-name: master1
slave-data-source-names: m1-slave1, m1-slave2
# 默认数据库分片策略,同分库策略(根据主键ID分库,全部适配)
default-database-strategy:
inline:
# 如果数据源命名不是很规范,此处就以最终的读写分离配置来匹配(master-slave-rules: ds0, ds1)
algorithm-expression: ds$->{id % 2}
sharding-column: id
tables:
# 用户表
tb_user:
# 数据节点
actual-data-nodes: ds$->{0..1}.tb_user$->{0..1}
# 生成主键值
key-generator:
column: id
type: SNOWFLAKE
# # 分库策略(根据主键ID分库,具体到表)
# database-strategy:
# inline:
# algorithm-expression: ds$->{id % 2}
# sharding-column: id
# 分表策略(根据性别分表)
table-strategy:
inline:
algorithm-expression: tb_user$->{sex % 2}
sharding-column: sex
# 日志表
tb_logs:
actual-data-nodes: ds$->{0..1}.tb_logs$->{0..1}
key-generator:
column: id
type: SNOWFLAKE
# 分表策略(根据用户ID分表)
table-strategy:
inline:
algorithm-expression: tb_logs$->{user_id % 2}
sharding-column: user_id
# props:
# # 开启SQL显示
# sql:
# show: true

# Mybatis
mybatis:
type-aliases-package: com.hellotech.cloud.example.entity
mapper-locations: classpath:mapper/*Mapper.xml
configuration:
# 驼峰命名转换
map-underscore-to-camel-case: true
# 使用列的别名替换列名
use-column-label: true
# 使用jdbc的getGeneratedKeys获取数据库自增主键值
use-generated-keys: true
# 控制台打印sql语句
# org.apache.ibatis.logging.stdout.StdOutImpl
# log4j打印sql语句
# org.apache.ibatis.logging.log4j2.Log4j2Impl
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

2. 读写分离

application-sms.yml

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
57
58
59
60
61
62
63
64
spring:
cloud:
# Nacos 注册中心
nacos:
discovery:
enabled: true
register-enabled: true
server-addr: 192.168.30.180:8848
namespace: a03fbe75-bfcf-4d9e-b848-5da01fc74201
# sharding jdbc
shardingsphere:
enabled: true
datasource:
names: master,slave1,slave2
master:
type: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://192.168.30.180:3201/cluster_db1?allowMultiQueries=true&useSSL=false&characterEncoding=utf8
username: db_cluster
password: db123
slave1:
type: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://192.168.30.180:3401/cluster_db1?allowMultiQueries=true&useSSL=false&characterEncoding=utf8
username: db_cluster
password: db123
slave2:
type: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://192.168.30.180:3402/cluster_db1?allowMultiQueries=true&useSSL=false&characterEncoding=utf8
username: db_cluster
password: db123
masterslave:
# 读写分离配置
load-balance-algorithm-type: round_robin
# 最终的数据源名称
name: dataSource
# 主库数据源名称
master-data-source-name: master
# 从库数据源名称列表,多个逗号分隔
slave-data-source-names:
- slave1
- slave2
# props:
# # 开启SQL显示
# sql:
# show: true

# Mybatis
mybatis:
type-aliases-package: com.hellotech.cloud.example.entity
mapper-locations: classpath:mapper/*Mapper.xml
configuration:
# 驼峰命名转换
map-underscore-to-camel-case: true
# 使用列的别名替换列名
use-column-label: true
# 使用jdbc的getGeneratedKeys获取数据库自增主键值
use-generated-keys: true
# 控制台打印sql语句
# org.apache.ibatis.logging.stdout.StdOutImpl
# log4j打印sql语句
# org.apache.ibatis.logging.log4j2.Log4j2Impl
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

3. 单库

由于表结构没有添加相关字段,这里就不展示数据脱敏相关配置了。仅做单库操作处理。

application-sin.yml

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
spring:
cloud:
# Nacos 注册中心
nacos:
discovery:
enabled: true
register-enabled: true
server-addr: 192.168.30.180:8848
namespace: a03fbe75-bfcf-4d9e-b848-5da01fc74201
# sharding jdbc
shardingsphere:
enabled: true
datasource:
name: master
master:
type: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://192.168.30.180:3201/cluster_db1?allowMultiQueries=true&useSSL=false&characterEncoding=utf8
username: db_cluster
password: db123
# props:
# # 开启SQL显示
# sql:
# show: true

# Mybatis Plus
mybatis-plus:
mapper-locations: classpath:/mapper/*Mapper.xml
type-aliases-package: com.lmaye.micros.service.user.entity.*
global-config:
db-config:
id-type: id_worker
insert-strategy: not_null
update-strategy: not_null
select-strategy: not_empty
table-underline: true
capital-mode: true
type-handlers-package: com.lmaye.micros.starter.mybatis.type
configuration:
jdbc-type-for-null: null
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

# 日志
logging:
config: classpath:log4j2-dev.xml

四. 相关资料

1. MySQL 脚本

image-20210128221324857

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for tb_logs
-- ----------------------------
DROP TABLE IF EXISTS `tb_logs`;
CREATE TABLE `tb_logs` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`user_id` bigint(20) NOT NULL COMMENT '用户ID',
`content` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '日志内容',
`type` int(2) NOT NULL DEFAULT 1 COMMENT '日志类型',
`remark` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '备注',
`create_at` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '创建时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '日志信息' ROW_FORMAT = Dynamic;

-- ----------------------------
-- Table structure for tb_logs0
-- ----------------------------
DROP TABLE IF EXISTS `tb_logs0`;
CREATE TABLE `tb_logs0` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`user_id` bigint(20) NOT NULL COMMENT '用户ID',
`content` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '日志内容',
`type` int(2) NOT NULL DEFAULT 1 COMMENT '日志类型',
`remark` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '备注',
`create_at` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '创建时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '日志信息' ROW_FORMAT = Dynamic;

-- ----------------------------
-- Table structure for tb_logs1
-- ----------------------------
DROP TABLE IF EXISTS `tb_logs1`;
CREATE TABLE `tb_logs1` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`user_id` bigint(20) NOT NULL COMMENT '用户ID',
`content` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '日志内容',
`type` int(2) NOT NULL DEFAULT 1 COMMENT '日志类型',
`remark` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '备注',
`create_at` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '创建时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '日志信息' ROW_FORMAT = Dynamic;

-- ----------------------------
-- Table structure for tb_user
-- ----------------------------
DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user` (
`id` bigint(20) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`username` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '用户名',
`password` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '密码',
`sex` int(1) NOT NULL DEFAULT 0 COMMENT '性别',
`status` int(1) NOT NULL DEFAULT 0 COMMENT '状态(0. 正常; 1. 停用; 2. 删除;)',
`create_at` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '创建时间',
`create_by` bigint(20) NOT NULL COMMENT '创建人',
`update_at` datetime(0) NULL DEFAULT NULL COMMENT '更新时间',
`update_by` bigint(20) NULL DEFAULT NULL COMMENT '更新人',
`remark` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '备注',
`ext` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '拓展字段',
`version` int(10) NOT NULL DEFAULT 1 COMMENT '版本号',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '用户信息' ROW_FORMAT = Dynamic;

-- ----------------------------
-- Table structure for tb_user0
-- ----------------------------
DROP TABLE IF EXISTS `tb_user0`;
CREATE TABLE `tb_user0` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`username` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '用户名',
`password` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '密码',
`sex` int(1) NOT NULL DEFAULT 0 COMMENT '性别',
`status` int(1) NOT NULL DEFAULT 0 COMMENT '状态(0. 正常; 1. 停用; 2. 删除;)',
`create_at` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '创建时间',
`create_by` bigint(20) NOT NULL COMMENT '创建人',
`update_at` datetime(0) NULL DEFAULT NULL COMMENT '更新时间',
`update_by` bigint(20) NULL DEFAULT NULL COMMENT '更新人',
`remark` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '备注',
`ext` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '拓展字段',
`version` int(10) NOT NULL DEFAULT 1 COMMENT '版本号',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '用户信息' ROW_FORMAT = Dynamic;

-- ----------------------------
-- Table structure for tb_user1
-- ----------------------------
DROP TABLE IF EXISTS `tb_user1`;
CREATE TABLE `tb_user1` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`username` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '用户名',
`password` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '密码',
`sex` int(1) NOT NULL DEFAULT 0 COMMENT '性别',
`status` int(1) NOT NULL DEFAULT 0 COMMENT '状态(0. 正常; 1. 停用; 2. 删除;)',
`create_at` datetime(0) NOT NULL DEFAULT CURRENT_TIMESTAMP(0) COMMENT '创建时间',
`create_by` bigint(20) NOT NULL COMMENT '创建人',
`update_at` datetime(0) NULL DEFAULT NULL COMMENT '更新时间',
`update_by` bigint(20) NULL DEFAULT NULL COMMENT '更新人',
`remark` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '备注',
`ext` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '拓展字段',
`version` int(10) NOT NULL DEFAULT 1 COMMENT '版本号',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '用户信息' ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;

2. MySQL主从部署

后续再写,先直接提供脚本给大家做练习。见谅~~

首先虚拟机或服务器需安装docker和docker compose,然后把mysql-cluster目录下的所有脚本(除了buid.sh,因为直接复制过去有点问题)复制到虚拟机或服务器中去,手动创建一个build.sh脚本,最后把build.sh内容复制过去。

:相关配置可以根据自己情况修改,例如用户、密码、端口等等。

1
2
3
4
5
6
7
8
9
10
# 创建目录
[root@nacos ~] mkdir mysql-cluster
# 进入到目录
[root@nacos ~] cd mysql-cluster
# 创建脚本
[root@nacos mysql-cluster] vi build.sh
# 保存后授予相关权限
[root@nacos mysql-cluster] chmod 777 build.sh
# 确认无误,一键部署
[root@nacos mysql-cluster] ./build.sh

[MySQL主从部署] 码云地址:mysql-cluster

image-20210128222648661

image-20210129000234640

3. 项目源码

码云地址:micros-gradle-examples

五. 效果展示

1. 日志分析

日志分析参考最难的分片即可,其他两种方式很简单自己去体验。

sdt: 读写分离 + 分表分库

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
[micros-user] [2021-01-28 23:04:13,162] [XNIO-1 task-1] [INFO] [org.apache.shardingsphere.underlying.executor.log.SQLLogger.log(SQLLogger.java:74)] SQLStatement: parameters=[1354807494895034370, admin, 123456, 0, 0, 2021-01-29 07:01:44.0, 0])], generatedKeyContext=Optional[GeneratedKeyContext(columnName=id, generated=false, generatedValues=[1354816605502095361])])
[micros-user] [2021-01-28 23:04:13,162] [XNIO-1 task-1] [INFO]
[org.apache.shardingsphere.underlying.executor.log.SQLLogger.log(SQLLogger.java:74)] Actual SQL: master1 ::: INSERT INTO tb_user0 ( id, username, password, sex, status, create_at, create_by )
VALUES (?, ?, ?, ?, ?, ?, ?) ::: [1354816605502095361, admin, 123456, 0, 0, 2021-01-29T07:01:44.000+0800, 0]
# --> master1 ::: tb_user0
# ---------------------- 分库 default-database-strategy
# 规则:algorithm-expression: ds$->{id % 2}
# 字段:sharding-column: id
# 结果 --> 1354816605502095361 % 2 = 1 (ds1[读写分离数据源]: master1, m1-slave1, m1-slave2)

# ---------------------- 分表 table-strategy
# 规则:algorithm-expression: tb_user$->{sex % 2}
# 字段:sharding-column: sex
# 结果 --> 0 % 2 = 0 (tb_user0)

# >>>>>>>>>>>>>>>>>>>>>> 读写分离数据源
# master-slave-rules:
# ds0:
# master-data-source-name: master0
# slave-data-source-names: m0-slave1, m0-slave2
# ds1:
# master-data-source-name: master1
# slave-data-source-names: m1-slave1, m1-slave2
#
# >>>>>>>>>>> 最终结果: master1(cluster_db2:3201), m1-slave1(cluster_db2:3401), m1-slave2(cluster_db2:3402)

2. HTTP请求:

image-20210128235000241

3. MySQL数据:

image-20210128234751047

4. Nacos配置中心

image-20210129000012447

最后更新: 2021年04月01日 11:03

原始链接: https://www.lmaye.com/2021/01/29/20210129000510/

× 多少都行~
打赏二维码