iis 手机网站,软件开发外包公司,游戏如何在网上推广,王烨飞✅ Spring Boot MyBatis-Flex 配置 ProxySQL 的完整指南
下面是一个详细的教程#xff0c;指导您如何在 Spring Boot 项目中使用 MyBatis-Flex 配置 ProxySQL 进行 读写分离 和 主从同步 的数据库访问。 #x1f3af; 目标
在 Spring Boot 中连接 ProxySQL。使用 MyBatis-…✅ Spring Boot MyBatis-Flex 配置 ProxySQL 的完整指南
下面是一个详细的教程指导您如何在 Spring Boot 项目中使用 MyBatis-Flex 配置 ProxySQL 进行 读写分离 和 主从同步 的数据库访问。 目标
在 Spring Boot 中连接 ProxySQL。使用 MyBatis-Flex 访问数据库。配置 读写分离 和 延迟检测。 步骤 1确保 ProxySQL 和 MySQL 主从同步已正确配置
首先确保您已经正确配置了 ProxySQL 和 MySQL 主从同步。
ProxySQL 的默认配置
Hostgroup ID描述10主库写操作20从库读操作
ProxySQL 的数据端口
端口6033默认数据接口端口 步骤 2在 Spring Boot 项目中引入依赖
1️⃣ 引入 MyBatis-Flex 的依赖
在 pom.xml 中添加以下依赖
dependencygroupIdcom.mybatisflex/groupIdartifactIdmybatis-flex-spring-boot-starter/artifactIdversion1.4.3/version
/dependency步骤 3配置 application.yml
在 application.yml 文件中配置 ProxySQL 的数据源。
spring:datasource:url: jdbc:mysql://127.0.0.1:6033/db_order_plus?useSSLfalseuseUnicodetruecharacterEncodingutf8serverTimezoneAsia/Shanghaiusername: rootpassword: rootmybatis-flex:mapper-locations: classpath:mapper/**/*Mapper.xmlconfiguration: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;private 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 version1.0 encodingUTF-8?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtdmapper namespacecom.example.demo.mapper.OrderMapperselect idselectOrderById resultTypecom.example.demo.entity.OrderSELECT * 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 {private final OrderMapper orderMapper;public OrderService(OrderMapper orderMapper) {this.orderMapper orderMapper;}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 orderService.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 中启用延迟检测