使用ShardingSphere-Proxy實現(xiàn)分表分庫
1. 環(huán)境準(zhǔn)備
- MySql 5.7
- apache-shardingsphere-4.1.1-sharding-proxy-bin.tar.gz
- jdk 1.8
- mysql-connector-java-5.1.49.jar
2. 數(shù)據(jù)庫腳本準(zhǔn)備
# 創(chuàng)建商品數(shù)據(jù)庫 CREATE DATABASE IF NOT EXISTS `products` DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci; # 創(chuàng)建商品代理數(shù)據(jù)庫 CREATE DATABASE IF NOT EXISTS `products-proxy` DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci; # 創(chuàng)建商品秒殺表 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; # 插入秒殺商品數(shù)據(jù) 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 包名稱過長導(dǎo)致解壓失敗,運行時會缺包報錯,如:
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 系統(tǒng)下通過
tar -zxvf apache-shardingsphere-4.1.1-sharding-proxy-bin.tar.gz
命令解壓
復(fù)制 mysql-connector-java-5.1.49.jar 到 ShardingSphere 的
bin
目錄中修改 conf 目錄下的
config-sharding.yaml
配置文件:# 3. 創(chuàng)建客戶端連接庫 schemaName: products-proxy # 1. 設(shè)置 MySQL 數(shù)據(jù)源 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. 設(shè)置分片規(guī)則 - 分表 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-Proxy
D:\Program\Java\apache-shardingsphere-4.1.1-sharding-proxy-bin\bin>start.bat # 通過啟動日志查看代理數(shù)據(jù)庫的默認(rèn)端口是 3307 # 新建 mysql 和 mysql-proxy 兩個連接備用
在 mysql 連接中,新建
products
和products-proxy
數(shù)據(jù)庫刷新 mysql-proxy 連接,就會看到數(shù)據(jù)庫已經(jīng)同步過來
打開 mysql-proxy 連接下的
products-proxy
數(shù)據(jù)庫,執(zhí)行創(chuàng)建 seckills 表的語句打開 mysql 連接下的
products
數(shù)據(jù)庫,就會發(fā)現(xiàn)sekills_0
和seckills_1
兩張拆分的表
分表原理解析
- 根據(jù)什么原理來分表:表的字段值
- 如何根據(jù)字段值分表:
- 取模運算(整數(shù)類型)
- hash 運算:先對字符串進行 hash 得到一個值,然后根據(jù) hash 值取模
- 范圍值:0 ~ 10000,10001 ~ 20000,...
到此這篇關(guān)于使用ShardingSphere-Proxy實現(xiàn)分表分庫的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
resultMap標(biāo)簽中里的collection標(biāo)簽詳解
這篇文章主要介紹了resultMap標(biāo)簽中里的collection標(biāo)簽,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02RabbitMQ中的Connection和Channel信道詳解
這篇文章主要介紹了RabbitMQ中的Connection和Channel信道詳解,信道是建立在 Connection 之上的虛擬連接,RabbitMQ 處理的每條 AMQP 指令都是通過信道完成的,需要的朋友可以參考下2023-08-08Springboot支持Emoji表情的實現(xiàn)方法
本文主要介紹了Springboot 支持Emoji 表情,本篇的實現(xiàn)方式是僅需后端處理,具有一定的參考價值,需要的朋友可以參考一下。2021-07-07SpringBoot?2.7.18?集成?Mybatis?Plus?+?Druid的實例詳解
Mybatis和MybatisPlus都是流行的持久層框架,MybatisPlus在Mybatis基礎(chǔ)上增加了更多便捷的功能,如自動CRUD、分頁插件等,文章還提到了Entity、Mapper、Service、Controller等組件的基本使用方法,為開發(fā)者提供了一套完整的集成方案2024-10-10Java 客戶端操作 FastDFS 實現(xiàn)文件上傳下載替換刪除功能
這篇文章主要介紹了Java 客戶端操作 FastDFS 實現(xiàn)文件上傳下載替換刪除功能,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10