SpringBoot集成ShardingSphere實(shí)現(xiàn)數(shù)據(jù)庫(kù)分表
ShardingSphere功能介紹
ShardingSphere 是一個(gè)開(kāi)源的分布式數(shù)據(jù)庫(kù)中間件,旨在為應(yīng)用提供數(shù)據(jù)庫(kù)分片、讀寫分離、分布式事務(wù)等功能。它能夠與現(xiàn)有的數(shù)據(jù)庫(kù)無(wú)縫集成,并且支持多種數(shù)據(jù)庫(kù)(包括 MySQL、SQL Server、Oracle 等),且無(wú)需對(duì)應(yīng)用代碼做太多改動(dòng)。
官網(wǎng):shardingsphere.apache.org/
ShardingSphere 提供以下核心功能
1.分庫(kù)分表 (Sharding):
- 通過(guò)配置規(guī)則將數(shù)據(jù)劃分到多個(gè)數(shù)據(jù)庫(kù)或表中,常見(jiàn)的分片方式包括基于范圍、哈希、復(fù)合字段等。
- 支持多種分片策略,如按某個(gè)字段值分片(例如,基于
user_id或order_id等字段),以及表和數(shù)據(jù)庫(kù)的分片。
2.讀寫分離 (Read/Write Splitting):
可以通過(guò)配置實(shí)現(xiàn)讀操作與寫操作分離。一般情況下,寫操作指向主庫(kù),讀操作可以從多個(gè)副本庫(kù)進(jìn)行負(fù)載均衡讀取。
3.數(shù)據(jù)脫敏與加密 (Data Masking & Encryption):
提供數(shù)據(jù)加密與脫敏功能,保護(hù)數(shù)據(jù)庫(kù)中的敏感數(shù)據(jù)。
4.分布式事務(wù) (Distributed Transaction):
支持分布式事務(wù),確??缍鄠€(gè)數(shù)據(jù)庫(kù)操作時(shí)的一致性。
5.透明性與兼容性:
ShardingSphere 是一個(gè)數(shù)據(jù)庫(kù)中間件,能夠透明地將分片策略應(yīng)用到應(yīng)用層。它支持與 Spring Boot、Spring Cloud、MyBatis、JPA 等框架的集成,應(yīng)用層代碼無(wú)需做額外改動(dòng)。
6.支持多種數(shù)據(jù)庫(kù):
ShardingSphere 支持 MySQL、PostgreSQL、SQL Server、Oracle 等常見(jiàn)數(shù)據(jù)庫(kù)。可以靈活地選擇需要支持的數(shù)據(jù)庫(kù)。
7.高可擴(kuò)展性:
ShardingSphere 提供了擴(kuò)展點(diǎn),支持靈活的功能擴(kuò)展與二次開(kāi)發(fā)。
Spring Boot中使用ShardingSphere實(shí)現(xiàn)分表
Spring Boot中使用ShardingSphere實(shí)現(xiàn)分表,并同時(shí)支持SQL Server、MySQL、Oracle、PostgreSQL
在 Spring Boot 中使用 ShardingSphere 實(shí)現(xiàn)分表,并同時(shí)支持 SQL Server、MySQL、Oracle 和 PostgreSQL 數(shù)據(jù)庫(kù),需要進(jìn)行以下步驟。通過(guò)配置 ShardingSphere 的分片策略,結(jié)合不同數(shù)據(jù)庫(kù)的連接設(shè)置,可以實(shí)現(xiàn)跨多個(gè)數(shù)據(jù)庫(kù)的數(shù)據(jù)分片。
步驟 1: 引入必要的依賴
在 pom.xml 文件中,添加 Spring Boot、ShardingSphere 和不同數(shù)據(jù)庫(kù)的 JDBC 驅(qū)動(dòng)。
<dependencies>
<!-- Spring Boot 基礎(chǔ)依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- ShardingSphere-JDBC for Spring Boot -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>5.0.0</version> <!-- 請(qǐng)根據(jù)需要選擇合適的版本 -->
</dependency>
<!-- MySQL 驅(qū)動(dòng) -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- SQL Server 驅(qū)動(dòng) -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>9.4.1.jre8</version>
</dependency>
<!-- Oracle 驅(qū)動(dòng) -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.8.0.0</version>
</dependency>
<!-- PostgreSQL 驅(qū)動(dòng) -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.5.0</version>
</dependency>
</dependencies>
步驟 2: 配置 application.yml
在 application.yml 中,配置 ShardingSphere 相關(guān)數(shù)據(jù)源以及分片策略。
spring:
datasource:
# 配置 MySQL 數(shù)據(jù)源
mysql:
url: jdbc:mysql://localhost:3306/mysql_db
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
# 配置 SQL Server 數(shù)據(jù)源
sqlserver:
url: jdbc:sqlserver://localhost:1433;databaseName=sqlserver_db
username: sa
password: password
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
# 配置 Oracle 數(shù)據(jù)源
oracle:
url: jdbc:oracle:thin:@localhost:1521:orcl
username: oracle
password: oracle
driver-class-name: oracle.jdbc.OracleDriver
# 配置 PostgreSQL 數(shù)據(jù)源
postgresql:
url: jdbc:postgresql://localhost:5432/postgresql_db
username: postgres
password: password
driver-class-name: org.postgresql.Driver
sharding:
jdbc:
config:
data-source:
mysql:
url: jdbc:mysql://localhost:3306/mysql_db
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
sqlserver:
url: jdbc:sqlserver://localhost:1433;databaseName=sqlserver_db
username: sa
password: password
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
oracle:
url: jdbc:oracle:thin:@localhost:1521:orcl
username: oracle
password: oracle
driver-class-name: oracle.jdbc.OracleDriver
postgresql:
url: jdbc:postgresql://localhost:5432/postgresql_db
username: postgres
password: password
driver-class-name: org.postgresql.Driver
rules:
sharding:
tables:
order:
actual-data-nodes: mysql.order_${0..1},sqlserver.order_${0..1},oracle.order_${0..1},postgresql.order_${0..1}
table-strategy:
inline:
sharding-column: order_id
algorithm-expression: order_${order_id % 2}
user:
actual-data-nodes: mysql.user_${0..1},sqlserver.user_${0..1},oracle.user_${0..1},postgresql.user_${0..1}
table-strategy:
inline:
sharding-column: user_id
algorithm-expression: user_${user_id % 2}
解釋:
- data-source: 定義了每個(gè)數(shù)據(jù)庫(kù)的數(shù)據(jù)源,包括 MySQL、SQL Server、Oracle 和 PostgreSQL。
- sharding: 配置了分片規(guī)則。每個(gè)表(如
order和user)在不同數(shù)據(jù)庫(kù)中都有實(shí)際的數(shù)據(jù)節(jié)點(diǎn),例如mysql.order_0,sqlserver.order_0等。 - table-strategy: 使用
inline策略,依據(jù)order_id或user_id來(lái)進(jìn)行分片,例如order_id % 2表示將數(shù)據(jù)分片為order_0或order_1。
步驟 3: 配置 ShardingSphere 數(shù)據(jù)源和分片規(guī)則
你需要編寫 Java 配置類來(lái)配置 ShardingSphere 數(shù)據(jù)源和分片規(guī)則。
ShardingConfig.java:
@Configuration
@EnableTransactionManagement
public class ShardingConfig {
@Bean
public DataSource dataSource() throws SQLException {
return ShardingDataSourceFactory.createDataSource(createDataSourceMap(), createShardingRuleConfig());
}
private Map<String, DataSource> createDataSourceMap() {
Map<String, DataSource> dataSourceMap = new HashMap<>();
dataSourceMap.put("mysql", createDataSource("mysql"));
dataSourceMap.put("sqlserver", createDataSource("sqlserver"));
dataSourceMap.put("oracle", createDataSource("oracle"));
dataSourceMap.put("postgresql", createDataSource("postgresql"));
return dataSourceMap;
}
private DataSource createDataSource(String dataSourceName) {
HikariDataSource dataSource = new HikariDataSource();
switch (dataSourceName) {
case "mysql":
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mysql_db");
dataSource.setUsername("root");
dataSource.setPassword("root");
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
break;
case "sqlserver":
dataSource.setJdbcUrl("jdbc:sqlserver://localhost:1433;databaseName=sqlserver_db");
dataSource.setUsername("sa");
dataSource.setPassword("password");
dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
break;
case "oracle":
dataSource.setJdbcUrl("jdbc:oracle:thin:@localhost:1521:orcl");
dataSource.setUsername("oracle");
dataSource.setPassword("oracle");
dataSource.setDriverClassName("oracle.jdbc.OracleDriver");
break;
case "postgresql":
dataSource.setJdbcUrl("jdbc:postgresql://localhost:5432/postgresql_db");
dataSource.setUsername("postgres");
dataSource.setPassword("password");
dataSource.setDriverClassName("org.postgresql.Driver");
break;
default:
throw new IllegalArgumentException("Unsupported DataSource");
}
return dataSource;
}
private ShardingRuleConfiguration createShardingRuleConfig() {
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
// 配置訂單表的分片規(guī)則
shardingRuleConfig.getTableRuleConfigs().add(createOrderTableRule());
// 配置用戶表的分片規(guī)則
shardingRuleConfig.getTableRuleConfigs().add(createUserTableRule());
// 設(shè)置默認(rèn)數(shù)據(jù)源
shardingRuleConfig.setDefaultDataSourceName("mysql");
return shardingRuleConfig;
}
private TableRuleConfiguration createOrderTableRule() {
TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration();
orderTableRuleConfig.setLogicTable("order");
orderTableRuleConfig.setActualDataNodes("mysql.order_${0..1},sqlserver.order_${0..1},oracle.order_${0..1},postgresql.order_${0..1}");
orderTableRuleConfig.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("order_id", "order_${order_id % 2}"));
return orderTableRuleConfig;
}
private TableRuleConfiguration createUserTableRule() {
TableRuleConfiguration userTableRuleConfig = new TableRuleConfiguration();
userTableRuleConfig.setLogicTable("user");
userTableRuleConfig.setActualDataNodes("mysql.user_${0..1},sqlserver.user_${0..1},oracle.user_${0..1},postgresql.user_${0..1}");
userTableRuleConfig.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "user_${user_id % 2}"));
return userTableRuleConfig;
}
}
說(shuō)明:
createDataSourceMap()方法將所有數(shù)據(jù)源注冊(cè)到ShardingDataSourceFactory。createShardingRuleConfig()方法定義了分片規(guī)則,分別為order和user表指定了分片策略。- 分片策略通過(guò)
InlineShardingStrategyConfiguration來(lái)設(shè)置,依據(jù)字段order_id或user_id進(jìn)行分片。
步驟 4: 創(chuàng)建實(shí)體類
你需要根據(jù)業(yè)務(wù)需求定義實(shí)體類。例如:
@Entity
public class Order {
@Id
private Long orderId;
private String orderDetails;
// getters and setters
}
@Entity
public class User {
@Id
private Long userId;
private String userName;
// getters and setters
}
步驟 5: 測(cè)試
創(chuàng)建一些簡(jiǎn)單的測(cè)試用例,來(lái)驗(yàn)證 ShardingSphere 的分片是否按預(yù)期工作。你可以通過(guò)執(zhí)行一些簡(jiǎn)單的 CRUD 操作來(lái)檢查分片效果。
@RunWith(SpringRunner.class)
@SpringBootTest
public class ShardingTest {
@Autowired
private OrderRepository orderRepository;
@Test
public void testOrderSharding() {
Order order = new Order();
order.setOrderId(1L);
order.setOrderDetails("Test order");
orderRepository.save(order);
Optional<Order> savedOrder = orderRepository.findById(1L);
assertTrue(savedOrder.isPresent());
}
}
總結(jié)
通過(guò)上面的配置,ShardingSphere 能夠?qū)崿F(xiàn)跨多個(gè)數(shù)據(jù)庫(kù)的分片,包括 MySQL、SQL Server、Oracle 和 PostgreSQL。在實(shí)際應(yīng)用中,你可以根據(jù)需要調(diào)整分片規(guī)則,結(jié)合數(shù)據(jù)源來(lái)實(shí)現(xiàn)分布式數(shù)據(jù)管理。
以上就是SpringBoot集成ShardingSphere實(shí)現(xiàn)數(shù)據(jù)庫(kù)分表的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot ShardingSphere數(shù)據(jù)庫(kù)分表的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- SpringBoot如何配置數(shù)據(jù)庫(kù)主從shardingsphere
- SpringBoot+Mybatis-plus+shardingsphere實(shí)現(xiàn)分庫(kù)分表的方案
- SpringBoot?整合?ShardingSphere4.1.1實(shí)現(xiàn)分庫(kù)分表功能
- SpringBoot3和ShardingSphere5框架實(shí)現(xiàn)數(shù)據(jù)分庫(kù)分表
- SpringBoot整合ShardingSphere的示例代碼
- Springboot2.x+ShardingSphere實(shí)現(xiàn)分庫(kù)分表的示例代碼
相關(guān)文章
Java中使用Levenshtein距離實(shí)現(xiàn)字符串相似度匹配方式
這篇文章主要介紹了Java中使用Levenshtein距離實(shí)現(xiàn)字符串相似度匹配方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-06-06
SpringBoot集成P6Spy的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot集成P6Spy的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-08-08
Linux將Spring Boot項(xiàng)目的Jar包注冊(cè)為開(kāi)機(jī)自啟動(dòng)系統(tǒng)服務(wù)的操作方法
jar文件是從maven package打包出來(lái)的,config/application.yml是原先在項(xiàng)目的resources文件夾里,外置出來(lái)方便適配開(kāi)發(fā)環(huán)境和正式環(huán)境,這篇文章主要介紹了Linux將Spring Boot項(xiàng)目的Jar包注冊(cè)為開(kāi)機(jī)自啟動(dòng)系統(tǒng)服務(wù)的操作方法,需要的朋友可以參考下2023-10-10
簡(jiǎn)單介紹線性表以及如何實(shí)現(xiàn)雙鏈表
本文先介紹線性表的幾個(gè)基本組成部分:數(shù)組、單向鏈表、雙向鏈表;隨后給出雙向鏈表的C、C++和Java三種語(yǔ)言的實(shí)現(xiàn),需要的朋友可以參考下2015-07-07
Spring Boot整合MybatisPlus逆向工程(MySQL/PostgreSQL)
MyBatis-Plus是MyBatis的增強(qiáng)工具,本文主要介紹了Spring Boot整合MybatisPlus逆向工程(MySQL/PostgreSQL),具有一定的參考價(jià)值,感興趣的可以了解一下2021-07-07
使用HttpServletResponse對(duì)象獲取請(qǐng)求行信息
這篇文章主要介紹了使用HttpServletResponse對(duì)象獲取請(qǐng)求行信息,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02

