Home>Article>Java> Introduction to integrating Sharding-JDBC reading and writing separation in Spring Boot (with code)

Introduction to integrating Sharding-JDBC reading and writing separation in Spring Boot (with code)

不言
不言 forward
2019-03-12 15:46:33 2896browse

This article brings you an introduction to the integration of Sharding-JDBC reading and writing separation in Spring Boot (with code). It has certain reference value. Friends in need can refer to it. , hope it helps you.

Before, I used XML to configure the data source, read-write separation strategy, sub-database and sub-table strategy, etc. A friend also asked me before if there is a Spring Boot method to configure it. Since I have already used Spring Boot It feels a bit incongruous to use XML for configuration.

Actually, I personally think that as long as it is usable, easy to read, and understandable, it will be fine. Isn’t the SQL of mybatis also written in XML?

Today I will introduce to you the use of Spring Boot method, mainly explaining the configuration of reading and writing separation, and the rest will be introduced later.

The so-called Spring Boot method is to directly configure the information we mentioned above through property files or YAML files.

Mainly use the starter provided by shardingjdbc, the configuration is as follows:

 io.shardingjdbc sharding-jdbc-spring-boot-starter 2.0.0.M3 

The configuration content is as follows:

server.port=8084 mybatis.config-location=classpath:META-INF/mybatis-config.xml sharding.jdbc.datasource.names=ds_master,ds_slave # 主数据源 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 # 从数据源 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 # 读写分离配置 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

Load balancing algorithm during query. There are currently two algorithms, round_robin (polling) and random (random). The algorithm interface is io.shardingjdbc.core.api.algorithm.masterslave .MasterSlaveLoadBalanceAlgorithm. The implementation classes include 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, separate multiple ones with commas

It’s that simple. The whole process is over. Here is the code test The effect of separation of reading and writing is obvious. I use mybatis here. The code is on my Github. I won’t post it in the article because everyone knows it.


The above is the detailed content of Introduction to integrating Sharding-JDBC reading and writing separation in Spring Boot (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete