最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot整合sharding-jdbc?實現(xiàn)分庫分表操作的示例代碼

 更新時間:2025年04月11日 09:36:36   作者:小馬不敲代碼  
在Spring?Boot中使用ShardingSphere的Sharding-JDBC來實現(xiàn)數(shù)據(jù)庫的分庫分表是一個常見的需求,下面就拉具體介紹一下實現(xiàn)步驟,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1、簡介

Sharding-JDBC 是一款開源的分布式數(shù)據(jù)庫中間件,旨在簡化分庫分表(Sharding)的實現(xiàn)。它通過透明化的方式將數(shù)據(jù)分布到多個數(shù)據(jù)庫或表中,同時提供與原生 JDBC 一致的開發(fā)體驗。Sharding-JDBC 是 Apache ShardingSphere 項目的子模塊之一,廣泛應(yīng)用于高并發(fā)、大數(shù)據(jù)量的場景。

核心功能

1、分庫分表:

支持水平分庫和水平分表,將數(shù)據(jù)分散到多個數(shù)據(jù)庫或表中,提升系統(tǒng)性能和存儲能力。

支持多種分片策略,如按范圍、哈希、時間等。

2、讀寫分離:

支持主從架構(gòu)的讀寫分離,將讀操作路由到從庫,寫操作路由到主庫,減輕主庫壓力。

3、分布式事務(wù):

提供基于 XA 和柔性 事務(wù)(Saga、TCC)的分布式事務(wù)支持,確保數(shù)據(jù)一致性。

4、數(shù)據(jù)脫敏:

支持對敏感數(shù)據(jù)進行加密和脫敏處理,保障數(shù)據(jù)安全。

5、多數(shù)據(jù)源管理:

支持動態(tài)數(shù)據(jù)源配置和管理,方便擴展和維護。

6、SQL 兼容性:

支持絕大多數(shù) SQL 語法,兼容主流數(shù)據(jù)庫(如 MySQL、PostgreSQL、Oracle 等)。

核心概念

1、邏輯表(Logic Table):

開發(fā)中使用的虛擬表名,例如 order。

2、真實表(Actual Table):

數(shù)據(jù)庫中實際存在的表,例如 order_0、order_1。

3、數(shù)據(jù)節(jié)點(Data Node):

數(shù)據(jù)分片的最小單元,由數(shù)據(jù)源名稱和真實表組成,例如 ds0.order_0。

4、分片鍵(Sharding Key):

用于分片的字段,例如訂單表中的 user_id。

5、分片算法(Sharding Algorithm):

定義如何根據(jù)分片鍵將數(shù)據(jù)路由到不同的數(shù)據(jù)庫或表。

6、綁定表(Binding Table):

具有相同分片規(guī)則的表,例如 order 和 order_item,可以避免跨表查詢。

架構(gòu)設(shè)計

Sharding-JDBC 采用輕量級的架構(gòu)設(shè)計,直接嵌入應(yīng)用程序中,無需額外部署中間件。其核心組件包括:

  • SQL 解析引擎:解析 SQL 語句,提取分片鍵。

  • 路由引擎:根據(jù)分片規(guī)則將 SQL 路由到正確的數(shù)據(jù)節(jié)點。

  • 改寫引擎:將邏輯 SQL 改寫為真實 SQL。

  • 執(zhí)行引擎:執(zhí)行 SQL 并合并結(jié)果。

使用場景

1、高并發(fā)場景:

通過分庫分表提升系統(tǒng)的并發(fā)處理能力。

2、大數(shù)據(jù)量場景:

通過數(shù)據(jù)分片解決單表數(shù)據(jù)量過大的問題。

3、讀寫分離場景:

通過讀寫分離提升數(shù)據(jù)庫的讀性能。

4、多租戶場景:

通過分庫分表實現(xiàn)多租戶數(shù)據(jù)隔離。

2、實戰(zhàn)流程

2.1. pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <artifactId>spring-boot-demo-sharding-jdbc</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>spring-boot-demo-sharding-jdbc</name>
  <description>Demo project for Spring Boot</description>

  <parent>
    <groupId>com.xiaoma</groupId>
    <artifactId>spring-boot-demo</artifactId>
    <version>1.0.0-SNAPSHOT</version>
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-boot-starter</artifactId>
      <version>3.1.0</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>

    <dependency>
      <groupId>io.shardingsphere</groupId>
      <artifactId>sharding-jdbc-core</artifactId>
      <version>3.1.0</version>
    </dependency>

    <dependency>
      <groupId>cn.hutool</groupId>
      <artifactId>hutool-all</artifactId>
    </dependency>

    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <optional>true</optional>
    </dependency>
  </dependencies>

  <build>
    <finalName>spring-boot-demo-sharding-jdbc</finalName>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

