一、简述
- 简述
ShardingSphere-Proxy4.0 已经升级到5.0了,但是两者的配置文件还有一定的差别的,这篇文章讲述的就是ShardingSphere-Proxy 5.0 的落地。概念、分表、分库、分库分表的原理的基本和4.0一样的,需要了解可查看 https://blog.csdn.net/Fu_Shi_rong/article/details/123541413?spm=1001.2014.3001.5501。 - 开发者文档
https://shardingsphere.apache.org/document/current/cn/dev-manual/
二、ShardingSphere-Proxy5.0 落地
-
范围算法 shardingAlgorithms:
use_BOUNDARY_RANGE:
type: BOUNDARY_RANGE
props:
sharding-ranges: 2,100
-
容量算法 shardingAlgorithms:
use_VOLUME_RANGE:
type: VOLUME_RANGE
props:
range-lower: '20000000'
range-upper: '40000000'
sharding-volume: '20000000'
最小值为2000万,也就是说表数据量小于等于2000万,最大数量为4000万
表与表的间隔为2000万
比如说分了两张表:
1-2000万 存到表0
2001万-4000万 存到表1
是根据分表的数量来定义最大值的
分了三张表,那最大值为6000万
-
HASH-CODE算法 【如果分片键是字符串类型,需要这种算法分表】 shardingAlgorithms:
use_HASH_MOD:
type: HASH_MOD
props:
sharding-count: '2' #分表数量,单引号必须要加
-
根据时间分表算法 shardingAlgorithms:
use_AUTO_INTERVAL:
type: AUTO_INTERVAL
props:
datetime-lower: '2020-01-01 23:59:59'
datetime-upper: '2022-12-31 23:59:59'
# 以1年度为单位进行划分
sharding-seconds: '31536000'
# 以1个月为单位进行划分
#sharding-seconds: '2678400'
# 以1天为单位进行划分
#sharding-seconds: '86400'
#设置的最大值必须和分多少表匹配才行,否者报错,找不到表
-
分布式ID config-sharding.yaml # 3、创建客户端连接库
schemaName: hmms
#1、连接mysql
dataSources:
hmmsdatasources-0:
url: jdbc:mysql://localhost:3306/hmms?serverTimezone=UTC&useSSL=false
username: root
password: 1QAZ2WSX3EDC
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 1
# 2、分片规则
rules:
- !SHARDING
tables:
user:
actualDataNodes: hmmsdatasources-0.user-${0..1}
tableStrategy:
standard:
shardingColumn: id
shardingAlgorithmName: use_HASH_MOD
keyGenerateStrategy:
column: id
keyGeneratorName: snowflake
shardingAlgorithms:
use_MOD:
type: MOD
props:
sharding-count: 2
use_HASH_MOD:
type: HASH_MOD
props:
sharding-count: '2'
keyGenerators:
snowflake:
type: SNOWFLAKE
props:
worker-id: 123
表结构sql语句 SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` varchar(100),
`useid` int(11) NOT NULL,
`usenam` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '登录名',
`usepwd` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '登录密码',
`usestate` int(11) NULL DEFAULT 2 COMMENT '-1:删除1:注销 2:正常 3:挂失',
`usekey` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户秘钥',
`usetel` varchar(11) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户手机',
`createbyid` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '添加人',
`createbytime` datetime(0) NULL DEFAULT NULL COMMENT '添加时间',
`modifybyid` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '修改人',
`modifybytime` datetime(0) NULL DEFAULT NULL COMMENT '修改时间',
PRIMARY KEY (`useid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
数据填充语句 INSERT INTO `user`(useid,usenam,usepwd,usestate,usekey,usetel,createbyid,createbytime,modifybyid,modifybytime) VALUES (1, 'admin', '202CB962AC59075B964B07152D234B70', 2, '123', '123123', 'xiaogang', '2021-08-25 20:12:15', 'xiaogang', NULL);
执行结构如下:
|