SpringBoot?如何使用sharding?jdbc進行分庫分表
基于4.0版本,Springboot2.1
之前寫過一篇使用sharding-jdbc進行分庫分表的文章,不過當時的版本還比較早,現(xiàn)在已經(jīng)不能用了。這一篇是基于最新版來寫的。
新版已經(jīng)變成了shardingsphere了,https://shardingsphere.apache.org/。
有點不同的是,這一篇,我們是采用多數(shù)據(jù)源,僅對一個數(shù)據(jù)源進行分表。也就是說在網(wǎng)上那些jpa多數(shù)據(jù)源的配置,用sharding jdbc一樣能完成。
也就是說我們有兩個庫,一個庫是正常使用,另一個庫其中的一個表進行分表。
老套路,我們還是使用Springboot進行集成,
在pom里確保有如下引用
<sharding-sphere.version>4.0.0-RC1</sharding-sphere.version>
<!-- 分庫分表--> <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-jdbc-spring-boot-starter</artifactId> <version>${sharding-sphere.version}</version> </dependency> <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-core-common</artifactId> <version>${sharding-sphere.version}</version> </dependency> <!-- 分庫分表 end--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
spring: application: name: t3cc profiles: active: sharding-databases-tables # datasource: # primary: # jdbc-url: jdbc:mysql://${MYSQL_HOST:localhost}:${MYSQL_PORT:3306}/${DB_NAME:dmp_t3cc}?useUnicode=true&characterEncoding=UTF8&serverTimezone=Hongkong # username: ${MYSQL_USER:root} # password: ${MYSQL_PASS:root} # secondary: # jdbc-url: jdbc:mysql://xxxxxxxxxxxxx/xxxxxx?useUnicode=true&characterEncoding=UTF8&serverTimezone=Hongkong # username: xxxxx # password: xxxxxxx jpa: database: mysql database-platform: org.hibernate.dialect.MySQL5InnoDBDialect #不加這句則默認為myisam引擎 hibernate: ddl-auto: none naming: physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy open-in-view: true properties: enable_lazy_load_no_trans: true show-sql: true
yml里還是老套路,大家注意,我把之前的多數(shù)據(jù)源的配置給注釋掉了,改成使用sharding來完成多數(shù)據(jù)源。
里面我profiles.active了另一個
sharding-databases-tables.yml
db: one: primary two: secondary spring: shardingsphere: datasource: names: ${db.one},${db.two} primary: type: com.zaxxer.hikari.HikariDataSource jdbc-url: jdbc:mysql://${MYSQL_HOST:localhost}:${MYSQL_PORT:3306}/${DB_NAME:dmp_t3cc}?useUnicode=true&characterEncoding=UTF8&serverTimezone=Hongkong username: ${MYSQL_USER:root} password: ${MYSQL_USER:root} max-active: 16 secondary: type: com.zaxxer.hikari.HikariDataSource jdbc-url: jdbc:mysql://xxxxxxx:3306/t3cc?useUnicode=true&characterEncoding=UTF8&serverTimezone=Hongkong username: xxx password: xxxxxx max-active: 16 sharding: tables: pt_call_info: actual-data-nodes: ${db.one}.pt_call_info_$->{1..14} table-strategy: inline: sharding-column: today algorithm-expression: pt_call_info_$->{today} key-generator: column: id type: SNOWFLAKE pre_cc_project: actual-data-nodes: ${db.two}.pre_cc_project pre_cc_biztrack: actual-data-nodes: ${db.two}.pre_cc_biztrack
可以看到datasource里,定義了2個數(shù)據(jù)源,names=primary,secondary,這個名字隨便起。之后分別對每個數(shù)據(jù)源配置了type、url等基本信息。
在sharding里,我針對要被分表的pt_call_info表做了配置,分為14個表pt_call_info_1到pt_call_info_14,分表的原則是根據(jù)today這個字段,today為1就分到pt_call_info_1這個表。這也是我這個數(shù)據(jù)源,唯一要做配置的表。
另外,secondary這個數(shù)據(jù)源里,也有兩個表,但我不想分表,只是當成普通的數(shù)據(jù)源進行操作。所以,我只是單獨列出來他們的表名,并指定actual-data-nodes為第二個數(shù)據(jù)源的表名。這里是必須要列出來所有表的,無論是否需要分表,不然對表操作時,會報錯找不到表。所以需要手工指定。
配完這個yml就ok了,別的什么都不用配了。也不需要像之前的多數(shù)據(jù)源時,像如下的配置都不用了。不需要指定model和repository的包位置什么的。
當yml配置好后,就可以把兩個數(shù)據(jù)源的model和Repository放在任意的包下,不影響。
無論是對哪個表進行分表,都還是正常定義這個entity就行了。譬如下面就是我用來分表的model,就是個普通的entity。
之后手工把表都建好
然后就可以像平時一樣操作這個model類了。
@RunWith(SpringRunner.class) @SpringBootTest public class T3ccApplicationTests { @Resource private ProjectManager projectManager; @Resource private PtCallInfoManager ptCallInfoManager; @Test public void contextLoads() { List<PreCcProject> preCcProjectList = projectManager.findAll(); System.out.println(preCcProjectList.size()); for (int i = 1; i <= 14; i++) { PtCallInfo ptCallInfo = new PtCallInfo(); ptCallInfo.setId((Long) new SnowflakeShardingKeyGenerator().generateKey()); ptCallInfo.setToday(i); ptCallInfoManager.add(ptCallInfo); } } }
寫個測試代碼
分別從第二個數(shù)據(jù)源取值,從第一個數(shù)據(jù)源插入值,查看分表情況。
注意,id是使用特定的算法生成的,避免分表后的主鍵沖突。
運行后,可以看到分表成功。
需要注意一個坑
不要使用jpa的saveAll功能,在sharding-jdbc中,用單條去添加,如果你用了saveAll,則會失敗,插入錯誤的數(shù)據(jù)。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- SpringBoot+MybatisPlus實現(xiàn)sharding-jdbc分庫分表的示例代碼
- SpringBoot+MybatisPlus+Mysql+Sharding-JDBC分庫分表
- SpringBoot整合sharding-jdbc實現(xiàn)自定義分庫分表的實踐
- SpringBoot整合sharding-jdbc實現(xiàn)分庫分表與讀寫分離的示例
- springboot整合shardingjdbc實現(xiàn)分庫分表最簡單demo
- SpringBoot 2.0 整合sharding-jdbc中間件實現(xiàn)數(shù)據(jù)分庫分表
- SpringBoot集成Sharding-JDBC實現(xiàn)分庫分表方式
相關文章
解析Oracle數(shù)據(jù)庫中的對象集合schema
這篇文章主要介紹了Oracle數(shù)據(jù)庫中的對象集合schema,是Oracle數(shù)據(jù)庫入門學習中的基礎知識,需要的朋友可以參考下2015-11-11IDEA 單元測試報錯:Class not found:xxxx springb
這篇文章主要介紹了IDEA 單元測試報錯:Class not found:xxxx springboot的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01Java旋轉數(shù)組中最小數(shù)字具體實現(xiàn)(圖文詳解版)
這篇文章主要給大家介紹了關于Java旋轉數(shù)組中最小數(shù)字具體實現(xiàn)的相關資料,旋轉數(shù)組,說明數(shù)據(jù)不變,只是改變位置,文中通過代碼示例介紹的非常詳細,需要的朋友可以參考下2023-08-08@Autowired與@Resource在實現(xiàn)對象注入時的區(qū)別
這篇文章主要介紹了@Autowired與@Resource在實現(xiàn)對象注入時的區(qū)別,有需要的朋友可以借鑒參考下,希望能夠有所幫助2023-04-04Java使用C3P0數(shù)據(jù)源鏈接數(shù)據(jù)庫
這篇文章主要為大家詳細介紹了Java使用C3P0數(shù)據(jù)源鏈接數(shù)據(jù)庫,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-08-08