【SQL】什么是数据库读写分离

数据库读写分离是一种数据库架构设计,用于提高数据库的性能和可扩展性。其基本思想是将数据库的读操作和写操作分离开来,通过不同的数据库实例处理,从而减轻单个数据库实例的负载,提升整体系统的吞吐量和响应速度。

读写分离的基本概念

  1. 主库(Master):处理所有的写操作(插入、更新、删除)和一些关键的读操作。主库是数据的唯一写入点,确保数据的一致性。
  2. 从库(Slave):从主库复制数据,处理所有的读操作。一个主库可以有多个从库,从库之间的数据是从主库同步过来的,通常是实时或接近实时的。

读写分离的实现步骤

  1. 主从复制:配置主库和从库,实现数据同步。常见的数据库,如 MySQL,提供了内置的主从复制功能。
  2. 读写路由:在应用程序层或中间件层实现读写操作的路由。写操作路由到主库,读操作路由到从库。
  3. 数据一致性:处理主从延迟,确保在允许的范围内读到最新的数据。

Java 实现读写分离

以下是一个简单的示例,演示如何在 Java 应用程序中实现数据库的读写分离。这个示例假设使用 Spring Framework 和 HikariCP 数据源池。

数据库配置
# application.yml
spring:
  datasource:
    url: jdbc:mysql://master-db:3306/mydb
    username: root
    password: root
    hikari:
      maximum-pool-size: 10

  replicas:
    - url: jdbc:mysql://slave-db1:3306/mydb
      username: root
      password: root
    - url: jdbc:mysql://slave-db2:3306/mydb
      username: root
      password: root
数据源配置
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class DataSourceConfig {

    @Value("${spring.datasource.url}")
    private String masterUrl;

    @Value("${spring.datasource.username}")
    private String masterUsername;

    @Value("${spring.datasource.password}")
    private String masterPassword;

    @Autowired
    private ReplicaDataSourceProperties replicaProperties;

    @Bean
    public DataSource dataSource() {
        AbstractRoutingDataSource routingDataSource = new ReplicationRoutingDataSource();

        HikariDataSource masterDataSource = new HikariDataSource();
        masterDataSource.setJdbcUrl(masterUrl);
        masterDataSource.setUsername(masterUsername);
        masterDataSource.setPassword(masterPassword);

        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put("master", masterDataSource);

        for (int i = 0; i < replicaProperties.getReplicas().size(); i++) {
            ReplicaProperties replica = replicaProperties.getReplicas().get(i);
            HikariDataSource slaveDataSource = new HikariDataSource();
            slaveDataSource.setJdbcUrl(replica.getUrl());
            slaveDataSource.setUsername(replica.getUsername());
            slaveDataSource.setPassword(replica.getPassword());
            targetDataSources.put("slave" + i, slaveDataSource);
        }

        routingDataSource.setTargetDataSources(targetDataSources);
        routingDataSource.setDefaultTargetDataSource(masterDataSource);

        return routingDataSource;
    }
}
路由数据源
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class ReplicationRoutingDataSource extends AbstractRoutingDataSource {

    private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();

    public static void setDataSourceType(String dataSourceType) {
        contextHolder.set(dataSourceType);
    }

    public static void clearDataSourceType() {
        contextHolder.remove();
    }

    @Override
    protected Object determineCurrentLookupKey() {
        return contextHolder.get();
    }
}
数据源选择器
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class DataSourceAspect {

    @Before("@annotation(com.example.annotation.Master)")
    public void setWriteDataSourceType() {
        ReplicationRoutingDataSource.setDataSourceType("master");
    }

    @Before("@annotation(com.example.annotation.Slave) || execution(* com.example.service..*.find*(..))")
    public void setReadDataSourceType() {
        ReplicationRoutingDataSource.setDataSourceType("slave0"); // 可实现负载均衡策略
    }
}
自定义注解
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Master {
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Slave {
}

示例服务

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    @Master
    @Transactional
    public void saveUser(User user) {
        userRepository.save(user);
    }

    @Slave
    public User findUserById(Long id) {
        return userRepository.findById(id).orElse(null);
    }
}

总结

读写分离通过将读操作和写操作分开处理,可以大幅提高数据库系统的性能和扩展性。上面的示例展示了如何使用 Spring 和 HikariCP 实现基本的读写分离。实际应用中,可以根据业务需求进一步优化和扩展,例如添加更多的从库,优化负载均衡策略,处理数据一致性问题等。

  • 19
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值