ApacheShardingSphere-創(chuàng)新互聯(lián)

title: Apache ShardingSphere
date: 2021-11-27 15:25:02
tags: 計(jì)算機(jī)

創(chuàng)新互聯(lián)長期為數(shù)千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為德州企業(yè)提供專業(yè)的成都網(wǎng)站制作、成都網(wǎng)站建設(shè),德州網(wǎng)站改版等技術(shù)服務(wù)。擁有十多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。

文章目錄
    • Sharding Sphere
      • 垂直切分
        • 垂直分表
        • 垂直分庫
      • 水平切分
        • 水平分庫
        • 水平分表
      • 公共表
    • 讀寫分離
        • 應(yīng)用:
        • 分庫分表的問題:
    • Sharding-JDBC

Sharding Sphere

Apache ShardingSphere是一套開源的分布式數(shù)據(jù)庫中間件。它由三個(gè)獨(dú)立且可混合使用的產(chǎn)品組成:Sharding-JDBC、ShardingProxy、Sharding-Sidecar。他們提供了標(biāo)準(zhǔn)化的數(shù)據(jù)分片、分布式事務(wù)、數(shù)據(jù)庫治理功能。

垂直切分 垂直分表

image-20211116203018230

垂直分庫

把單一數(shù)據(jù)庫按照業(yè)務(wù)進(jìn)行專庫,專表進(jìn)行劃分,減少數(shù)據(jù)庫io壓力

image-20211116204451102

# 配置數(shù)據(jù)源,給多個(gè)數(shù)據(jù)源命名
spring.shardingsphere.datasource.names=m1,m2,m0 

#配置第一個(gè)數(shù)據(jù)源具體內(nèi)容,包含連接池,驅(qū)動(dòng),地址,用戶名和密碼
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://localhost:3306/edu_db_1?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=root

#配置第二個(gè)數(shù)據(jù)源具體內(nèi)容,包含連接池,驅(qū)動(dòng),地址,用戶名和密碼
spring.shardingsphere.datasource.m2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m2.url=jdbc:mysql://localhost:3306/edu_db_2?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m2.username=root
spring.shardingsphere.datasource.m2.password=root 

#配置第三個(gè)數(shù)據(jù)源具體內(nèi)容,包含連接池,驅(qū)動(dòng),地址,用戶名和密碼
spring.shardingsphere.datasource.m0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m0.url=jdbc:mysql://localhost:3306/user_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m0.username=root
spring.shardingsphere.datasource.m0.password=root 

# 配置 user_db 數(shù)據(jù)庫里面 t_user 專庫專表
spring.shardingsphere.sharding.tables.t_user.actual-data-nodes=m$->{0}.t_user

# 指定 course 表里面主鍵 cid 生成策略 SNOWFLAKE
spring.shardingsphere.sharding.tables.t_user.key-generator.column=user_id
spring.shardingsphere.sharding.tables.t_user.key-generator.type=SNOWFLAKE

# 指定表分片策略
spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.shardingcolumn=user_id
spring.shardingsphere.sharding.tables.t_user.table-strategy.inline.algorithmexpression=t_user
水平切分 水平分庫

分布在不同的服務(wù)器上,不便于維護(hù)

