SpringBoot?如何使用sharding?jdbc進(jìn)行分庫(kù)分表
基于4.0版本,Springboot2.1
之前寫(xiě)過(guò)一篇使用sharding-jdbc進(jìn)行分庫(kù)分表的文章,不過(guò)當(dāng)時(shí)的版本還比較早,現(xiàn)在已經(jīng)不能用了。這一篇是基于最新版來(lái)寫(xiě)的。
新版已經(jīng)變成了shardingsphere了,https://shardingsphere.apache.org/。
有點(diǎn)不同的是,這一篇,我們是采用多數(shù)據(jù)源,僅對(duì)一個(gè)數(shù)據(jù)源進(jìn)行分表。也就是說(shuō)在網(wǎng)上那些jpa多數(shù)據(jù)源的配置,用sharding jdbc一樣能完成。
也就是說(shuō)我們有兩個(gè)庫(kù),一個(gè)庫(kù)是正常使用,另一個(gè)庫(kù)其中的一個(gè)表進(jìn)行分表。
老套路,我們還是使用Springboot進(jìn)行集成,
在pom里確保有如下引用
<sharding-sphere.version>4.0.0-RC1</sharding-sphere.version>
<!-- 分庫(kù)分表--> <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> <!-- 分庫(kù)分表 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 #不加這句則默認(rèn)為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來(lái)完成多數(shù)據(jù)源。
里面我profiles.active了另一個(gè)
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個(gè)數(shù)據(jù)源,names=primary,secondary,這個(gè)名字隨便起。之后分別對(duì)每個(gè)數(shù)據(jù)源配置了type、url等基本信息。
在sharding里,我針對(duì)要被分表的pt_call_info表做了配置,分為14個(gè)表pt_call_info_1到pt_call_info_14,分表的原則是根據(jù)today這個(gè)字段,today為1就分到pt_call_info_1這個(gè)表。這也是我這個(gè)數(shù)據(jù)源,唯一要做配置的表。
另外,secondary這個(gè)數(shù)據(jù)源里,也有兩個(gè)表,但我不想分表,只是當(dāng)成普通的數(shù)據(jù)源進(jìn)行操作。所以,我只是單獨(dú)列出來(lái)他們的表名,并指定actual-data-nodes為第二個(gè)數(shù)據(jù)源的表名。這里是必須要列出來(lái)所有表的,無(wú)論是否需要分表,不然對(duì)表操作時(shí),會(huì)報(bào)錯(cuò)找不到表。所以需要手工指定。
配完這個(gè)yml就ok了,別的什么都不用配了。也不需要像之前的多數(shù)據(jù)源時(shí),像如下的配置都不用了。不需要指定model和repository的包位置什么的。
當(dāng)yml配置好后,就可以把兩個(gè)數(shù)據(jù)源的model和Repository放在任意的包下,不影響。
無(wú)論是對(duì)哪個(gè)表進(jìn)行分表,都還是正常定義這個(gè)entity就行了。譬如下面就是我用來(lái)分表的model,就是個(gè)普通的entity。
之后手工把表都建好
然后就可以像平時(shí)一樣操作這個(gè)model類(lèi)了。
@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); } } }
寫(xiě)個(gè)測(cè)試代碼
分別從第二個(gè)數(shù)據(jù)源取值,從第一個(gè)數(shù)據(jù)源插入值,查看分表情況。
注意,id是使用特定的算法生成的,避免分表后的主鍵沖突。
運(yùn)行后,可以看到分表成功。
需要注意一個(gè)坑
不要使用jpa的saveAll功能,在sharding-jdbc中,用單條去添加,如果你用了saveAll,則會(huì)失敗,插入錯(cuò)誤的數(shù)據(jù)。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- SpringBoot+MybatisPlus實(shí)現(xiàn)sharding-jdbc分庫(kù)分表的示例代碼
- SpringBoot+MybatisPlus+Mysql+Sharding-JDBC分庫(kù)分表
- SpringBoot整合sharding-jdbc實(shí)現(xiàn)自定義分庫(kù)分表的實(shí)踐
- SpringBoot整合sharding-jdbc實(shí)現(xiàn)分庫(kù)分表與讀寫(xiě)分離的示例
- springboot整合shardingjdbc實(shí)現(xiàn)分庫(kù)分表最簡(jiǎn)單demo
- SpringBoot 2.0 整合sharding-jdbc中間件實(shí)現(xiàn)數(shù)據(jù)分庫(kù)分表
- SpringBoot集成Sharding-JDBC實(shí)現(xiàn)分庫(kù)分表方式
相關(guān)文章
解析Oracle數(shù)據(jù)庫(kù)中的對(duì)象集合schema
這篇文章主要介紹了Oracle數(shù)據(jù)庫(kù)中的對(duì)象集合schema,是Oracle數(shù)據(jù)庫(kù)入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-11-11IDEA 單元測(cè)試報(bào)錯(cuò):Class not found:xxxx springb
這篇文章主要介紹了IDEA 單元測(cè)試報(bào)錯(cuò):Class not found:xxxx springboot的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01Java旋轉(zhuǎn)數(shù)組中最小數(shù)字具體實(shí)現(xiàn)(圖文詳解版)
這篇文章主要給大家介紹了關(guān)于Java旋轉(zhuǎn)數(shù)組中最小數(shù)字具體實(shí)現(xiàn)的相關(guān)資料,旋轉(zhuǎn)數(shù)組,說(shuō)明數(shù)據(jù)不變,只是改變位置,文中通過(guò)代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08零基礎(chǔ)如何系統(tǒng)的學(xué)習(xí)Java
這篇文章主要介紹了零基礎(chǔ)如何系統(tǒng)的學(xué)習(xí)Java,很多朋友糾結(jié)這個(gè)問(wèn)題,教材書(shū)不知道從何學(xué)起,今天小編給大家分享一篇教程幫助到家梳理這方面的知識(shí)2020-07-07@Autowired與@Resource在實(shí)現(xiàn)對(duì)象注入時(shí)的區(qū)別
這篇文章主要介紹了@Autowired與@Resource在實(shí)現(xiàn)對(duì)象注入時(shí)的區(qū)別,有需要的朋友可以借鑒參考下,希望能夠有所幫助2023-04-04java 代理模式及動(dòng)態(tài)代理機(jī)制深入分析
這篇文章主要介紹了java 代理模式及動(dòng)態(tài)代理機(jī)制深入分析的相關(guān)資料, 代理是一種常用的設(shè)計(jì)模式,其目的就是為其他對(duì)象提供一個(gè)代理以控制對(duì)某個(gè)對(duì)象的訪(fǎng)問(wèn),需要的朋友可以參考下2017-03-03Java使用C3P0數(shù)據(jù)源鏈接數(shù)據(jù)庫(kù)
這篇文章主要為大家詳細(xì)介紹了Java使用C3P0數(shù)據(jù)源鏈接數(shù)據(jù)庫(kù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08