最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Springboot + Mysql8實(shí)現(xiàn)讀寫分離功能

 更新時(shí)間:2019年10月12日 09:13:34   作者:張堅(jiān)  
這篇文章主要介紹了Springboot + Mysql8實(shí)現(xiàn)讀寫分離功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

在實(shí)際的生產(chǎn)環(huán)境中,為了確保數(shù)據(jù)庫(kù)的穩(wěn)定性,我們一般會(huì)給數(shù)據(jù)庫(kù)配置雙機(jī)熱備機(jī)制,這樣在master數(shù)據(jù)庫(kù)崩潰后,slave數(shù)據(jù)庫(kù)可以立即切換成主數(shù)據(jù)庫(kù),通過(guò)主從復(fù)制的方式將數(shù)據(jù)從主庫(kù)同步至從庫(kù),在業(yè)務(wù)代碼中編寫代碼實(shí)現(xiàn)讀寫分離(讓主數(shù)據(jù)庫(kù)處理 事務(wù)性增、改、刪操作,而從數(shù)據(jù)庫(kù)處理查詢操作)來(lái)提升數(shù)據(jù)庫(kù)的并發(fā)負(fù)載能力。

下面我們使用最新版本的Mysql數(shù)據(jù)庫(kù)(8.0.16)結(jié)合SpringBoot實(shí)現(xiàn)這一完整步驟(一主一從)。

安裝配置mysql

https://dev.mysql.com/downloads/mysql/頁(yè)面下載mysql安裝包,我這里下載的是mysql8.0.16 Linux-Generic.

準(zhǔn)備兩臺(tái)虛擬機(jī)用作安裝mysql,并將下載后的文件mysql-8.0.16-linux-glibc2.12-x86_64.tar.xz上傳至服務(wù)器/app/mysql

192.168.249.131 CENTOS7 主
192.168.249.129 CENTOS7 從

查看防火墻狀態(tài),如果啟動(dòng)需要先關(guān)閉防火墻

service firewalld status ## 查看防火墻狀態(tài)
service firewalld stop  ## 關(guān)閉防火墻
使用如下命令將xz文件解壓成tar文件
xz -d mysql-8.0.16-linux-glibc2.12-x86_64.tar.xz

解壓安裝包

tar -xvf mysql-8.0.16-linux-gl-ibc2.12-x86_64.tar

在/app/mysql下建立data文件夾,用于存放數(shù)據(jù)

創(chuàng)建mysql用戶組和mysql用戶

groupadd mysql                 ## 創(chuàng)建用戶組
useradd -g mysql -d /app/mysql mysql  ## 在用戶組下創(chuàng)建mysql用戶并授權(quán)相關(guān)目錄
groupdel mysql                 ## 刪除用戶組名(若報(bào)已存在相關(guān)用戶組)
userdel mysql   ## 刪除用戶(若報(bào)已存在相關(guān)用戶)

初始化安裝mysql數(shù)據(jù)庫(kù)

./mysql-8.0.16-linux-glibc2.12-x86_64/bin/mysqld --user=mysql --basedir=/app/mysql --datadir=/app/mysql/data --initialize

2019-07-01T02:05:52.681626Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2019-07-01T02:05:52.681694Z 0 [System] [MY-013169] [Server] /app/mysql/mysql-8.0.16-linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.16) initializing of server in progress as process 1479
2019-07-01T02:05:52.681726Z 0 [ERROR] [MY-010338] [Server] Can't find error-message file '/app/mysql/share/errmsg.sys'. Check error-message file location and 'lc-messages-dir' configuration directive.
2019-07-01T02:05:55.713747Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: xa6(H>rK/r<E
2019-07-01T02:05:57.303240Z 0 [System] [MY-013170] [Server] /app/mysql/mysql-8.0.16-linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.16) initializing of server has completed

注意,此時(shí)mysql會(huì)生成一個(gè)默認(rèn)的臨時(shí)密碼,如上所示,需要先保存下來(lái)然后修改

建立mysql服務(wù)并增加執(zhí)行權(quán)限

cp mysql-8.0.16-linux-glibc2.12-x86_64/support-files/mysql.server /etc/init.d/mysqld

修改mysql配置文件 vi /etc/my.cnf 增加如下配置

[mysqld]
port=3306
basedir=/app/mysql/mysql-8.0.16-linux-glibc2.12-x86_64
datadir=/app/mysql/data
socket=/tmp/mysql.sock
symbolic-links=0

