当前位置:网站首页>Shardingsphere proxy 4.1 sub database and sub table
Shardingsphere proxy 4.1 sub database and sub table
2022-06-25 10:05:00 【@@Mr.Fu】
One 、ShardingSphere-Proxy Core concept of
ShardingSphere-Proxy Concept
Official address :https://shardingsphere.apache.org/index_zh.html
ShardingSphere-Proxy Is the agent of the database , Pictured :

ShardingSphere-Proxy Which databases are the main agents
Default agent :Mysql、PostSql
Achieve the purpose of the agent
It is mainly to complete the division of warehouse and table
Read and write separation
These two are also ShardingSphere-Proxy The two core functions of .
Sub database and sub table
Concept and purpose of sub database
Concept
The tables in the database are stored in different databases ; Pictured :

Purpose
Prevent resource competition among multiple tables in a library 【CPU、 Memory 】, Performance degradation .
Concept and purpose of sub table
Concept
Divide a table in the database into multiple tables , Pictured :

Purpose
Split table is to solve the problem that the amount of data in the table is too large , Improve the performance of user query and data addition .
such as : With mysql Database, for example , When users add data, they will pass mysql Of InnoDB The engine stores data ,InnoDB The engine should ensure that the performance of data is within a certain range , The peak value of the largest amount of data in the table is 2000w, If exceeded 2000W Then the performance of adding data will decline , So we're going to surpass 2000W The table of data volume is split into multiple tables , Only in this way can we ensure the user experience .
defects
Too much concurrency , There will be resource competition in the table [CPU、 Memory ] The problem of , This leads to performance degradation , The user experience becomes worse .
Solution : sub-treasury
Sub database and sub table
Purpose
Solve the problems of table resource competition and large amount of data .
Two 、ShardingSphere-Proxy Application scenarios of
scene
Both individual projects and micro service projects can use sub database and sub table .
3、 ... and 、ShardingSphere-Proxy Distribution table landing
Tools
ShardingSphere-Proxy
programme
In process
Pictured :

- defects
- Resource competition .
- Abnormal influence problem .
- defects
Out of process 【 recommend 】
Pictured :

- defects
- The problem of large amount of maintenance .
- The performance is weaker than that in the process .
- It can be placed in the intranet for communication 【docker】
- defects
Realization
Conditions
Mysql database edition :5.7
ShardingSphere-Proxy
Download address of online disk
link :https://pan.baidu.com/s/15yUIDQOdDDwUtVLNxNa9Cg Extraction code :3hp3
Java Of JDK
Download address of online disk
link :https://pan.baidu.com/s/1A-ksNN0YicT3hXjFscGGwA Extraction code :r9e0
download Mysql The connection driver of Put the file in the root directory lab Under the folder
Download address of online disk :
link :https://pan.baidu.com/s/1924iUe7wxGpStAzxxv2K3g Extraction code :jy7z
To configure
table
To configure
config-sharding.yaml Fragmented configuration file
# 3、 Create client connection Library hmms: Virtual database name 【 It's better to have the same name as the real database 】 stay server.yaml name schemaName: hmms # 1、 Connect mysql dataSources: hmmsdatasources-0: # The name of the node Customize url: jdbc:mysql://127.0.0.1:3306/ Real database name ?serverTimezone=UTC&useSSL=false username: Database user name password: Database password connectionTimeoutMilliseconds: 30000 idleTimeoutMilliseconds: 60000 maxLifetimeMilliseconds: 1800000 maxPoolSize: 50 # 2、 Fragmentation rule shardingRule: tables: # surface user: # Logical table name Which table to divide actualDataNodes: hmmsdatasources-0.user-${ 0..1} # Divide it into several tables This is two tables #hmmsdatasources-0: The name of the node tableStrategy: # The data is divided into tables inline: shardingColumn: useid # Sub table field algorithmExpression: user-${ useid % 2} # Yes useid Die taking table # Create multiple tables # Table name : # Logical table name Which table to divide #actualDataNodes: hmmsdatasources-0. Table name -${0..1} # Divide it into several tables This is two tables #hmmsdatasources-0: The name of the nodeserver.yaml
authentication: users: root: # Database user name password: Data password sharding: password: sharding authorizedSchemas: hmmsShardingSpere-Proxy
Run the command
# root directory bin To execute a command under a document start.batThe operation result is as shown in the figure :

MySql database
New real database name
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 ' Login name ', `usepwd` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT ' The login password ', `usestate` int(11) NULL DEFAULT 2 COMMENT '-1: Delete 1: Cancellation 2: normal 3: Report the loss of ', `usekey` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT ' User secret key ', `usetel` varchar(11) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT ' User's mobile phone ', `createbyid` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT ' Add by ', `createbytime` datetime(0) NULL DEFAULT NULL COMMENT ' Add the time ', `modifybyid` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT ' Modifier ', `modifybytime` datetime(0) NULL DEFAULT NULL COMMENT ' Modification time ', PRIMARY KEY (`useid`) USING BTREE ) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; SET FOREIGN_KEY_CHECKS = 1;Pictured :

newly build 3307 Virtual database connection
Pictured :

Delete table
Delete 3306 in hmms Library user surface , And execute the script of creating a new table in the virtual database , The operation results are as follows :
Virtual database :

Real database

Add data
Add two pieces of data to the virtual database , The operation results are as follows :

Real database Table 1 , The operation results are as follows :

Real database Table two , The operation results are as follows :

