Detailed explanation of Sharding JDBC read write separation example integrated in Spring Boot

  • 2021-07-03 00:12:22
  • OfStack

In my book "Spring Cloud Microservice-Full Stack Technology and Case Analysis", the use of Sharding-JDBC is explained in detail in Chapter 18.

Before, I used XML to configure data sources, read-write separation strategy, sub-library and sub-table strategy, etc. Some friends also asked me if there was Spring Boot to configure them. Now that I have used Spring Boot and XML to configure them, it feels a little incongruous.

Actually, I personally think that as long as it can be used, it is convenient to read and understand, SQL of mybatis is also written in XML.

Today, I will introduce the use of Spring Boot mode, mainly talking about the configuration of separation of interpretation and writing, and the rest will be introduced later.

The so-called Spring Boot mode means that the information mentioned above can be configured directly through the properties file or YAML file.

starter provided by shardingjdbc is mainly used, and the configuration is as follows:


<dependency>
  <groupId>io.shardingjdbc</groupId>
  <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
  <version>2.0.0.M3</version>
</dependency>

The configuration is as follows:


server.port=8084

mybatis.config-location=classpath:META-INF/mybatis-config.xml

sharding.jdbc.datasource.names=ds_master,ds_slave

#  Master data source 
sharding.jdbc.datasource.ds_master.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.ds_master.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.ds_master.url=jdbc:mysql://localhost:3306/ds_0?characterEncoding=utf-8
sharding.jdbc.datasource.ds_master.username=root
sharding.jdbc.datasource.ds_master.password=123456

#  From a data source 
sharding.jdbc.datasource.ds_slave.type=com.alibaba.druid.pool.DruidDataSource
sharding.jdbc.datasource.ds_slave.driver-class-name=com.mysql.jdbc.Driver
sharding.jdbc.datasource.ds_slave.url=jdbc:mysql://localhost:3306/ds_1?characterEncoding=utf-8
sharding.jdbc.datasource.ds_slave.username=root
sharding.jdbc.datasource.ds_slave.password=123456

#  Read-write separation configuration 
sharding.jdbc.config.masterslave.load-balance-algorithm-type=round_robin
sharding.jdbc.config.masterslave.name=dataSource
sharding.jdbc.config.masterslave.master-data-source-name=ds_master
sharding.jdbc.config.masterslave.slave-data-source-names=ds_slave
sharding.jdbc.config.masterslave.load-balance-algorithm-type

At present, there are two load balancing algorithms for query, round_robin (polling) and random (random). The algorithm interface is io. shardingjdbc. core. algorithm. masterslave. MasterSlaveLoadBalanceAlgorithm. The implementation classes are RandomMasterSlaveLoadBalanceAlgorithm and RoundRobinMasterSlaveLoadBalanceAlgorithm.

sharding.jdbc.config.masterslave.master-data-source-name

Master data source name

sharding.jdbc.config.masterslave.slave-data-source-names

From the data source name, multiple are separated by commas

It's as simple as that. The whole process is over. The following is the effect of writing code to test the separation of reading and writing. I use mybatis here, and the code is on my Github. It won't be posted in the article. Everyone will.

Reference code: https://github.com/yinjihuan/spring-cloud/tree/master/fangjia-sjdbc-read-write-springboot


Related articles: