Sharding-Jdbc配置示例

#应用名称
spring:
  application:
    name: sharding-jdbc-test

  #开发环境设置
  profiles:
    active: dev

  #配置为内存模式
  shardingsphere:
    mode:
      type: Memory

    # 配置真实数据源
    datasource:
      names: server-order00,server-order01

      # 配置第 1 个数据源
      server-order00:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.jdbc.Driver
        jdbc-url: jdbc:mysql://127.0.0.1:3306/sharding_jdbc_order01
        username: root
        password: 123456

      # 配置第 2 个数据源
      server-order01:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.jdbc.Driver
        jdbc-url: jdbc:mysql://127.0.0.1:3306/sharding_jdbc_order02
        username: root
        password: 123456


    #--------------------------------------标准分片的配置(数据节点配置)
    #由数据源名 + 表名组成,以小数点分隔。多个表以逗号分隔,支持 inline 表达式。
    #缺省表示使用已知数据源与逻辑表名称生成数据节点,用于广播表(即每个库中都需要一个同样的表用于关联查询,多为字典表)或只分库不分表且所有库的表结构完全一致的情况
    #https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/yaml-config/rules/sharding/
    rules:
      sharding:
        tables: # 数据分片规则配置
          t_order: # 逻辑表名称
            actualDataNodes: server-order0$->{0..1}.t_order$->{0..1} # 由数据源名 + 表名组成(参考 Inline 语法规则)
            databaseStrategy: # 分库策略,缺省表示使用默认分库策略,以下的分片策略只能选其一
              standard: # 用于单分片键的标准分片场景
                shardingColumn: user_id # 分片列名称(数据库列字段,用户名称)
                shardingAlgorithmName: t-order-inline # 分片算法名称
            tableStrategy: # 分表策略,同分库策略
              standard: # 用于单分片键的标准分片场景
                shardingColumn: order_no # 分片列名称(数据库列字段,订单号)
                shardingAlgorithmName: t-order-hash-mod # 分片算法名称
            keyGenerateStrategy: # 分布式序列策略
              column: id # 自增列名称,缺省表示不使用自增主键生成器
              keyGeneratorName: kgan-snowflake # 分布式序列算法名称

          t_order_item: # 逻辑表名称
            actualDataNodes: server-order0$->{0..1}.t_order_item$->{0..1} # 由数据源名 + 表名组成(参考 Inline 语法规则)
            databaseStrategy: # 分库策略,缺省表示使用默认分库策略,以下的分片策略只能选其一
              standard: # 用于单分片键的标准分片场景
                shardingColumn: user_id # 分片列名称(数据库列字段,用户名称)
                shardingAlgorithmName: t-order-inline # 分片算法名称
            tableStrategy: # 分表策略,同分库策略
              standard: # 用于单分片键的标准分片场景
                shardingColumn: order_no # 分片列名称(数据库列字段,订单号)
                shardingAlgorithmName: t-order-hash-mod # 分片算法名称
            keyGenerateStrategy: # 分布式序列策略
              column: id # 自增列名称,缺省表示不使用自增主键生成器
              keyGeneratorName: kgan-snowflake # 分布式序列算法名称


          t_dict: # 逻辑表名称
            actualDataNodes: server-order0$->{0..1}.t_dict # 由数据源名 + 表名组成(参考 Inline 语法规则)


        # ---------------------------------------分片算法配置
        #https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/yaml-config/algorithm/
        shardingAlgorithms:
          #行表达式分片算法
          t-order-inline: # 分片算法名称
            # type 和 props,请参考分片内置算法:https://shardingsphere.apache.org/document/current/cn/user-manual/common-config/builtin-algorithm/sharding/
            type: INLINE # 分片算法类型
            props: # 分片算法属性配置
              #t_order0$->{user_id % 2} 表示 t_order 表根据 user_id 取模(取余) 2,而分成 2 张表,表名称为 t_order01 到 t_order02
              algorithm-expression: server-order0$->{user_id % 2}
          #取模分片算法
          t-order-mod: # 分片算法名称
            # type 和 props,请参考分片内置算法:https://shardingsphere.apache.org/document/current/cn/user-manual/common-config/builtin-algorithm/sharding/
            type: MOD # 分片算法类型
            props: # 分片算法属性配置
              #分片数量
              sharding-count: 2
          #哈希取模分片算法
          t-order-hash-mod: # 分片算法名称
            # type 和 props,请参考分片内置算法:https://shardingsphere.apache.org/document/current/cn/user-manual/common-config/builtin-algorithm/sharding/
            type: HASH_MOD # 分片算法类型
            props: # 分片算法属性配置
              #分片数量
              sharding-count: 2
            # 分布式序列算法配置


        #---------------------------------------分布式序列算法
        #https://shardingsphere.apache.org/document/current/cn/user-manual/common-config/builtin-algorithm/keygen/
        keyGenerators:
          kgan-snowflake: # 分布式序列算法名称
            type: SNOWFLAKE # 分布式序列算法类型
            #props: # 分布式序列算法属性配置

        #指分片规则一致的一组分片表。 使用绑定表进行多表关联查询时,必须使用分片键进行关联,否则会出现笛卡尔积关联或跨库关联,从而影响查询效率。
        #例如:t_order 表和 t_order_item 表,均按照 order_id 分片,并且使用 order_id 进行关联,则此两张表互为绑定表关系。
        #绑定表之间的多表关联查询不会出现笛卡尔积关联,关联查询效率将大大提升
        #https://shardingsphere.apache.org/document/current/cn/features/sharding/concept/
        bindingTables: # 绑定表规则列表
          - t_order,t_order_item  #解决笛卡尔积查询

        #指所有的分片数据源中都存在的表,表结构及其数据在每个数据库中均完全一致。 适用于数据量不大且需要与海量数据的表进行关联查询的场景,例如:字典表。
        #https://shardingsphere.apache.org/document/current/cn/features/sharding/concept/
        broadcastTables: # 广播表规则列表
          - t_dict

    props:
      #是否在日志中打印 SQL 打印 SQL 默认值 false
      sql-show: true

表结构:

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值