參考:Sharding-Proxy的基本功能使用
1. 環境準備
- MySql 5.7
- apache-shardingsphere-4.1.1-sharding-proxy-bin.tar.gz
- jdk 1.8
- mysql-connector-java-5.1.49.jar
2. 資料庫腳本準備
# 創建商品資料庫
CREATE DATABASE IF NOT EXISTS `products` DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci;
# 創建商品代理資料庫
CREATE DATABASE IF NOT EXISTS `products-proxy` DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci;
# 創建商品秒殺表
CREATE TABLE IF NOT EXISTS `seckills` (
`Id` INT(11) NOT NULL AUTO_INCREMENT,
`SeckillType` INT(11) NOT NULL,
`SeckillName` TEXT NULL,
`SeckillUrl` TEXT NULL,
`SeckillPrice` DECIMAL(18, 2) NOT NULL,
`SeckillStock` INT(11) NOT NULL,
`SeckillPercent` TEXT NULL,
`TimeId` INT(11) NOT NULL,
`ProductId` INT(11) NOT NULL,
`SeckillLimit` INT(11) NOT NULL,
`SeckillDescription` TEXT NULL,
`SeckillIstop` INT(11) NOT NULL,
`SeckillStatus` INT(11) NOT NULL,
PRIMARY KEY (`Id`),
INDEX `ProductId` (`ProductId`)
) COLLATE = 'utf8mb4_general_ci' ENGINE = INNODB AUTO_INCREMENT = 2;
# 插入秒殺商品資料
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);
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);
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);
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 包名稱過長導致解壓失敗,運行時會缺包報錯,如:
Starting the Sharding-Proxy ... Exception in thread "main" Cannot create property=orchestration for JavaBean=org.apache.shardingsphere.shardingproxy.config.yaml.YamlProxyServerConfiguration@1517365b in 'reader', line 24, column 1: orchestration: ^ Type org.apache.shardingsphere.orchestration.center.yaml.config.YamlCenterRepositoryConfiguration not present in 'reader', line 25, column 3: 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組態檔:# 3. 創建客戶端連接庫 schemaName: products-proxy # 1. 設定 MySQL 資料源 dataSources: ds: url: jdbc:mysql://127.0.0.1:3306/products?serverTimezone=UTC&useSSL=false username: root password: 1010 connectionTimeoutMilliseconds: 30000 idleTimeoutMilliseconds: 60000 maxLifetimeMilliseconds: 1800000 maxPoolSize: 50 # 2. 設定分片規則 - 分表 shardingRule: tables: seckills: # 邏輯表名 actualDataNodes: ds.seckills_${0..1} # 分 2 張表 tableStrategy: # 分表策略 inline: shardingColumn: ProductId # 分表欄位 algorithmExpression: seckills_${ProductId % 2} # 對 ProductId 取模分表 -
修改 conf 目錄下的
server.yaml組態檔:authentication: users: root: password: 123456 sharding: password: sharding authorizedSchemas: products-proxy props: max.connections.size.per.query: 1 acceptor.size: 16 # The default value is available processors count * 2. executor.size: 16 # Infinite by default. proxy.frontend.flush.threshold: 128 # The default value is 128. # LOCAL: Proxy will run with LOCAL transaction. # XA: Proxy will run with XA transaction. # BASE: Proxy will run with B.A.S.E transaction. proxy.transaction.type: LOCAL proxy.opentracing.enabled: false proxy.hint.enabled: false query.with.cipher.column: true sql.show: false allow.range.query.with.inline.sharding: false -
啟動
ShardingSphere-ProxyD:\Program\Java\apache-shardingsphere-4.1.1-sharding-proxy-bin\bin>start.bat # 通過啟動日志查看代理資料庫的默認埠是 3307 # 新建 mysql 和 mysql-proxy 兩個連接備用 -
在 mysql 連接中,新建
products和products-proxy資料庫 -
重繪 mysql-proxy 連接,就會看到資料庫已經同步過來
-
打開 mysql-proxy 連接下的
products-proxy資料庫,執行創建 seckills 表的陳述句 -
打開 mysql 連接下的
products資料庫,就會發現sekills_0和seckills_1兩張拆分的表
分表原理決議
- 根據什么原理來分表:表的欄位值
- 如何根據欄位值分表:
- 取模運算(整數型別)
- hash 運算:先對字串進行 hash 得到一個值,然后根據 hash 值取模
- 范圍值:0 ~ 10000,10001 ~ 20000,...
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/274703.html
標籤:MySQL
上一篇:如何找臟資料
下一篇:資料庫---基礎知識(1)
