Spring Boot项目中结合MyBatis实现MySQL的自动主从切换

原理解析

1. MySQL主从复制(Master-Slave Replication)
  • 工作原理:MySQL主从复制通过二进制日志(binary log)来同步数据。主服务器记录所有更改操作到二进制日志中,从服务器读取这些日志并执行相应的SQL语句来保持与主服务器的数据一致。
  • 延迟问题:由于网络传输和处理时间,从库可能会有短暂的数据滞后,这对于需要实时一致性的场景是一个挑战。
2. 读写分离
  • 目的:提高系统性能和可用性。通过将读请求分配给从库,写请求发送给主库,可以减少主库的压力,提升系统的整体性能。
  • 实现方式:可以通过数据库中间件或框架层面的配置来实现。在这个例子中,我们使用Apache ShardingSphere来实现读写分离。
3. 自动故障转移
  • 原理:当检测到主库不可用时,系统会自动选择一个从库作为新的主库,并重新调整读写分配。这通常涉及到心跳检测、状态监控等机制。
  • 工具支持:除了ShardingSphere外,还可以使用MHA(MySQL Master High Availability)或其他高可用解决方案。

实现步骤详解

1. 引入依赖

pom.xml中添加必要的依赖,包括Spring Boot Starter、MyBatis Starter以及ShardingSphere:

复制代码

xml

深色版本

复制代码
<dependencies>
    <!-- Spring Boot Starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <!-- MyBatis Starter -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.2.0</version>
    </dependency>

    <!-- ShardingSphere for read-write splitting -->
    <dependency>
        <groupId>org.apache.shardingsphere</groupId>
        <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
        <version>4.1.1</version>
    </dependency>
</dependencies>
2. 配置数据源

编辑application.yml文件以配置多个数据源,并设置ShardingSphere规则以实现读写分离:

复制代码

yaml

深色版本

复制代码
spring:
  shardingsphere:
    datasource:
      names: master,slave0,slave1
      master:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://master_host:3306/your_db?useSSL=false&serverTimezone=UTC
        username: your_username
        password: your_password
      slave0:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://slave0_host:3306/your_db?useSSL=false&serverTimezone=UTC
        username: your_username
        password: your_password
      slave1:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://slave1_host:3306/your_db?useSSL=false&serverTimezone=UTC
        username: your_username
        password: your_password
    masterslave:
      load-balance-algorithm-type: round_robin # 负载均衡策略
      name: ms_ds # 数据源名称
      master-data-source-name: master # 主库数据源名称
      slave-data-source-names: slave0,slave1 # 从库数据源名称列表
    props:
      sql:
        show: true # 是否显示SQL语句
3. 配置MyBatis

创建MyBatis Mapper接口,并使用注解或XML配置SQL语句。这里以注解方式为例:

复制代码

java

深色版本

复制代码
package com.example.demo.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface UserMapper {

    /**
     * 查询所有用户信息.
     * 注意:对于查询操作,ShardingSphere会选择其中一个从库来执行查询。
     *
     * @return 用户信息列表
     */
    @Select("SELECT * FROM users")
    List<Map<String, Object>> findAllUsers();

    /**
     * 更新用户信息.
     * 注意:写操作只会针对主库执行。
     *
     * @param id 用户ID
     * @param newName 新用户名
     */
    void updateUserById(@Param("id") Long id, @Param("newName") String newName);
}

确保你的Spring Boot应用扫描到Mapper接口。可以在主类上添加@MapperScan注解:

复制代码

java

深色版本

复制代码
package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
4. 使用Mapper进行数据库操作

现在你可以在Service层中注入并使用Mapper接口来进行数据库操作:

复制代码

java

深色版本

复制代码
package com.example.demo.service;

import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 获取所有用户信息.
     *
     * @return 用户信息列表
     */
    public List<Map<String, Object>> getAllUsers() {
        return userMapper.findAllUsers();
    }

    /**
     * 更新用户名称.
     *
     * @param id       用户ID
     * @param newName 新用户名
     */
    public void updateUserName(Long id, String newName) {
        userMapper.updateUserById(id, newName);
    }
}

注意事项及最佳实践

  • 事务管理 :确保所有写操作都在同一个事务中执行,并且只对主库进行写操作。可以使用@Transactional注解来管理事务。

  • 数据一致性:考虑到主从复制延迟的问题,在某些场景下(如刚完成写操作后立即读取),可能需要直接查询主库以保证数据一致性。

  • 健康检查:建议定期监控主从状态,确保从库同步正常以及主库可访问。可以通过定时任务或者外部工具来实现。

  • 性能优化:根据实际业务需求调整负载均衡策略,例如采用权重轮询或其他高级算法来优化查询效率。

相关推荐
MrWho不迷糊14 分钟前
Spring Boot 怎么打印日志
spring boot·后端·微服务
玛奇玛丶26 分钟前
数据库索引失效了...
后端·mysql
花千树-0101 小时前
MySQL 数据库备份和恢复全指南
数据库·mysql
·薯条大王1 小时前
Node.js 操作 MySQL 数据库
javascript·数据库·mysql
Java_SuSheng2 小时前
关于SQLite轻量数据库的研究
java·数据库·spring boot·sqlite·mybatis
Warren982 小时前
Springboot项目正常启动,访问资源却出现404错误如何解决?
java·spring boot·spring
fly spider4 小时前
一站式Windows下Docker开启MySQL并链接本地Navicat(附乱码解决方案)
windows·mysql·docker·乱码解决
Chandler245 小时前
一图掌握 MySQL 核心要点
数据库·mysql
老李不敲代码6 小时前
榕壹云无人共享系统:基于SpringBoot+MySQL+UniApp的物联网共享解决方案
spring boot·物联网·mysql·微信小程序·uni-app·软件需求
计算机学姐8 小时前
基于SpringBoo的地方美食分享网站
java·vue.js·mysql·tomcat·mybatis·springboot·美食