SpringBoot+MyBatis-Flex配置ProxySQL的实现步骤

2025-02-14 18:50

本文主要是介绍SpringBoot+MyBatis-Flex配置ProxySQL的实现步骤,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

《SpringBoot+MyBatis-Flex配置ProxySQL的实现步骤》本文主要介绍了SpringBoot+MyBatis-Flex配置ProxySQL的实现步骤,文中通过示例代码介绍的非常详...

 目标

  • 在 Spring Boot 中连接 ProxySQL。
  • 使用 MyBatis-Flex 访问数据库
  • 配置 读写分离 和 延迟检测。

 步骤 1:确保 ProxySQL 和 MySQL 主从同步已正确配置

首先,确保您已经正确配置了 ProxySQL 和 MySQL 主从同步。

ProxySQL 的默认配置

Hostgroup ID描述
10主库(写操作)
20从库(读操作)

ProxySQL 的数据端口

  • 端口:6033(默认数据接口端口)

 步骤 2:在 Spring Boot 项目中引入依赖

引入 MyBatis-Flex 的依赖

在 pom.xml 中添加以下依赖:

<dependency>
    <groupId>com.mybatisflex</groupId>
    <artifactId>mybatis-flex-spring-boot-starter</artifactId>
    &pythonlt;version>1.4.3</version>
</dependency>

 步骤 3:配置 application.yml

在 application.yml 文件中配置 ProxySQL 的数据源。

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:6033/db_order_plus?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
    username: root
    password: root
  mybatis-flex:
    mapper-locations: classpath:mapper/**/*Mapper.xml
    configuration:
      map-underscore-to-camel-case: true

 步骤 4:配置数据源的读写分离

在 ProxySQL 中配置 读写分离规则:

在 ProxySQL 管理界面执行以下 SQL

INSERT INTO mysql_query_rules (rule_id, active, match_pattern, destination_hostgroup)
VALUES (1, 1, '^SELECT.*', 20),
       (2, 1, '^INSERT.*|^UPDATE.*|^DELETE.*', 10);

LOAD MYSQL QUERY RULES TO RUNTIME;
SAVE MYSQL QUERY RULES TO DISK;

 步骤 5:创建 MyBatis-Flex 的 Mapper 和实体类

1、创建实体类

在 com.example.demo.entity 包中创建一个实体类,例如 Order.Java

package com.example.demo.entity;

public class Order {
    private Long id;
    priphpvate String orderName;

    // Getters and Setters
}

2、创建 Mapper 接口

在 com.example.demo.mapper 包中创建一个 Mapper 接口:

package com.example.demo.mapper;

import com.example.demo.entity.Order;
import org.apache.ibatis.annotations.Select;

public interface OrderMapper {
    
    @Select("SELECT * FROM orders WHERE id = #{id}")
    Order selectOrderById(Long id);
}

3、创建 Mapper XML 文件

在 src/main/resources/mapper 目录下创建 OrderMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.example.demo.mapper.OrderMapper">
    <select id="selectOrderById" resultType="com.example.demo.entity.Order">
        SELECT * FROM orders WHERE id = #{id}
    </select>
</mapper>

 步骤 6:创建 Service 和 Controller

1、 创建 Service

在 com.example.demo.service 包中创建一个 OrderService

package com.example.demo.service;

import com.example.demo.entity.Order;
import com.example.demo.mapper.OrderMapper;
import org.springframework.stereotype.Service;

@Service
public class OrderService {

www.chinasem.cn    private final OrderMapper orderMapper;

    public OrderService(OrderMapper orderMapper) {
        this.orderMapper = orderMapper;
    }

http://www.chinasem.cn    public Order getOrderById(Long id) {
        return orderMapper.selectOrderById(id);
    }
}

2、创建 Controller

在 com.example.demo.controller 包中创建一个 OrderController

package com.example.demo.controller;

import com.example.demo.entity.Order;
import com.example.demo.service.OrderService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OrderController {

    private final OrderService orderService;

    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }

    @GetMapping("/orders/{id}")
    public Order getOrderById(@PathVariable Long id) {
        return orjsderService.getOrderById(id);
    }
}

 步骤 7:测试读写分离

  • 启动 Spring Boot 项目:
mvn spring-boot:run
  • 测试写操作(INSERT/UPDATE/DELETE)

通过 Navicat 或其他工具,向数据库执行写操作,确保这些操作路由到 主库。

  • 测试读操作(SELECT)

访问 http://localhost:8080/orders/{id},验证读操作是否路由到 从库。

 步骤 8:配置延迟检测

在 ProxySQL 中启用 延迟检测:

SET mysql-monitor_replication_lag_interval_ms = 1000;

INSERT INTO mysql_replication_hostgroups (writer_hostgroup, reader_hostgroup, check_type, max_replication_lag)
VALUES (10, 20, 'seconds_behind_master', 5);

LOAD MYSQL VARIABLES TO RUNTIME;
SAVE MYSQL VARIABLES TO DISK;

✅ 总结

步骤描述
安装 MyBatis-Flex在项目中引入 MyBatis-Flex
配置数据源在 application.yml 中配置 ProxySQL 的数据源
配置读写分离规则在 ProxySQL 中配置读写分离规则
创建实体类、Mapper、Service、Controller实现数据库访问逻辑
启用延迟检测在 ProxySQL 中启用延迟检测

到此这篇关于SpringBoot+MyBatis-Flex配置ProxySQL的实现步骤的文章就介绍到这了,更多相关SpringBoot MyBatis-Flex配置ProxySQL内容请搜索China编程(www.chinasem.cn)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程China编程(www.chinasem.cn)!

这篇关于SpringBoot+MyBatis-Flex配置ProxySQL的实现步骤的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1153444

相关文章

Java导入、导出excel用法步骤保姆级教程(附封装好的工具类)

《Java导入、导出excel用法步骤保姆级教程(附封装好的工具类)》:本文主要介绍Java导入、导出excel的相关资料,讲解了使用Java和ApachePOI库将数据导出为Excel文件,包括... 目录前言一、引入Apache POI依赖二、用法&步骤2.1 创建Excel的元素2.3 样式和字体2.

Java实现将Markdown转换为纯文本

《Java实现将Markdown转换为纯文本》这篇文章主要为大家详细介绍了两种在Java中实现Markdown转纯文本的主流方法,文中的示例代码讲解详细,大家可以根据需求选择适合的方案... 目录方法一:使用正则表达式(轻量级方案)方法二:使用 Flexmark-Java 库(专业方案)1. 添加依赖(Ma

使用EasyExcel实现简单的Excel表格解析操作

《使用EasyExcel实现简单的Excel表格解析操作》:本文主要介绍如何使用EasyExcel完成简单的表格解析操作,同时实现了大量数据情况下数据的分次批量入库,并记录每条数据入库的状态,感兴... 目录前言固定模板及表数据格式的解析实现Excel模板内容对应的实体类实现AnalysisEventLis

使用国内镜像源优化pip install下载的方法步骤

《使用国内镜像源优化pipinstall下载的方法步骤》在Python开发中,pip是一个不可或缺的工具,用于安装和管理Python包,然而,由于默认的PyPI服务器位于国外,国内用户在安装依赖时可... 目录引言1. 为什么需要国内镜像源?2. 常用的国内镜像源3. 临时使用国内镜像源4. 永久配置国内镜

Mybatis从3.4.0版本到3.5.7版本的迭代方法实现

《Mybatis从3.4.0版本到3.5.7版本的迭代方法实现》本文主要介绍了Mybatis从3.4.0版本到3.5.7版本的迭代方法实现,包括主要的功能增强、不兼容的更改和修复的错误,具有一定的参考... 目录一、3.4.01、主要的功能增强2、selectCursor example3、不兼容的更改二、

Spring Boot拦截器Interceptor与过滤器Filter详细教程(示例详解)

《SpringBoot拦截器Interceptor与过滤器Filter详细教程(示例详解)》本文详细介绍了SpringBoot中的拦截器(Interceptor)和过滤器(Filter),包括它们的... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)详细教程1. 概述1

SpringBoot利用dynamic-datasource-spring-boot-starter解决多数据源问题

《SpringBoot利用dynamic-datasource-spring-boot-starter解决多数据源问题》dynamic-datasource-spring-boot-starter是一... 目录概要整体架构构想操作步骤创建数据源切换数据源后续问题小结概要自己闲暇时间想实现一个多租户平台,

mybatis-plus分页无效问题解决

《mybatis-plus分页无效问题解决》本文主要介绍了mybatis-plus分页无效问题解决,原因是配置分页插件的版本问题,旧版本和新版本的MyBatis-Plus需要不同的分页配置,感兴趣的可... 昨天在做一www.chinasem.cn个新项目使用myBATis-plus分页一直失败,后来经过多方

如何使用C#串口通讯实现数据的发送和接收

《如何使用C#串口通讯实现数据的发送和接收》本文详细介绍了如何使用C#实现基于串口通讯的数据发送和接收,通过SerialPort类,我们可以轻松实现串口通讯,并结合事件机制实现数据的传递和处理,感兴趣... 目录1. 概述2. 关键技术点2.1 SerialPort类2.2 异步接收数据2.3 数据解析2.

mybatis-plus 实现查询表名动态修改的示例代码

《mybatis-plus实现查询表名动态修改的示例代码》通过MyBatis-Plus实现表名的动态替换,根据配置或入参选择不同的表,本文主要介绍了mybatis-plus实现查询表名动态修改的示... 目录实现数据库初始化依赖包配置读取类设置 myBATis-plus 插件测试通过 mybatis-plu