电竞比分网-中国电竞赛事及体育赛事平台

分享

數(shù)據(jù)庫(kù)分庫(kù)分表

 印度阿三17 2020-12-08
  • 垂直分表

將字段拆分出多個(gè)表

  • 水平分表

將數(shù)據(jù)拆分多個(gè)表

<!--   分庫(kù)分表插件     -->
<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
</dependency>

在執(zhí)行sql時(shí),按照配置的策略,動(dòng)態(tài)改變表名查找對(duì)應(yīng)的數(shù)據(jù)庫(kù)表進(jìn)行操作

?

# 多個(gè)數(shù)據(jù)庫(kù)水平分表配置

# 數(shù)據(jù)源名稱(chēng),多數(shù)據(jù)源以逗號(hào)分隔
spring.shardingsphere.datasource.names=datasource1,datasource2

# 數(shù)據(jù)源 datasource1
spring.shardingsphere.datasource.datasource1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.datasource1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.datasource1.url=jdbc:mysql://localhost:3306/spring?characterEncoding=utf8&useSSL=false&serverTimezone=GMT+8&rewriteBatchedStatements=true
spring.shardingsphere.datasource.datasource1.username=root
spring.shardingsphere.datasource.datasource1.password=root

# 數(shù)據(jù)源 datasource2
spring.shardingsphere.datasource.datasource2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.datasource2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.datasource2.url=jdbc:mysql://localhost:3306/spring1?characterEncoding=utf8&useSSL=false&serverTimezone=GMT+8&rewriteBatchedStatements=true
spring.shardingsphere.datasource.datasource2.username=root
spring.shardingsphere.datasource.datasource2.password=root

# 允許一個(gè)實(shí)體類(lèi)對(duì)應(yīng)多張表
spring.main.allow-bean-definition-overriding=true

# 標(biāo)準(zhǔn)分片表配置
# 指定course表分布情況( 配置表在 哪個(gè)數(shù)據(jù)庫(kù) 的 哪些表里 ),一共有兩個(gè)數(shù)據(jù)源datasource1,datasource2,每個(gè)庫(kù)中有3張表course_1,course_2,course_2
spring.shardingsphere.rules.sharding.tables.course.actual-data-nodes=datasource$->{1..2}.course_$->{1..3}

# 分布式序列策略配置 指定course表里的主鍵cid生成策略為snowflake
spring.shardingsphere.rules.sharding.tables.course.key-generate-strategy.column=cid # 分布式序列列名稱(chēng)
spring.shardingsphere.rules.sharding.tables.course.key-generate-strategy.key-generator-name=SNOWFLAKE # 分布式序列算法名稱(chēng)

# 指定分片策略-表  約定 cid%3 取模的值對(duì)應(yīng)存放的表序號(hào)
spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=cid
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->{cid % 3   1}

# 指定分片策略-數(shù)據(jù)庫(kù)  約定 (user_id%2  1) 對(duì)應(yīng)存放的數(shù)據(jù)源序號(hào)
spring.shardingsphere.sharding.tables.course.database-strategy.inline.sharding-column=user_id
spring.shardingsphere.sharding.tables.course.database-strategy.inline.algorithm-expression=datasource$->{user_id % 2   1}

# 打開(kāi)sql輸出日志
spring.shardingsphere.props.sql.show=true
來(lái)源:https://www./content-2-779451.html

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多