ShardingSphere jdbc集成多數(shù)據(jù)源的實(shí)現(xiàn)步驟
最近有個(gè)項(xiàng)目的幾張表,數(shù)量級(jí)在千萬(wàn)以上,技術(shù)棧是SpringBoot+Mybatis-plus+MySQL。如果使用單表,在進(jìn)行查詢(xún)操作,非常耗時(shí),經(jīng)過(guò)一番調(diào)研,決定使用分表中間件:ShardingSphere。
ShardingSphere今年4月份成為了 Apache 軟件基金會(huì)的頂級(jí)項(xiàng)目,目前支持?jǐn)?shù)據(jù)分片、讀寫(xiě)分離、多數(shù)據(jù)副本、數(shù)據(jù)加密、影子庫(kù)壓測(cè)等功能,同時(shí)兼容多種數(shù)據(jù)庫(kù),通過(guò)可插拔架構(gòu),理想情況下,可以做到對(duì)業(yè)務(wù)代碼無(wú)感知。
ShardingSphere下有兩款成熟的產(chǎn)品:sharding jdbc和sharding proxy
- sharding jdbc:可理解為增強(qiáng)版的 JDBC 驅(qū)動(dòng);
- sharding proxy:透明化的數(shù)據(jù)庫(kù)代理端,可以看做是一個(gè)虛擬的數(shù)據(jù)庫(kù)服務(wù)。
集成sharding jdbc
僅是集成sharding jdbc還是很簡(jiǎn)單的,為了更好的理解,這里以訂單表為例。
1. 引入依賴(lài)
<properties>
<sharding-sphere.version>4.1.0</sharding-sphere.version>
</properties>
<!-- 分庫(kù)分表:https://mvnrepository.com/artifact/org.apache.shardingsphere/sharding-jdbc-spring-boot-starter -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>${sharding-sphere.version}</version>
</dependency>
2. 配置分表規(guī)則
spring:
shardingsphere:
datasource:
names: sharding-order-system
sharding-order-system:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/order_system?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&useTimezone=true
username: root
password: root
props:
# 日志顯示SQL
sql.show: true
sharding:
tables:
# 訂單表 分表:20
order:
# 真實(shí)表 order_0
actualDataNodes: sharding-order-system.order_$->{0..19}
# 分庫(kù)策略
databaseStrategy:
none:
# 分表策略
tableStrategy:
inline:
shardingColumn: order_key
# 分片算法行表達(dá)式,需符合groovy語(yǔ)法 '& Integer.MAX_VALUE' 位運(yùn)算使hash值為正數(shù)
algorithmExpression: order_$->{(order_key.hashCode() & Integer.MAX_VALUE) % 20}
問(wèn)題
上面雖然完成了對(duì)訂單表(order)的分表,但是sharding jdbc對(duì)一些語(yǔ)法不支持,官方的文檔里說(shuō)的比較籠統(tǒng),如下圖:

像insert into ... select這些語(yǔ)法是不支持的,**而且對(duì)于沒(méi)有涉及到分表的語(yǔ)句,也有同樣的限制。**例如,項(xiàng)目里有個(gè)SQL:insert into user_temp select * from user;在集成了sharding jdbc后,即使user表沒(méi)有配置分表,執(zhí)行該SQL也會(huì)報(bào)錯(cuò)。
官方的問(wèn)答中提到,使用多數(shù)據(jù)源分別處理分片和不分片的情況,對(duì)分表的SQL使用sharding jdbc數(shù)據(jù)源,對(duì)不涉及到分表的SQL,使用普通數(shù)據(jù)源。

