springboot集成mysql、mybatis-plus、shardingsphere-jdbc实现读写分离、分表:BOUNDARY_RANGE基于分片边界的范围分片算法

详情请见上一篇读写分离

《springboot集成mysql、mybatis-plus、shardingsphere-jdbc实现读写分离》

警告:一定要先创建好表!!!

提供yml和properties2种配置

yml

mybatis-plus:
  mapper-locations: classpath:mapper/*.xml

spring:
  shardingsphere:
    props:
      sql-show: false
    datasource:
      names: master,slave1
      master:
        type: com.zaxxer.hikari.HikariDataSource
        url: jdbc:mysql://192.168.1.11:3306/shardingjdbc?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
        username: root
        password: 123456
      slave1:
        type: com.zaxxer.hikari.HikariDataSource
        url: jdbc:mysql://192.168.1.22:3306/shardingjdbc?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
        username: root
        password: 123456
    rules:
      readwrite-splitting:
        data-sources:
          myds:
            static-strategy:
              write-data-source-name: master
              read-data-source-names:
                - slave1
            load-balancer-name: round-robin
        load-balancers:
          round-robin:
            type: ROUND_ROBIN
      sharding:
        tables:
          user:
          	#actual-data-nodes: myds.user_$->{0..1}
            actual-data-nodes: myds.user_$->{0..2}
            table-strategy:
              standard:
                sharding-column: num
                sharding-algorithm-name: user-boundary-range
            key-generate-strategy:
              column: id
              key-generator-name: snowflake
        key-generators:
          snowflake:
            type: SNOWFLAKE
        sharding-algorithms:
          user-boundary-range:
            type: BOUNDARY_RANGE
            props:
              #分片的范围边界,多个范围边界以逗号分隔。即:[0,6)和[6,+无穷]。因此有2张表:myds.user_$->{0..1}
              #sharding-ranges: '6'
              #即:[0,6)和[6,11)和[11,+无穷]。因此有3张表:myds.user_$->{0..2}
              sharding-ranges: '6,11'

properties

#打印sql
spring.shardingsphere.props.sql-show=true
# 配置真实数据源
spring.shardingsphere.datasource.names=master,slave1
# 配置第 1 个数据源
spring.shardingsphere.datasource.master.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.master.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.master.jdbc-url=jdbc:mysql://192.168.1.11:3306/shardingjdbc?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
spring.shardingsphere.datasource.master.username=root
spring.shardingsphere.datasource.master.password=123456

# 配置第 2 个数据源
spring.shardingsphere.datasource.slave1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.slave1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.slave1.jdbc-url=jdbc:mysql://192.168.1.22:3306/shardingjdbc?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
spring.shardingsphere.datasource.slave1.username=root
spring.shardingsphere.datasource.slave1.password=123456

#写数据源名称
spring.shardingsphere.rules.readwrite-splitting.data-sources.myds.static-strategy.write-data-source-name=master
#读数据源名称,多个从数据源用逗号分隔。如:slave1,slave2
spring.shardingsphere.rules.readwrite-splitting.data-sources.myds.static-strategy.read-data-source-names=slave1
#负载均衡算法名称
spring.shardingsphere.rules.readwrite-splitting.data-sources.myds.load-balancer-name=round_robin
#负载均衡算法类型
spring.shardingsphere.rules.readwrite-splitting.load-balancers.round_robin.type=ROUND_ROBIN

#分表
spring.shardingsphere.rules.sharding.tables.user.actual-data-nodes=myds.user_$->{0..2}
spring.shardingsphere.rules.sharding.tables.user.table-strategy.standard.sharding-column=num
spring.shardingsphere.rules.sharding.tables.user.table-strategy.standard.sharding-algorithm-name=user-boundary-range
spring.shardingsphere.rules.sharding.tables.user.key-generate-strategy.column=id
spring.shardingsphere.rules.sharding.tables.user.key-generate-strategy.key-generator-name=snowflake
spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE
spring.shardingsphere.rules.sharding.sharding-algorithms.user-boundary-range.type=BOUNDARY_RANGE
#不要加''单引号或者""双引号包住数字
spring.shardingsphere.rules.sharding.sharding-algorithms.user-boundary-range.props.sharding-ranges=6,11

测试

数据库表:
在这里插入图片描述
表结构:分片容量不一定要叫这个,自增id也可以作为分片容量,这里只是演示,数据存到哪张表跟num的值有关
在这里插入图片描述

写入master主库:localhost:88/user/insert

	@PutMapping("insert")
    public boolean insert(){
        Collection<User> users = new ArrayList<>();
        for (int i = 0;i<13;i++){
            User user = new User();
            user.setUsername("Meta39");
            user.setNum(i);
            user.setCreateTime(LocalDateTime.now());
            users.add(user);
        }
        return userService.saveBatch(users);
    }

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值