[mysqld_safe]
log-error=/app/mysql/data/log/error.log
pid-file=/app/mysql/data/mysql.pid
user=mysql
tmpdir=/tmp
character_set_server=utf8
default-storage-engine=INNODB
init_connect='SET NAMES utf8'

!includedir /etc/my.cnf.d

如果報(bào)日志權(quán)限相關(guān)錯(cuò)誤,請(qǐng)先建立對(duì)應(yīng)日志文件,并給mysql用戶授權(quán)

chown -R mysql:mysql /app/mysql/data/log/error.log

啟動(dòng)mysql服務(wù)

service mysqld start

建立mysql客戶端軟連接

ln -s /app/mysql/mysql-8.0.16-linux-glibc2.12-x86_64/bin/mysql /usr/local/bin/mysql

登錄mysql修改密碼

mysql -uroot -p密碼    ## 登錄 
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '000000';

設(shè)置遠(yuǎn)程登錄

use mysql;
update user set host='%' where user='root' limit 1;
flush privileges;

配置mysql主從同步(binlog)

復(fù)制原理

  • Master將數(shù)據(jù)改變記錄到二進(jìn)制日志(binary log)中,也就是配置文件log-bin指定的文件,這些記錄叫做二進(jìn)制日志事件(binary log events)
  • Slave通過(guò)I/O線程讀取Master中的binary log events并寫入到它的中繼日志(relay log)
  • Slave重做中繼日志中的事件,把中繼日志中的事件信息一條一條的在本地執(zhí)行一次,完成數(shù)據(jù)在本地的存儲(chǔ),從而實(shí)現(xiàn)將改變反映到它自己的數(shù)據(jù)(數(shù)據(jù)重放)

復(fù)制要求

  • 主從服務(wù)器操作系統(tǒng)版本和位數(shù)一致
  • Master和Slave數(shù)據(jù)庫(kù)的版本要一致
  • Master和Slave數(shù)據(jù)庫(kù)中的數(shù)據(jù)要一致
  • Master開(kāi)啟二進(jìn)制日志,Master和Slave的server_id在局域網(wǎng)內(nèi)必須唯一

配置步驟

主數(shù)據(jù)庫(kù)(192.168.249.131)

創(chuàng)建同步用戶并授權(quán)

CREATE USER 'slave'@'192.168.249.129' IDENTIFIED WITH 'mysql_native_password' BY '000000';
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'192.168.249.129';
FLUSH PRIVILEGES;

注意這里創(chuàng)建用戶時(shí)需要選用mysql_native_password加密方式插件,否則默認(rèn)會(huì)使用caching_sha2_password加密方式,這樣在同步的時(shí)候需要使用SSL的身份進(jìn)行驗(yàn)證,為了方便簡(jiǎn)單,我們直接采用mysql_native_password方式

修改配置/etc/my.cnf,新增如下配置,開(kāi)啟binlog,并重啟mysql服務(wù)

[mysqld] # 開(kāi)啟二進(jìn)制日志功能 log-bin=mysql-bin # 設(shè)置server_id,,注意在網(wǎng)段內(nèi)要唯一 server-id=131 #(可選配置)要同步的數(shù)據(jù)庫(kù)名,要同步多個(gè)數(shù)據(jù)庫(kù),就多加幾個(gè)replicate-db-db=數(shù)據(jù)庫(kù)名 binlog-do-db=mydb #(可選配置)要忽略的數(shù)據(jù)庫(kù) binlog-ignore-db=mysql

查看主服務(wù)器狀態(tài)

show master status
file

注意看里面的參數(shù),特別前面兩個(gè)File和Position,在從服務(wù)器(Slave)配置主從關(guān)系會(huì)有用到的。

從數(shù)據(jù)庫(kù)(192.168.249.129)

修改/etc/my.cnf,新增如下配置,并重啟服務(wù)

[mysqld]
server-id=129
log-bin=mysql-bin
replicate-do-db=mydb
replicate-ignore-db=mysql

在slave中設(shè)置master信息,指定同步位置

stop slave;
change master to master_host='192.168.249.131',master_user='slave',master_password='000000',master_log_file='mysql-bin.000001',master_log_pos=155;
start slave;

參數(shù)說(shuō)明:

