经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MySQL » 查看文章
如何通过 ShardingSphere-Proxy 落地分表分库?
来源:cnblogs  作者:Run2948  时间:2021/4/12 9:47:16  对本文有异议

参考:Sharding-Proxy的基本功能使用

1. 环境准备

2. 数据库脚本准备

  1. # 创建商品数据库
  2. CREATE DATABASE IF NOT EXISTS `products` DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci;
  3. # 创建商品代理数据库
  4. CREATE DATABASE IF NOT EXISTS `products-proxy` DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci;
  5. # 创建商品秒杀表
  6. CREATE TABLE IF NOT EXISTS `seckills` (
  7. `Id` INT(11) NOT NULL AUTO_INCREMENT,
  8. `SeckillType` INT(11) NOT NULL,
  9. `SeckillName` TEXT NULL,
  10. `SeckillUrl` TEXT NULL,
  11. `SeckillPrice` DECIMAL(18, 2) NOT NULL,
  12. `SeckillStock` INT(11) NOT NULL,
  13. `SeckillPercent` TEXT NULL,
  14. `TimeId` INT(11) NOT NULL,
  15. `ProductId` INT(11) NOT NULL,
  16. `SeckillLimit` INT(11) NOT NULL,
  17. `SeckillDescription` TEXT NULL,
  18. `SeckillIstop` INT(11) NOT NULL,
  19. `SeckillStatus` INT(11) NOT NULL,
  20. PRIMARY KEY (`Id`),
  21. INDEX `ProductId` (`ProductId`)
  22. ) COLLATE = 'utf8mb4_general_ci' ENGINE = INNODB AUTO_INCREMENT = 2;
  23. # 插入秒杀商品数据
  24. INSERT INTO `seckills`(`Id`, `SeckillType`, `SeckillName`, `SeckillUrl`, `SeckillPrice`, `SeckillStock`, `SeckillPercent`, `TimeId`, `ProductId`, `SeckillLimit`, `SeckillDescription`, `SeckillIstop`, `SeckillStatus`) VALUES (1, 1, '22', 'https://img2020.cnblogs.com/blog/1191201/202007/1191201-20200720143227139-1714696954.png', 12.00, 2222, '1', 3, 1, 1, 'iphone6是最好的', 1, 1);
  25. INSERT INTO `seckills`(`Id`, `SeckillType`, `SeckillName`, `SeckillUrl`, `SeckillPrice`, `SeckillStock`, `SeckillPercent`, `TimeId`, `ProductId`, `SeckillLimit`, `SeckillDescription`, `SeckillIstop`, `SeckillStatus`) VALUES (2, 1, '22', 'https://img2020.cnblogs.com/blog/1191201/202007/1191201-20200720143227139-1714696954.png', 12.00, 2222, '1', 3, 2, 1, 'iphone6是最好的', 1, 1);
  26. INSERT INTO `seckills`(`Id`, `SeckillType`, `SeckillName`, `SeckillUrl`, `SeckillPrice`, `SeckillStock`, `SeckillPercent`, `TimeId`, `ProductId`, `SeckillLimit`, `SeckillDescription`, `SeckillIstop`, `SeckillStatus`) VALUES (3, 1, '22', 'https://img2020.cnblogs.com/blog/1191201/202007/1191201-20200720143227139-1714696954.png', 12.00, 2222, '1', 3, 3, 1, 'iphone6是最好的', 1, 1);
  27. INSERT INTO `seckills`(`Id`, `SeckillType`, `SeckillName`, `SeckillUrl`, `SeckillPrice`, `SeckillStock`, `SeckillPercent`, `TimeId`, `ProductId`, `SeckillLimit`, `SeckillDescription`, `SeckillIstop`, `SeckillStatus`) VALUES (4, 1, '22', 'https://img2020.cnblogs.com/blog/1191201/202007/1191201-20200720143227139-1714696954.png', 12.00, 2222, '1', 3, 4, 1, 'iphone6是最好的', 1, 1);

