依赖
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding‐jdbc‐spring‐boot‐starter</artifactId>
<version>4.0.0‐RC1</version>
</dependency>
水平分表
###第一步:配置数据源
spring.shardingsphere.datasource.names = m1
#数据源m1
spring.shardingsphere.datasource.m1.type = com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver‐class‐name = com.mysql.jdbc.Driver
spring.shardingsphere.datasource.m1.url = jdbc:mysql://localhost:3306/order_db_1?useUnicode=true
spring.shardingsphere.datasource.m1.username = root
spring.shardingsphere.datasource.m1.password = root
###第二步:配置每张表 保证每张表都有对应(即使不分表)如果有分库分表 则跟着在表下面些分片策略
###指定表数据分布格式:spring.shardingsphere.sharding.tables.<逻辑表名>.actual‐data‐nodes=映射的实际表(可以唯一,那么就不用配置分片时的表达式)
###指定要分表依据字段的生成策略:spring.shardingsphere.sharding.tables.<逻辑表名>.key‐generator.column=依据字段
### spring.shardingsphere.sharding.tables.<逻辑表名>.key‐generator.type=SNOWFLAKE
###指定逻辑表分表策略:spring.shardingsphere.sharding.tables.<逻辑表名>.table‐strategy.inline.sharding‐column = 依据字段
### spring.shardingsphere.sharding.tables.<逻辑表名>.table‐strategy.inline.algorithm‐expression =表的分表表达式
#配置逻辑表t_order
### 实际表与逻辑表映射,指定t_order表的数据分布情况,配置数据节点
spring.shardingsphere.sharding.tables.t_order.actual‐data‐nodes = m1.t_order_$‐>{1..2}
### 指定t_order表的主键生成策略为SNOWFLAKE
spring.shardingsphere.sharding.tables.t_order.key‐generator.column=order_id
spring.shardingsphere.sharding.tables.t_order.key‐generator.type=SNOWFLAKE
### 分表策略,指定t_order表的分片策略,分片策略包括分片键和分片算法
spring.shardingsphere.sharding.tables.t_order.table‐strategy.inline.sharding‐column = order_id
spring.shardingsphere.sharding.tables.t_order.table‐strategy.inline.algorithm‐expression =t_order_$‐>{order_id % 2 + 1}
# 打开sql输出日志
spring.shardingsphere.props.sql.show = true
水平分库
# 定义多个数据源
spring.shardingsphere.datasource.names = m1,m2
#m1
spring.shardingsphere.datasource.m1.type = com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver‐class‐name = com.mysql.jdbc.Driver
spring.shardingsphere.datasource.m1.url = jdbc:mysql://localhost:3306/order_db_1?useUnicode=true
spring.shardingsphere.datasource.m1.username = root
spring.shardingsphere.datasource.m1.password = root
#m2
spring.shardingsphere.datasource.m2.type = com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m2.driver‐class‐name = com.mysql.jdbc.Driver
spring.shardingsphere.datasource.m2.url = jdbc:mysql://localhost:3306/order_db_2?useUnicode=true
spring.shardingsphere.datasource.m2.username = root
spring.shardingsphere.datasource.m2.password = root
#配置逻辑表t_order
### 实际表与逻辑表映射,指定t_order表的数据分布情况,配置数据节点
spring.shardingsphere.sharding.tables.t_order.actual‐data‐nodes = m$‐>{1..2}.t_order
### 指定t_order表的主键生成策略为SNOWFLAKE
spring.shardingsphere.sharding.tables.t_order.key‐generator.column=order_id
spring.shardingsphere.sharding.tables.t_order.key‐generator.type=SNOWFLAKE
### 分库策略,以order_id为分片键,分片策略为order_id% 2 + 1,order_id为偶数操作m1数据源,否则操作m2。
spring.shardingsphere.sharding.tables.t_order.database‐strategy.inline.sharding‐column = order_id
spring.shardingsphere.sharding.tables.t_order.database‐strategy.inline.algorithm‐expression =m$‐>{order_id% 2 + 1}
水平分库+分表
# 定义多个数据源
spring.shardingsphere.datasource.names = m1,m2
#m1
spring.shardingsphere.datasource.m1.type = com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver‐class‐name = com.mysql.jdbc.Driver
spring.shardingsphere.datasource.m1.url = jdbc:mysql://localhost:3306/order_db_1?useUnicode=true
spring.shardingsphere.datasource.m1.username = root
spring.shardingsphere.datasource.m1.password = root
#m2
spring.shardingsphere.datasource.m2.type = com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m2.driver‐class‐name = com.mysql.jdbc.Driver
spring.shardingsphere.datasource.m2.url = jdbc:mysql://localhost:3306/order_db_2?useUnicode=true
spring.shardingsphere.datasource.m2.username = root
spring.shardingsphere.datasource.m2.password = root
#配置逻辑表t_order
### 实际表与逻辑表映射,指定t_order表的数据分布情况,配置数据节点
spring.shardingsphere.sharding.tables.t_order.actual‐data‐nodes = m$‐>{1..2}.t_order_$‐>{1..2}
### 指定t_order表的主键生成策略为SNOWFLAKE
spring.shardingsphere.sharding.tables.t_order.key‐generator.column=order_id
spring.shardingsphere.sharding.tables.t_order.key‐generator.type=SNOWFLAKE
### 分库策略,以order_id为分片键,分片策略为order_id% 2 + 1,order_id为偶数操作m1数据源,否则操作m2。
spring.shardingsphere.sharding.tables.t_order.database‐strategy.inline.sharding‐column = order_id
spring.shardingsphere.sharding.tables.t_order.database‐strategy.inline.algorithm‐expression =
m$‐>{order_id% 2 + 1}
### 分表策略,指定t_order表的分片策略,分片策略包括分片键和分片算法
spring.shardingsphere.sharding.tables.t_order.table‐strategy.inline.sharding‐column = order_id
spring.shardingsphere.sharding.tables.t_order.table‐strategy.inline.algorithm‐expression =t_order_$‐>{order_id % 2 + 1}
垂直分表
# 垂直分表即设置两张表为绑定表 不然如果分拆开的两张表都有分片 那么在使用join查询时会查询笛卡尔积次
# 注意 设置绑定表要保证两张表分库分表在分片依据的字段与表达式都相同
# 例如:设置逻辑表product_info,product_descript为绑定表
# 分库策略(因为下面两个表的分库策略相同 所以抽离出来,配置数据源m1、m2省略)
spring.shardingsphere.sharding.default‐database‐strategy.inline.sharding‐column = tag_id
spring.shardingsphere.sharding.default‐database‐strategy.inline.algorithm‐expression = m$‐>{tag_id% 2 + 1}
# product_info分表策略,
spring.shardingsphere.sharding.tables.product_info.actual‐data‐nodes = m$‐>{1..2}.product_info_$‐>{1..2}
###注意这里关联的外键增长策略
spring.shardingsphere.sharding.tables.product_info.key‐generator.column=product_info_id
spring.shardingsphere.sharding.tables.product_info.key‐generator.type=SNOWFLAKE
###分表策略也要保持相同
spring.shardingsphere.sharding.tables.product_info.table‐strategy.inline.sharding‐column =product_info_id
spring.shardingsphere.sharding.tables.product_info.table‐strategy.inline.algorithm‐expression =product_info_$‐>{product_info_id % 2 + 1}
# product_descript分表策略
spring.shardingsphere.sharding.tables.product_descript.actual‐data‐nodes = m$‐>{1..2}.product_descript_$‐>{1..2}
#注意这里为主键增长策略
spring.shardingsphere.sharding.tables.product_descript.key‐generator.column=id
spring.shardingsphere.sharding.tables.product_descript.key‐generator.type=SNOWFLAKE
###分表策略也要保持相同
spring.shardingsphere.sharding.tables.product_descript.table‐strategy.inline.sharding‐column =product_info_id
spring.shardingsphere.sharding.tables.product_descript.table‐strategy.inline.algorithm‐expression = product_descript_$‐>{product_info_id % 2 + 1}
#绑定
spring.shardingsphere.sharding.binding‐tables[0] = product_info,product_descript
垂直分库
# 较上一步新增m0数据源,对应user_db
spring.shardingsphere.datasource.names = m0,m1,m2
#m0
spring.shardingsphere.datasource.m0.type = com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m0.driver‐class‐name = com.mysql.jdbc.Driver
spring.shardingsphere.datasource.m0.url = jdbc:mysql://localhost:3306/user_db?useUnicode=true
spring.shardingsphere.datasource.m0.username = root
spring.shardingsphere.datasource.m0.password = root
# 将不同数据库的表映射位一个数据库的逻辑表
### 将m0的t_user真实表映射为新增的逻辑表t_user(如对应的实际表只有一个 那么不用写表达式)
spring.shardingsphere.sharding.tables.t_user.actual‐data‐nodes = m$‐>{0}.t_user
spring.shardingsphere.sharding.tables.t_user.table‐strategy.inline.sharding‐column = user_id
spring.shardingsphere.sharding.tables.t_user.table‐strategy.inline.algorithm‐expression = t_user
公共表
# 指定公共表不用指定“数据库.表" 因为公共表必须每个数据库都有
# 例如:指定t_dict为公共表
spring.shardingsphere.sharding.broadcast‐tables=t_dict
读写分离
- 数据库主从同步集群需要自己搭建 sharding-jdbc只管分发 参考:https://blog.csdn.net/weixin_43934607/article/details/102762570
# 增加数据源s0,使用上面主从同步配置的从库。
spring.shardingsphere.datasource.names = m0,s0
#m0
spring.shardingsphere.datasource.m0.type = com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m0.driver‐class‐name = com.mysql.jdbc.Driver
spring.shardingsphere.datasource.m0.url = jdbc:mysql://localhost:3306/user_db?useUnicode=true
spring.shardingsphere.datasource.m0.username = root
spring.shardingsphere.datasource.m0.password = root
#s0
spring.shardingsphere.datasource.s0.type = com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.s0.driver‐class‐name = com.mysql.jdbc.Driver
spring.shardingsphere.datasource.s0.url = jdbc:mysql://localhost:3307/user_db?useUnicode=true
spring.shardingsphere.datasource.s0.username = root
spring.shardingsphere.datasource.s0.password = root
#设置逻辑数据库 用于代替下面后面【实际表与逻辑表映射时、数据库分片表达式】中使用数据库
### 设置逻辑数据库ds0代替m0、s0
### 主:spring.shardingsphere.sharding.master‐slave‐rules.<逻辑数据库>.master‐data‐source‐name=主库
### 从:spring.shardingsphere.sharding.master‐slave‐rules.<逻辑数据库>.slave‐data‐source‐names=从库
spring.shardingsphere.sharding.master‐slave‐rules.ds0.master‐data‐source‐name=m0
spring.shardingsphere.sharding.master‐slave‐rules.ds0.slave‐data‐source‐names=s0
### 设置主从集群中实际表与逻辑表映射,例如集群ds0的t_user真实表映射到逻辑表t_user分表策略
spring.shardingsphere.sharding.tables.t_user.actual‐data‐nodes = ds0.t_user
总结
- 垂直分库在现在的分布式应用中,已经保证了每个微服务之间使用不同的数据库
- 垂直分表应该在表设计阶段就考虑好,要分冷热字段和大小字段来拆分表的字段
- 当数据量超百万急需优化时,有先考虑使用缓存,如redis