shardingsphere-JDBC功能演示

shardingsphere-JDBC功能演示

  1. 分庫分表
  2. 讀寫分離
  3. 加密功能

準(zhǔn)備工作

  1. 執(zhí)行example模塊下的manual_schema.sql,初始化數(shù)據(jù)庫結(jié)構(gòu)
  2. 修改sharding-raw-jdbc-example模塊下META-INF文件夾下的配置文件,把數(shù)據(jù)庫配置修改為本地配置
  3. 選擇其中一個(gè)啟動(dòng)類啟動(dòng)-ShardingRawYamlConfigurationExample

啟動(dòng)類

不同啟動(dòng)類加載datesource信息的方式不同

yaml vs java

  1. yaml模式-從yaml文件中讀取config配置
  2. java模式-在java類中配置config

rang vs 非rang


啟動(dòng)類中,使用模板模式調(diào)用ExampleService接口

    public static void run(final ExampleService exampleService) throws SQLException {
        try {
            exampleService.initEnvironment();//初始化
            exampleService.processSuccess();//處理過程
        } finally {
            exampleService.cleanEnvironment();//刪除所有記錄
        }
    }

分庫分表

分表

分表規(guī)則

分庫

分庫規(guī)則

rules:
- !SHARDING
  tables:
    t_order: 
      actualDataNodes: ds_${0..1}.t_order
      keyGenerateStrategy:
        column: order_id
        keyGeneratorName: snowflake
    t_order_item:
      actualDataNodes: ds_${0..1}.t_order_item
      keyGenerateStrategy:
        column: order_item_id
        keyGeneratorName: snowflake
  bindingTables:
    - t_order,t_order_item
  broadcastTables:
    - t_address
  defaultDatabaseStrategy:
    standard:
      shardingColumn: user_id
      shardingAlgorithmName: database_inline
  defaultTableStrategy:
    none:
  
  shardingAlgorithms:
    database_inline:
      type: INLINE
      props:
        algorithm-expression: ds_${user_id % 2}
    
  keyGenerators:
    snowflake:
      type: SNOWFLAKE
      props:
          worker-id: 123
  1. 有ds_0和ds_1兩個(gè)庫
  2. t_order和t_order_item兩張表都是按照ds_${user_id % 2}規(guī)則--用user_id對2取模的值進(jìn)行數(shù)據(jù)庫路由,將數(shù)據(jù)分配到不同的數(shù)據(jù)庫中
image.png
image.png

可以看到,user_id為基數(shù)的數(shù)據(jù)分到了ds_1庫,偶數(shù)分到了ds_0庫,實(shí)現(xiàn)了分庫

讀寫分離

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容