ShardingSphere-Proxy5搭建使用過(guò)程分析
Apache ShardingSphere 是一款開(kāi)源分布式數(shù)據(jù)庫(kù)生態(tài)項(xiàng)目,旨在碎片化的異構(gòu)數(shù)據(jù)庫(kù)上層構(gòu)建生態(tài),在最大限度的復(fù)用數(shù)據(jù)庫(kù)原生存算能力的前提下,進(jìn)一步提供面向全局的擴(kuò)展和疊加計(jì)算能力。其核心采用可插拔架構(gòu),對(duì)上以數(shù)據(jù)庫(kù)協(xié)議及 SQL 方式提供諸多增強(qiáng)功能,包括數(shù)據(jù)分片、訪問(wèn)路由、數(shù)據(jù)安全等
ShardingSphere-Proxy是跨語(yǔ)言的數(shù)據(jù)庫(kù)代理服務(wù)端,主要用來(lái)處理:分表、分庫(kù)、讀寫(xiě)分離 等。 【默認(rèn)端口 3307 】
官網(wǎng)地址:https://shardingsphere.apache.org/index_zh.html
Apache ShardingSphere下的一個(gè)產(chǎn)品,定位為透明化的數(shù)據(jù)庫(kù)代理端,與mycat類(lèi)似,所有的分片都由其完成。
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的用戶(hù) - 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. 連接測(cè)試
連接方式與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ò)張依賴(lài)需要放在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分庫(kù)分表
創(chuàng)建數(shù)據(jù)源
修改config-sharding.yaml文件添加數(shù)據(jù)源配置
databaseName: sharding_db ##邏輯庫(kù)。配置多個(gè)邏輯庫(kù)時(shí)需要?jiǎng)?chuàng)建其他的配置文件,并且配置文件格式需為config-sharding***.yaml dataSources: ## 數(shù)據(jù)源,連接真實(shí)物理庫(kù),注意物理庫(kù)必須有相應(yīng)的庫(kù)存在,負(fù)責(zé)proxy無(wú)法啟動(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í)物理庫(kù),注意物理庫(kù)必須有相應(yīng)的庫(kù)存在,負(fù)責(zé)proxy無(wú)法啟動(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: # 分庫(kù)規(guī)則 standard: # 標(biāo)準(zhǔn)類(lèi)型分片,目前官方有四種分片類(lèi)型 shardingColumn: user_id shardingAlgorithmName: alg_mod # 算法名稱(chēng) tableStrategy: # 分表規(guī)則 standard: shardingColumn: order_no shardingAlgorithmName: alg_hash_mod # 算法名稱(chēng),具體使用哪一種算法下面會(huì)根據(jù)算法名稱(chēng)配置 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ù)上面的算法名稱(chēng)配置算法的類(lèi)型和算法接收的參數(shù) alg_mod: type: MOD props: sharding-count: 2 alg_hash_mod: type: HASH_MOD props: sharding-count: 2
配置完成后重啟proxy。
連接proxy創(chuàng)建分片表
配置分片表后,并沒(méi)有生成相應(yīng)的分片表,需要連接上sharding-proxy,在proxy中執(zhí)行建表語(yǔ)句,在創(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;
插入測(cè)試數(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);
插入后,觀察物理庫(kù)的表數(shù)據(jù)存儲(chǔ)情況。
到此這篇關(guān)于ShardingSphere-Proxy5搭建使用過(guò)程的文章就介紹到這了,更多相關(guān)ShardingSphere-Proxy使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)戰(zhàn)之OutOfMemoryError異常問(wèn)題及解決方法
這篇文章主要介紹了Java實(shí)戰(zhàn)之OutOfMemoryError異常,主要結(jié)合著深入理解Java虛擬機(jī)一書(shū)當(dāng)中整理了本篇內(nèi)容,感興趣的朋友一起看看吧2022-04-04Java Synchronize下的volatile關(guān)鍵字詳解
這篇文章主要介紹了Java Synchronize下的volatile關(guān)鍵字詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03IDEA之啟動(dòng)參數(shù),配置文件默認(rèn)參數(shù)的操作
這篇文章主要介紹了IDEA之啟動(dòng)參數(shù),配置文件默認(rèn)參數(shù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01java之多線程搶火車(chē)票的實(shí)現(xiàn)示例
生活中有很多多線程的案例,購(gòu)票就是一個(gè)很常見(jiàn)的問(wèn)題,本文主要介紹了java之多線程搶火車(chē)票的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02spring-data-jpa中findOne與getOne的區(qū)別說(shuō)明
這篇文章主要介紹了spring-data-jpa中findOne與getOne的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11利用Java如何獲取Mybatis動(dòng)態(tài)生成的sql接口實(shí)現(xiàn)
MyBatis 的強(qiáng)大特性之一便是它的動(dòng)態(tài)SQL,下面這篇文章主要給大家介紹了關(guān)于利用Java如何獲取Mybatis動(dòng)態(tài)生成的sql接口實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01