分库分表之ShardingSphere读写分离(springboot)

1. 问题

数据库主从如何实现读写分离

1) select语句走从表

2) insert,update走主表

2. jar包引入

见之前的文章

3. springboot配置

#shardingsphere
spring.shardingsphere.datasource.names=master,slave
#主库
spring.shardingsphere.datasource.master.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master.url=jdbc:mysql://xx:3306/apporder?useUnicode=true&characterEncoding=utf-8
spring.shardingsphere.datasource.master.jdbcUrl=jdbc:mysql://xx:3306/apporder?useUnicode=true&characterEncoding=utf-8
spring.shardingsphere.datasource.master.username=root
spring.shardingsphere.datasource.master.password=xx
spring.shardingsphere.datasource.master.max-active=30
spring.shardingsphere.datasource.master.min-idle=5
spring.shardingsphere.datasource.master.time-between-eviction-runs-millis=600000
spring.shardingsphere.datasource.master.min-evictable-idle-time-millis=600000
spring.shardingsphere.datasource.master.remove-abandoned-timeout=10
spring.shardingsphere.datasource.master.remove-abandoned=true
# 从库
spring.shardingsphere.datasource.slave.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.slave.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.slave.url=jdbc:mysql://xx:3306/apporder?useUnicode=true&characterEncoding=utf-8
spring.shardingsphere.datasource.slave.jdbcUrl=jdbc:mysql://xx:3306/apporder?useUnicode=true&characterEncoding=utf-8
spring.shardingsphere.datasource.slave.username=root
spring.shardingsphere.datasource.slave.password=xx
spring.shardingsphere.datasource.slave.max-active=30
spring.shardingsphere.datasource.slave.min-idle=5
spring.shardingsphere.datasource.slave.time-between-eviction-runs-millis=600000
spring.shardingsphere.datasource.slave.min-evictable-idle-time-millis=600000
spring.shardingsphere.datasource.slave.remove-abandoned-timeout=10
spring.shardingsphere.datasource.slave.remove-abandoned=true
# 主从库 不支持分库分表
spring.shardingsphere.masterslave.name=ms
spring.shardingsphere.masterslave.master-data-source-name=master
spring.shardingsphere.masterslave.slave-data-source-names=slave
# SQL日志
spring.shardingsphere.props.sql.show=true
spring.shardingsphere.props.query.with.cipher.column=true
#mybatis
mybatis.mapper-locations=classpath:mybatis/*.xml

4. 测试类

    @Test
    public void testApp() {
        AppAuthRegisterExample example = new AppAuthRegisterExample();
        List<AppAuthRegister> appAuthRegisters = appAuthRegisterDAO.selectByExample(example);
        System.out.println(appAuthRegisters);
    }

    @Test
    public void testInsertApp() {
        AppAuthRegister record = new AppAuthRegister();
        record.setLogin(0);
        record.setCreateTime(new Date());
        record.setUpdateTime(new Date());
        int insert = appAuthRegisterDAO.insert(record);
        System.out.println(insert);
    }

5. 效果

查询走从库(SQL:slave)

[2019-12-24 15:55:36 INFO  org.apache.shardingsphere.core.route.SQLLogger:log] - Actual SQL: slave ::: select
         
         
        
        id, user_id, type, login, display, cp_category, secret_key, cp_secret_key,
        url, create_time, update_time
     
        from app_auth_register 

插入走主库 

发布了210 篇原创文章 · 获赞 105 · 访问量 18万+

猜你喜欢

转载自blog.csdn.net/u010627840/article/details/103684387