SpringBoot詳解如何實(shí)現(xiàn)讀寫分離
前言
根據(jù)公司業(yè)務(wù)需求,項(xiàng)目需要讀寫分離,所以記錄下讀寫分離的過程。
分為兩個(gè)部分:
1.項(xiàng)目的讀寫分離。
2.mysql數(shù)據(jù)庫(kù)的主從復(fù)制。
本篇使用的依賴包為sharding-jdbc-spring-boot-starter,也有考慮直接用dynamic-datasource-spring-boot-starter,但是需要在程序中顯式的聲明所指定的數(shù)據(jù)源,并且在從庫(kù)>=2 的時(shí)候需要自己寫算法進(jìn)行讀庫(kù)的選擇。而sharding-jdbc支持讀庫(kù)的負(fù)載均衡策略,sharding會(huì)根據(jù)語句的關(guān)鍵字來決定是讀操作還是寫操作
Insert選擇主庫(kù)

Select選擇從庫(kù)2(由于設(shè)置的了輪詢,所以下一次就是從庫(kù)1)

1.項(xiàng)目引入依賴
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>
2.yml配置
共有三臺(tái)機(jī)器,
主庫(kù)一臺(tái)(127.0.0.1)
從庫(kù)兩臺(tái)(192.168.1.5 192.168.1.6)
spring:
shardingsphere:
props:
sql:
show: false
sharding:
default-data-source-name: master
masterslave:
name: ms
master-data-source-name: master
slave-data-source-names: slave1,slave2
#配置slave節(jié)點(diǎn)的負(fù)載均衡均衡策略,采用輪詢機(jī)制
load-balance-algorithm-type: round_robin
datasource:
names: master,slave1,slave2
master:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/life_account_db?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: root
maxPoolSize: 100
minPoolSize: 5
slave1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.1.5:3306/life_account_db?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
username: test
password: Houxuyang123!@#
maxPoolSize: 100
minPoolSize: 5
slave2:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.1.6:3306/life_account_db?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
username: test
password: Houxuyang123!@#
maxPoolSize: 100
minPoolSize: 5
3.啟動(dòng)

4.測(cè)試
第一次讀數(shù)據(jù)(從庫(kù)1)



第二次讀數(shù)據(jù)(從庫(kù)2)



主庫(kù)寫



項(xiàng)目讀寫分離基本實(shí)現(xiàn)。
5.中間所遇到的問題
mysql查詢問題
Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'life_account_db.acc_order.serial_no' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
原因:
沒有遵循原則的sql會(huì)被認(rèn)為是不合法的sql
1.order by后面的列必須是在select后面存在的
2.select、having或order by后面存在的非聚合列必須全部在group by中存在
解決方法:
修改配置文件:vim /etc/my.cnf
添加:sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
重啟mysql:systemctl restart mysqld

:wq
到此這篇關(guān)于SpringBoot詳解如何實(shí)現(xiàn)讀寫分離的文章就介紹到這了,更多相關(guān)SpringBoot讀寫分離內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot基于Mybatis mysql實(shí)現(xiàn)讀寫分離
- Springboot + Mysql8實(shí)現(xiàn)讀寫分離功能
- SpringBoot+Mybatis-Plus實(shí)現(xiàn)mysql讀寫分離方案的示例代碼
- SpringBoot整合sharding-jdbc實(shí)現(xiàn)分庫(kù)分表與讀寫分離的示例
- springboot結(jié)合mysql主從來實(shí)現(xiàn)讀寫分離的方法示例
- SpringBoot整合MyCat實(shí)現(xiàn)讀寫分離的方法
- 使用springboot aop來實(shí)現(xiàn)讀寫分離和事物配置
- SpringBoot自定義注解使用讀寫分離Mysql數(shù)據(jù)庫(kù)的實(shí)例教程
- springboot多數(shù)據(jù)源配合docker部署mysql主從實(shí)現(xiàn)讀寫分離效果
- SpringBoot+MyBatis+AOP實(shí)現(xiàn)讀寫分離的示例代碼
- SpringBoot項(xiàng)目中如何實(shí)現(xiàn)MySQL讀寫分離詳解
- SpringBoot配置主從數(shù)據(jù)庫(kù)實(shí)現(xiàn)讀寫分離
相關(guān)文章
java實(shí)現(xiàn)pdf文件截圖的方法【附PDFRenderer.jar下載】
這篇文章主要介紹了java實(shí)現(xiàn)pdf文件截圖的方法,結(jié)合實(shí)例形式分析了java基于PDFRenderer.jar進(jìn)行pdf文件截圖的相關(guān)操作技巧,并附帶PDFRenderer.jar文件供讀者下載使用,需要的朋友可以參考下2018-01-01
SpringBoot使用CommandLineRunner接口完成資源初始化方式
這篇文章主要介紹了SpringBoot使用CommandLineRunner接口完成資源初始化方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
Springboot使用POI進(jìn)行excel文件的導(dǎo)出與下載方式
這篇文章主要介紹了Springboot使用POI進(jìn)行excel文件的導(dǎo)出與下載方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Spring學(xué)習(xí)筆記之bean的基礎(chǔ)知識(shí)
ean在Spring和SpringMVC中無所不在,將這個(gè)概念內(nèi)化很重要,所以下面這篇文章主要給大家介紹了關(guān)于Spring學(xué)習(xí)筆記之bean基礎(chǔ)的相關(guān)資料,文中通過示例代碼介紹的非常詳解,需要的朋友可以參考下。2017-12-12
基于java ssm springboot實(shí)現(xiàn)選課推薦交流平臺(tái)系統(tǒng)
這篇文章主要介紹了選課推薦交流平臺(tái)系統(tǒng)是基于java ssm springboot來的實(shí)現(xiàn)的,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08

