溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何使用sharding-jdbc實(shí)現(xiàn)水平分庫+水平分表

發(fā)布時間:2021-12-17 12:32:46 來源:億速云 閱讀:243 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)如何使用sharding-jdbc實(shí)現(xiàn)水平分庫+水平分表的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

分庫分表策略:將id為偶數(shù)的存入到庫1中,奇數(shù)存入到庫2中,在每個庫中,再根據(jù)學(xué)生的性別分別存到到表1和表2中。

新建兩個數(shù)據(jù)庫sharding_db1和sharding_db2,在兩個數(shù)據(jù)庫中在分別創(chuàng)建結(jié)構(gòu)相同的兩張表,student_1和student_2。

CREATE TABLE `NewTable` (
`ID`  bigint(20) NOT NULL ,
`NAME`  varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL ,
`AGE`  int(11) NOT NULL ,
`GENDER`  int(1) NOT NULL ,
PRIMARY KEY (`ID`)
);

相比前面文章中,將gender性別字段設(shè)置成了int類型,方便根據(jù)性別再進(jìn)行分表。

修改配置文件

spring.main.allow-bean-definition-overriding=true
# 配置Sharding-JDBC的分片策略
# 配置數(shù)據(jù)源,給數(shù)據(jù)源起名g1,g2...此處可配置多數(shù)據(jù)源
spring.shardingsphere.datasource.names=g1,g2
# 配置數(shù)據(jù)源具體內(nèi)容:連接池,驅(qū)動,地址,用戶名,密碼
spring.shardingsphere.datasource.g1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.g1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.g1.url=jdbc:mysql://localhost:3306/sharding_db1?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC
spring.shardingsphere.datasource.g1.username=root
spring.shardingsphere.datasource.g1.password=123456
spring.shardingsphere.datasource.g2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.g2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.g2.url=jdbc:mysql://localhost:3306/sharding_db2?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC
spring.shardingsphere.datasource.g2.username=root
spring.shardingsphere.datasource.g2.password=123456
# 配置數(shù)據(jù)庫的分布,表的分布
spring.shardingsphere.sharding.tables.student.actual-data-nodes=g$->{1..2}.student_$->{1..2}
# 指定student表 主鍵gid 生成策略為 SNOWFLAKE
spring.shardingsphere.sharding.tables.student.key-generator.column=id
spring.shardingsphere.sharding.tables.student.key-generator.type=SNOWFLAKE
# 指定數(shù)據(jù)庫分片策略 約定id值是偶數(shù)添加到sharding_db1中,奇數(shù)添加到sharding_db2中
spring.shardingsphere.sharding.tables.student.database-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.student.database-strategy.inline.algorithm-expression=g$->{id % 2 + 1}
# 指定表分片策略 約定gender值是0添加到student_1表,如果gender是1添加到student_2表
spring.shardingsphere.sharding.tables.student.table-strategy.inline.sharding-column=gender
spring.shardingsphere.sharding.tables.student.table-strategy.inline.algorithm-expression=student_$->{gender % 2 + 1}
# 打開sql輸出日志
spring.shardingsphere.props.sql.show=true

配置多個數(shù)據(jù)源時,使用逗號隔開,分別配置其屬性。除了配置表分片策略,還需配置庫分配策略。

測試類

@SpringBootTest
class ShardingJdbcDemoApplicationTests {

    @Autowired
    private StudentMapper studentMapper;

    @Test
    public void test01() {
        for (int i = 0; i < 15; i++) {
            Student student = new Student();
            student.setName("wuwl");
            student.setAge(27);
            student.setGender(i%2);
            studentMapper.insert(student);
        }
    }
}

運(yùn)行效果:

如何使用sharding-jdbc實(shí)現(xiàn)水平分庫+水平分表

看樣子是成功了,查看數(shù)據(jù)庫數(shù)據(jù)。

sharding_db1.student_1:

如何使用sharding-jdbc實(shí)現(xiàn)水平分庫+水平分表

sharding_db1.student_2:

如何使用sharding-jdbc實(shí)現(xiàn)水平分庫+水平分表

sharding_db2.student_1:

如何使用sharding-jdbc實(shí)現(xiàn)水平分庫+水平分表

sharding_db2.student_2:

如何使用sharding-jdbc實(shí)現(xiàn)水平分庫+水平分表

感謝各位的閱讀!關(guān)于“如何使用sharding-jdbc實(shí)現(xiàn)水平分庫+水平分表”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI