ShardingSphere-Proxy5搭建使用過程分析
Apache ShardingSphere 是一款開源分布式數(shù)據(jù)庫生態(tài)項(xiàng)目,旨在碎片化的異構(gòu)數(shù)據(jù)庫上層構(gòu)建生態(tài),在最大限度的復(fù)用數(shù)據(jù)庫原生存算能力的前提下,進(jìn)一步提供面向全局的擴(kuò)展和疊加計(jì)算能力。其核心采用可插拔架構(gòu),對(duì)上以數(shù)據(jù)庫協(xié)議及 SQL 方式提供諸多增強(qiáng)功能,包括數(shù)據(jù)分片、訪問路由、數(shù)據(jù)安全等
ShardingSphere-Proxy是跨語言的數(shù)據(jù)庫代理服務(wù)端,主要用來處理:分表、分庫、讀寫分離 等。 【默認(rèn)端口 3307 】
官網(wǎng)地址:https://shardingsphere.apache.org/index_zh.html
Apache ShardingSphere下的一個(gè)產(chǎn)品,定位為透明化的數(shù)據(jù)庫代理端,與mycat類似,所有的分片都由其完成。
ShardingSphere-Proxy5下載安裝
官網(wǎng)提供三種安裝方式,這里主要記錄兩種
二進(jìn)制包安裝
- 官網(wǎng)下載二進(jìn)制包apache-shardingsphere-5.2.0-shardingsphere-proxy-bin.tar.gz
- 下載MySQL驅(qū)動(dòng)mysql-connector-java-8.0.22.jar(根據(jù)所使用的mysql下載對(duì)應(yīng)版本)
- 將MySQl驅(qū)動(dòng)放至shardingsphere-proxy解壓目錄中的
ext-lib目錄 - 修改配置conf/server.yaml
server.yaml是與shardingsphere-proxy服務(wù)相關(guān)的配置,這里主要配置下權(quán)限
rules:
- !AUTHORITY
users: # 配置連接shardingsphere-proxy的用戶
- root@127.0.0.1:root
- sharding@:sharding
provider: # 授權(quán)模式
type: ALL_PERMITTED # 不用授權(quán),獲取所有權(quán)限
- 啟動(dòng)shardingsphere-proxy
# windows
# 指定端口號(hào)和配置文件目錄, 默認(rèn)端口為3307
bin/start.bat ${proxy_port} ${proxy_conf_directory}
![[Pasted image 20220922095217.png]]
出現(xiàn)以上信息代表部署成功
6. 連接測試
連接方式與mysql差不多,可以用mysql命令行連接,也可以用navicat連接。
Docker 方式安裝
# 拉取鏡像 docker pull apache/shardingsphere-proxy # 啟動(dòng)臨時(shí)容器 docker run -d --name tmp --entrypoint=bash apache/shardingsphere-proxy:5.2.0 # 配置文件拷貝 docker cp tmp:/opt/shardingsphere-proxy/conf /mnt/data/shardingsphere-proxy/ #刪除臨時(shí)容器 docker rm tmp # 注意鏡像的擴(kuò)張依賴需要放在ext-lib目錄,不能直接覆蓋lib目錄 docker run --name shardingsphere-proxy -d -v /mnt/data/shardingsphere-proxy/conf:/opt/shardingsphere-proxy/conf -v /mnt/data/shardingsphere-proxy/ext-lib:/opt/shardingsphere-proxy/ext-lib -e PORT=3307 -p 33307:3307 apache/shardingsphere-proxy:5.2.0 docker run --name shardingsphere-proxy -d -v /mnt/data/shardingsphere-proxy/conf:/opt/shardingsphere-proxy/conf -e PORT=3307 -p 33307:3307 apache/shardingsphere-proxy:5.2.0
ShardingSphere-Proxy5分庫分表
創(chuàng)建數(shù)據(jù)源
修改config-sharding.yaml文件添加數(shù)據(jù)源配置
databaseName: sharding_db ##邏輯庫。配置多個(gè)邏輯庫時(shí)需要?jiǎng)?chuàng)建其他的配置文件,并且配置文件格式需為config-sharding***.yaml dataSources: ## 數(shù)據(jù)源,連接真實(shí)物理庫,注意物理庫必須有相應(yīng)的庫存在,負(fù)責(zé)proxy無法啟動(dòng)。 ds_0: url: jdbc:mysql://127.0.0.1:13307/demo_ds_0?serverTimezone=UTC&useSSL=false username: root password: sunday connectionTimeoutMilliseconds: 30000 idleTimeoutMilliseconds: 60000 maxLifetimeMilliseconds: 1800000 maxPoolSize: 50 minPoolSize: 1 ds_1: url: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false username: root password: sunday connectionTimeoutMilliseconds: 30000 idleTimeoutMilliseconds: 60000 maxLifetimeMilliseconds: 1800000 maxPoolSize: 50 minPoolSize: 1
配置分片規(guī)則
繼續(xù)修改config-sharding.yaml文件添加數(shù)據(jù)分片規(guī)則
databaseName: sharding_db
dataSources: ## 數(shù)據(jù)源,連接真實(shí)物理庫,注意物理庫必須有相應(yīng)的庫存在,負(fù)責(zé)proxy無法啟動(dòng)。
ds_0:
url: jdbc:mysql://127.0.0.1:13307/demo_ds_0?serverTimezone=UTC&useSSL=false
username: root
password: sunday
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 1
ds_1:
url: jdbc:mysql://127.0.0.1:3306/demo_ds_1?serverTimezone=UTC&useSSL=false
username: root
password: sunday
connectionTimeoutMilliseconds: 30000
idleTimeoutMilliseconds: 60000
maxLifetimeMilliseconds: 1800000
maxPoolSize: 50
minPoolSize: 1
## 分片規(guī)則配置
rules:
- !SHARDING
tables:
t_order: # 分片表
actualDataNodes: ds_${0..1}.t_order${0..1}
databaseStrategy: # 分庫規(guī)則
standard: # 標(biāo)準(zhǔn)類型分片,目前官方有四種分片類型
shardingColumn: user_id
shardingAlgorithmName: alg_mod # 算法名稱
tableStrategy: # 分表規(guī)則
standard:
shardingColumn: order_no
shardingAlgorithmName: alg_hash_mod # 算法名稱,具體使用哪一種算法下面會(huì)根據(jù)算法名稱配置
keyGenerateStrategy: # 主鍵生成規(guī)則
column: id
keyGeneratorName: snowflake
# bindingTables: # 綁定表。對(duì)于相同分片算法的表,設(shè)置綁定,避免相互關(guān)聯(lián)時(shí)產(chǎn)生笛卡爾關(guān)聯(lián)
# broadcastTables: # 廣播表
keyGenerators: # 主鍵生成規(guī)則配置
snowflake:
type: SNOWFLAKE
shardingAlgorithms: # 分片算法配置,根據(jù)上面的算法名稱配置算法的類型和算法接收的參數(shù)
alg_mod:
type: MOD
props:
sharding-count: 2
alg_hash_mod:
type: HASH_MOD
props:
sharding-count: 2
配置完成后重啟proxy。
連接proxy創(chuàng)建分片表
配置分片表后,并沒有生成相應(yīng)的分片表,需要連接上sharding-proxy,在proxy中執(zhí)行建表語句,在創(chuàng)建邏輯表時(shí)分片表會(huì)被proxy自動(dòng)按照配置的規(guī)則進(jìn)行創(chuàng)建。
CREATE TABLE `t_order` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `order_no` varchar(30) DEFAULT NULL, `user_id` bigint(20) DEFAULT NULL, `amount` decimal(10,2) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=779468255126355969 DEFAULT CHARSET=utf8mb4;
插入測試數(shù)據(jù)
INSERT INTO `sharding_db`.`t_order`(`id`, `order_no`, `user_id`, `amount`) VALUES (779468213359476737, '22', 22, 22.00); INSERT INTO `sharding_db`.`t_order`(`id`, `order_no`, `user_id`, `amount`) VALUES (779468285585391617, '44', 44, 44.00); INSERT INTO `sharding_db`.`t_order`(`id`, `order_no`, `user_id`, `amount`) VALUES (779468168534949888, '11', 11, 11.00); INSERT INTO `sharding_db`.`t_order`(`id`, `order_no`, `user_id`, `amount`) VALUES (779468255126355968, '33', 33, 33.00);
插入后,觀察物理庫的表數(shù)據(jù)存儲(chǔ)情況。
到此這篇關(guān)于ShardingSphere-Proxy5搭建使用過程的文章就介紹到這了,更多相關(guān)ShardingSphere-Proxy使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)序列化與反序列化的簡單示例
序列化與反序列化是指Java對(duì)象與字節(jié)序列的相互轉(zhuǎn)換,一般在保存或傳輸字節(jié)序列的時(shí)候會(huì)用到,下面有兩個(gè)Java實(shí)現(xiàn)序列化與反序列化的簡單示例,不過還是先來看看序列和反序列化的具體概念:2016-05-05
java 中類似js encodeURIComponent 函數(shù)的實(shí)現(xiàn)案例
這篇文章主要介紹了java 中類似js encodeURIComponent 函數(shù)的實(shí)現(xiàn)案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10
spring mvc @PathVariable綁定URI模板變量值方式
這篇文章主要介紹了spring mvc @PathVariable綁定URI模板變量值方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
基于Java代碼實(shí)現(xiàn)數(shù)字在數(shù)組中出現(xiàn)次數(shù)超過一半
這篇文章主要介紹了基于Java代碼實(shí)現(xiàn)數(shù)字在數(shù)組中出現(xiàn)次數(shù)超過一半的相關(guān)資料,需要的朋友可以參考下2016-02-02

