title: Apache ShardingSphere
date: 2021-11-27 15:25:02
tags: 計算機
垂直切分 垂直分表垂直分庫Apache ShardingSphere是一套開源的分布式數(shù)據(jù)庫中間件。它由三個獨立且可混合使用的產(chǎn)品組成:Sharding-JDBC、ShardingProxy、Sharding-Sidecar。他們提供了標準化的數(shù)據(jù)分片、分布式事務、數(shù)據(jù)庫治理功能。
把單一數(shù)據(jù)庫按照業(yè)務進行專庫,專表進行劃分,減少數(shù)據(jù)庫io壓力
# 配置數(shù)據(jù)源,給多個數(shù)據(jù)源命名
spring.shardingsphere.datasource.names=m1,m2,m0
#配置第一個數(shù)據(jù)源具體內(nèi)容,包含連接池,驅(qū)動,地址,用戶名和密碼
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
#配置第二個數(shù)據(jù)源具體內(nèi)容,包含連接池,驅(qū)動,地址,用戶名和密碼
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
#配置第三個數(shù)據(jù)源具體內(nèi)容,包含連接池,驅(qū)動,地址,用戶名和密碼
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
水平切分
水平分庫分布在不同的服務器上,不便于維護
[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(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ù)源,給多個數(shù)據(jù)源命名
spring.shardingsphere.datasource.names=m0,m1
# 配置第一個數(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
#配置第二個數(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
# 一個實體類對應兩張表,覆蓋
spring.main.allow-bean-definition-overriding=true
水平分表[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(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
# 一個實體類對應兩張表,覆蓋
spring.main.allow-bean-definition-overriding=true
公共表存儲固定數(shù)據(jù)的表,表的數(shù)據(jù)很少發(fā)生變化,并且查詢時經(jīng)常關聯(lián)到
每個數(shù)據(jù)庫都創(chuàng)建相同結構的公共表(字典表)
每次寫每個數(shù)據(jù)庫的公共表
spring.shardingsphere.sharding.broadcast-tables=t_udict
讀寫分離Sharding-JDBC通過sql語句分析,實現(xiàn)讀寫分離過程,不會做數(shù)據(jù)同步
[外鏈圖片轉(zhuǎn)存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(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)存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(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)]
應用:定義為java輕量級框架,在JDBC層提供而外的服務,它使用客戶端直連數(shù)據(jù)庫,以jar包的形式提供服務,無需額外部署,可以理解為增強版的JDBC,完全兼容JDBC和各種ORM框架。
簡化分庫分表后的操作,數(shù)據(jù)分片,讀寫分離
?
你是否還在尋找穩(wěn)定的海外服務器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