Springboot实现Mysql读写分离

1、读写分离是一种数据库优化技术,它将读操作和写操作分别路由到不同的数据库服务器上。读写分离的基本思想是在应用程序的读写负载较大时,通过将读请求分发到多个从数据库上,减轻主数据库的负担,提高系统性能和可靠性。
2、在Springboot项目中实现读写分离,需要完成以下配置类的编写。
3、在配置文件中配置数据库连接信息(请先测试数据库是否能正常连接,同时主数据库和从数据库已经实现主从复制)

在这里插入代码片# 主数据库
spring.datasource.master.jdbc-url=jdbc:mysql://localhost:3306/master_db
spring.datasource.master.username=username
spring.datasource.master.password=password

# 从数据库
spring.datasource.slave.jdbc-url=jdbc:mysql://localhost:3306/slave_db
spring.datasource.slave.username=username
spring.datasource.slave.password=password

4、创建数据源配置类,用于配置主从数据源:

@Configuration
public class DataSourceConfig {

    @Bean(name = "masterDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.master")
    public DataSource masterDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "slaveDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.slave")
    public DataSource slaveDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name = "routingDataSource")
    public AbstractRoutingDataSource routingDataSource(@Qualifier("masterDataSource") DataSource masterDataSource,
                                                       @Qualifier("slaveDataSource") DataSource slaveDataSource) {
        RoutingDataSource routingDataSource = new RoutingDataSource();

        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put(DatabaseType.MASTER, masterDataSource);
        targetDataSources.put(DatabaseType.SLAVE, slaveDataSource);

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

        return routingDataSource;
    }
}

5、创建一个枚举类用于区分主从数据库:

public enum DatabaseType {
    MASTER, SLAVE
}

6、创建一个路由数据源类,根据不同的操作类型选择主或从数据库:

public class RoutingDataSource extends AbstractRoutingDataSource {

    @Override
    protected Object determineCurrentLookupKey() {
        return DatabaseContextHolder.getDatabaseType();
    }
}

7、创建一个数据源上下文类,用于设置当前线程的数据库类型:

public class DatabaseContextHolder {

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

    public static void setDatabaseType(DatabaseType type) {
        contextHolder.set(type);
    }

    public static DatabaseType getDatabaseType() {
        return contextHolder.get();
    }

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

8、创建一个AOP切面,用于在Service层根据读写操作类型设置数据源:

@Aspect
@Component
public class DataSourceAspect {

    @Pointcut("@annotation(com.example.demo.annotation.ReadOnly)")
    public void readOnlyPointcut() {
    }

    @Before("readOnlyPointcut()")
    public void setReadOnlyDataSource(JoinPoint joinPoint) {
        DatabaseContextHolder.setDatabaseType(DatabaseType.SLAVE);
    }

    @After("readOnlyPointcut()")
    public void clearReadOnlyDataSource(JoinPoint joinPoint) {
        DatabaseContextHolder.clearDatabaseType();
    }
}

9、创建@ReadOnly注解,当调用带有@ReadOnly注解的方法时,AOP切面会设置数据源为从数据库(SLAVE),其他方法默认使用主数据库(MASTER)。这样就实现了简单的数据库读写分离。

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

10、使用案例

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    public User getUserById(Long userId) {
        return userMapper.selectById(userId);
    }

    @ReadOnly
    public List<User> getAllUsers() {
        return userMapper.selectAll();
    }

    public void saveUser(User user) {
        userMapper.insert(user);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值