ShardingSphere-Proxy|ShardingSphere-Proxy 5.0 分库分表(一)

@[toc]
一、简述

  • 简述
    ShardingSphere-Proxy4.0 已经升级到5.0了,但是两者的配置文件还有一定的差别的,这篇文章讲述的就是ShardingSphere-Proxy 5.0 的落地。概念、分表、分库、分库分表的原理的基本和4.0一样的,需要了解可查看 https://blog.csdn.net/Fu_Shi_...。
  • 开发者文档
    https://shardingsphere.apache...
二、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
  • 落地
    • 条件
      1. 真实数据库
      2. 逻辑数据库
      3. ShardingSphere-Proxy5.0 配置文件
    • 步骤
      1. 新建真实数据库和表【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-Proxy|ShardingSphere-Proxy 5.0 分库分表(一)
          文章图片

2. 修改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: - root@%:***** - sharding@:sharding provider: type: ALL_PRIVILEGES_PERMITTED ```运行命令``` #在ShardingSphere-Proxy bin start.bat 端口号 ```运行结果如下:![在这里插入图片描述](https://img-blog.csdnimg.cn/8d7b5ca3c7e84edb927779bcc84da367.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAQEDnpZ7lhpw=,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)3. 新建逻辑数据连接,并执行表结构和数据脚本逻辑库执行结果如下:![在这里插入图片描述](https://img-blog.csdnimg.cn/fcb4986d5c6142b58892c09826ea715e.png#pic_center)逻辑数据库添加两条数据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); ```执行结果如下:![在这里插入图片描述](https://img-blog.csdnimg.cn/2e27942bdd1b472fb4451ead2f76c133.png#pic_center)

ShardingSphere-Proxy|ShardingSphere-Proxy 5.0 分库分表(一)
文章图片

  • 分库分表
【ShardingSphere-Proxy|ShardingSphere-Proxy 5.0 分库分表(一)】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: 1hmmsdatasources-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

  • 分片算法
    1. 取模算法
    shardingAlgorithms:#分表算法 use_MOD:#取模算法 type: MOD props: sharding-count: 2#分表数据和分表的表达式必须是一致的

  1. 范围算法
    shardingAlgorithms: use_BOUNDARY_RANGE: type: BOUNDARY_RANGE props: sharding-ranges: 2,100#多个节点是以逗号分割[是根据分的表结合配置节点] #备注 #分片键为:useidint类型 #分表的为3个表 #sharding-ranges: 2,100 是以2为节点,小于2的数据存到表0,2到99存到表1,100以上存到表3

  2. 容量算法
    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万

  3. HASH-CODE算法 【如果分片键是字符串类型,需要这种算法分表】
    shardingAlgorithms: use_HASH_MOD: type: HASH_MOD props: sharding-count: '2'#分表数量,单引号必须要加

  4. 根据时间分表算法
    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);

    执行结构如下:
    ShardingSphere-Proxy|ShardingSphere-Proxy 5.0 分库分表(一)
    文章图片

ShardingSphere-Proxy|ShardingSphere-Proxy 5.0 分库分表(一)
文章图片

ShardingSphere-Proxy|ShardingSphere-Proxy 5.0 分库分表(一)
文章图片

    推荐阅读