3. 配置 ShardingSphere-Proxy

  • 解压 ShardingSphere 到 apache-shardingsphere-4.1.1-sharding-proxy-bin 文件夹

    • 有些 jar 包名称过长导致解压失败,运行时会缺包报错,如:

      1. Starting the Sharding-Proxy ...
      2. Exception in thread "main" Cannot create property=orchestration for JavaBean=org.apache.shardingsphere.shardingproxy.config.yaml.YamlProxyServerConfiguration@1517365b
      3. in 'reader', line 24, column 1:
      4. orchestration:
      5. ^
      6. Type org.apache.shardingsphere.orchestration.center.yaml.config.YamlCenterRepositoryConfiguration not present
      7. in 'reader', line 25, column 3:
      8. orchestration_ds:
    • 推荐到 linux 系统下通过 tar -zxvf apache-shardingsphere-4.1.1-sharding-proxy-bin.tar.gz 命令解压

  • 复制 mysql-connector-java-5.1.49.jar 到 ShardingSphere 的 bin 目录中

  • 修改 conf 目录下的 config-sharding.yaml 配置文件:

    1. # 3. 创建客户端连接库
    2. schemaName: products-proxy
    3. # 1. 设置 MySQL 数据源
    4. dataSources:
    5. ds:
    6. url: jdbc:mysql://127.0.0.1:3306/products?serverTimezone=UTC&useSSL=false
    7. username: root
    8. password: 1010
    9. connectionTimeoutMilliseconds: 30000
    10. idleTimeoutMilliseconds: 60000
    11. maxLifetimeMilliseconds: 1800000
    12. maxPoolSize: 50
    13. # 2. 设置分片规则 - 分表
    14. shardingRule:
    15. tables:
    16. seckills: # 逻辑表名
    17. actualDataNodes: ds.seckills_${0..1} # 分 2 张表
    18. tableStrategy: # 分表策略
    19. inline:
    20. shardingColumn: ProductId # 分表字段
    21. algorithmExpression: seckills_${ProductId % 2} # 对 ProductId 取模分表
  • 修改 conf 目录下的 server.yaml 配置文件:

    1. authentication:
    2. users:
    3. root:
    4. password: 123456
    5. sharding:
    6. password: sharding
    7. authorizedSchemas: products-proxy
    8. props:
    9. max.connections.size.per.query: 1
    10. acceptor.size: 16 # The default value is available processors count * 2.
    11. executor.size: 16 # Infinite by default.
    12. proxy.frontend.flush.threshold: 128 # The default value is 128.
    13. # LOCAL: Proxy will run with LOCAL transaction.
    14. # XA: Proxy will run with XA transaction.
    15. # BASE: Proxy will run with B.A.S.E transaction.
    16. proxy.transaction.type: LOCAL
    17. proxy.opentracing.enabled: false
    18. proxy.hint.enabled: false
    19. query.with.cipher.column: true
    20. sql.show: false
    21. allow.range.query.with.inline.sharding: false
  • 启动 ShardingSphere-Proxy

    1. D:\Program\Java\apache-shardingsphere-4.1.1-sharding-proxy-bin\bin>start.bat
    2. # 通过启动日志查看代理数据库的默认端口是 3307
    3. # 新建 mysql 和 mysql-proxy 两个连接备用
  • 在 mysql 连接中,新建 productsproducts-proxy数据库

  • 刷新 mysql-proxy 连接,就会看到数据库已经同步过来

  • 打开 mysql-proxy 连接下的 products-proxy 数据库,执行创建 seckills 表的语句

  • 打开 mysql 连接下的 products 数据库,就会发现 sekills_0seckills_1 两张拆分的表

分表原理解析

  • 根据什么原理来分表:表的字段值
  • 如何根据字段值分表:
    • 取模运算(整数类型)
    • hash 运算:先对字符串进行 hash 得到一个值,然后根据 hash 值取模
    • 范围值:0 ~ 10000,10001 ~ 20000,...

原文链接:http://www.cnblogs.com/Run2948/p/ShardingSphere_Proxy_MySql.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号