版本号:5.1.1
首先需要创建两个以上的数据库
application.properties 配置
# 应用名称
spring.application.name=sharging-jdbc-demo
# 开发环境设置
spring.profiles.active=dev
# 内存模式
spring.shardingsphere.mode.type=Memory
# 配置真实数据源
spring.shardingsphere.datasource.names=master,slave1
# 配置第 1 个数据源
spring.shardingsphere.datasource.master.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master.jdbc-url=jdbc:mysql://localhost:3306/course_master_db
spring.shardingsphere.datasource.master.username=root
spring.shardingsphere.datasource.master.password=123456
# 配置第 2 个数据源
spring.shardingsphere.datasource.slave1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.slave1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave1.jdbc-url=jdbc:mysql://localhost:3306/course_db
spring.shardingsphere.datasource.slave1.username=root
spring.shardingsphere.datasource.slave1.password=123456
# 读写分离类型,如: Static,Dynamic:动态的
spring.shardingsphere.rules.readwrite-splitting.data-sources.myds.type=Static
# 写数据源名称
spring.shardingsphere.rules.readwrite-splitting.data-sources.myds.props.write-data-source-name= master
# 读数据源名称,多个从数据源用逗号分隔
spring.shardingsphere.rules.readwrite-splitting.data-sources.myds.props.read-data-source-names= slave1
# 负载均衡算法名称
spring.shardingsphere.rules.readwrite-splitting.data-sources.myds.load-balancer-name=alg_weight
# 负载均衡算法配置
# 负载均衡算法类型
#轮询
spring.shardingsphere.rules.readwrite-splitting.load-balancers.alg_round.type=ROUND_ROBIN
#随机
spring.shardingsphere.rules.readwrite-splitting.load-balancers.alg_random.type=RANDOM
#权重
spring.shardingsphere.rules.readwrite-splitting.load-balancers.alg_weight.type=WEIGHT
#读的权重
spring.shardingsphere.rules.readwrite-splitting.load-balancers.alg_weight.props.slave1=1
# 打印SQl
spring.shardingsphere.props.sql-show=true
application.yml配置:
spring:
profiles:
active: dev
application:
name: sharging-jdbc-demo
shardingsphere:
datasource:
names: master,slave1
master:
type: com.zaxxer.hikari.HikariDataSource # 数据源类型
url: jdbc:mysql://localhost:3306/course_master_db # 数据库连接地址
username: root # 用户名
password: 123456 # 密码
driver-class-name: com.mysql.jdbc.Driver # 数据库驱动
slave1:
type: com.zaxxer.hikari.HikariDataSource # 数据源类型
url: jdbc:mysql://localhost:3306/course_db # 数据库连接地址
username: root # 用户名
password: 123456 # 密码
driver-class-name: com.mysql.jdbc.Driver # 数据库驱动
rules:
readwrite-splitting:
data-sources:
mds: #名字自定义
type: static #类型 静态获取
props:
auto-aware-data-source-name: master
write-data-source-name: master
read-data-source-names: slave1
load-balancer-name: read-random #读写分离规则自定义命名
load-balancers:
read-random:
type: ROUND_ROBIN # 轮询负载均衡
props:
sql-show: true # 是否打印sql
sql-simple: true # 打印简单的sql
测试:
sql
CREATE TABLE `t_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
/**
* 读写分离:写入数据的测试
*/
@Test
public void testInsert() {
User user = new User();
user.setName("王5");
int count = userMapper.insert(user);
System.out.println(count);
List users1 = userMapper.selectList(null);
users1.forEach(System.out::println);
}
sql
CREATE TABLE t_order (
id BIGINT AUTO_INCREMENT,
order_no VARCHAR(30),
user_id BIGINT,
amount DECIMAL(10,2),
PRIMARY KEY(id)
)
application.properties
# 应用名称
spring.application.name=sharging-jdbc-demo
# 开发环境设置
spring.profiles.active=dev
# 内存模式
spring.shardingsphere.mode.type=Memory
# 配置真实数据源
spring.shardingsphere.datasource.names=server-order,server-user
# 配置第 1 个数据源
spring.shardingsphere.datasource.server-order.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.server-order.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.server-order.jdbc-url=jdbc:mysql://localhost:3306/course_master_db
spring.shardingsphere.datasource.server-order.username=root
spring.shardingsphere.datasource.server-order.password=123456
# 配置第 2 个数据源
spring.shardingsphere.datasource.server-user.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.server-user.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.server-user.jdbc-url=jdbc:mysql://localhost:3306/course_db
spring.shardingsphere.datasource.server-user.username=root
spring.shardingsphere.datasource.server-user.password=123456
# 标准分片表配置
# 由数据源名 + 表名组成,以小数点分隔。多个表以逗号分隔,支持 inline 表达式。缺省表示使用已知数据源与逻辑表名称生成数据节点,用于广播表(即每个库中都需要一个同样的表用于关联查询,多为字典表)或只分库不分表且所有库的表结构完全一致的情况
spring.shardingsphere.rules.sharding.tables.t_user.actual-data-nodes=server-user.t_user
spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes=server-order.t_order
# 打印SQl
spring.shardingsphere.props.sql-show=true
application.yml
spring:
profiles:
active: dev
application:
name: sharging-jdbc-demo
shardingsphere:
datasource:
names: master,slave1
master:
type: com.zaxxer.hikari.HikariDataSource # 数据源类型
url: jdbc:mysql://localhost:3306/course_master_db # 数据库连接地址
username: root # 用户名
password: 123456 # 密码
driver-class-name: com.mysql.jdbc.Driver # 数据库驱动
slave1:
type: com.zaxxer.hikari.HikariDataSource # 数据源类型
url: jdbc:mysql://localhost:3306/course_db # 数据库连接地址
username: root # 用户名
password: 123456 # 密码
driver-class-name: com.mysql.jdbc.Driver # 数据库驱动
rules:
sharding:
tables:
t_user:
actual-data-nodes: master.t_user
t_order:
actual-data-nodes: slave1.t_order
props:
sql-show: true # 是否打印sql
sql-simple: true # 打印简单的sql
测试代码
/**
* 垂直分片:插入数据测试
*/
@Test
public void testInsertOrderAndUser() {
User user = new User();
user.setName("张飞");
userMapper.insert(user);
Order order = new Order();
order.setOrderNo("ATGUIGU005");
order.setUserId(user.getId());
order.setAmount(new BigDecimal(1000));
orderMapper.insert(order);
}
3.1 数据库分片
sql
CREATE TABLE t_order0 (
id BIGINT,
order_no VARCHAR(30),
user_id BIGINT,
amount DECIMAL(10,2),
PRIMARY KEY(id)
);
CREATE TABLE t_order1 (
id BIGINT,
order_no VARCHAR(30),
user_id BIGINT,
amount DECIMAL(10,2),
PRIMARY KEY(id)
);
application.properties
# 应用名称
spring.application.name=sharging-jdbc-demo
# 开发环境设置
spring.profiles.active=dev
# 内存模式
spring.shardingsphere.mode.type=Memory
# 打印SQl
spring.shardingsphere.props.sql-show=true
# 配置真实数据源
spring.shardingsphere.datasource.names=server-order0,server-order1
# 配置第 1 个数据源
spring.shardingsphere.datasource.server-order.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.server-order.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.server-order.jdbc-url=jdbc:mysql://localhost:3306/course_master_db
spring.shardingsphere.datasource.server-order.username=root
spring.shardingsphere.datasource.server-order.password=123456
# 配置第 2 个数据源
spring.shardingsphere.datasource.server-order1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.server-order1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.server-order1.jdbc-url=jdbc:mysql://localhost:3306/course_db
spring.shardingsphere.datasource.server-order1.username=root
spring.shardingsphere.datasource.server-order1.password=123456
# 标准分片表配置
# 由数据源名 + 表名组成,以小数点分隔。多个表以逗号分隔,支持 inline 表达式。缺省表示使用已知数据源与逻辑表名称生成数据节点,用于广播表(即每个库中都需要一个同样的表用于关联查询,多为字典表)或只分库不分表且所有库的表结构完全一致的情况
spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes=server-order$->{0..1}.t_order0
# 分片列名称
spring.shardingsphere.rules.sharding.tables.t_order.database-strategy.standard.sharding-column=user_id
# 分片算法名称 自定义名称
spring.shardingsphere.rules.sharding.tables.t_order.database-strategy.standard.sharding-algorithm-name=alg_inline_userid
#------------------------分片算法配置
# 行表达式分片算法
# 分片算法类型
spring.shardingsphere.rules.sharding.sharding-algorithms.alg_inline_userid.type=INLINE
# 分片算法属性配置$->{user_id % 2}
spring.shardingsphere.rules.sharding.sharding-algorithms.alg_inline_userid.props.algorithm-expression==server-order$->{user_id % 2}
# 取模分片算法
# 分片算法类型
#spring.shardingsphere.rules.sharding.sharding-algorithms.alg_mod.type=MOD
# 分片算法属性配置
#spring.shardingsphere.rules.sharding.sharding-algorithms.alg_mod.props.sharding-count=2
application.yml
spring:
profiles:
active: dev
application:
name: sharging-jdbc-demo
shardingsphere:
# 是否开启
datasource:
# 数据源(逻辑名字)
# 配置数据源
names: server-order0,server-order1
server-order0:
type: com.zaxxer.hikari.HikariDataSource # 数据源类型
url: jdbc:mysql://localhost:3306/course_master_db # 数据库连接地址
username: root # 用户名
password: 123456 # 密码
driver-class-name: com.mysql.jdbc.Driver # 数据库驱动
server-order1:
type: com.zaxxer.hikari.HikariDataSource # 数据源类型
url: jdbc:mysql://localhost:3306/course_db # 数据库连接地址
username: root # 用户名
password: 123456 # 密码
driver-class-name: com.mysql.jdbc.Driver # 数据库驱动
# 分片的配置
rules:
sharding:
# 表的分片策略
tables:
# 逻辑表的名称
t_order:
# 数据节点配置,采用Groovy表达式
actual-data-nodes: server-order$->{0..1}.t_order0
# 配置策略
database-strategy:
# 用于单分片键的标准分片场景
standard:
sharding-column: user_id
# 分片算法名字
sharding-algorithm-name: user_inline
key-generate-strategy: # 主键生成策略
column: id # 主键列
key-generator-name: snowflake # 策略算法名称(推荐使用雪花算法)
key-generators:
snowflake:
type: SNOWFLAKE
sharding-algorithms:
user_inline:
type: inline
props:
algorithm-expression: server-order$->{user_id % 2}
props:
# 日志显示具体的SQL
sql-show: true
3.2,水平分表
application.properties
#------------------------基本配置
# 应用名称
spring.application.name=sharging-jdbc-demo
# 开发环境设置
spring.profiles.active=dev
# 内存模式
spring.shardingsphere.mode.type=Memory
# 打印SQl
spring.shardingsphere.props.sql-show=true
#------------------------数据源配置
# 配置真实数据源
spring.shardingsphere.datasource.names=server-order0,server-order1
# 配置第 2 个数据源
spring.shardingsphere.datasource.server-order0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.server-order0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.server-order0.jdbc-url=jdbc:mysql://localhost:3306/course_master_db
spring.shardingsphere.datasource.server-order0.username=root
spring.shardingsphere.datasource.server-order0.password=123456
# 配置第 3 个数据源
spring.shardingsphere.datasource.server-order1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.server-order1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.server-order1.jdbc-url=jdbc:mysql://localhost:3306/course_db
spring.shardingsphere.datasource.server-order1.username=root
spring.shardingsphere.datasource.server-order1.password=123456
#------------------------标准分片表配置(数据节点配置)
# 由数据源名 + 表名组成,以小数点分隔。多个表以逗号分隔,支持 inline 表达式。
# 缺省表示使用已知数据源与逻辑表名称生成数据节点,用于广播表(即每个库中都需要一个同样的表用于关联查询,多为字典表)或只分库不分表且所有库的表结构完全一致的情况
#spring.shardingsphere.rules.sharding.tables.t_user.actual-data-nodes=server-user.t_user
# server-order0.t_order0,server-order0.t_order1,server-order1.t_order0,server-order1.t_order1
spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes=server-order0.t_order$->{0..1}
#------------------------分库策略,缺省表示使用默认分库策略,以下的分片策略只能选其一
# 用于单分片键的标准分片场景 分库策略
# 分片列名称
#spring.shardingsphere.rules.sharding.tables.t_order.database-strategy.standard.sharding-column=user_id
# 分片算法名称
#spring.shardingsphere.rules.sharding.tables.t_order.database-strategy.standard.sharding-algorithm-name=alg_inline_userid
# 用于单分片键的标准分片场景
#spring.shardingsphere.rules.sharding.sharding-algorithms.alg_inline_userid.type=INLINE
#spring.shardingsphere.rules.sharding.sharding-algorithms.alg_inline_userid.props.algorithm-expression=server-order$->{user_id % 2}
#------------------------分表策略
# 用于单分片键的标准分片场景
# 分片列名称
spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-column=user_id
# 分片算法名称
spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-algorithm-name=alg_hash_mod
spring.shardingsphere.rules.sharding.sharding-algorithms.alg_hash_mod.type=INLINE
spring.shardingsphere.rules.sharding.sharding-algorithms.alg_hash_mod.props.algorithm-expression=t_order$->{user_id % 2}
application.yml
spring:
profiles:
active: dev
application:
name: sharging-jdbc-demo
shardingsphere:
# 是否开启
datasource:
# 数据源(逻辑名字)
# 配置数据源
names: server-order0,server-order1
server-order0:
type: com.zaxxer.hikari.HikariDataSource # 数据源类型
url: jdbc:mysql://localhost:3306/course_master_db # 数据库连接地址
username: root # 用户名
password: 123456 # 密码
driver-class-name: com.mysql.jdbc.Driver # 数据库驱动
server-order1:
type: com.zaxxer.hikari.HikariDataSource # 数据源类型
url: jdbc:mysql://localhost:3306/course_db # 数据库连接地址
username: root # 用户名
password: 123456 # 密码
driver-class-name: com.mysql.jdbc.Driver # 数据库驱动
# 分片的配置
rules:
sharding:
# 表的分片策略
tables:
# 逻辑表的名称
t_order:
# 数据节点配置,采用Groovy表达式
actual-data-nodes: server-order$->{0..1}.t_order0
# 配置策略
database-strategy:
# 用于单分片键的标准分片场景
standard:
sharding-column: user_id
# 分片算法名字
sharding-algorithm-name: user_inline
key-generate-strategy: # 主键生成策略
column: id # 主键列
key-generator-name: snowflake # 策略算法名称(推荐使用雪花算法)
key-generators:
snowflake:
type: SNOWFLAKE
sharding-algorithms:
user_inline:
type: inline
props:
algorithm-expression: server-order$->{user_id % 2}
props:
# 日志显示具体的SQL
sql-show: true
测试代码
/**
* 水平分片:分库插入数据测试
*/
@Test
public void testInsertOrderDatabaseStrategy() {
for (long i = 1; i < 18; i++) {
Order order = new Order();
order.setOrderNo("ATGUIGU001");
order.setUserId(i + 1);
order.setAmount(new BigDecimal(100));
orderMapper.insert(order);
}
}