sharding jdbc(sphere) 3.1.0 spring boot配置


  sharding jdbc 2.x系列詳解參見https://www.cnblogs.com/zhjh256/p/9221634.html

  最近將sharding jdbc的配置從xml切換到了spring boot。首先引入依賴:

            <dependency>
                <groupId>io.shardingsphere</groupId>
                <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
                <version>3.1.0</version>
            </dependency>

  然后按照https://shardingsphere.apache.org/document/legacy/3.x/document/en/manual/sharding-jdbc/configuration/config-spring-boot/配置即可,基本上和xml配置是一樣的。

  需要注意的是,2.x版本占位符是${},如下:

    <sharding:inline-strategy id="orderTableStrategy" sharding-column="order_id" algorithm-expression="t_order_${order_id % 2}" />

  3.x版本則是$->{},原因是跟spring占位符沖突,如下:

sharding.jdbc.config.sharding.tables.ibf_acco_confirm.actual-data-nodes=ds$->{0..1}_$->{0..1}.ibf_acco_confirm

  規則:

sharding.jdbc.config.sharding.tables.ibf_acco_confirm.actual-data-nodes=ds$->{0..1}.ibf_acco_confirm
# 分庫策略,定義了目標庫列表,沒有定義分庫策略的,默認為default-database-strategy,實際上分庫策略應該就一種,所以應不定義才對
sharding.jdbc.config.sharding.tables.ibf_acco_confirm.database-strategy.hint.algorithm-class-name=com.hundsun.ta.tabase.sharding.DatabaseShardingAlgorithmByHint 
# 表的目標庫列表
sharding.jdbc.config.sharding.tables.t_order_item.actual-data-nodes=ds$->{0..1}.t_order_item$->{0..1}
# 分表策略 sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.inline.sharding-column=order_id sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.inline.algorithm-expression=t_order_item$->{order_id % 2}
# 自定生成id值的列,一般用ID中心,不應使用具體框架的 sharding.jdbc.config.sharding.tables.t_order_item.key-generator-column-name=order_item_id
# 主子表關系定義 #sharding.jdbc.config.sharding.binding-tables=t_order,t_order_item
# 廣播表,既不在sharding.jdbc.config.sharding.tables定義,又沒有定義在broadcast-tables定義的就是全局表
#sharding.jdbc.config.sharding.broadcast-tables=t_config
# 默認的分庫策略,一般不應使用行內算法(groovy的),建議使用algorithm-class-name
sharding.jdbc.config.sharding.default-database-strategy.inline.sharding-column=user_id 
sharding.jdbc.config.sharding.default-database-strategy.inline.algorithm-expression=ds$->{user_id % 2}

 

參考:

  3.1.0系列:https://shardingsphere.apache.org/document/legacy/3.x/document/en/manual/sharding-jdbc/configuration/config-spring-boot/

  4.0系列:https://shardingsphere.apache.org/document/current/cn/manual/sharding-jdbc/configuration/config-spring-boot/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM