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

利用Sharding-Jdbc進(jìn)行分庫(kù)分表的操作代碼

 更新時(shí)間:2022年01月22日 11:23:41   作者:有過的理想依然堅(jiān)信  
sharding-jdbc是一個(gè)分布式的關(guān)系型數(shù)據(jù)庫(kù)中間件,今天通過本文給大家介紹利用Sharding-Jdbc進(jìn)行分庫(kù)分表的操作代碼,代碼簡(jiǎn)單易懂對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧

1. Sharding-Jdbc介紹

https://shardingsphere.apache.org/

  • sharding-jdbc是一個(gè)分布式的關(guān)系型數(shù)據(jù)庫(kù)中間件
  • 客戶端代理模式,不需要搭建服務(wù)器,只需要后端數(shù)據(jù)庫(kù)即可,有個(gè)IDE就行了
  • 定位于輕量級(jí)的Java框架,以jar的方式提供服務(wù)
  • 可以理解為增強(qiáng)版的jdbc驅(qū)動(dòng)
  • 完全兼容主流的ORM框架

sharding-jdbc提供了4種配置

  • Java API
  • yaml
  • properties
  • spring命名空間

與MyCat的區(qū)別

  • MyCat是服務(wù)端的代理,Sharding-Jdbc是客戶端代理
  • 實(shí)際開發(fā)中如果企業(yè)有DBA建議使用MyCat,都是開發(fā)人員建議使用sharding-jdbc
  • MyCat不支持在一個(gè)庫(kù)內(nèi)進(jìn)行水平分表,而sharding-jdbc支持在同一個(gè)數(shù)據(jù)庫(kù)中進(jìn)行水平分表

名詞解釋

  • 邏輯表:物流的合并表
  • 真實(shí)表:存放數(shù)據(jù)的地方
  • 數(shù)據(jù)節(jié)點(diǎn):存儲(chǔ)數(shù)據(jù)的MySQL節(jié)點(diǎn)
  • 綁定表:相當(dāng)于MyCat中的子表
  • 廣播表:相當(dāng)于MyCat中的全局表

2. Sharding-Jdbc引入使用

