docker搭建mysql主從搭建及Sharding-Jdbc讀寫分離

在這裏插入圖片描述

理解讀寫分離

面對日益增加的系統訪問量,數據庫的吞吐量面臨着巨大瓶頸。 對於同一時刻有大量併發讀操作和較少寫操作類
型的應用系統來說,將數據庫拆分爲主庫和從庫,主庫負責處理事務性的增刪改操作,從庫負責處理查詢操作,能夠有效的避免由數據更新導致的行鎖,使得整個系統的查詢性能得到極大的改善。
在這裏插入圖片描述
通過一主多從的配置方式,可以將查詢請求均勻的分散到多個數據副本,能夠進一步的提升系統的處理能力。 使用多主多從的方式,不但能夠提升系統的吞吐量,還能夠提升系統的可用性,可以達到在任何一個數據庫宕機,甚至磁盤物理損壞的情況下仍然不影響系統的正常運行。

在這裏插入圖片描述
讀寫分離的數據節點中的數據內容是一致的,而水平分片的每個數據節點的數據內容卻並不相同。將水平分片和讀寫分離聯合使用,能夠更加有效的提升系統的性能。

**Sharding-JDBC讀寫分離則是根據SQL語義的分析,將讀操作和寫操作分別路由至主庫與從庫。**它提供透明化讀寫分離,讓使用方儘量像使用一個數據庫一樣使用主從數據庫集羣。

docker環境mysql主從搭建

創建目錄如下
在這裏插入圖片描述

具體腳本查看 https://github.com/fafeidou/fast-cloud-nacos/tree/master/dockerfile/mysql

到mysql目錄下執行

docker-compose build

Master Mysql

SHOW MASTER STATUS;

在這裏插入圖片描述

Slave Mysql

stop slave;
change master to master_host='mysql-master', 
master_port=3306,
master_user='root',
master_password='root', 
master_log_file='replicas-mysql-bin.000003 ',
master_log_pos=0;
start slave;
show slave status;

注意這個log_pos要和上面的master的position對應,執行完後 ,只需要 關注
Slave_IO_Running
Slave_SQL_Running
這兩個字段是否 都是 YES ,如果是 則配置完成,No的話 可以看Last_IO_ERROR 或 Last_SQL_ERROR 排錯就可以啦。

實現sharding-jdbc讀寫分離

Sharding-JDBC規則

server.port=56081

spring.application.name = fast-common-sharding-rw-separation

server.servlet.context-path = /fast-common-sharding-rw-separation
spring.http.encoding.enabled = true
spring.http.encoding.charset = UTF-8
spring.http.encoding.force = true

spring.main.allow-bean-definition-overriding = true

mybatis.configuration.map-underscore-to-camel-case = true

#sharding-jdbc分片規則配置
#數據源
spring.shardingsphere.datasource.names = m0,m1,m2,s0

spring.shardingsphere.datasource.m0.type = com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m0.driver-class-name = com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m0.url = jdbc:mysql://192.168.56.121:33065/user_db?useUnicode=true
spring.shardingsphere.datasource.m0.username = root
spring.shardingsphere.datasource.m0.password = root

spring.shardingsphere.datasource.s0.type = com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.s0.driver-class-name = com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.s0.url = jdbc:mysql://192.168.56.121:33066/user_db?useUnicode=true
spring.shardingsphere.datasource.s0.username = root
spring.shardingsphere.datasource.s0.password = root


spring.shardingsphere.datasource.m1.type = com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver-class-name = com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m1.url = jdbc:mysql://192.168.56.121:33065/order_db_1?useUnicode=true
spring.shardingsphere.datasource.m1.username = root
spring.shardingsphere.datasource.m1.password = root

spring.shardingsphere.datasource.m2.type = com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m2.driver-class-name = com.mysql.jdbc.Driver
spring.shardingsphere.datasource.m2.url = jdbc:mysql://192.168.56.121:33065/order_db_2?useUnicode=true
spring.shardingsphere.datasource.m2.username = root
spring.shardingsphere.datasource.m2.password = root

# 主庫從庫邏輯數據源定義 ds0爲user_db
spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=m0
spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names=s0

# 指定t_order表的數據分佈情況,配置數據節點 m1.t_order_1,m1.t_order_2,m2.t_order_1,m2.t_order_2
spring.shardingsphere.sharding.tables.t_order.actual-data-nodes = m$->{1..2}.t_order_$->{1..2}
#spring.shardingsphere.sharding.tables.t_user.actual-data-nodes = m$->{0}.t_user
spring.shardingsphere.sharding.tables.t_user.actual-data-nodes = ds0.t_user

# 指定t_order表的主鍵生成策略爲SNOWFLAKE
spring.shardingsphere.sharding.tables.t_order.key-generator.column=order_id
spring.shardingsphere.sharding.tables.t_order.key-generator.type=SNOWFLAKE

# 指定t_order表的分片策略,分片策略包括分片鍵和分片算法
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.sharding-column = order_id
spring.shardingsphere.sharding.tables.t_order.table-strategy.inline.algorithm-expression = t_order_$->{order_id % 2 + 1}

# 分庫策略,以user_id爲分片鍵,分片策略爲user_id % 2 + 1,user_id爲偶數操作m1數據源,否則操作m2。
spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.sharding-column= user_id
spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.algorithm-expression = t_user

# 打開sql輸出日誌
spring.shardingsphere.props.sql.show = true

# 指定t_dict爲公共表
spring.shardingsphere.sharding.broadcast-tables=t_dict

swagger.enable = true

logging.level.root = info
logging.level.org.springframework.web = info
fast.cloud.nacos.rw.separation.dao = debug
logging.level.druid.sql = debug


測試

執行testInsertUser單元測試:

在這裏插入圖片描述

通過日誌可以看出,所有寫操作落入m0數據源。
執行testSelectUserbyIds單元測試:
在這裏插入圖片描述
通過日誌可以看出,所有寫操作落入s0數據源,達到目標。

github地址: https://github.com/fafeidou/fast-cloud-nacos/tree/master/fast-common-examples/fast-common-sharding-rw-separation

遇到的問題

centos安裝docker-compose遇到的問題

https://www.cnblogs.com/eddie1127/p/12003358.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章