master_host='192.168.249.131' ## Master的IP地址
master_user='slave' ## 用于同步數(shù)據(jù)的用戶(在Master中授權(quán)的用戶)
master_password='000000' ## 同步數(shù)據(jù)用戶的密碼
master_port=3306 ## Master數(shù)據(jù)庫(kù)服務(wù)的端口
masterlogfile='mysql-bin.000001' ##指定Slave從哪個(gè)日志文件開(kāi)始讀復(fù)制數(shù)據(jù)(Master上執(zhí)行命令的結(jié)果的File字段)
masterlogpos=155 ## 從哪個(gè)POSITION號(hào)開(kāi)始讀(Master上執(zhí)行命令的結(jié)果的Position字段)
masterconnectretry=30 ##當(dāng)重新建立主從連接時(shí),如果連接建立失敗,間隔多久后重試。單位為秒,默認(rèn)設(shè)置為60秒,同步延遲調(diào)優(yōu)參數(shù)。

查看從服務(wù)器狀態(tài)

show slave status\G;

至此數(shù)據(jù)庫(kù)層面主從配置完成。

SpringBoot中配置主從讀寫分離

在主從模式下請(qǐng)遵守如下規(guī)則:
主數(shù)據(jù)庫(kù) 只執(zhí)行 INSERT,UPDATE,DELETE 操作
從數(shù)據(jù)庫(kù) 只執(zhí)行SELECT操作

我們這里使用開(kāi)源項(xiàng)目[dynamic-datasource-spring-boot-starter](https://gitee.com/baomidou/dynamic-datasource-spring-boot-starter/wikis/)作為讀寫分離的工具包

使用方法

在mydb主數(shù)據(jù)庫(kù)中建立一個(gè)簡(jiǎn)單數(shù)據(jù)表user,建好后從數(shù)據(jù)庫(kù)會(huì)自動(dòng)同步

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`account` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`position` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

引入相關(guān)依賴

<dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.0.1</version>
    </dependency>
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
      <version>2.5.5</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.15</version>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

配置數(shù)據(jù)源

spring:
 datasource:
  dynamic:
   primary: master #設(shè)置默認(rèn)的數(shù)據(jù)源或者數(shù)據(jù)源組,默認(rèn)值即為master
   strict: false #設(shè)置嚴(yán)格模式,默認(rèn)false不啟動(dòng). 啟動(dòng)后再為匹配到指定數(shù)據(jù)源時(shí)候回拋出異常,不啟動(dòng)會(huì)使用默認(rèn)數(shù)據(jù)源.
   datasource:
    master:
     type: com.zaxxer.hikari.HikariDataSource
     url: jdbc:mysql://192.168.249.131:3306/mydb?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false
     username: root
     password: '000000'
     driver-class-name: com.mysql.cj.jdbc.Driver
    slave_1:
     type: com.zaxxer.hikari.HikariDataSource
     url: jdbc:mysql://192.168.249.129:3306/mydb?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false
     username: root
     password: '000000'
     driver-class-name: com.mysql.cj.jdbc.Driver

在啟動(dòng)類入口加入mybatis掃描包

@SpringBootApplication@MapperScan("com.jianzh5.dynamic.mapper")
public class DynamicDatsourceBootstrap {  
  public static void main(String[] args) {    
    SpringApplication.run(DynamicDatsourceBootstrap.class, args);
  }
}

建立實(shí)體類User

@Data
public class User {
  private int id;
  private String account;
  private String name;
  private String position;
}

建立mapper接口文件,新增兩個(gè)方法addUser(User user),getById(int id)

public interface UserDao {
  @Insert("INSERT INTO user(account, name, position) VALUES(#{account}, #{name}, #{position})")
  @Options(useGeneratedKeys = true,keyProperty = "id")
  int addUser(User user);

  @Select("SELECT * FROM user WHERE id = #{id}")
  User getById(int id);
}

建立Service服務(wù)層相關(guān)實(shí)現(xiàn)

public interface UserService {
    int addUser(User user);
    User getById(int id);
}
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    @Override
    public int addUser(User user) {
      return userDao.addUser(user);
    }
    @DS("slave")
    @Override
    public User getById(int id) {
      return userDao.getById(id);
    }
}

由于在數(shù)據(jù)源中配置了primary: master,默認(rèn)操作都會(huì)從主庫(kù)執(zhí)行,使用注解@DS切換數(shù)據(jù)源,此注解也可直接用于類文件上,同時(shí)存在方法注解優(yōu)先于類上注解。

編寫單元測(cè)試進(jìn)行測(cè)試

public class UserServiceTest extends DynamicDatsourceBootstrapTests {
  @Autowired
  private UserService userService;
  @Test
  public void testAddUser(){
    User user = new User();
    user.setName("李四");
    user.setAccount("sili");
    user.setPosition("JAVA開(kāi)發(fā)工程師");
    int i = userService.addUser(user);
    System.out.println(user);
  }
  @Test
  public void testGetById(){
    int id = 4;
    User user = userService.getById(id);
    Assert.assertEquals("sanzhang",user.getAccount());
  }
}