[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來直接上傳(img-uuZADc6n-1669378667304)(https://llx-img.oss-cn-shanghai.aliyuncs.com/%E6%B0%B4%E5%B9%B3%E5%88%86%E5%BA%93.png)]

# 配置數(shù)據(jù)源,給多個(gè)數(shù)據(jù)源命名
spring.shardingsphere.datasource.names=m0,m1

# 配置第一個(gè)數(shù)據(jù)源
spring.shardingsphere.datasource.m0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m0.url=jdbc:mysql://localhost:3306/edu_db_1?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m0.username=root
spring.shardingsphere.datasource.m0.password=root

#配置第二個(gè)數(shù)據(jù)源
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://localhost:3306/edu_db_2?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=root 

#指定數(shù)據(jù)庫分布情況,數(shù)據(jù)庫里面表分布情況
# m0 m1 course_0 course_1
spring.shardingsphere.sharding.tables.course.actual-data-nodes=m$->{0..1}.course_$->{0..1} 

# 指定 course 表里面主鍵 cid 生成策略 SNOWFLAKE
spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE

# 指定表分片策略 約定 cid 值偶數(shù)添加到 course_1 表,如果 cid 是奇數(shù)添加到course_0 表
spring.shardingsphere.sharding.tables.course.table-strategy.inline.shardingcolumn=cid
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithmexpression=course_$->{cid % 2 + 1} 
# 指定數(shù)據(jù)庫分片策略 約定 user_id 是偶數(shù)添加 m1,是奇數(shù)添加 m2
#spring.shardingsphere.sharding.default-database-strategy.inline.shardingcolumn=user_id
#spring.shardingsphere.sharding.default-database-strategy.inline.algorithmexpression=m$->{user_id % 2 + 1}
spring.shardingsphere.sharding.tables.course.databasestrategy.inline..sharding-column=user_id
spring.shardingsphere.sharding.tables.course.databasestrategy.inline.algorithm-expression=m$->{user_id % 2 + 1} 
# 打開sql輸出日志
spring.shardingsphere.props.sql.show=true

# 一個(gè)實(shí)體類對(duì)應(yīng)兩張表,覆蓋	
spring.main.allow-bean-definition-overriding=true
水平分表

[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來直接上傳(img-ARuhToWQ-1669378667305)(https://llx-img.oss-cn-shanghai.aliyuncs.com/%E6%B0%B4%E5%B9%B3%E5%88%86%E8%A1%A8.png)]

# 數(shù)據(jù)源命名
spring.shardingsphere.datasource.names=m1

# 配置數(shù)據(jù)源
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://localhost:3306/edu_db_1?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=123456

# 指定course數(shù)據(jù)分布
spring.shardingsphere.sharding.tables.course.actual-data-nodes=m1.course_$->{0..1}

# 指定course表中的cid生成規(guī)則為SNOWFLAKE
spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE

# 指定分片策略,cid為奇數(shù)在course_1表,cid為偶數(shù)在course_0表中
spring.shardingsphere.sharding.tables.course.table-strategy.inline.shardingcolumn=cid
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithmexpression=course_$->{cid % 2 } 

# 打開sql輸出日志
spring.shardingsphere.props.sql.show=true

# 一個(gè)實(shí)體類對(duì)應(yīng)兩張表,覆蓋	
spring.main.allow-bean-definition-overriding=true
公共表

存儲(chǔ)固定數(shù)據(jù)的表,表的數(shù)據(jù)很少發(fā)生變化,并且查詢時(shí)經(jīng)常關(guān)聯(lián)到

每個(gè)數(shù)據(jù)庫都創(chuàng)建相同結(jié)構(gòu)的公共表(字典表)

每次寫每個(gè)數(shù)據(jù)庫的公共表

spring.shardingsphere.sharding.broadcast-tables=t_udict
讀寫分離

Sharding-JDBC通過sql語句分析,實(shí)現(xiàn)讀寫分離過程,不會(huì)做數(shù)據(jù)同步

[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來直接上傳(img-AQfOlBBJ-1669378667305)(https://llx-img.oss-cn-shanghai.aliyuncs.com/%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB%E6%A6%82%E5%BF%B5.png)]

[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機(jī)制,建議將圖片保存下來直接上傳(img-hNh070KL-1669378667305)(https://llx-img.oss-cn-shanghai.aliyuncs.com/%E8%AF%BB%E5%86%99%E5%88%86%E7%A6%BB%E5%8E%9F%E7%90%86.png)]

應(yīng)用:
  1. 在數(shù)據(jù)庫設(shè)計(jì)時(shí)考慮垂直分庫和分表
  2. 隨著數(shù)據(jù)量增加,不要急于做水平分表,首先應(yīng)考慮緩存處理,讀寫分離,使用索引等方式。如果都不能根本的解決問題,在考慮水平分庫分表
分庫分表的問題:
  1. 跨節(jié)點(diǎn)連接查詢問題(分頁,排序)
  2. 多數(shù)據(jù)源管理問題
Sharding-JDBC

定義為java輕量級(jí)框架,在JDBC層提供而外的服務(wù),它使用客戶端直連數(shù)據(jù)庫,以jar包的形式提供服務(wù),無需額外部署,可以理解為增強(qiáng)版的JDBC,完全兼容JDBC和各種ORM框架。

簡化分庫分表后的操作,數(shù)據(jù)分片,讀寫分離

?

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧

名稱欄目:ApacheShardingSphere-創(chuàng)新互聯(lián)
網(wǎng)頁鏈接:http://muchs.cn/article36/cecppg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT做網(wǎng)站、域名注冊、品牌網(wǎng)站制作網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)