Realize the read-write separation of mysql database through shardingsphere

In the previous document, mysql master-slave databases were deployed through docker:

Deploy mysql master-slave architecture in docker mode_ Repair Baron's blog - CSDN blog

This paper introduces how to connect and use mysql master-slave database in java code to realize the separation of reading and writing.

mybatis-plusPersistence layer framework

shardingsphere, one of the three core suites, is positioned as a plug-in used on the client.

It can realize the functions of reading and writing separation, database and table.

Introduction to shardingsphere

The three core suites of shardingsphere are: sharding JDBC, sharding proxy and sharding sidecar

At present, sharding JDBC and sharding proxy are more important. Sharding JDBC is used more.

Sharding JDBC: it is a plug-in that provides services in the form of jar package, so it can be used directly after being introduced by the client.

Basic principle: it will rewrite your SQL, then request the database and merge the results, so as to realize the functions of separation of reading and writing, database and table. The text uses {sharding JDBC.

Sharding proxy: positioned as a middleware. It can be understood that it is disguised as a MySQL database. When the client uses it, it does not connect to the actual mysql, but instead to the sharding proxy. The implementation of read-write separation and database and table separation is left to sharding proxy, which does not need to be paid attention to in the client code. It needs to be deployed separately.

Introducing maven dependency

<!-- mysql-connector -->
<!-- mybatis-plus -->
<!-- sharding-jdbc -->

Among them, springboot uses 2.5 X version.  

Configure master-slave data sources

The more important thing of read-write separation is the configuration of master-slave data sources, which can be easily realized through shardingsphere:

    # Data source configuration
      names: master,slave
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://
        username: root
        password: 123456
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://
        username: root
        password: 123456
    # Set master-slave polling policy
      name: masterslave
      load-balance-algorithm-type: round_robin
      master-data-source-name: master
      slave-data-source-names: slave
    # Print SQL
        show: true

Well, it's actually over here. You can start using it. It's quite simple!

Note: the data source name here should not be marked with strange symbols (for example, the middle line is not OK), otherwise it will not be resolved!

Test read write separation

1) Write Mapper

Don't talk about creating tables and writing entity classes. Paste Mapper code. Because mybatis plus is used, there is no need to write any code. It is empty:

public interface ItemMapper extends BaseMapper<Item> {


2) Test read data

void get() {
	Item item = itemMapper.selectById(1);

The result is obvious. The slave library is used:

3) Test write data

void update() {
	Item item = new Item();
	item.setName("Springboot Source code");

The result is also obvious. The master library is used:

Of course, the master library will also be used for insert and delete operations.

Keywords: Java MySQL Middleware

Added by Muddy_Funster on Thu, 10 Mar 2022 07:31:02 +0200