sub-treasury
To configure
# 3、 Create client connection Library schemaName: hmms # 1、 Connect mysql dataSources: hmmsdatasources-0: # Real database 0 url: jdbc:mysql://127.0.0.1:3306/hmms-0?serverTimezone=UTC&useSSL=false username: user name password: password connectionTimeoutMilliseconds: 30000 idleTimeoutMilliseconds: 60000 maxLifetimeMilliseconds: 1800000 maxPoolSize: 50 hmmsdatasources-1: # Real database 1 url: jdbc:mysql://127.0.0.1:3306/hmms-1?serverTimezone=UTC&useSSL=false username: user name password: password connectionTimeoutMilliseconds: 30000 idleTimeoutMilliseconds: 60000 maxLifetimeMilliseconds: 1800000 maxPoolSize: 50 # 2、 Fragmentation rule shardingRule: tables: # surface user: # Logical table name actualDataNodes: hmmsdatasources-${ 0..1}.user # table tableStrategy: # The data is divided into tables inline: shardingColumn: useid # Sub table field algorithmExpression: user-${ useid % 2} # Yes useid Die taking table defaultDatabaseStrategy: # Data sub database strategy inline: shardingColumn: useid # Sub database field algorithmExpression: hmmsdatasources-${ useid % 2} # Yes Id Take the mold branch library productdatasources-0
Sub database and sub table
To configure
# 3、 Create client connection Library schemaName: hmms # 1、 Connect mysql dataSources: hmmsdatasources-0: # Real database 0 url: jdbc:mysql://127.0.0.1:3306/hmms-0?serverTimezone=UTC&useSSL=false username: user name password: password connectionTimeoutMilliseconds: 30000 idleTimeoutMilliseconds: 60000 maxLifetimeMilliseconds: 1800000 maxPoolSize: 50 hmmsdatasources-1: # Real database 1 url: jdbc:mysql://127.0.0.1:3306/hmms-1?serverTimezone=UTC&useSSL=false username: user name password: password connectionTimeoutMilliseconds: 30000 idleTimeoutMilliseconds: 60000 maxLifetimeMilliseconds: 1800000 maxPoolSize: 50 # 2、 Fragmentation rule shardingRule: tables: # surface user: # Logical table name actualDataNodes: hmmsdatasources-${0..1}.user-${0..1} # table tableStrategy: # The data is divided into tables inline: shardingColumn: useid # Sub table field algorithmExpression: user-${useid % 2} # Yes useid Die taking table defaultDatabaseStrategy: # Data sub database strategy inline: shardingColumn: useid # Sub database field algorithmExpression: hmmsdatasources-${useid % 2} # Yes Id Take the mold branch library productdatasources-0
Four 、ShardingSphere-Proxy Operation principle
The overall architecture

in total 6 Stages :
1、Database Adaptors: The choice of database
2、SQL Parser: analysis sql
3、SQL Router:sql route Which real database to execute
4、SQL Rewriter:sql Optimized rewriting The core Guaranteed performance
5、SQL Executor Engine: perform sql sentence Get results from real database
6、Result Merger: The results merge Get results from multiple tables
5、 ... and 、ShardingSphere_Proxy Fragmentation principle
The concept of fragmentation
Is to slice data into different tables .
Patch key
The fragment key is the field in the table . It is divided according to what field .
Sharding algorithm
According to rules 【 Sharding algorithm 】 Press the fragment key to divide the data into different tables .
Modular algorithm
defects
Only numeric types can be used
hash+ modulus
If the fragment key is of character type , Just use hash+ Take the mold and divide it .
Math.abs( Patch key .hashCode()%2)
边栏推荐
- Fluent: target support file /pods runner / pods runner frameworks Sh: permission denied - stack overflow
- Force buckle -104 Maximum depth of binary tree
- 力扣-104. 二叉树的最大深度
- Kotlin common standard functions
- How to make small programs on wechat? How to make small programs on wechat
- Is GF Securities reliable? Is it legal? Is it safe to open a stock account?
- Swift recursively queries the array for the number closest to the specified value
- MySQL source code reading (II) login connection debugging
- 22 mathematical modeling contest 22 contest C
- js工具函数,自己封装一个节流函数
猜你喜欢

Methodchannel of flutter

Wallys/MULTI-FUNCTION IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL

NetCore性能排查

Etcd tutorial - Chapter 4 etcd cluster security configuration

Jetpack compose layout (IV) - constraintlayout
![[wechat applet full stack development course] course directory (mpvue+koa2+mysql)](/img/1c/ab39cf0a69d90a85665a099f5dbd26.jpg)
[wechat applet full stack development course] course directory (mpvue+koa2+mysql)
![[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate](/img/75/a06e20b4394579cbd9f6d3a075907a.jpg)
[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate

Vscode attempted to write the procedure to a pipeline that does not exist

CyCa children's physical etiquette Yueqing City training results assessment successfully concluded

CYCA少儿形体礼仪 乐清市培训成果考核圆满落幕
随机推荐
Jetpack compose layout (IV) - constraintlayout
An auxiliary MVP architecture project quick development library -mvpfastdagger
C语言刷题随记 —— 猴子吃桃
Cocopod error failed: undefined method `map 'for nil:nilclass
Etcd tutorial - Chapter 4 etcd cluster security configuration
汇付国际为跨境电商赋能:做合规的跨境支付平台!
Get started quickly with jetpack compose Technology
Rxjs TakeUntil 操作符的学习笔记
Reasons for Meiye to choose membership system
Kotlin advanced set
Why should the terminal retail industry choose the member management system
Nano data World Cup data interface, CSL data, sports data score, world cup schedule API, real-time data interface of football match
Free platform for wechat applet making, steps for wechat applet making
Fluent: target support file /pods runner / pods runner frameworks Sh: permission denied - stack overflow
Neat Syntax Design of an ETL Language (Part 2)
匯付國際為跨境電商賦能:做合規的跨境支付平臺!
How to make a self-made installer and package the program to generate an installer
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
Kotlin Foundation
pmp考试题型需要注意哪些?