shardindjdbc-inline策略

shardingjdbc官網https://shardingsphere.apache.org/document/current/cn/overview/
Apache ShardingSphere 是一款開源的分布式數據庫生態項目,由 JDBC 和 Proxy 兩款產品組成。其核心采用微內核+可插拔架構,通過插件開放擴展功能。它提供多源異構數據庫增強平臺,進而圍繞其上層構建生態。
Apache ShardingSphere 設計哲學為 Database Plus,旨在構建異構數據庫上層的標準和生態。它關注如何充分合理地利用數據庫的計算和存儲能力,而并非實現一個全新的數據庫。它站在數據庫的上層視角,關注它們之間的協作多于數據庫自身。
sql和源碼
https://gitee.com/zhangjijige/shardingjdbc.git

分片策略

InlineShardingStrategy策略

最常用的分片策略
application-inline.yml 文件

spring:
  main:
    allow-bean-definition-overriding: true
  shardingsphere:
    # 參數配置,顯示 sql
    props:
      sql:
        show: true
    datasource:
      # 數據源別名
      names: db0, db1, db2, db3
      # db0數據源信息
      db0:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/order_db_0?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
        username: root
        password: root
      db1:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/order_db_1?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
        username: root
        password: root
      db2:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/order_db_2?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
        username: root
        password: root
      db3:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/order_db_3?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
        username: root
        password: root
    sharding:
      default-database-strategy:
        inline:
          sharding-column: user_id
          algorithm-expression: db$->{user_id % 4}
      default-table-strategy:
        inline:
          sharding-column: order_id
          algorithm-expression: tbl_order_$->{order_id % 4}
      tables:
        # 邏輯表名
        tbl_order:
          # 指定數據節點
          actual-data-nodes: db$->{0..3}.tbl_order_$->{0..3}
#          # 分庫策略
#          database-strategy:
#            inline:
#              sharding-column: user_id
#              algorithm-expression: db$->{user_id % 4}
#          # 分表策略
#          table-strategy:
#            inline:
#              sharding-column: order_id
#              algorithm-expression: tbl_order_$->{order_id % 4}
mybatis:
  mapper-locations: classpath:mapper/*.xml

default-database-strategy和default-table-strategy的意思是默認的分庫分表策略,這里要注意的是
要寫清楚邏輯表和指定的節點

   # 邏輯表名
      tbl_order:
        # 指定數據節點,可以配合默認的分庫分表策略default-database-strategy和default-table-strategy
        actual-data-nodes: db$->{0..3}.tbl_order_$->{0..3}

當然也可以用自己的策略,策略可以看到是針對4取模

          # 分庫策略
          database-strategy:
            inline:
              sharding-column: user_id
              algorithm-expression: db$->{user_id % 4}
          # 分表策略
          table-strategy:
            inline:
              sharding-column: order_id
              algorithm-expression: tbl_order_$->{order_id % 4}

如果查詢的表,不需要分庫分表就需要

    sharding:
#這個就是默認的數據源,比如要查詢 user表,沒有給user表配置策略,那就直接使用默認的db0數據庫
#表就是使用user表,這樣就找到了 db0庫的user表
      default-data-source-name: db0
      tables:
        # 邏輯表名
        tbl_order: 
          # 指定數據節點
          actual-data-nodes: db$->{0..3}.tbl_order_$->{0..3}
          # 分庫策略
          database-strategy:
            inline:
              sharding-column: user_id
              algorithm-expression: db$->{user_id % 4}
          # 分表策略
          table-strategy:
            inline:
              sharding-column: order_id
              algorithm-expression: tbl_order_$->{order_id % 4}

這里介紹一下,tbl_order是表名稱在配置文件中是如何生效的其實很簡單


image.png

image.png

圖中

  private Map<String, YamlTableRuleConfiguration> tables = new LinkedHashMap<>();

map的key就是tbl_order,這樣就能解釋在配置文件中使用動態屬性

從上述配置可以知道user_id作為分庫鍵,order_id作為分表鍵
如何使用

    @Test
    public void insertOrder() {
        for (int i = 0 ; i <100; ++i) {
            OrderDto orderDto = new OrderDto();
//使用雪花算法作為orderid和userid,在入庫的時候根據配置的策略入庫
            orderDto.setOrderId(SnowFlakeUtil.getId());
            orderDto.setUserId(SnowFlakeUtil.getId());
            orderDto.setCreateTime(new Date());
            orderDto.setUpdateTime(new Date());
            orderDto.setPrice(new BigDecimal("100"));
            orderMapper.addOrder(orderDto);
        }
    }

inline策略比較常用,配上也比較簡單,db$->{user_id % 4}策略,使用的是Groovy表達式.查詢的時候支持 =和in的方式,如果是范圍 比如between 大于小于是不支持的

綁定關聯和廣播

綁定關聯
      tables:
        # 邏輯表名
        tbl_order:
          # 指定數據節點
          actual-data-nodes: db$->{0..3}.tbl_order_$->{0..3}
          # 分庫策略
          database-strategy:
            inline:
              sharding-column: user_id
              algorithm-expression: db$->{user_id % 4}
          # 分表策略
          table-strategy:
            inline:
              sharding-column: order_id
              algorithm-expression: tbl_order_$->{order_id % 4}
        # 邏輯表名
        tbl_user:
          # 指定數據節點
          actual-data-nodes: db$->{0..3}.tbl_user_$->{0..3}
          # 分庫策略
          database-strategy:
            inline:
              sharding-column: user_id
              algorithm-expression: db$->{user_id % 4}
          # 分表策略
          table-strategy:
            inline:
              sharding-column: order_id
              algorithm-expression: tbl_user_$->{order_id % 4}
      bindingTables:
        - tbl_order,tbl_user

bindingTables標簽代表綁定關系,兩張表做join的情況下使用
首先兩張表的分庫分表策略要完全一致,分片建數值也要完全一致, on的條件必須得是分片建
這樣tbl_order表和tbl_user表在關聯的時候只需關聯一次即可,而不是做笛卡爾積
不綁定在查詢所有數據的時候會出現tbl_order_0關聯tbl_user_1 的情況,因為分表的策略是一樣的
導致tbl_user的數據為空


image.png

示例中分庫和分表的列不同,所以on作為條件的時候要把分庫建和分表建都作為條件,
如果on條件不是分片建,很可能會出現本應該匹配的數據匹配不上.

廣播表

廣播表就是每個數據庫都有一張表,比如字典表,數據量不大,就可以讓所有的數據同時放到每個數據庫中,每個數據庫的數據是一致的,這樣在查詢數據的時候直接從本地庫查找字典表的數據就可以了
配置非常的簡單
spring.shardingsphere.sharding.broadcast-tables=t_dict


image.png

當insert數據的時候,會向4個庫同時插入

數據就會同步到每個數據庫中,查詢的時候只會從一個數據庫中,每次查詢的數據庫是不一樣的,應該是隨機的,這塊沒有太細的研究.


image.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容