Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the Separation of read and write by SpringBoot

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to achieve the separation of reading and writing in SpringBoot". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to achieve reading and writing separation in SpringBoot".

Preface

According to the company's business needs, the project needs to be read-write separation, so record the process of read-write separation.

It is divided into two parts:

1. The read-write separation of the project.

Master-slave replication of the 2.mysql database.

The dependency package used in this article is sharding-jdbc-spring-boot-starter, and it is also considered to use dynamic-datasource-spring-boot-starter directly, but you need to explicitly declare the specified data source in the program, and you need to write your own algorithm to read the library when you > = 2 from the library. On the other hand, sharding-jdbc supports the load balancing strategy of reading library, and sharding will decide whether to read or write according to the keywords of the statement.

Insert selects the main library

Select chooses from Library 2 (since polling is set up, the next time is from Library 1)

1. The project introduces dependent org.apache.shardingsphere sharding-jdbc-spring-boot-starter 4.0.0-RC1 2.yml configuration

There are three machines in total

One main library (127.0.0.1)

Two sets from the library (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

# configure the load balancing policy of slave nodes, using polling mechanism

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 customers #

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 customers #

MaxPoolSize: 100

MinPoolSize: 5

3. Start

4. test

Read data for the first time (from Library 1)

Read data for the second time (from Library 2)

Main library write

The separation of project reading and writing is basically realized.

5. The problems encountered in the middle

Mysql query problem

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

Reason:

Sql that does not follow the principles will be considered illegal sql.

The column after the 1.order by must exist after the select

Nonaggregate columns that exist after 2.select, having, or order by must all exist in group by

Solution:

Modify the configuration file: vim / etc/my.cnf

Add: sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION

Restart mysql:systemctl restart mysqld

: wq

Thank you for reading, the above is the content of "how to achieve the separation of reading and writing in SpringBoot". After the study of this article, I believe you have a deeper understanding of how to achieve the separation of reading and writing in SpringBoot, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report