吴中网站开发建设多少钱,昆山网站开发的公司,网站规划的主要任务是什么,seo网站优化培训班原理解析
1. MySQL主从复制#xff08;Master-Slave Replication#xff09;
工作原理#xff1a;MySQL主从复制通过二进制日志#xff08;binary log#xff09;来同步数据。主服务器记录所有更改操作到二进制日志中#xff0c;从服务器读取这些日志并执行相应的SQL语…原理解析
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 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactId/dependency!-- MyBatis Starter --dependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion2.2.0/version/dependency!-- ShardingSphere for read-write splitting --dependencygroupIdorg.apache.shardingsphere/groupIdartifactIdsharding-jdbc-spring-boot-starter/artifactIdversion4.1.1/version/dependency
/dependencies
2. 配置数据源
编辑application.yml文件以配置多个数据源并设置ShardingSphere规则以实现读写分离 yaml
深色版本
spring:shardingsphere:datasource:names: master,slave0,slave1master:type: com.zaxxer.hikari.HikariDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://master_host:3306/your_db?useSSLfalseserverTimezoneUTCusername: your_usernamepassword: your_passwordslave0:type: com.zaxxer.hikari.HikariDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://slave0_host:3306/your_db?useSSLfalseserverTimezoneUTCusername: your_usernamepassword: your_passwordslave1:type: com.zaxxer.hikari.HikariDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://slave1_host:3306/your_db?useSSLfalseserverTimezoneUTCusername: your_usernamepassword: your_passwordmasterslave: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)ListMapString, 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 {Autowiredprivate UserMapper userMapper;/*** 获取所有用户信息.** return 用户信息列表*/public ListMapString, Object getAllUsers() {return userMapper.findAllUsers();}/*** 更新用户名称.** param id 用户ID* param newName 新用户名*/public void updateUserName(Long id, String newName) {userMapper.updateUserById(id, newName);}
}
注意事项及最佳实践 事务管理确保所有写操作都在同一个事务中执行并且只对主库进行写操作。可以使用Transactional注解来管理事务。 数据一致性考虑到主从复制延迟的问题在某些场景下如刚完成写操作后立即读取可能需要直接查询主库以保证数据一致性。 健康检查建议定期监控主从状态确保从库同步正常以及主库可访问。可以通过定时任务或者外部工具来实现。 性能优化根据实际业务需求调整负载均衡策略例如采用权重轮询或其他高级算法来优化查询效率。