ApacheShardingSphere-创新互联

title: Apache ShardingSphere
date: 2021-11-27 15:25:02
tags: 计算机

创新互联长期为数千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为德州企业提供专业的成都网站制作、成都网站建设,德州网站改版等技术服务。拥有十多年丰富建站经验和众多成功案例,为您定制开发。

文章目录
    • Sharding Sphere
      • 垂直切分
        • 垂直分表
        • 垂直分库
      • 水平切分
        • 水平分库
        • 水平分表
      • 公共表
    • 读写分离
        • 应用:
        • 分库分表的问题:
    • Sharding-JDBC

Sharding Sphere

Apache ShardingSphere是一套开源的分布式数据库中间件。它由三个独立且可混合使用的产品组成:Sharding-JDBC、ShardingProxy、Sharding-Sidecar。他们提供了标准化的数据分片、分布式事务、数据库治理功能。

垂直切分 垂直分表

image-20211116203018230

垂直分库

把单一数据库按照业务进行专库,专表进行划分,减少数据库io压力

image-20211116204451102

# 配置数据源,给多个数据源命名
spring.shardingsphere.datasource.names=m1,m2,m0 

#配置第一个数据源具体内容,包含连接池,驱动,地址,用户名和密码
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

#配置第二个数据源具体内容,包含连接池,驱动,地址,用户名和密码
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 

#配置第三个数据源具体内容,包含连接池,驱动,地址,用户名和密码
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 数据库里面 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
水平切分 水平分库

分布在不同的服务器上,不便于维护

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uuZADc6n-1669378667304)(https://llx-img.oss-cn-shanghai.aliyuncs.com/%E6%B0%B4%E5%B9%B3%E5%88%86%E5%BA%93.png)]

# 配置数据源,给多个数据源命名
spring.shardingsphere.datasource.names=m0,m1

# 配置第一个数据源
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

#配置第二个数据源
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 

#指定数据库分布情况,数据库里面表分布情况
# 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 值偶数添加到 course_1 表,如果 cid 是奇数添加到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} 
# 指定数据库分片策略 约定 user_id 是偶数添加 m1,是奇数添加 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
水平分表

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ARuhToWQ-1669378667305)(https://llx-img.oss-cn-shanghai.aliyuncs.com/%E6%B0%B4%E5%B9%B3%E5%88%86%E8%A1%A8.png)]

# 数据源命名
spring.shardingsphere.datasource.names=m1

# 配置数据源
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数据分布
spring.shardingsphere.sharding.tables.course.actual-data-nodes=m1.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为奇数在course_1表,cid为偶数在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
公共表

存储固定数据的表,表的数据很少发生变化,并且查询时经常关联到

每个数据库都创建相同结构的公共表(字典表)

每次写每个数据库的公共表

spring.shardingsphere.sharding.broadcast-tables=t_udict
读写分离

Sharding-JDBC通过sql语句分析,实现读写分离过程,不会做数据同步

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(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)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(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)]

应用:
  1. 在数据库设计时考虑垂直分库和分表
  2. 随着数据量增加,不要急于做水平分表,首先应考虑缓存处理,读写分离,使用索引等方式。如果都不能根本的解决问题,在考虑水平分库分表
分库分表的问题:
  1. 跨节点连接查询问题(分页,排序)
  2. 多数据源管理问题
Sharding-JDBC

定义为java轻量级框架,在JDBC层提供而外的服务,它使用客户端直连数据库,以jar包的形式提供服务,无需额外部署,可以理解为增强版的JDBC,完全兼容JDBC和各种ORM框架。

简化分库分表后的操作,数据分片,读写分离

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧

名称栏目:ApacheShardingSphere-创新互联
网页链接:https://www.cdcxhl.com/article36/cecppg.html

成都网站建设公司_创新互联,为您提供ChatGPT做网站域名注册品牌网站制作网站建设网站内链

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

小程序开发