<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          SpringBoot + ShardingSphere 秒級分庫分表!

          共 5318字,需瀏覽 11分鐘

           ·

          2022-01-21 03:56

          Spring Boot 作為主流微服務(wù)框架,擁有成熟的社區(qū)生態(tài)。市場應(yīng)用廣泛,為了方便大家,整理了一個基于spring boot的常用中間件快速集成入門系列手冊,涉及RPC、緩存、消息隊列、分庫分表、注冊中心、分布式配置等常用開源組件,大概有幾十篇文章,陸續(xù)會開放出來,感興趣同學請?zhí)崆瓣P(guān)注&收藏

          互聯(lián)網(wǎng)高速發(fā)展,同時也帶來的海量數(shù)據(jù)存儲問題。傳統(tǒng)關(guān)系型數(shù)據(jù)庫的單庫單表已經(jīng)很難支撐,如何高效存儲和訪問這些數(shù)據(jù),成為業(yè)內(nèi)急需解決的問題。解決思路有兩個方向:

          • NoSQL數(shù)據(jù)庫,非關(guān)系型數(shù)據(jù)庫,天然集成了類似分布式分片的功能,支持海量數(shù)據(jù)存儲,但是不具備事務(wù)管理
          • 分庫分表,對多個單庫單表資源整合,并配備資源調(diào)度模塊,從而形成一個具有海量數(shù)據(jù)儲存的邏輯表。

          今天我們主要介紹,如何基于Springboot快速集成分庫分表框架,盡量做到開箱即用

          當然除了ShardingSphere之外,還有其他分庫分表框架,如:CobarMyCat

          ShardingSphere介紹

          ShardingSphere 是一套開源的分布式數(shù)據(jù)庫中間件解決方案組成的生態(tài)圈,它由 3 款相互獨立,卻又能夠混合部署配合使用的產(chǎn)品組成。它們均提供標準化的數(shù)據(jù)分片、分布式事務(wù)和數(shù)據(jù)庫治理功能

          ShardingSphere 由三個子項目組成,形成一個完整的數(shù)據(jù)庫解決方案。

          1、ShardingSphere-JDBC:定位為輕量級 Java 框架,在 Java 的 JDBC 層提供額外服務(wù)。它使用客戶端直連數(shù)據(jù)庫,以 jar 包形式提供服務(wù),無需額外部署和依賴,可理解為增強版的 JDBC 驅(qū)動,完全兼容 JDBC 和各種 ORM 框架。

          適用于任何基于Java的ORM框架,如:JPA, Hibernate, Mybatis, Spring JDBC Template或直接使用JDBC。基于任何第三方的數(shù)據(jù)庫連接池,如:DBCP, C3P0, BoneCP, Druid, HikariCP等。支持任意實現(xiàn)JDBC規(guī)范的數(shù)據(jù)庫。目前支持MySQL,Oracle,SQLServer和PostgreSQL。

          2、ShardingSphere-Proxy:定位為透明化的數(shù)據(jù)庫代理端,提供封裝了數(shù)據(jù)庫二進制協(xié)議的服務(wù)端版本,用于完成對異構(gòu)語言的支持。目前提供 MySQL/PostgreSQL 版本,它可以使用任何兼容 MySQL/PostgreSQL 協(xié)議的訪問客戶端操作數(shù)據(jù),對 DBA 更加友好。

          3、ShardingSphere-Sidecar(規(guī)劃中):定位為 Kubernetes 的云原生數(shù)據(jù)庫代理,以 Sidecar 的形式代理所有對數(shù)據(jù)庫的訪問。通過無中心、零侵入的方案提供與數(shù)據(jù)庫交互的的嚙合層,即 Database Mesh,又可稱數(shù)據(jù)網(wǎng)格。

          優(yōu)勢:

          • 解決方案完備性,它集客戶端分片、代理服務(wù)器,以及分布式數(shù)據(jù)庫的核心功能于一身。

          • 開發(fā)友好性,提供了友好的集成方式,業(yè)務(wù)開發(fā)人員只需要引入一個 JAR 包就能在業(yè)務(wù)代碼中嵌入數(shù)據(jù)分片、讀寫分離、分布式事務(wù)、數(shù)據(jù)庫治理等一系列功能。

          • 可插拔的系統(tǒng)擴展性:它的很多核心功能均通過插件的形式提供,供開發(fā)者排列組合來定制屬于自己的獨特系統(tǒng)。

          項目示例

          首先,新建一個工程spring-boot-bulking-sharding-sphere,在pom.xml 文件中添加分庫分表的 starter 依賴包


          ????org.apache.shardingsphere
          ????sharding-jdbc-spring-boot-starter
          ????4.0.0-RC1

          數(shù)據(jù)環(huán)境準備

          分別創(chuàng)建兩個數(shù)據(jù)庫ds0ds1,在ds0數(shù)據(jù)庫中創(chuàng)建 user_0user_2兩張用戶表

          CREATE?TABLE?`user_0`?(
          ??`id`?bigint(20)?NOT?NULL?AUTO_INCREMENT,
          ??`user_name`?varchar(128)?NOT?NULL?DEFAULT?''?COMMENT?'用戶名',
          ??`age`?int(11)?NOT?NULL??COMMENT?'年齡',
          ??`address`?varchar(128)?COMMENT?'地址',
          ???PRIMARY?KEY?(`id`)
          )?ENGINE=InnoDB??DEFAULT?CHARSET=utf8mb4?COMMENT='用戶表';

          CREATE?TABLE?`user_2`?(
          ??`id`?bigint(20)?NOT?NULL?AUTO_INCREMENT,
          ??`user_name`?varchar(128)?NOT?NULL?DEFAULT?''?COMMENT?'用戶名',
          ??`age`?int(11)?NOT?NULL??COMMENT?'年齡',
          ??`address`?varchar(128)?COMMENT?'地址',
          ???PRIMARY?KEY?(`id`)
          )?ENGINE=InnoDB??DEFAULT?CHARSET=utf8mb4?COMMENT='用戶表';

          在ds1數(shù)據(jù)庫中創(chuàng)建 user_1user_3兩張用戶表

          CREATE?TABLE?`user_1`?(
          ??`id`?bigint(20)?NOT?NULL?AUTO_INCREMENT,
          ??`user_name`?varchar(128)?NOT?NULL?DEFAULT?''?COMMENT?'用戶名',
          ??`age`?int(11)?NOT?NULL??COMMENT?'年齡',
          ??`address`?varchar(128)?COMMENT?'地址',
          ???PRIMARY?KEY?(`id`)
          )?ENGINE=InnoDB??DEFAULT?CHARSET=utf8mb4?COMMENT='用戶表';

          CREATE?TABLE?`user_3`?(
          ??`id`?bigint(20)?NOT?NULL?AUTO_INCREMENT,
          ??`user_name`?varchar(128)?NOT?NULL?DEFAULT?''?COMMENT?'用戶名',
          ??`age`?int(11)?NOT?NULL??COMMENT?'年齡',
          ??`address`?varchar(128)?COMMENT?'地址',
          ???PRIMARY?KEY?(`id`)
          )?ENGINE=InnoDB??DEFAULT?CHARSET=utf8mb4?COMMENT='用戶表';

          我們采用Mybatis作為ORM框架,遵循Mybatis的開發(fā)流程,首先需要定義業(yè)務(wù)實體類UserMapper接口類文件,以及對應(yīng)的sql語句的xml文件。

          @Mapper
          public?interface?UserMapper?{

          ????Long?addUser(User?user);

          ????List?queryAllUser();

          ????User?queryUserById(Long?id);

          ????Page?querUserByPage();
          }

          Spring boot 框架最為閃亮的設(shè)計就是約定勝于配置,廢棄了之前繁瑣的xml形式定義Bean實例,將一系列框架的配置項遷移到 application.properties 中,借助 EnableAutoConfiguration自動完成裝載,并實例化相應(yīng)的Bean實例到 spring 容器中,IOC統(tǒng)一管理。

          針對兩個數(shù)據(jù)庫初始化兩個DataSource對象,這兩個 DataSource 對象將組成一個 Map 并傳遞給ShardingDataSourceFactory 工廠類,application.properties 配置文件:

          server.port=8090
          application.name=spring-boot-bulking-sharding-sphere
          mybatis.config-location=classpath:config/mybatis-config.xml
          spring.shardingsphere.datasource.names=ds0,ds1

          #?數(shù)據(jù)源
          spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
          spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
          spring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/ds0?characterEncoding=utf-8
          spring.shardingsphere.datasource.ds0.username=root
          spring.shardingsphere.datasource.ds0.password=111111
          spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource
          spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
          spring.shardingsphere.datasource.ds1.url=jdbc:mysql://localhost:3306/ds1?characterEncoding=utf-8
          spring.shardingsphere.datasource.ds1.username=root
          spring.shardingsphere.datasource.ds1.password=111111

          搞定數(shù)據(jù)源后,接下來我們需要設(shè)置分庫、分表策略。

          #?不分表(application.properties沒有為表單獨配置),默認數(shù)據(jù)源策略
          spring.shardingsphere.sharding.default-data-source-name=ds1

          #?user表的分表配置
          spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds0.user_$->{[0,2]},ds1.user_$->{[1,3]}
          #?user庫策略(也可以采用默認的)
          spring.shardingsphere.sharding.tables.user.database-strategy.inline.sharding-column=id
          spring.shardingsphere.sharding.tables.user.database-strategy.inline.algorithm-expression=ds$->{id?%?2}
          #?user表策略
          spring.shardingsphere.sharding.tables.user.table-strategy.inline.sharding-column=id
          spring.shardingsphere.sharding.tables.user.table-strategy.inline.algorithm-expression=user_$->{id?%?4}
          #spring.shardingsphere.sharding.tables.user.key-generator.column=id
          #spring.shardingsphere.sharding.tables.user.key-generator.type=SNOWFLAKE

          在 ShardingSphere 中存在一組 ShardingStrategyConfiguration,這里使用的是基于行表達式的 InlineShardingStrategyConfiguration。

          InlineShardingStrategyConfiguration包含兩個參數(shù),一個是指定分片列名稱的 shardingColumn,另一個是指定分片算法行表達式的 algorithmExpression。上面的示例,將基于 id 列對 2 的取模值來確定數(shù)據(jù)應(yīng)該存儲在哪一個數(shù)據(jù)庫中

          我們對user表做了分庫分表,拆分成4個表,并分別歸屬到兩個庫中。分表鍵是id字段。

          通過單元測試,插入 10條用戶記錄,驗收下數(shù)據(jù)的插入情況~

          @Test
          public?void?addUser()?{
          ????for?(long?i?=?1;?i?????????User?user?=?User.builder().id(i).userName("TomGE").age(29).address("杭州").build();
          ????????userMapper.addUser(user);
          ????????System.out.println("插入用戶成功,uid="?+?user.getId());
          ????}

          }

          其中id=4,id=8 兩條記錄,插入到ds0庫的user_0表中。

          項目源碼

          https://github.com/aalansehaiyang/spring-boot-bulking??

          模塊:spring-boot-bulking-sharding-sphere


          熱衷于收集高并發(fā)、系統(tǒng)架構(gòu)、微服務(wù)、消息中間件、 RPC框架、高性能緩存、搜索、分布式數(shù)據(jù)框架、分布式協(xié)同服務(wù)、分布式配置中心、中臺架構(gòu)、領(lǐng)域驅(qū)動設(shè)計、系統(tǒng)監(jiān)控、系統(tǒng)穩(wěn)定性等技術(shù)知識。

          瀏覽 102
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  日韩在线se | 夫妻干网| A片一区 爱干视频 | 影音先锋aV成人无码电影 | 国产色婷婷导航 |