SpringBoot+MyBatis-Flex配置ProxySQL的實現(xiàn)步驟
?? 目標
- 在 Spring Boot 中連接 ProxySQL。
- 使用 MyBatis-Flex 訪問數(shù)據(jù)庫。
- 配置 讀寫分離 和 延遲檢測。
?? 步驟 1:確保 ProxySQL 和 MySQL 主從同步已正確配置
首先,確保您已經(jīng)正確配置了 ProxySQL 和 MySQL 主從同步。
ProxySQL 的默認配置
Hostgroup ID | 描述 |
---|---|
10 | 主庫(寫操作) |
20 | 從庫(讀操作) |
ProxySQL 的數(shù)據(jù)端口
- 端口:6033(默認數(shù)據(jù)接口端口)
?? 步驟 2:在 Spring Boot 項目中引入依賴
引入 MyBatis-Flex 的依賴
在 pom.xml
中添加以下依賴:
<dependency> <groupId>com.mybatisflex</groupId> <artifactId>mybatis-flex-spring-boot-starter</artifactId> <version>1.4.3</version> </dependency>
?? 步驟 3:配置 application.yml
在 application.yml
文件中配置 ProxySQL 的數(shù)據(jù)源。
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:配置數(shù)據(jù)源的讀寫分離
在 ProxySQL 中配置 讀寫分離規(guī)則:
在 ProxySQL 管理界面執(zhí)行以下 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:創(chuàng)建 MyBatis-Flex 的 Mapper 和實體類
1、創(chuàng)建實體類
在 com.example.demo.entity
包中創(chuàng)建一個實體類,例如 Order.java
:
package com.example.demo.entity; public class Order { private Long id; private String orderName; // Getters and Setters }
2、創(chuàng)建 Mapper 接口
在 com.example.demo.mapper
包中創(chuàng)建一個 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、創(chuàng)建 Mapper XML 文件
在 src/main/resources/mapper
目錄下創(chuàng)建 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:創(chuàng)建 Service 和 Controller
1、 創(chuàng)建 Service
在 com.example.demo.service
包中創(chuàng)建一個 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、創(chuàng)建 Controller
在 com.example.demo.controller
包中創(chuàng)建一個 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 或其他工具,向數(shù)據(jù)庫執(zhí)行寫操作,確保這些操作路由到 主庫。
- 測試讀操作(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;
? 總結(jié)
步驟 | 描述 |
---|---|
安裝 MyBatis-Flex | 在項目中引入 MyBatis-Flex |
配置數(shù)據(jù)源 | 在 application.yml 中配置 ProxySQL 的數(shù)據(jù)源 |
配置讀寫分離規(guī)則 | 在 ProxySQL 中配置讀寫分離規(guī)則 |
創(chuàng)建實體類、Mapper、Service、Controller | 實現(xiàn)數(shù)據(jù)庫訪問邏輯 |
啟用延遲檢測 | 在 ProxySQL 中啟用延遲檢測 |
到此這篇關(guān)于SpringBoot+MyBatis-Flex配置ProxySQL的實現(xiàn)步驟的文章就介紹到這了,更多相關(guān)SpringBoot MyBatis-Flex配置ProxySQL內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決問題:Failed to execute goal org.apache.m
這篇文章主要給大家介紹了關(guān)于解決問題:Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources的相關(guān)資料,文中將解決的辦法介紹的非常詳細,需要的朋友可以參考下2023-03-03Ubuntu安裝JDK與IntelliJ?IDEA的詳細過程
APT是Linux系統(tǒng)上的包管理工具,能自動解決軟件包依賴關(guān)系并從遠程存儲庫中獲取安裝軟件包,這篇文章主要介紹了Ubuntu安裝JDK與IntelliJ?IDEA的過程,需要的朋友可以參考下2023-08-08spring如何快速穩(wěn)定解決循環(huán)依賴問題
這篇文章主要介紹了spring如何快速穩(wěn)定解決循環(huán)依賴問題,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03MyEclipse如何將項目的開發(fā)環(huán)境與服務器的JDK 版本保持一致
我們使用MyEclipse開發(fā)Java項目開發(fā)中,偶爾會遇到因項目開發(fā)環(huán)境不協(xié)調(diào),導致這樣那樣的問題,在這里以把所有環(huán)境調(diào)整為JDK1.6 為例,給大家詳細介紹MyEclipse如何將項目的開發(fā)環(huán)境與服務器的JDK 版本保持一致,需要的朋友參考下吧2024-04-04Spring Boot中@ConditionalOnProperty的使用方法
這篇文章主要給大家介紹了關(guān)于Spring Boot中@ConditionalOnProperty的使用方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者使用Spring Boot具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-12-12