当前位置:网站首页>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 提取码:r9e0JDBC数据连接驱动下载
https://pan.baidu.com/s/1924iUe7wxGpStAzxxv2K3g 提取码:jy7zShardingSphere-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: 2server.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);执行结构如下:



边栏推荐
- Data-driven anomaly detection and early warning of item C in the May 1st mathematical modeling competition in 2021
- Wallys/MULTI-FUNCTION IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL
- Force buckle -104 Maximum depth of binary tree
- Pytorch_Geometric(PyG)使用DataLoader报错RuntimeError: Sizes of tensors must match except in dimension 0.
- puzzle(019.2)六边锁
- 8、智慧交通项目(1)
- 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
- Simple waterfall effect
- Nano data World Cup data interface, CSL data, sports data score, world cup schedule API, real-time data interface of football match
- [shared farm] smart agriculture applet, customized development and secondary development of Kaiyuan source code, which is more appropriate?
猜你喜欢

Question B of the East China Cup: how to establish a population immune barrier against novel coronavirus?

How to "transform" small and micro businesses (I)?

Modbus协议与SerialPort端口读写

Flutter dialog: cupertinoalertdialog

Applet cloud development joint table data query and application in cloud function

How much money have I made by sticking to fixed investment for 3 years?

瑞萨RA系列-开发环境搭建

MongoDB的原理、基本使用、集群和分片集群

Rxjs TakeUntil 操作符的学习笔记

Remove the mosaic, there's a way, attached with the running tutorial
随机推荐
[MySQL learning notes 20] MySQL architecture
字符串 最长公共前缀
Processing picture class library
Is GF Securities reliable? Is it legal? Is it safe to open a stock account?
在指南针上面开股票账户好不好,安不安全?
How do dating applets make millions a year? What is the profit model?
(forwarding articles) after skipping multiple pages, shuttle returns to the first page and passes parameters
ScheduleMaster分布式任务调度中心基本使用和原理
Mengyou Technology: six elements of tiktok's home page decoration, how to break ten thousand dollars in three days
Is it harder to find a job in 2020? Do a good job in these four aspects and find a good job with high salary
[project part - structure and content writing of technical scheme] software system type mass entrepreneurship and innovation project plan and Xinmiao guochuang (Dachuang) application
An auxiliary MVP architecture project quick development library -mvpfastdagger
[buuctf.reverse] 117-120
C语言刷题随记 —— 猴子吃桃
Huipay international permet au commerce électronique transfrontalier de devenir une plate - forme de paiement transfrontalière conforme!
2台三菱PLC走BCNetTCP协议,能否实现网口无线通讯?
Shuttle JSON, list, map inter transfer
8、智慧交通项目(1)
Kotlin common standard functions
How to "transform" small and micro businesses (II)?