SpringBoot集成ShardingSphere實(shí)現(xiàn)數(shù)據(jù)庫(kù)分表
ShardingSphere功能介紹
ShardingSphere 是一個(gè)開源的分布式數(shù)據(jù)庫(kù)中間件,旨在為應(yīng)用提供數(shù)據(jù)庫(kù)分片、讀寫分離、分布式事務(wù)等功能。它能夠與現(xiàn)有的數(shù)據(jù)庫(kù)無縫集成,并且支持多種數(shù)據(jù)庫(kù)(包括 MySQL、SQL Server、Oracle 等),且無需對(duì)應(yīng)用代碼做太多改動(dòng)。
官網(wǎng):shardingsphere.apache.org/
ShardingSphere 提供以下核心功能
1.分庫(kù)分表 (Sharding):
- 通過配置規(guī)則將數(shù)據(jù)劃分到多個(gè)數(shù)據(jù)庫(kù)或表中,常見的分片方式包括基于范圍、哈希、復(fù)合字段等。
- 支持多種分片策略,如按某個(gè)字段值分片(例如,基于
user_id或order_id等字段),以及表和數(shù)據(jù)庫(kù)的分片。
2.讀寫分離 (Read/Write Splitting):
可以通過配置實(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)用層代碼無需做額外改動(dòng)。
6.支持多種數(shù)據(jù)庫(kù):
ShardingSphere 支持 MySQL、PostgreSQL、SQL Server、Oracle 等常見數(shù)據(jù)庫(kù)??梢造`活地選擇需要支持的數(shù)據(jù)庫(kù)。
7.高可擴(kuò)展性:
ShardingSphere 提供了擴(kuò)展點(diǎn),支持靈活的功能擴(kuò)展與二次開發(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)行以下步驟。通過配置 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來進(jìn)行分片,例如order_id % 2表示將數(shù)據(jù)分片為order_0或order_1。
步驟 3: 配置 ShardingSphere 數(shù)據(jù)源和分片規(guī)則
你需要編寫 Java 配置類來配置 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;
}
}
說明:
createDataSourceMap()方法將所有數(shù)據(jù)源注冊(cè)到ShardingDataSourceFactory。createShardingRuleConfig()方法定義了分片規(guī)則,分別為order和user表指定了分片策略。- 分片策略通過
InlineShardingStrategyConfiguration來設(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è)試用例,來驗(yàn)證 ShardingSphere 的分片是否按預(yù)期工作。你可以通過執(zhí)行一些簡(jiǎn)單的 CRUD 操作來檢查分片效果。
@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é)
通過上面的配置,ShardingSphere 能夠?qū)崿F(xiàn)跨多個(gè)數(shù)據(jù)庫(kù)的分片,包括 MySQL、SQL Server、Oracle 和 PostgreSQL。在實(shí)際應(yīng)用中,你可以根據(jù)需要調(diào)整分片規(guī)則,結(jié)合數(shù)據(jù)源來實(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)文章
Springboot項(xiàng)目通過redis實(shí)現(xiàn)接口的冪等性
這篇文章主要為大家介紹了Springboot項(xiàng)目通過redis實(shí)現(xiàn)接口的冪等性,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
springboot項(xiàng)目賬戶注冊(cè)邏輯功能實(shí)現(xiàn)
這篇文章主要介紹了springboot項(xiàng)目賬戶注冊(cè)邏輯功能實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-12-12
SpringBoot整合Mybatis-Plus+Druid實(shí)現(xiàn)多數(shù)據(jù)源配置功能
本文主要講解springboot?+mybatisplus?+?druid?實(shí)現(xiàn)多數(shù)據(jù)源配置功能以及一些必要的準(zhǔn)備及代碼說明,具有一定的參考價(jià)值,感興趣的小伙伴可以借鑒一下2023-06-06
Spring Cloud重試機(jī)制與各組件的重試總結(jié)
這篇文章主要給大家介紹了關(guān)于Spring Cloud中重試機(jī)制與各組件的重試的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
Java利用Redis實(shí)現(xiàn)高并發(fā)計(jì)數(shù)器的示例代碼
這篇文章主要介紹了Java利用Redis實(shí)現(xiàn)高并發(fā)計(jì)數(shù)器的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
詳解spring cloud分布式整合zipkin的鏈路跟蹤
這篇文章主要介紹了詳解spring cloud分布式整合zipkin的鏈路跟蹤,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07