集成多數(shù)據(jù)源
我們項(xiàng)目中使用到了baomidou團(tuán)隊(duì)開(kāi)源的mybatis-plus,其團(tuán)隊(duì)還開(kāi)源了一個(gè)多數(shù)據(jù)源的組件:dynamic-datasource-spring-boot-starter,集成后,使用@DS注解就可以切換數(shù)據(jù)源,非常方便。
1. 引入依賴(lài)
<!-- https://mvnrepository.com/artifact/com.baomidou/dynamic-datasource-spring-boot-starter --> <dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>3.1.1</version> </dependency>
2. 多數(shù)據(jù)源配置
核心思路是將sharding jdbc數(shù)據(jù)源,加入到多數(shù)據(jù)源中。
/**
* 動(dòng)態(tài)數(shù)據(jù)源配置:
*
* 使用{@link com.baomidou.dynamic.datasource.annotation.DS}注解,切換數(shù)據(jù)源
*
* <code>@DS(DataSourceConfiguration.SHARDING_DATA_SOURCE_NAME)</code>
*
* @author songyinyin
* @date 2020/7/27 15:19
*/
@Configuration
@AutoConfigureBefore({DynamicDataSourceAutoConfiguration.class,
SpringBootConfiguration.class})
public class DataSourceConfiguration {
/**
* 分表數(shù)據(jù)源名稱(chēng)
*/
private static final String SHARDING_DATA_SOURCE_NAME = "gits_sharding";
/**
* 動(dòng)態(tài)數(shù)據(jù)源配置項(xiàng)
*/
@Autowired
private DynamicDataSourceProperties properties;
/**
* shardingjdbc有四種數(shù)據(jù)源,需要根據(jù)業(yè)務(wù)注入不同的數(shù)據(jù)源
*
* <p>1. 未使用分片, 脫敏的名稱(chēng)(默認(rèn)): shardingDataSource;
* <p>2. 主從數(shù)據(jù)源: masterSlaveDataSource;
* <p>3. 脫敏數(shù)據(jù)源:encryptDataSource;
* <p>4. 影子數(shù)據(jù)源:shadowDataSource
*
*/
@Lazy
@Resource(name = "shardingDataSource")
AbstractDataSourceAdapter shardingDataSource;
@Bean
public DynamicDataSourceProvider dynamicDataSourceProvider() {
Map<String, DataSourceProperty> datasourceMap = properties.getDatasource();
return new AbstractDataSourceProvider() {
@Override
public Map<String, DataSource> loadDataSources() {
Map<String, DataSource> dataSourceMap = createDataSourceMap(datasourceMap);
// 將 shardingjdbc 管理的數(shù)據(jù)源也交給動(dòng)態(tài)數(shù)據(jù)源管理
dataSourceMap.put(SHARDING_DATA_SOURCE_NAME, shardingDataSource);
return dataSourceMap;
}
};
}
/**
* 將動(dòng)態(tài)數(shù)據(jù)源設(shè)置為首選的
* 當(dāng)spring存在多個(gè)數(shù)據(jù)源時(shí), 自動(dòng)注入的是首選的對(duì)象
* 設(shè)置為主要的數(shù)據(jù)源之后,就可以支持shardingjdbc原生的配置方式了
*
* @return
*/
@Primary
@Bean
public DataSource dataSource(DynamicDataSourceProvider dynamicDataSourceProvider) {
DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource();
dataSource.setPrimary(properties.getPrimary());
dataSource.setStrict(properties.getStrict());
dataSource.setStrategy(properties.getStrategy());
dataSource.setProvider(dynamicDataSourceProvider);
dataSource.setP6spy(properties.getP6spy());
dataSource.setSeata(properties.getSeata());
return dataSource;
}
}
sharding jdbc有四種數(shù)據(jù)源:
- 未使用分片, 脫敏的名稱(chēng)(默認(rèn)):shardingDataSource;
- 主從數(shù)據(jù)源: masterSlaveDataSource;
- 脫敏數(shù)據(jù)源:encryptDataSource;
- 影子數(shù)據(jù)源:shadowDataSource
需要需要根據(jù)不同的場(chǎng)景,注入不同的數(shù)據(jù)源,本文以分表舉例,所以將shardingDataSource放到了多數(shù)據(jù)源(dataSourceMap)中。
3. 增加多數(shù)據(jù)源配置
在第2步,我們指定了shardingsphere數(shù)據(jù)源的名稱(chēng)為:gits_sharding
spring:
datasource:
# 動(dòng)態(tài)數(shù)據(jù)源配置
dynamic:
datasource:
master:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/gits?useUnicode=true&characterEncoding=utf-8&useSSL=false&rewriteBatchedStatements=true
username: root
password: root
# 指定默認(rèn)數(shù)據(jù)源名稱(chēng)
primary: master
# 分表配置
shardingsphere:
datasource:
names: sharding-order-system
sharding-order-system:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://172.20.20.19:3306/order_system?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&useTimezone=true
username: root
password: root
props:
# 日志顯示SQL
sql.show: true
sharding:
tables:
# 訂單表 分表:20
order:
# 真實(shí)表 order_0
actualDataNodes: sharding-order-system.order_$->{0..19}
# 分庫(kù)策略
databaseStrategy:
none:
# 分表策略
tableStrategy:
inline:
shardingColumn: order_key
# 分片算法行表達(dá)式,需符合groovy語(yǔ)法 '& Integer.MAX_VALUE' 位運(yùn)算使hash值為正數(shù)
algorithmExpression: order_$->{(order_key.hashCode() & Integer.MAX_VALUE) % 20}
這里將默認(rèn)數(shù)據(jù)源指定為了普通數(shù)據(jù)源。
4. 使用
在需要分表的service方法上加上@DS("gits_sharding"),即可切換為sharding jdbc數(shù)據(jù)源。
@Service
@Slf4j
public class OrderServiceImpl extends OrderService {
@Override
@DS("gits_sharding")
public List<Order> getOrderByUser(OrderQueryDTO dto) throws Exception {
// 省略若干業(yè)務(wù)代碼
...
}
}
總結(jié)
sharding jdbc雖然是Apache的頂級(jí)項(xiàng)目,但也不是對(duì)有所SQL兼容,使用多數(shù)據(jù)源 + sharding jdbc則能跳過(guò)很多sharding jdbc的不足。
到此這篇關(guān)于ShardingSphere jdbc集成多數(shù)據(jù)源的實(shí)現(xiàn)步驟的文章就介紹到這了,更多相關(guān)ShardingSphere jdbc多數(shù)據(jù)源內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringMVC實(shí)現(xiàn)通過(guò)郵件找回密碼功能
本篇文章主要介紹的是SpringMVC實(shí)現(xiàn)通過(guò)郵件找回密碼功能,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧。2016-10-10
Java時(shí)間戳類(lèi)Instant的使用詳解
這篇文章主要為大家詳細(xì)介紹了Java中時(shí)間戳類(lèi)Instant的使用方法,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Java有一定幫助,需要的可以參考一下2022-09-09
通過(guò)實(shí)例解析java過(guò)濾器和攔截器的區(qū)別
這篇文章主要介紹了通過(guò)實(shí)例解析java過(guò)濾器和攔截器的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
SpringBoot+Websocket實(shí)現(xiàn)一個(gè)簡(jiǎn)單的網(wǎng)頁(yè)聊天功能代碼
本篇文章主要介紹了SpringBoot+Websocket實(shí)現(xiàn)一個(gè)簡(jiǎn)單的網(wǎng)頁(yè)聊天功能代碼,具有一定的參考價(jià)值,有需要的可以了解一下2017-08-08
詳解Java并發(fā)編程中的優(yōu)先級(jí)隊(duì)列PriorityBlockingQueue
PriorityBlockingQueue是Java中實(shí)現(xiàn)了堆數(shù)據(jù)結(jié)構(gòu)的線(xiàn)程安全的有界阻塞隊(duì)列。本文將會(huì)深入解讀PriorityBlockingQueue的源碼實(shí)現(xiàn),感興趣的可以了解一下2023-05-05
Intellij idea下使用不同tomcat編譯maven項(xiàng)目的服務(wù)器路徑方法詳解
今天小編就為大家分享一篇關(guān)于Intellij idea下使用不同tomcat編譯maven項(xiàng)目的服務(wù)器路徑方法詳解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02
mybatis自動(dòng)生成時(shí)如何設(shè)置不生成Example類(lèi)詳解
這篇文章主要給大家介紹了關(guān)于mybatis自動(dòng)生成時(shí)如何設(shè)置不生成Example類(lèi)的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-05-05