通過(guò)觀察執(zhí)行日志,發(fā)現(xiàn)讀寫數(shù)據(jù)庫(kù)會(huì)根據(jù)@DS注解進(jìn)行切換,至此Springboot集成數(shù)據(jù)庫(kù)主從讀寫分離完成。

總結(jié)

以上所述是小編給大家介紹的Springboot + Mysql8實(shí)現(xiàn)讀寫分離功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

  • 詳解SpringMVC從基礎(chǔ)到源碼

    詳解SpringMVC從基礎(chǔ)到源碼

    這篇文章主要介紹了詳解SpringMVC從基礎(chǔ)到源碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • 關(guān)于Maven混合配置私有倉(cāng)庫(kù)和公共倉(cāng)庫(kù)的問(wèn)題

    關(guān)于Maven混合配置私有倉(cāng)庫(kù)和公共倉(cāng)庫(kù)的問(wèn)題

    這篇文章主要介紹了Maven混合配置私有倉(cāng)庫(kù)和公共倉(cāng)庫(kù),通過(guò)實(shí)例代碼詳細(xì)介紹了私有和公共倉(cāng)庫(kù)混合配置的方法,需要的朋友可以參考下
    2022-06-06
  • 詳解Java動(dòng)態(tài)代理的實(shí)現(xiàn)機(jī)制

    詳解Java動(dòng)態(tài)代理的實(shí)現(xiàn)機(jī)制

    這篇文章主要為大家詳細(xì)介紹了Java動(dòng)態(tài)代理的實(shí)現(xiàn)機(jī)制,感興趣的小伙伴們可以參考一下
    2016-03-03
  • Spring Boot Gradle發(fā)布war到tomcat的方法示例

    Spring Boot Gradle發(fā)布war到tomcat的方法示例

    本篇文章主要介紹了Spring Boot Gradle發(fā)布war到tomcat的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • Maven如何修改打包文件名稱

    Maven如何修改打包文件名稱

    這篇文章主要介紹了Maven如何修改打包文件名稱問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Java?Optional的使用技巧與最佳實(shí)踐

    Java?Optional的使用技巧與最佳實(shí)踐

    在?Java?中,Optional?是用于優(yōu)雅處理?null?的容器類,其核心目標(biāo)是?顯式提醒開(kāi)發(fā)者處理空值場(chǎng)景,避免?NullPointerException,本文給大家介紹Java?Optional的使用技巧,感興趣的朋友一起看看吧
    2025-04-04
  • Fluent Mybatis讓你擺脫Xml文件的技巧

    Fluent Mybatis讓你擺脫Xml文件的技巧

    Fluent-Mybatis類似于Mybatis-Plus是對(duì)Mybatis進(jìn)一步的封裝,可以只用一個(gè)實(shí)體類對(duì)象,通過(guò)代碼生成器,在編譯的過(guò)程中生成所需要的各類文件,簡(jiǎn)化了項(xiàng)目的基礎(chǔ)構(gòu)建,提高開(kāi)發(fā)效率,本文重點(diǎn)給大家介紹Fluent Mybaits讓你擺脫Xml文件的技巧,一起看看吧
    2021-08-08
  • Spring Boot配置接口WebMvcConfigurer的實(shí)現(xiàn)

    Spring Boot配置接口WebMvcConfigurer的實(shí)現(xiàn)

    這篇文章主要介紹了SpringBoot配置接口WebMvcConfigurer的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • 使用SpringCloudApiGateway之支持Cors跨域請(qǐng)求

    使用SpringCloudApiGateway之支持Cors跨域請(qǐng)求

    這篇文章主要介紹了使用SpringCloudApiGateway之支持Cors跨域請(qǐng)求的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • SpringMVC 限流的示例代碼

    SpringMVC 限流的示例代碼

    這篇文章主要介紹了SpringMVC 限流的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12

最新評(píng)論

西峡县| 山阴县| 西安市| 磴口县| 本溪市| 土默特左旗| 仁化县| 芦溪县| 容城县| 随州市| 云和县| 探索| 慈利县| 江城| 邳州市| 上思县| 彰化市| 平阳县| 麻城市| 合水县| 安新县| 肃南| 双鸭山市| 方城县| 岳池县| 泾川县| 巴林右旗| 汝南县| 连州市| 思南县| 格尔木市| 萨迦县| 腾冲县| 汉沽区| 河北省| 江津市| 府谷县| 噶尔县| 东兰县| 手游| 崇信县|