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

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:

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

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. If there is any infringement, please contact admin@php.cn delete
How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?Mar 17, 2025 pm 05:46 PM

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?How do I create and use custom Java libraries (JAR files) with proper versioning and dependency management?Mar 17, 2025 pm 05:45 PM

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?Mar 17, 2025 pm 05:44 PM

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?Mar 17, 2025 pm 05:43 PM

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

How does Java's classloading mechanism work, including different classloaders and their delegation models?How does Java's classloading mechanism work, including different classloaders and their delegation models?Mar 17, 2025 pm 05:35 PM

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version