设为首页 加入收藏

TOP

Shading-JDBC、ShadingSphere、ShardingProxy 使用详解(四)
2023-07-23 13:43:50 】 浏览:77
Tags:Shading-JDBC ShadingSphere ShardingProxy
2的余数作为数据库表的下标。

分表配置

修改application.yml,配置分表策略,配置如下:


spring:
  main:
    allow-bean-definition-overriding: true
  shardingsphere:
    datasource:
      names: ds1,ds2
      ds1:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        username: root
        password: root
        url: jdbc:mysql://localhost:3306/sd1?serverTimezone=Asia/Shanghai&characterEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useSSL=false
      ds2:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        username: root
        password: root
        url: jdbc:mysql://localhost:3306/sd2?serverTimezone=Asia/Shanghai&characterEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useSSL=false
    sharding:
      tables:
        goods:
          actualDataNodes: ds${1..2}.goods_${0..1}
          databaseStrategy:
            inline:
              shardingColumn: type
              algorithmExpression: ds${type % 2 + 1}
          # 表分片策略
          tableStrategy:
            inline:
              shardingColumn: id
              algorithmExpression: goods_${id % 2}
          keyGenerator:
            type: SNOWFLAKE
            column: id
    props:
      sql.show: true
logging:
  pattern:
    console: '%d{HH:mm:ss} %msg %n\'
  level:
    root: info
    com:
      execise: debug

配置参数说明


上面我们完成了分表分库的配置,但很多配置并未说明是什么意思,参数详情如下:


dataSources: # 省略数据源配置,请参考使用手册

rules:
- !SHARDING
  tables: # 数据分片规则配置
    <logic-table-name> (+): # 逻辑表名称
      actualDataNodes (?): # 由数据源名 + 表名组成(参考Inline语法规则)
      databaseStrategy (?): # 分库策略,缺省表示使用默认分库策略,以下的分片策略只能选其一
        standard: # 用于单分片键的标准分片场景
          shardingColumn: # 分片列名称
          shardingAlgorithmName: # 分片算法名称
        complex: # 用于多分片键的复合分片场景
          shardingColumns: #分片列名称,多个列以逗号分隔
          shardingAlgorithmName: # 分片算法名称
        hint: # Hint 分片策略
          shardingAlgorithmName: # 分片算法名称
        none: # 不分片
      tableStrategy: # 分表策略,同分库策略
      keyGenerateStrategy: # 分布式序列策略
        column: # 自增列名称,缺省表示不使用自增主键生成器
        keyGeneratorName: # 分布式序列算法名称
  autoTables: # 自动分片表规则配置
    t_order_auto: # 逻辑表名称
      actualDataSources (?): # 数据源名称
      shardingStrategy: # 切分策略
        standard: # 用于单分片键的标准分片场景
          shardingColumn: # 分片列名称
          shardingAlgorithmName: # 自动分片算法名称
  bindingTables (+): # 绑定表规则列表
    - <logic_table_name_1, logic_table_name_2, ...> 
    - <logic_table_name_1, logic_table_name_2, ...> 
  broadcastTables (+): # 广播表规则列表
    - <table-name>
    - <table-name>
  defaultDatabaseStrategy: # 默认数据库分片策略
  defaultTableStrategy: # 默认表分片策略
  defaultKeyGenerateStrategy: # 默认的分布式序列策略
  defaultShardingColumn: # 默认分片列名称
  
  # 分片算法配置
  shardingAlgorithms:
    <sharding-algorithm-name> (+): # 分片算法名称
      type: # 分片算法类型
      props: # 分片算法属性配置
      # ...
  
  # 分布式序列算法配置
  keyGenerators:
    <key-generate-algorithm-name> (+): # 分布式序列算法名称
      type: # 分布式序列算法类型
      props: # 分布式序列算法属性配置
      # ...

props:
  # ...

?

ShardingProxy 使用


下载与安装


访问 https://shardingsphere.apache.org/document/current/en/downloads/ 下载


也可获取历史版本的下载

https://archive.apache.org/dist/shardingsphere/


  1. 解压缩后修改conf/server.yaml和以config-前缀开头的文件,如:conf/config-xxx.yaml文件,进行分片规则、读写分离规则配置。

需要修改server.yaml后方可启动。把authentication这块原来的注释符(#)都删除即可


在这里插入图片描述


  1. 如果使用mysql数据库时,需要把mysql的jar复制到它的lib目录下。

  2. windows下双击start.bat启动: 默认使用3307端口,可通过命令修改。

    
    start.bat 端口
    

在这里插入图片描述


  1. Linux操作系统请运行bin/start.sh,Windows操作系统请运行bin/start.bat启动Sharding-Proxy。如需配置启动端口、配置文件位置后进行启动

分库案例


修改配置文件config-sharding.yaml如下


schemaName: sharding_db
dataSources:
  sp_1:
    url: jdbc:mysql://12
首页 上一页 1 2 3 4 5 6 下一页 尾页 4/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇排序算法中的冒泡和选择排序详解.. 下一篇IO流 p7 对象流-ObjectInputStrea..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目