St. Regis Takeaway Project - Separation of Read and Write

read-write separation

All the pressure of reading and writing is borne by one database. If the disk of the database server is damaged under high pressure, the data will be lost, which is a single point of failure.

Mysql master-slave replication

introduce

MySQL master-slave replication is an asynchronous replication process, and the bottom layer is based on the binary log function that comes with the Nysql database.

It is one or more MysQL databases (slave, that is, from the library ) to copy the log from another MySQL database (master, that is, the main library ), and then parse the log and apply it to itself, and finally realize the data from the database and the main library data remains consistent.

MySQL master-slave replication is a built-in function of the MySQL database without the need for third-party tools.

The MySQL replication process is divided into three steps:

  • The master records changes to the binary log (binary log)
  • The slave copies the master's binary log to its relay log (relay log)
  • The slave redoes events in the relay log and applies the changes to its own database

1

Configuration - Preconditions

Prepare two servers in advance, install Mysql respectively and start the service successfully

Configuration - main library Master

Step 1: Modify the configuration file /etc/my.cnf of the Mysql database (you can use find / -name my.cnf to find it)

[mysqld]
log-bin=mysql-bin # [必须]启用二进制日志
server-id=100 #[必须]服务器唯一ID

Step 2: Restart the Mysql service

systemctl restart mysqld

Step 3: Log in to the Mysql database and execute the following SQL

GRANT REPLICATION SLAVE ON *.* to 'xiaoming'@'%' identified by 'Root@123456';

Note: The function of the above SQL is to create a user xiaoming, the password is Root@123456, and grant REPLICATION SLAVE permission to user xiaoming.

It is often used to establish the user authority required for replication, that is, the slave must be authorized by the master to be a user with this authority, in order to be able to replicate through this user.

Step 4: Log in to the Mysql database, execute the following SQL, and record the values ​​​​of File and Position in the result

show master status;

Write down the binary log file name and Position

Note: The function of the above SQL is to check the status of the Master, and do not perform any operations after executing this SQL

Configuration - Slave from the library

Step 1: Modify the configuration file /etc/my.cnf of the Mysql database (ubuntu20 is /etc/mysql/mysql.conf.d/mysqld.cnf)

[mysqld]
server-id=101 #[必须]服务器唯一ID

Step 2: Restart the Mysql service

systemctl restart mysqld

using ubuntu22

service mysql restart

Step 3: Log in to the Mysql database and execute the following SQL

change master to
master_host='这里输入master的ip地址', master_user='这里输入master中有REPLICATION SLAVE权限的用户名' ,master_password='这里输入用户密码', master_log_file= '这里输入master里的二进制log文件名', master_log_pos=这里输入master里的Position;
start slave;

Step 4: Log in to the Mysql database, execute the following SQL, and check the status of the slave database

show slave status;
  • use here
change master to
master_host='192.168.5.131', master_user='小明' ,master_password='Root@123456', master_log_file= 'mysql-bin.000002', master_log_pos=154;
  • Stop the SLAVE service
STOP SLAVE IO_THREAD;

Read and write separation case

background

In the face of increasing system access, the throughput of the database is facing a huge bottleneck. For application systems with a large number of concurrent read operations and fewer write operations at the same time, the database is split into a master database and a slave database. Effectively avoid row locks caused by data updates, greatly improving the query performance of the entire system.

Introduction to Sharding-JDBC

Sharding-JDBC is positioned as a lightweight Java framework, providing additional services at the Java JDBC layer. It uses the client to directly connect to the database and provides services in the form of jar packages without additional deployment and dependencies. It can be understood as an enhanced version of the JDBC driver and is fully compatible with JDBC and various ORM frameworks.

Using Sharding-JDBC can easily realize the separation of database reading and writing in the program.

  • Applicable to any JDBC-based ORM framework, such as: JPA, Hibernate, Mybatis, Spring JDBC Template or use JDBC directly.
  • Support any third-party database connection pool, such as: DBCP, C3P0, BoneCP, Druid, HikariCP, etc.
  • Any database that implements the JDBC specification is supported. Currently supports MySQL, Oracle, SQLServer, PostgreSQL and any database that follows the SQL92 standard.
<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
    <version>4.0.O-RC1</version>
</dependency>

Use Sharding-JDBC to realize read and write separation steps:

  1. Import maven coordinates

    <dependency>
        <groupId>org.apache.shardingsphere</groupId>
        <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
        <version>4.0.0-RC1</version>
    </dependency>
    
  2. Configure read-write separation rules in the configuration file

    spring:
      shardingsphere:
        datasource:
          names:
            master,slave
          # 主数据源
          master:
            type: com.alibaba.druid.pool.DruidDataSource
            driver-class-name: com.mysql.cj.jdbc.Driver
            url: jdbc:mysql://主库ip/使用的数据库名称?characterEncoding=utf-8
            username: root
            password: root
          # 从数据源
          slave:
            type: com.alibaba.druid.pool.DruidDataSource
            driver-class-name: com.mysql.cj.jdbc.Driver
            url: jdbc:mysql://从库ip/使用的数据库名称?characterEncoding=utf-8
            username: root
            password: root
        masterslave:
          # 读写分离配置
          load-balance-algorithm-type: round_robin #多从库策略:轮询
          # 最终的数据源名称
          name: dataSource
          # 主库数据源名称
          master-data-source-name: master
          # 从库数据源名称列表,多个逗号分隔
          slave-data-source-names: slave
        props:
          sql:
            show: true #开启SQL显示在控制台输出,默认false
    
  3. Configuration in the configuration file allows bean definitions to override configuration items

    spring:
      main:
        allow-bean-definition-overriding: true
    

The project achieves read-write separation

Database environment preparation (master-slave replication)

Create the business database reggie of the St. Regis takeaway project in the main library and import related table structures and data.

code modification

Add Sharding-JDBC to the project to achieve read-write separation steps:

  1. Import maven coordinates

    <dependency>
        <groupId>org.apache.shardingsphere</groupId>
        <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
        <version>4.0.O-RC1</version>
    </dependency>
    
  2. Configure read-write separation rules in the configuration file

    spring:
      shardingsphere:
        datasource:
          names:
            master,slave
          # 主数据源
          master:
            type: com.alibaba.druid.pool.DruidDataSource
            driver-class-name: com.mysql.cj.jdbc.Driver
            url: jdbc:mysql://主库ip/使用的数据库名称?characterEncoding=utf-8
            username: root
            password: root
          # 从数据源
          slave:
            type: com.alibaba.druid.pool.DruidDataSource
            driver-class-name: com.mysql.cj.jdbc.Driver
            url: jdbc:mysql://从库ip/使用的数据库名称?characterEncoding=utf-8
            username: root
            password: root
        masterslave:
          # 读写分离配置
          load-balance-algorithm-type: round_robin #多从库策略:轮询
          # 最终的数据源名称
          name: dataSource
          # 主库数据源名称
          master-data-source-name: master
          # 从库数据源名称列表,多个逗号分隔
          slave-data-source-names: slave
        props:
          sql:
            show: true #开启SQL显示在控制台输出,默认false
    
  3. Configuration in the configuration file allows bean definitions to override configuration items

    spring:
      main:
        allow-bean-definition-overriding: true
    

Guess you like

Origin blog.csdn.net/jihuaTEL/article/details/130255550