当前位置:网站首页>ShardingSphere-Proxy 4.1 分库分表
ShardingSphere-Proxy 4.1 分库分表
2022-06-25 09:37:00 【@@Mr.Fu】
一、ShardingSphere-Proxy的核心概念
ShardingSphere-Proxy概念
官方地址:https://shardingsphere.apache.org/index_zh.html
ShardingSphere-Proxy就是数据库的代理,如图:
ShardingSphere-Proxy主要代理哪些数据库
默认代理:Mysql、PostSql
实现代理的目的
主要是为了完成分库分表
实现读写分离
这两个也是ShardingSphere-Proxy的两大核心功能。
分库分表
分库的概念和目的
概念
数据库中的表存储到不同的数据库中;如图:
目的
防止一个库中多个表出现资源竞争【CPU、内存】,导致性能下降。
分表的概念和目的
概念
将数据库中一张表分成多张表,如图:
目的
分表是解决表中数据量过大,提升用户查询和添加数据的性能。
比如:以mysql数据库为例,当用户添加数据会通过mysql的InnoDB引擎存储到数据中,InnoDB引擎要想保证数据的性能在一定的范围之内,表中的数据量最大的峰值为2000w,如果超过2000W那么添加数据的性能会下降,所以我们要将超过2000W数据量的表拆分成多个表,这样才能保证用户的体验度。
缺陷
并发量过大,表会出现资源竞争[CPU、内存]的问题,这样导致性能下降,用户的体验度变差。
解决方案:分库
分库分表
目的
解决表资源竞争和数据量过大的问题。
二、ShardingSphere-Proxy的应用场景
场景
单体项目和微服务项目都能用到分库分表。
三、ShardingSphere-Proxy分布分表落地
工具
ShardingSphere-Proxy
方案
进程内
如图:
- 缺陷
- 资源竞争问题。
- 异常影响问题。
- 缺陷
进程外 【推荐】
如图:
- 缺陷
- 维护量大的问题。
- 性能相对进程内弱一些。
- 可以放在内网中进行通信【docker】
- 缺陷
实现
条件
Mysql数据库 版本:5.7
ShardingSphere-Proxy
网盘下载地址
链接:https://pan.baidu.com/s/15yUIDQOdDDwUtVLNxNa9Cg 提取码:3hp3
Java的JDK
网盘下载地址
链接:https://pan.baidu.com/s/1A-ksNN0YicT3hXjFscGGwA 提取码:r9e0
下载Mysql的连接驱动 文件放到根目录 lab文件夹下
网盘下载地址:
链接:https://pan.baidu.com/s/1924iUe7wxGpStAzxxv2K3g 提取码:jy7z
配置
分表
配置
config-sharding.yaml 分片的配置文件
# 3、创建客户端连接库 hmms:虚拟的数据库名称【最好和真实的数据库名称一样】 在server.yaml命名 schemaName: hmms # 1、连接mysql dataSources: hmmsdatasources-0: #节点名称 自定义 url: jdbc:mysql://127.0.0.1:3306/真实数据库名称?serverTimezone=UTC&useSSL=false username: 数据库用户名 password: 数据库密码 connectionTimeoutMilliseconds: 30000 idleTimeoutMilliseconds: 60000 maxLifetimeMilliseconds: 1800000 maxPoolSize: 50 # 2、分片规则 shardingRule: tables: #表 user: #逻辑表名 要对哪个表进行分表 actualDataNodes: hmmsdatasources-0.user-${ 0..1} #分几张表 这个是两个表 #hmmsdatasources-0:节点名称 tableStrategy: #数据分表策越 inline: shardingColumn: useid #分表字段 algorithmExpression: user-${ useid % 2} #对useid取模分表 #创建多个表 #表名: #逻辑表名 要对哪个表进行分表 #actualDataNodes: hmmsdatasources-0.表名-${0..1} #分几张表 这个是两个表 #hmmsdatasources-0:节点名称
server.yaml
authentication: users: root: #数据库用户名 password: 数据密码 sharding: password: sharding authorizedSchemas: hmms
ShardingSpere-Proxy
运行命令
#根目录bin文件下执行命令 start.bat
运行结果如图:
MySql 数据库
新建真实数据库名称
SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for user -- ---------------------------- DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `useid` int(11) NOT NULL, `usenam` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '登录名', `usepwd` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '登录密码', `usestate` int(11) NULL DEFAULT 2 COMMENT '-1:删除1:注销 2:正常 3:挂失', `usekey` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户秘钥', `usetel` varchar(11) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户手机', `createbyid` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '添加人', `createbytime` datetime(0) NULL DEFAULT NULL COMMENT '添加时间', `modifybyid` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '修改人', `modifybytime` datetime(0) NULL DEFAULT NULL COMMENT '修改时间', PRIMARY KEY (`useid`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; SET FOREIGN_KEY_CHECKS = 1;
如图:
新建 3307 虚拟数据库连接
如图:
删除表
删除3306中hmms库的user表,并在虚拟的数据库中执行新建表的脚本,运行结果如下:
虚拟数据库:
真实数据库
添加数据
虚拟数据库中添加两条数据,运行结果如下:
真实数据库 表一,运行结果如下:
真实数据库 表二,运行结果如下:
分库
配置
# 3、创建客户端连接库 schemaName: hmms # 1、连接mysql dataSources: hmmsdatasources-0: #真实数据库0 url: jdbc:mysql://127.0.0.1:3306/hmms-0?serverTimezone=UTC&useSSL=false username: 用户名 password: 密码 connectionTimeoutMilliseconds: 30000 idleTimeoutMilliseconds: 60000 maxLifetimeMilliseconds: 1800000 maxPoolSize: 50 hmmsdatasources-1: #真实数据库1 url: jdbc:mysql://127.0.0.1:3306/hmms-1?serverTimezone=UTC&useSSL=false username: 用户名 password: 密码 connectionTimeoutMilliseconds: 30000 idleTimeoutMilliseconds: 60000 maxLifetimeMilliseconds: 1800000 maxPoolSize: 50 # 2、分片规则 shardingRule: tables: #表 user: #逻辑表名 actualDataNodes: hmmsdatasources-${ 0..1}.user #分表 tableStrategy: #数据分表策越 inline: shardingColumn: useid #分表字段 algorithmExpression: user-${ useid % 2} #对useid取模分表 defaultDatabaseStrategy: # 数据分库策略 inline: shardingColumn: useid #分库字段 algorithmExpression: hmmsdatasources-${ useid % 2} #对Id取模分库productdatasources-0
分库分表
配置
# 3、创建客户端连接库 schemaName: hmms # 1、连接mysql dataSources: hmmsdatasources-0: #真实数据库0 url: jdbc:mysql://127.0.0.1:3306/hmms-0?serverTimezone=UTC&useSSL=false username: 用户名 password: 密码 connectionTimeoutMilliseconds: 30000 idleTimeoutMilliseconds: 60000 maxLifetimeMilliseconds: 1800000 maxPoolSize: 50 hmmsdatasources-1: #真实数据库1 url: jdbc:mysql://127.0.0.1:3306/hmms-1?serverTimezone=UTC&useSSL=false username: 用户名 password: 密码 connectionTimeoutMilliseconds: 30000 idleTimeoutMilliseconds: 60000 maxLifetimeMilliseconds: 1800000 maxPoolSize: 50 # 2、分片规则 shardingRule: tables: #表 user: #逻辑表名 actualDataNodes: hmmsdatasources-${0..1}.user-${0..1} #分表 tableStrategy: #数据分表策越 inline: shardingColumn: useid #分表字段 algorithmExpression: user-${useid % 2} #对useid取模分表 defaultDatabaseStrategy: # 数据分库策略 inline: shardingColumn: useid #分库字段 algorithmExpression: hmmsdatasources-${useid % 2} #对Id取模分库productdatasources-0
四、ShardingSphere-Proxy运行原理
整体架构
总共6个阶段:
1、Database Adaptors:数据库的选择
2、SQL Parser:解析sql
3、SQL Router:sql路由 去哪一个真实数据库执行
4、SQL Rewriter:sql优化重写 核心 保证性能
5、SQL Executor Engine:执行sql语句 真实数据库获取结果
6、Result Merger:结果合并 从多个表获取结果
五、ShardingSphere_Proxy 分片原理
分片的概念
就是将数据分片到不同的表中。
分片键
分片键就是表中的字段。就是根据什么字段分片的。
分片算法
根据规则【分片算法】按分片键将数据分到不同的表中。
取模算法
缺陷
只能时数字类型
hash+取模
如果分片键为字符类型,就用hash+取模的方式进行分片。
Math.abs(分片键.hashCode()%2)
边栏推荐
- Processing picture class library
- Simple waterfall effect
- Set the location permission in the shutter to "always allow"
- puzzle(019.2)六边锁
- x86电脑上下载debian的arm64的包
- 2台三菱PLC走BCNetTCP协议,能否实现网口无线通讯?
- JS tool function, self encapsulating a throttling function
- Remove the mosaic, there's a way, attached with the running tutorial
- Tiktok brand goes to sea: both exposure and transformation are required. What are the skills of information flow advertising?
- [competition -kab micro entrepreneurship competition] KAB National College Students' micro entrepreneurship action participation experience sharing (including the idea of writing the application form)
猜你喜欢
[MySQL learning notes 21] storage engine
[buuctf.reverse] 117-120
Creo makes a mobius belt in the simplest way
CyCa children's physical etiquette Yueqing City training results assessment successfully concluded
Chitubox micromake l3+ slicing software configuration correspondence
[competition - Rural Revitalization] experience sharing of Zhejiang Rural Revitalization creative competition
Is it harder to find a job in 2020? Do a good job in these four aspects and find a good job with high salary
Rxjs TakeUntil 操作符的学习笔记
Data-driven anomaly detection and early warning of item C in the May 1st mathematical modeling competition in 2021
独步武林,架构选型手册(包含 PDF)
随机推荐
[buuctf.reverse] 117-120
Use evo
MongoDB的原理、基本使用、集群和分片集群
The way that flutter makes the keyboard disappear (forwarding from the dependent window)
Puzzle (019.2) hexagonal lock
Exception: gradle task assemblydebug failed with exit code 1
Fluent creates, reads and writes JSON files
Armbian version name comparison
How to delete a blank page that cannot be deleted in word
Data-driven anomaly detection and early warning of 21 May Day C
Is GF Securities reliable? Is it legal? Is it safe to open a stock account?
clang frontend command failed with exit code 250
[smart agriculture program] smart agriculture small program project is currently popular.
Reza RA series - development environment construction
C语言刷题随记 —— 猴子吃桃
Flutter dialog: cupertinoalertdialog
Online notes on Mathematics for postgraduate entrance examination (9): a series of courses on probability theory and mathematical statistics
Get started quickly with jetpack compose Technology
22 mathematical modeling contest 22 contest C
pmp考试题型需要注意哪些?