当前位置:网站首页>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: hmmsShardingSpere-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)
边栏推荐
- MySQL source code reading (II) login connection debugging
- Puzzle (019.2) hexagonal lock
- An auxiliary MVP architecture project quick development library -mvpfastdagger
- The problem of wirengpi program running permission
- Analysis on the thinking of 2022 meisai C question
- Grabcut image segmentation in opencv
- Can two Mitsubishi PLC adopt bcnettcp protocol to realize wireless communication of network interface?
- PHP obtains the IP address, and the apache2 server runs without error
- Cubemx stm32f105rb USB flash drive reading and writing detailed tutorial
- 2022 postgraduate entrance examination experience post -- Alibaba Business School of Hangzhou Normal University -- management science and Engineering (including the recommendation of books and course
猜你喜欢

Title B of the certification cup of the pistar cluster in the Ibagu catalog

【mysql学习笔记22】索引

With the QQ group file storage function of super nice, you immediately have n cloud disks that are easy to download and never expire
![[MySQL learning notes 21] storage engine](/img/3a/a3cd573281efc689cafdb7d7562ce0.png)
[MySQL learning notes 21] storage engine

2台三菱PLC走BCNetTCP协议,能否实现网口无线通讯?

Online notes on Mathematics for postgraduate entrance examination (9): a series of courses on probability theory and mathematical statistics

Remove the mosaic, there's a way, attached with the running tutorial
![[zufe expense reimbursement] zhecai invoice reimbursement specification (taking Xinmiao reimbursement as an example), which can be passed in one trip at most](/img/28/c5c6b6d03b459745dc3735f8b39ea9.jpg)
[zufe expense reimbursement] zhecai invoice reimbursement specification (taking Xinmiao reimbursement as an example), which can be passed in one trip at most

Consul的基本使用与集群搭建

C语言刷题随记 —— 猴子吃桃
随机推荐
Flutter Gaode map privacy compliance error
Mysql 源码阅读(二)登录连接调试
[buuctf.reverse] 117-120
【mysql学习笔记22】索引
Kotlin advanced generic
I put a two-dimensional code with rainbow candy
Tiktok brand goes to sea: both exposure and transformation are required. What are the skills of information flow advertising?
Match a mobile number from a large number of mobile numbers
Creo makes a mobius belt in the simplest way
[buuctf.reverse] 121-125
[zero foundation understanding innovation and entrepreneurship competition] overall cognition and introduction of mass entrepreneurship and innovation competition (including FAQs and integration of bl
clang frontend command failed with exit code 250
Jetpack compose layout (I) - basic knowledge of layout
瑞吉外卖项目(二)
PHP obtains the IP address, and the apache2 server runs without error
oracle 函数 触发器
链表 删除链表中的节点
[MySQL learning notes 21] storage engine
Puzzle (019.2) hexagonal lock
Is it safe to open an account on the compass?