# 0.首先在兩個(gè)MySQL上創(chuàng)建兩個(gè)數(shù)據(jù):shard_order
# 1.分表給兩個(gè)庫(kù)創(chuàng)建兩個(gè)表order_info_1,order_info_2
CREATE TABLE `order_info_1` (
  `id` int(11) NOT NULL,
  `order_amount` decimal(10,2) DEFAULT NULL,
  `order_status` int(255) DEFAULT NULL,
  `user_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `order_info_2` (
  `id` int(11) NOT NULL,
  `order_amount` decimal(10,2) DEFAULT NULL,
  `order_status` int(255) DEFAULT NULL,
  `user_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
# 2.切分規(guī)則,按照id的奇偶數(shù)切分到兩個(gè)數(shù)據(jù)庫(kù),在自己的數(shù)據(jù)庫(kù)按照user_id進(jìn)行表切分

代碼導(dǎo)入POM依賴

 <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>4.0.0-RC2</version>
        </dependency>

配置properties

# 給兩個(gè)數(shù)據(jù)源命名
spring.shardingsphere.datasource.names=ds0,ds1
# 數(shù)據(jù)源鏈接ds0要和命名一致
spring.shardingsphere.datasource.ds0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds0.jdbcUrl=jdbc:mysql://39.103.163.215:3306/shard_order
spring.shardingsphere.datasource.ds0.username=gavin
spring.shardingsphere.datasource.ds0.password=123456
# 數(shù)據(jù)源鏈接ds1要和命名一致
spring.shardingsphere.datasource.ds1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1.jdbcUrl=jdbc:mysql://39.101.221.95:3306/shard_order
spring.shardingsphere.datasource.ds1.username=gavin
spring.shardingsphere.datasource.ds1.password=123456
# 具體的分片規(guī)則,基于數(shù)據(jù)節(jié)點(diǎn)
spring.shardingsphere.sharding.tables.order_info.actual-data-nodes=ds$->{0..1}.order_info_$->{1..2}
# 分庫(kù)的規(guī)則
spring.shardingsphere.sharding.tables.order_info.database-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.order_info.database-strategy.inline.algorithm-expression=ds$->{id % 2}
# 分表的規(guī)則
spring.shardingsphere.sharding.tables.order_info.table-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.order_info.table-strategy.inline.algorithm-expression=order_info_$->{user_id % 2 + 1}
//測(cè)試代碼
@SpringBootTest
class ShardingjdbcProjectApplicationTests {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Test
    void insertTest(){
        String sql = "insert into order_info(id,order_amount,order_status,user_id) values(3,213.88,1,2)";
        int i = jdbcTemplate.update(sql);
        System.out.println("影響行數(shù):"+i);
    }
}

作業(yè):自己練習(xí)一下sharding-jdbc的分庫(kù)分表

3. 配置廣播表

先在兩個(gè)庫(kù)上創(chuàng)建廣播表province_info

CREATE TABLE `province_info` (
  `id` int(11) NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

在properties里增加配置

spring.shardingsphere.sharding.broadcast-tables=province_info

測(cè)試插入和查詢的代碼

 @Test
    void insertBroadcast(){
        String sql = "insert into province_info(id,name) values(1,'beijing')";
        int i = jdbcTemplate.update(sql);
        System.out.println("******* 影響的結(jié)果:"+i);
    }

    @Test
    void selectBroadcast(){
        String sql = "select * from province_info";
        List<Map<String,Object>> result = jdbcTemplate.queryForList(sql);
        for (Map<String,Object> val: result) {
            System.out.println("=========== "+val.get("id")+" ----- "+val.get("name"));

        }
    }

4. 配置綁定表

首先按照order_info的建表順序創(chuàng)建order_item分別在兩個(gè)庫(kù)上建立order_item_1,order_item_2

?
 @Test
    void insertBroadcast(){
        String sql = "insert into province_info(id,name) values(1,'beijing')";
        int i = jdbcTemplate.update(sql);
        System.out.println("******* 影響的結(jié)果:"+i);
    }

    @Test
    void selectBroadcast(){
        String sql = "select * from province_info";
        List<Map<String,Object>> result = jdbcTemplate.queryForList(sql);
        for (Map<String,Object> val: result) {
            System.out.println("=========== "+val.get("id")+" ----- "+val.get("name"));

        }
    }

?

配置綁定表,將兩個(gè)表的分表邏輯和order_info保持一致

# 給兩個(gè)數(shù)據(jù)源命名
spring.shardingsphere.datasource.names=ds0,ds1
# 數(shù)據(jù)源鏈接ds0要和命名一致
spring.shardingsphere.datasource.ds0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds0.jdbcUrl=jdbc:mysql://39.103.163.215:3306/shard_order
spring.shardingsphere.datasource.ds0.username=gavin
spring.shardingsphere.datasource.ds0.password=123456
# 數(shù)據(jù)源鏈接ds1要和命名一致
spring.shardingsphere.datasource.ds1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1.jdbcUrl=jdbc:mysql://39.101.221.95:3306/shard_order
spring.shardingsphere.datasource.ds1.username=gavin
spring.shardingsphere.datasource.ds1.password=123456

# 具體的分片規(guī)則,基于數(shù)據(jù)節(jié)點(diǎn)
spring.shardingsphere.sharding.tables.order_info.actual-data-nodes=ds$->{0..1}.order_info_$->{1..2}
# 分庫(kù)的規(guī)則
spring.shardingsphere.sharding.tables.order_info.database-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.order_info.database-strategy.inline.algorithm-expression=ds$->{id % 2}
# 分表的規(guī)則
spring.shardingsphere.sharding.tables.order_info.table-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.order_info.table-strategy.inline.algorithm-expression=order_info_$->{user_id % 2 + 1}

# 具體的分片規(guī)則,基于數(shù)據(jù)節(jié)點(diǎn)
spring.shardingsphere.sharding.tables.order_item.actual-data-nodes=ds$->{0..1}.order_item_$->{1..2}
# 分庫(kù)的規(guī)則
spring.shardingsphere.sharding.tables.order_item.database-strategy.inline.sharding-column=order_id
spring.shardingsphere.sharding.tables.order_item.database-strategy.inline.algorithm-expression=ds$->{order_id % 2}
# 分表的規(guī)則
spring.shardingsphere.sharding.tables.order_item.table-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.order_item.table-strategy.inline.algorithm-expression=order_item_$->{user_id % 2 + 1}

# 綁定表關(guān)系
spring.shardingsphere.sharding.binding-tables=order_info,order_item

# 廣播表
spring.shardingsphere.sharding.broadcast-tables=province_info

5. 讀寫分離配置

首先配置properties的數(shù)據(jù)源,如果有主機(jī)配置就必須要有從機(jī)配置

# 指定主從的配置節(jié)點(diǎn)
spring.shardingsphere.datasource.names=master0,master0slave0,master1,master1slave0
# master0數(shù)據(jù)源鏈接配置
spring.shardingsphere.datasource.master0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.master0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master0.jdbcUrl=jdbc:mysql://39.103.163.215:3306/shard_order
spring.shardingsphere.datasource.master0.username=gavin
spring.shardingsphere.datasource.master0.password=123456
# master0slave0數(shù)據(jù)源鏈接配置
spring.shardingsphere.datasource.master0slave0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.master0slave0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master0slave0.jdbcUrl=jdbc:mysql://39.99.212.46:3306/shard_order
spring.shardingsphere.datasource.master0slave0.username=gavin
spring.shardingsphere.datasource.master0slave0.password=123456
# master1數(shù)據(jù)源鏈接配置
spring.shardingsphere.datasource.master1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.master1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master1.jdbcUrl=jdbc:mysql://39.101.221.95:3306/shard_order
spring.shardingsphere.datasource.master1.username=gavin
spring.shardingsphere.datasource.master1.password=123456
# master1slave0數(shù)據(jù)源鏈接配置
spring.shardingsphere.datasource.master1slave0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.master1slave0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.master1slave0.jdbcUrl=jdbc:mysql://localhost:3306/shard_order
spring.shardingsphere.datasource.master1slave0.username=root
spring.shardingsphere.datasource.master1slave0.password=gavin

# 具體的分片規(guī)則,基于數(shù)據(jù)節(jié)點(diǎn)
spring.shardingsphere.sharding.tables.order_info.actual-data-nodes=ds$->{0..1}.order_info_$->{1..2}
# 分庫(kù)的規(guī)則
spring.shardingsphere.sharding.tables.order_info.database-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.order_info.database-strategy.inline.algorithm-expression=ds$->{id % 2}
# 分表的規(guī)則
spring.shardingsphere.sharding.tables.order_info.table-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.order_info.table-strategy.inline.algorithm-expression=order_info_$->{user_id % 2 + 1}

# 具體的分片規(guī)則,基于數(shù)據(jù)節(jié)點(diǎn)
spring.shardingsphere.sharding.tables.order_item.actual-data-nodes=ds$->{0..1}.order_item_$->{1..2}
# 分庫(kù)的規(guī)則
spring.shardingsphere.sharding.tables.order_item.database-strategy.inline.sharding-column=order_id
spring.shardingsphere.sharding.tables.order_item.database-strategy.inline.algorithm-expression=ds$->{order_id % 2}
# 分表的規(guī)則
spring.shardingsphere.sharding.tables.order_item.table-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.order_item.table-strategy.inline.algorithm-expression=order_item_$->{user_id % 2 + 1}

# 綁定表關(guān)系
spring.shardingsphere.sharding.binding-tables=order_info,order_item

# 廣播表
spring.shardingsphere.sharding.broadcast-tables=province_info

# 讀寫分離主從關(guān)系綁定
spring.shardingsphere.sharding.master-slave-rules.ds0.master-data-source-name=master0
spring.shardingsphere.sharding.master-slave-rules.ds0.slave-data-source-names=master0slave0
spring.shardingsphere.sharding.master-slave-rules.ds0.load-balance-algorithm-type=round_robin

spring.shardingsphere.sharding.master-slave-rules.ds1.master-data-source-name=master1
spring.shardingsphere.sharding.master-slave-rules.ds1.slave-data-source-names=master1slave0
spring.shardingsphere.sharding.master-slave-rules.ds1.load-balance-algorithm-type=random

到此這篇關(guān)于Sharding-Jdbc進(jìn)行分庫(kù)分表的文章就介紹到這了,更多相關(guān)Sharding-Jdbc分庫(kù)分表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java?函數(shù)式編程梳理

    Java?函數(shù)式編程梳理

    這篇文章主要介紹了Java?函數(shù)式編程梳理,文章通過Lambda表達(dá)式展開詳細(xì)的內(nèi)容介紹,具有一定參考價(jià)值,需要的小伙伴可以參考一下
    2022-07-07
  • Java實(shí)現(xiàn)指定線程執(zhí)行順序的三種方式示例

    Java實(shí)現(xiàn)指定線程執(zhí)行順序的三種方式示例

    這篇文章主要介紹了Java實(shí)現(xiàn)指定線程執(zhí)行順序的三種方式,包括通過共享對(duì)象鎖加上可見變量,通過主線程Join()以及通過線程執(zhí)行時(shí)Join()等三種實(shí)現(xiàn)方法,需要的朋友可以參考下
    2019-01-01
  • Spring Boot 多環(huán)境配置Maven Profile vs 啟動(dòng)參數(shù)注入深入解析

    Spring Boot 多環(huán)境配置Maven Profile vs 啟

    本文介紹了在Java項(xiàng)目開發(fā)和部署中使用多環(huán)境配置的方法,比較了兩種主要方式:MavenProfile方式和啟動(dòng)參數(shù)方式,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2025-12-12
  • SpringBoot下載docx文檔方式

    SpringBoot下載docx文檔方式

    這篇文章主要介紹了SpringBoot下載docx文檔方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-06-06
  • Mybatis-Plus saveBatch()批量保存失效的解決

    Mybatis-Plus saveBatch()批量保存失效的解決

    本文主要介紹了Mybatis-Plus saveBatch()批量保存失效的解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-01-01
  • 詳解SpringBoot+Dubbo集成ELK實(shí)戰(zhàn)

    詳解SpringBoot+Dubbo集成ELK實(shí)戰(zhàn)

    這篇文章主要介紹了詳解SpringBoot+Dubbo集成ELK實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Java實(shí)現(xiàn)的日期處理類完整實(shí)例

    Java實(shí)現(xiàn)的日期處理類完整實(shí)例

    這篇文章主要介紹了Java實(shí)現(xiàn)的日期處理類,結(jié)合完整實(shí)例形式分析了Java針對(duì)日期的獲取、運(yùn)算、轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下
    2017-09-09
  • MybatisPlus處理大表查詢的實(shí)現(xiàn)步驟

    MybatisPlus處理大表查詢的實(shí)現(xiàn)步驟

    在實(shí)際工作中當(dāng)指定查詢數(shù)據(jù)過大時(shí),我們一般使用分頁(yè)查詢的方式一頁(yè)一頁(yè)的將數(shù)據(jù)放到內(nèi)存處理,本文主要介紹了MybatisPlus處理大表查詢的實(shí)現(xiàn)步驟,感興趣的可以了解一下
    2024-08-08
  • mybatis中insert返回值為1,但數(shù)據(jù)庫(kù)卻沒有數(shù)據(jù)

    mybatis中insert返回值為1,但數(shù)據(jù)庫(kù)卻沒有數(shù)據(jù)

    這篇文章主要介紹了mybatis中insert返回值為1,但數(shù)據(jù)庫(kù)卻沒有數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • Java內(nèi)存泄漏問題的排查、優(yōu)化與最佳實(shí)踐

    Java內(nèi)存泄漏問題的排查、優(yōu)化與最佳實(shí)踐

    在?Java?開發(fā)中,內(nèi)存泄漏是一個(gè)常見且令人頭疼的問題,內(nèi)存泄漏指的是程序在運(yùn)行過程中,已經(jīng)不再使用的對(duì)象沒有被及時(shí)釋放,從而導(dǎo)致內(nèi)存占用不斷增加,最終可能導(dǎo)致程序崩潰或性能顯著下降,本文給大家介紹了Java內(nèi)存泄漏排查、優(yōu)化與最佳實(shí)踐
    2025-01-01

最新評(píng)論

宜宾县| 桂东县| 金平| 淄博市| 山阴县| 个旧市| 江门市| 沂南县| 茌平县| 瑞昌市| 义马市| 邵武市| 砀山县| 镇江市| 航空| 永清县| 合水县| 克拉玛依市| 赣榆县| 和田县| 常德市| 新乡市| 集安市| 应用必备| 皮山县| 偃师市| 通榆县| 张北县| 确山县| 汾西县| 昭苏县| 菏泽市| 左贡县| 泰顺县| 临沧市| 桐柏县| 安岳县| 锡林浩特市| 湖南省| 威远县| 赣榆县|