</project>

2.2. CustomSnowflakeKeyGenerator.java

package com.xiaoma.sharding.jdbc.config;

import cn.hutool.core.lang.Snowflake;
import io.shardingsphere.core.keygen.KeyGenerator;

public class CustomSnowflakeKeyGenerator implements KeyGenerator {
    private Snowflake snowflake;

    public CustomSnowflakeKeyGenerator(Snowflake snowflake) {
        this.snowflake = snowflake;
    }
    
    @Override
    public Number generateKey() {
        return snowflake.nextId();
    }
}

2.3. DataSourceShardingConfig.java

@Configuration
public class DataSourceShardingConfig {
    private static final Snowflake snowflake = IdUtil.createSnowflake(1, 1);

    /**
     * 需要手動配置事務(wù)管理器
     */
    @Bean
    public DataSourceTransactionManager transactionManager(@Qualifier("dataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "dataSource")
    @Primary
    public DataSource dataSource() throws SQLException {
        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
        // 設(shè)置分庫策略
        shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "ds${user_id % 2}"));
        // 設(shè)置規(guī)則適配的表
        shardingRuleConfig.getBindingTableGroups().add("t_order");
        // 設(shè)置分表策略
        shardingRuleConfig.getTableRuleConfigs().add(orderTableRule());
        shardingRuleConfig.setDefaultDataSourceName("ds0");
        shardingRuleConfig.setDefaultTableShardingStrategyConfig(new NoneShardingStrategyConfiguration());

        Properties properties = new Properties();
        properties.setProperty("sql.show", "true");

        return ShardingDataSourceFactory.createDataSource(dataSourceMap(), shardingRuleConfig, new ConcurrentHashMap<>(16), properties);
    }

    private TableRuleConfiguration orderTableRule() {
        TableRuleConfiguration tableRule = new TableRuleConfiguration();
        // 設(shè)置邏輯表名
        tableRule.setLogicTable("t_order");
        // ds${0..1}.t_order_${0..2} 也可以寫成 ds$->{0..1}.t_order_$->{0..1}
        tableRule.setActualDataNodes("ds${0..1}.t_order_${0..2}");
        tableRule.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("order_id", "t_order_$->{order_id % 3}"));
        tableRule.setKeyGenerator(customKeyGenerator());
        tableRule.setKeyGeneratorColumnName("order_id");
        return tableRule;
    }

    private Map<String, DataSource> dataSourceMap() {
        Map<String, DataSource> dataSourceMap = new HashMap<>(16);

        // 配置第一個數(shù)據(jù)源
        HikariDataSource ds0 = new HikariDataSource();
        ds0.setDriverClassName("com.mysql.cj.jdbc.Driver");
        ds0.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test1?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8");
        ds0.setUsername("root");
        ds0.setPassword("root");

        // 配置第二個數(shù)據(jù)源
        HikariDataSource ds1 = new HikariDataSource();
        ds1.setDriverClassName("com.mysql.cj.jdbc.Driver");
        ds1.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test2?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8");
        ds1.setUsername("root");
        ds1.setPassword("root");

        dataSourceMap.put("ds0", ds0);
        dataSourceMap.put("ds1", ds1);
        return dataSourceMap;
    }

    /**
     * 自定義主鍵生成器
     */
    private KeyGenerator customKeyGenerator() {
        return new CustomSnowflakeKeyGenerator(snowflake);
    }

}

2.4. SpringBootDemoShardingJdbcApplicationTests.java

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootDemoShardingJdbcApplicationTests {
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 測試新增
     */
    @Test
    public void testInsert() {
        for (long i = 1; i < 10; i++) {
            for (long j = 1; j < 20; j++) {
                Order order = Order.builder().userId(i).orderId(j).remark(RandomUtil.randomString(20)).build();
                orderMapper.insert(order);
            }
        }
    }

    /**
     * 測試更新
     */
    @Test
    public void testUpdate() {
        Order update = new Order();
        update.setRemark("修改備注信息");
        orderMapper.update(update, Wrappers.<Order>update().lambda().eq(Order::getOrderId, 2).eq(Order::getUserId, 2));
    }

    /**
     * 測試刪除
     */
    @Test
    public void testDelete() {
        orderMapper.delete(new QueryWrapper<>());
    }

    /**
     * 測試查詢
     */
    @Test
    public void testSelect() {
        List<Order> orders = orderMapper.selectList(Wrappers.<Order>query().lambda().in(Order::getOrderId, 1, 2));
        log.info("【orders】= {}", JSONUtil.toJsonStr(orders));
    }
    
}

