溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么實(shí)現(xiàn)Sharding Sphere讀寫分離

發(fā)布時(shí)間:2021-12-20 09:21:20 來源:億速云 閱讀:304 作者:柒染 欄目:大數(shù)據(jù)

這篇文章將為大家詳細(xì)講解有關(guān)怎么實(shí)現(xiàn)Sharding Sphere讀寫分離,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

一、讀寫分離

為了確保數(shù)據(jù)庫產(chǎn)品的穩(wěn)定性,很多數(shù)據(jù)庫擁有雙機(jī)熱備功能。也就是,第一臺(tái)數(shù)據(jù)庫服務(wù)器,是對(duì)外提供增刪改查業(yè)務(wù)的生產(chǎn)服務(wù)器;第二臺(tái)數(shù)據(jù)庫服務(wù)器,僅僅接收來自第一臺(tái)服務(wù)器的備份數(shù)據(jù)(注意,不同數(shù)據(jù)庫產(chǎn)品,第一臺(tái)數(shù)據(jù)庫服務(wù)器,向第二臺(tái)數(shù)據(jù)庫服務(wù)器發(fā)送備份數(shù)據(jù)的方式不同)。當(dāng)?shù)谝慌_(tái)數(shù)據(jù)庫崩潰后,第二臺(tái)數(shù)據(jù)庫服務(wù)器,可以立即上線來代替第一臺(tái)數(shù)據(jù)庫服務(wù)器。

怎么實(shí)現(xiàn)Sharding Sphere讀寫分離

sharding-jdbc分析sql語義,實(shí)現(xiàn)路由讀寫操作到不同的數(shù)據(jù)庫服務(wù)器。

由數(shù)據(jù)庫進(jìn)行數(shù)據(jù)同步。

二、Mysql配置讀寫分離

啟動(dòng)兩個(gè)mysql實(shí)例

一個(gè)是我本機(jī)、一個(gè)是我的云服務(wù)器。

Master

Master為服務(wù)器上節(jié)點(diǎn)

vim /etc/my.cnf

怎么實(shí)現(xiàn)Sharding Sphere讀寫分離

重啟

service mysql restart

創(chuàng)建主從復(fù)制賬號(hào)

172.16.15.25 為windows的slave機(jī)器

GRANT REPLICATION SLAVE ON *.* to 'myslave'@'%' identified by 'xxxxx';

刷新權(quán)限

flush privileges;
SHOW MASTER STATUS

怎么實(shí)現(xiàn)Sharding Sphere讀寫分離

從服務(wù)器

怎么實(shí)現(xiàn)Sharding Sphere讀寫分離

STOP SLAVE
CHANGE MASTER TOMASTER_HOST = '49.232.61.212',
MASTER_USER =  'myslave',
MASTER_PASSWORD = 'xxxxx',
MASTER_LOG_FILE = 'mysql-bin.000002',
MASTER_LOG_POS = 154;
start slave;

設(shè)置成功

怎么實(shí)現(xiàn)Sharding Sphere讀寫分離

這樣就實(shí)現(xiàn)了mysql的主從配置,master庫負(fù)責(zé)增刪改操作,slave專注于查詢,實(shí)現(xiàn)了性能與數(shù)據(jù)安全的提升。

三、shardingJDBC讀寫分離

shardingJDBC不提供數(shù)據(jù)同步,我們做的是通過shardingJDBC配置,解析sql語義,實(shí)現(xiàn)讀寫分離。

注意兩個(gè)數(shù)據(jù)庫需要結(jié)構(gòu)數(shù)據(jù)一樣。(先同步)

配置

# 讀寫分離# 一個(gè)實(shí)體類可對(duì)應(yīng)兩張表spring.main.allow-bean-definition-overriding=true# sharding 多數(shù)據(jù)源配置spring.shardingsphere.datasource.names=ds1,ds2
# ds1 212Masterspring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSourcespring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.cj.jdbc.Driverspring.shardingsphere.datasource.ds1.url=jdbc:mysql://49.232.61.212:3306/sharding_1?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghaispring.shardingsphere.datasource.ds1.username=rootspring.shardingsphere.datasource.ds1.password=xxxxxxxxx
# ds2 本地slavespring.shardingsphere.datasource.ds2.type=com.alibaba.druid.pool.DruidDataSourcespring.shardingsphere.datasource.ds2.driver-class-name=com.mysql.cj.jdbc.Driverspring.shardingsphere.datasource.ds2.url=jdbc:mysql://127.0.0.1:3306/sharding_1?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghaispring.shardingsphere.datasource.ds2.username=rootspring.shardingsphere.datasource.ds2.password=gwssi
# 主從邏輯  ds為統(tǒng)一的spring.shardingsphere.sharding.master-slave-rules.ds.master-data-source-name=ds1spring.shardingsphere.sharding.master-slave-rules.ds.slave-data-source-names=ds2
# 表配置spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds.user_1

#  控制臺(tái)顯示sqlspring.shardingsphere.props.sql.show=true

怎么實(shí)現(xiàn)Sharding Sphere讀寫分離


怎么實(shí)現(xiàn)Sharding Sphere讀寫分離

關(guān)于怎么實(shí)現(xiàn)Sharding Sphere讀寫分離就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI