Use Spring for read-write separation

background

Our general application is "read more and write less" for the database, which means that the pressure on the database to read data is relatively large. One idea is to use the database cluster solution.

One of them is the main library, responsible for writing data, we call it: writing library;

Others are from the library, responsible for reading data, we call it: read library;

Then, the requirements for us are:

1. The data in the read library and the write library are consistent;

2. Write data must go to the writing library;

3. Read data must go to the reading library;

Program

There are two solutions to the separation of read and write: application layer solution and middleware solution.

Application layer resolution:

Xiaoma shares (how to use Spring to achieve read-write separation (MySQL achieves master-slave replication))

advantage:

1. It is convenient to switch between multiple data sources and is automatically completed by the program;

2. No need to introduce middleware;

3. Theoretically support any database;

Disadvantages:

1. Completed by programmers, no participation in operation and maintenance;

2. Can not add data sources dynamically;

Middleware solution

Xiaoma shares (how to use Spring to achieve read-write separation (MySQL achieves master-slave replication))

Advantages and disadvantages:

advantage:

1. The source program can realize the separation of reading and writing without any changes;

2. No need to restart the program when adding data sources dynamically;

Disadvantages:

1. The program relies on middleware, which will make it difficult to switch databases;

2. The middleware is used as a transit agent, and the performance is reduced;

Use of related middleware products:

mysql-proxy:

1. Amoeba for MySQL。

2. Use Spring to implement based on the application layer,

principle:

Xiaoma shares (how to use Spring to achieve read-write separation (MySQL achieves master-slave replication))

Before entering the Service, use AOP to make a judgment, whether to use the writing library or the reading library. The judgment basis can be judged according to the method name, such as the reading library starting with query, find, get, etc., and other writing libraries.

Guess you like

Origin blog.csdn.net/keepfriend/article/details/113849533