当前位置:网站首页>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)
边栏推荐
- Kotlin common standard functions
- How to make a self-made installer and package the program to generate an installer
- 字符串 实现 strStr()
- x86的编码格式
- 22 mathematical modeling contest 22 contest C
- Data-driven anomaly detection and early warning of 21 May Day C
- Kotlin advanced generic
- [buuctf.reverse] 121-125
- 2台三菱PLC走BCNetTCP协议,能否实现网口无线通讯?
- Repo sync will automatically switch the correspondence between the local branch and the remote branch - how to customize this behavior
猜你喜欢

Get started quickly with jetpack compose Technology
![[2020 cloud development + source code] 30 minutes to create and launch wechat applet practical project | zero cost | cloud database | cloud function](/img/89/39851fee714be872a599ad4ddd4852.jpg)
[2020 cloud development + source code] 30 minutes to create and launch wechat applet practical project | zero cost | cloud database | cloud function

Solution to the problem of repeated startup of esp8266

How do wechat sell small commodity programs do? How to open wechat apps to sell things?

Download the arm64 package of Debian on X86 computer

Rxjs TakeUntil 操作符的学习笔记

x86电脑上下载debian的arm64的包

How to "transform" small and micro businesses (II)?

可穿戴设备或将会泄露个人隐私

I put a two-dimensional code with rainbow candy
随机推荐
CyCa children's physical etiquette Yueqing City training results assessment successfully concluded
[buuctf.reverse] 117-120
Use evo
How do wechat sell small commodity programs do? How to open wechat apps to sell things?
【mysql学习笔记22】索引
Data-driven anomaly detection and early warning of 21 May Day C
Redis(一)原理与基本使用
MySQL创建给出语句
Grabcut image segmentation in opencv
如何自制一个安装程序,将程序打包生成安装程序的办法
Flutter dialog: cupertinoalertdialog
BUG-00x bug description + resolve ways
Redis(二)分布式锁与Redis集群搭建
clang frontend command failed with exit code 250
NetCore性能排查
Mengyou Technology: six elements of tiktok's home page decoration, how to break ten thousand dollars in three days
Wearable devices may reveal personal privacy
puzzle(019.2)六边锁
Jetpack compose layout (I) - basic knowledge of layout
Bug- solve the display length limitation of log distinguished character encoding (edittext+lengthfilter)