SoFunction
Updated on 2025-04-11

How to configure Master-slave Read and Write Separation Mode of Sharding-Jdbc

Sharding-Jdbc configures master-slave mode

The master-slave mode of reading and writing separation of our project can generally be implemented in multiple ways. We can manually configure multiple data sources in code Java to achieve reading and writing separation mode. We can also use a third-party framework to achieve reading and writing separation, such as ourSharding-jdbc, it can be oursMyCatto realize read and write separation.

  1. Java code to configure multiple data sources
  2. Sharding-Jdbc
  3. MyCat

Today we will explain how to use itSharding-jdbcto achieve read and write separation.

useYMALto implement configuration.

ConfigurationmasterData source andslaveData source. This time, our plan is to achieve reading and writing separation in a one-master and multiple slave way.mysqlWe won’t talk about the master and slave construction this time.

This time we are mainly doing itsharding-jdbcto realize read-write separation

Sharding-jdbc read-write separation configuration

We need to introduceMAVENrely

<dependency>
    <groupId></groupId>
    <artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
    <version>5.1.1</version>
</dependency>

The version we are using this time is a relatively new version.

Configure the data source and the name of the data source

spring:
  shardingsphere:
  	# Memory mode    mode:
      type: Memory
    # Data source configuration    datasource:
      names: master,slave
      #Main Data Source      master:
        type: 
        driver-class-name: 
        jdbc-url: jdbc:mysql://localhost:3306/shardingjdbc?allowPublicKeyRetrieval=true
        username: root
        password: 123456
      # Slave data source      slave:
        type: 
        driver-class-name: 
        jdbc-url: jdbc:mysql://localhost:3306/shardingjdbc?allowPublicKeyRetrieval=true
        username: root
        password: 123456

It mainly configures the link address, account and password of the data source.

Strategy for separation of read and write

spring:
  shardingsphere:
    rules:
      readwrite-splitting:
        data-sources:
          mydatasource:
          	# Is the type static or dynamic            type: Static
            props:
              # Write data              write-data-source-name: master
              # Read data              read-data-source-names: slave
            # Logical strategies used            load-balancer-name: round-alg
        # Load balancing strategy        load-balancers:
        	# Invest            round-alg:
              type: ROUND_ROBIN

Here is the data source used to configure the writing and reading data

Complete configuration

spring:
  shardingsphere:
    mode:
      type: Memory
    # Data source configuration    datasource:
      names: master,slave
      #Main Data Source      master:
        type: 
        driver-class-name: 
        jdbc-url: jdbc:mysql://localhost:3306/shardingjdbc?allowPublicKeyRetrieval=true
        username: root
        password: 123456
      # Slave data source      slave:
        type: 
        driver-class-name: 
        jdbc-url: jdbc:mysql://localhost:3306/shardingjdbc?allowPublicKeyRetrieval=true
        username: root
        password: 123456
    rules:
      readwrite-splitting:
        data-sources:
          mydatasource:
          	# Is the type static or dynamic            type: Static
            props:
              write-data-source-name: master
              read-data-source-names: slave
            # Logical strategies used            load-balancer-name: round-alg
        # Load balancing strategy        load-balancers:
        	# Invest            round-alg:
              type: ROUND_ROBIN
            # Random            random-alg:
              type: RANDOM
            # Weight, when configuring weights, we must configure the weight values ​​of each slave, the data type of the value is double            weight-alg:
              type: WEIGHT
              props:
                slave: 1.0

    props:
      show-sql: true

Finish!

I encountered a while I startedmysqlofConnection exception

Public Key Retrieval is not allowed

In this case, you can set the connection parameters when configuring the parameters?allowPublicKeyRetrieval=true

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.