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: trueyml里還是老套路,大家注意,我把之前的多數(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實現(xiàn)分庫分表
- SpringBoot整合sharding-jdbc實現(xiàn)自定義分庫分表的實踐
- SpringBoot整合sharding-jdbc實現(xiàn)分庫分表與讀寫分離的示例
- Spring Boot 集成 Sharding-JDBC + Mybatis-Plus 實現(xiàn)分庫分表功能
- springboot jpa分庫分表項目實現(xiàn)過程詳解
- Springboot2.x+ShardingSphere實現(xiàn)分庫分表的示例代碼
- SpringBoot 2.0 整合sharding-jdbc中間件實現(xiàn)數(shù)據(jù)分庫分表
- Spring Boot 分庫分表策略示例展示
相關文章
SpringCloudAlibaba是一款優(yōu)秀的微服務架構,在市面上有著廣泛的應用,這篇文章介紹了SpringCloudAlibaba的一些基本使用,適合初學者,希望能夠給大家?guī)韼椭?/div> 2024-08-08
在windows下揪出java程序占用cpu很高的線程并完美解決
這篇文章主要介紹了在windows下揪出java程序占用cpu很高的線程并完美解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
Java使用ByteBuffer進行多文件合并和拆分的代碼實現(xiàn)
因為驗證證書的需要,需要把證書文件和公鑰給到客戶,考慮到多個文件交互的不便性,所以決定將2個文件合并成一個文件交互給客戶,但是由于是加密文件,采用字符串形式合并后,拆分后文件不可用,本文給大家介紹了Java使用ByteBuffer進行多文件合并和拆分,需要的朋友可以參考下2024-09-09
java使用MulticastSocket實現(xiàn)基于廣播的多人聊天室
這篇文章主要為大家詳細介紹了java使用MulticastSocket實現(xiàn)基于廣播的多人聊天室,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01最新評論