到此這篇關(guān)于SpringBoot整合sharding-jdbc 實現(xiàn)分庫分表操作的示例代碼的文章就介紹到這了,更多相關(guān)SpringBoot sharding-jdbc分庫分表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • 詳解Java是如何通過接口來創(chuàng)建代理并進行http請求

    詳解Java是如何通過接口來創(chuàng)建代理并進行http請求

    今天給大家?guī)淼闹R是關(guān)于Java的,文章圍繞Java是如何通過接口來創(chuàng)建代理并進行http請求展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • springboot中在非bean類中調(diào)用bean的實現(xiàn)方法

    springboot中在非bean類中調(diào)用bean的實現(xiàn)方法

    在Spring Boot中,非Bean類調(diào)用Bean方法通常需要通過靜態(tài)方法獲取Bean實例,然后調(diào)用相應(yīng)的方法,這種方法避免了直接在非Bean類中注入Bean,保持了代碼的簡潔和可維護性,通過這種方式,可以在不改變原有代碼結(jié)構(gòu)的情況下,實現(xiàn)Bean方法的調(diào)用
    2025-02-02
  • java static塊和構(gòu)造函數(shù)的實例詳解

    java static塊和構(gòu)造函數(shù)的實例詳解

    這篇文章主要介紹了java static塊和構(gòu)造函數(shù)的實例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握Java static關(guān)鍵字的函數(shù)方法,需要的朋友可以參考下
    2017-09-09
  • SpringBoot登錄攔截配置詳解(實測可用)

    SpringBoot登錄攔截配置詳解(實測可用)

    這篇文章主要介紹了SpringBoot登錄攔截配置詳解(實測可用),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Spring:@Async注解和AsyncResult與CompletableFuture使用問題

    Spring:@Async注解和AsyncResult與CompletableFuture使用問題

    這篇文章主要介紹了Spring:@Async注解和AsyncResult與CompletableFuture使用問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • MyBatis-Plus配置未生效的問題及解決

    MyBatis-Plus配置未生效的問題及解決

    本文主要探討了在使用MyBatis-Plus進行數(shù)據(jù)庫操作時遇到的問題,包括SQL語句未生效、Mapper接口未掃描、屬性未注入、Bean未注冊等問題,通過分析SpringBoot的配置類加載機制,本文提出了兩種解決方案:添加@Configuration注解和使用AutoConfiguration.imports文件
    2025-12-12
  • 基于SpringBoot實現(xiàn)自動裝配返回屬性的設(shè)計思路

    基于SpringBoot實現(xiàn)自動裝配返回屬性的設(shè)計思路

    這篇文章主要介紹了基于SpringBoot實現(xiàn)自動裝配返回屬性,這里涉及到的技術(shù)知識點有注解解析器,為什么用ResponseBodyAdvice這里解析?不在Filter,Interceptors,本文結(jié)合示例代碼給大家介紹的非常詳細,需要的朋友參考下吧
    2022-03-03
  • springboot更新配置Swagger3的一些小技巧

    springboot更新配置Swagger3的一些小技巧

    今天給大家分享springboot更新配置Swagger3的方法,大家需要注意Swagger3版本需要引入依賴,具體示例代碼參考下本文
    2021-07-07
  • java模擬cookie登陸操作

    java模擬cookie登陸操作

    這篇文章主要為大家詳細介紹了java模擬cookie登陸操作,模擬登陸,取得cookie以記錄身份,下次請求時發(fā)送cookie以表明身份,感興趣的小伙伴們可以參考一下
    2016-07-07
  • 一篇文章帶你復(fù)習(xí)java知識點

    一篇文章帶你復(fù)習(xí)java知識點

    以下簡單介紹了下我對于這些java基本知識點和技術(shù)點的一些看法和心得,這些內(nèi)容都源自于我這些年來使用java的一些總結(jié),希望能夠給你帶來幫助
    2021-06-06

最新評論

隆安县| 大港区| 襄城县| 修水县| 尖扎县| 惠安县| 方城县| 丹阳市| 偏关县| 株洲县| 永新县| 玛纳斯县| 赣州市| 巫溪县| 鹿泉市| 安丘市| 临沧市| 沿河| 金门县| 乌苏市| 宜兰市| 年辖:市辖区| 布拖县| 建水县| 喀什市| 吉首市| 平原县| 临安市| 海晏县| 内乡县| 海门市| 黎城县| 米泉市| 东台市| 巧家县| 阳东县| 延寿县| 成都市| 准格尔旗| 紫云| 合山市|