shardingsphere的读写分离和分表设置

2023-05-22

本篇内容主要讲解“shardingsphere的读写分离和分表设置”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“shardingsphere的读写分离和分表设置”吧!

shardingsphere版本

<dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>4.1.1</version>
        </dependency>

springboot yml配置

spring:
  shardingsphere:
    datasource:
      names: master,slave0
      master: #主库
        type: com.zaxxer.hikari.HikariDataSource
        driverClassName: com.mysql.cj.jdbc.Driver
        jdbcUrl: jdbc:mysql://xxx.xxxx.xxxx:3432/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useLocalSessionState=true
        username: db_dev
        password: db_dev123
      slave0: #从库(读库)
        type: com.zaxxer.hikari.HikariDataSource
        driverClassName: com.mysql.cj.jdbc.Driver
        jdbcUrl: jdbc:mysql://xxxx.xxxx.xxxx.xxxx:3433/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useLocalSessionState=true
        username: db_dev
        password: db_dev123

    sharding: #分片设置
      tables:  #分表规则
        t_order: #要做分表的表名(或者叫表名前辍)
          actualDataNodes: ms.t_order$->{0..2}  # t_order0~2子表, 这里的ms是主从名: ms
          tableStrategy: #表分片设置
            inline:
              shardingColumn: customer_id #以此字段做为分片条件
              algorithmExpression: t_order$->{customer_id % 3}  #数据分配策略,这里考虑客户维度的数据分表
          keyGenerator:
            column: id
            type: SNOWFLAKE #雪花算法
      masterSlaveRules: #主从设置(读写设置)
        ms:  #主从名
          masterDataSourceName: master #主库
          slaveDataSourceNames: slave0  #从库列表
          loadBalanceAlgorithmType: round_robin  #主从轮询规则
    props:
      sql:
        show: true

参考文档

https://shardingsphere.apache.org/document/legacy/3.x/document/cn/manual/sharding-proxy/configuration/ https://www.cnblogs.com/qdhxhz/p/11656205.html https://www.jianshu.com/p/cb48ba2941cb

到此,相信大家对“shardingsphere的读写分离和分表设置”有了更深的了解,不妨来实际操作一番吧!这里是本站网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

《shardingsphere的读写分离和分表设置.doc》

下载本文的Word格式文档,以方便收藏与打印。