当前位置:网站首页>ShardingSphere-Proxy 5.0 分库分表(一)
ShardingSphere-Proxy 5.0 分库分表(一)
2022-06-25 09:37:00 【@@Mr.Fu】
一、简述
- 简述
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 落地
环境
JAVA JDK下载
https://pan.baidu.com/s/1A-ksNN0YicT3hXjFscGGwA 提取码:r9e0
JDBC数据连接驱动下载
https://pan.baidu.com/s/1924iUe7wxGpStAzxxv2K3g 提取码:jy7z
ShardingSphere-Proxy 5.0 下载、
https://archive.apache.org/dist/shardingsphere/5.0.0/
Window 11
落地
条件
- 真实数据库
- 逻辑数据库
- ShardingSphere-Proxy5.0 配置文件
步骤
新建真实数据库和表【hmms.user 表为例】
数据库表结构语句
SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for user -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `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;
运行结果如下:
修改ShardingSphere-Proxy5.0 配置文件,并连接
config-sharding.yaml
# 3、创建客户端连接库 schemaName: hmms #1、连接mysql dataSources: hmmsdatasources-0: url: jdbc:mysql://localhost:3306/hmms?serverTimezone=UTC&useSSL=false username: root password: **** connectionTimeoutMilliseconds: 30000 idleTimeoutMilliseconds: 60000 maxLifetimeMilliseconds: 1800000 maxPoolSize: 50 minPoolSize: 1 # 2、分片规则 rules: - !SHARDING tables: user: #表名 actualDataNodes: hmmsdatasources-0.user-${0..1} #分表规则 tableStrategy: standard: shardingColumn: useid #分片键 shardingAlgorithmName: use_MOD #分表算法 shardingAlgorithms: #分表算法 use_MOD: #取模算法 type: MOD props: sharding-count: 2
server.yaml
rules: - !AUTHORITY users: - [email protected]%:***** - [email protected]:sharding provider: type: ALL_PRIVILEGES_PERMITTED
运行命令
#在ShardingSphere-Proxy bin start.bat 端口号
运行结果如下:
新建逻辑数据连接,并执行表结构和数据脚本
逻辑库执行结果如下:
逻辑数据库添加两条数据
SQL语句
INSERT INTO `user` VALUES (1, 'admin', '202CB962AC59075B964B07152D234B70', 2, '123', '123123', 'xiaogang', '2021-08-25 20:12:15', 'xiaogang', NULL); INSERT INTO `user` VALUES (2, 'admin', '202CB962AC59075B964B07152D234B70', 2, '123', '123123', 'xiaogang', '2021-08-25 20:12:15', 'xiaogang', NULL);
执行结果如下:
分库分表
ShardingSphere-Proxy 5.0 配置# 3、创建客户端连接库 schemaName: hmms #1、连接mysql dataSources: hmmsdatasources-0: url: jdbc:mysql://主机地址:端口号/hmms-0?serverTimezone=UTC&useSSL=false username: root password: 密码 connectionTimeoutMilliseconds: 30000 idleTimeoutMilliseconds: 60000 maxLifetimeMilliseconds: 1800000 maxPoolSize: 50 minPoolSize: 1 hmmsdatasources-1: url: jdbc:mysql://主机地址:端口号/hmms-1?serverTimezone=UTC&useSSL=false username: root password: 密码 connectionTimeoutMilliseconds: 30000 idleTimeoutMilliseconds: 60000 maxLifetimeMilliseconds: 1800000 maxPoolSize: 50 minPoolSize: 1 # 2、分片规则 rules: - !SHARDING tables: user: actualDataNodes: hmmsdatasources-${0..1}.user-${0..1} tableStrategy: standard: shardingColumn: useid shardingAlgorithmName: use_MOD databaseStrategy: #分库规则 standard: shardingColumn: useid shardingAlgorithmName: use_MOD keyGenerateStrategy: column: useid 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
分片算法
- 取模算法
shardingAlgorithms: #分表算法 use_MOD: #取模算法 type: MOD props: sharding-count: 2 #分表数据和分表的表达式必须是一致的
范围算法
shardingAlgorithms: use_BOUNDARY_RANGE: type: BOUNDARY_RANGE props: sharding-ranges: 2,100 #多个节点是以逗号分割 [是根据分的表结合配置节点] #备注 #分片键为:useid int类型 #分表的为3个表 #sharding-ranges: 2,100 是以2为节点,小于2的数据存到表0,2到99存到表1,100以上存到表3
容量算法
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);
执行结构如下:
边栏推荐
- Etcd教程 — 第四章 Etcd集群安全配置
- Kotlin Foundation
- ShardingSphere-Proxy 4.1 分库分表
- Applet cloud development joint table data query and application in cloud function
- puzzle(019.2)六边锁
- Wechat official account can reply messages normally, but it still prompts that the service provided by the official account has failed. Please try again later
- Jetpack compose layout (III) - custom layout
- 【mysql学习笔记20】mysql体系结构
- Processing picture class library
- Creo makes a mobius belt in the simplest way
猜你喜欢
Data-driven anomaly detection and early warning of 21 May Day C
CyCa 2022 children's physical etiquette primary teacher class Shenzhen headquarters station successfully concluded
Creo makes a mobius belt in the simplest way
Kotlin advanced generic
Redis(二)分布式锁与Redis集群搭建
Rxjs TakeUntil 操作符的学习笔记
Why should the terminal retail industry choose the member management system
可穿戴设备或将会泄露个人隐私
Remove the mosaic, there's a way, attached with the running tutorial
汇付国际为跨境电商赋能:做合规的跨境支付平台!
随机推荐
51 SCM time stamp correlation function
The problem of wirengpi program running permission
Can two Mitsubishi PLC adopt bcnettcp protocol to realize wireless communication of network interface?
ScheduleMaster分布式任务调度中心基本使用和原理
Guiding principle - read source code
Reasons for Meiye to choose membership system
Etcd教程 — 第四章 Etcd集群安全配置
Test Development Engineer
The way that flutter makes the keyboard disappear (forwarding from the dependent window)
Creating a binary tree (binary linked list) from a generalized table
Can two Mitsubishi PLC adopt bcnettcp protocol to realize wireless communication of network interface?
The problem of automatic page refresh after the flyer WebView pops up the soft keyboard
2台三菱PLC走BCNetTCP协议,能否实现网口无线通讯?
oracle 函数 触发器
Cocopod error failed: undefined method `map 'for nil:nilclass
Vscode attempted to write the procedure to a pipeline that does not exist
vscode试图过程写入管道不存在
203 postgraduate entrance examination Japanese self-study postgraduate entrance examination experience post; Can I learn Japanese by myself?
[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate
瑞吉外卖项目(二)