当前位置:网站首页>The construction of scalable distributed database cluster and the partition design of oneproxy sub database
The construction of scalable distributed database cluster and the partition design of oneproxy sub database
2022-07-02 17:22:00 【Jue seconds of writing code】
OneProxy Sub warehouse and partition design
For super large table storage ,MySQL Support partition table design , You can press a certain field to press the range (Range)、 List by value (List) Or hash algorithm (Hash) And so on .
OneProxy The concept of partitioned table is abstracted from the database layer to SQL Transponder layer of , By analyzing the communication protocol , According to SQL The table name of the query statement and the passed in parameter pair . The upper layer application carries out transparent intelligent routing , So as to achieve the effect of virtual partition , This partition is completely transparent to applications .
stay OneProxy By range is also supported in 、 Design virtual sub database and sub table according to value list or hash algorithm , In terms of content , And MySQL The key information for creating partitioned tables is very similar .
The following describes the database and table allocation of these three partition methods .

Divide the database and table according to the scope
When dividing databases and tables by range , There must be a virtual table name for the application (Table) , And specify one for The fields of the partition (PKey)、 Type of field (Type) , And the method of partitioning (Method). At the same time, for each - A division , Can be used to add suffixes ( Suffix) Set the independent table name in the same way , And specify the cluster group where the partition is located (Group) , And the upper limit of the value of the partition field (Value) etc. .
for example , The design of partitioning an order table by range is as follows :
{
{
"table": "t order",
"pkey"
: "id",
"type" : "int",
"method" : "range",
"partitions":
{
{"suffix" : " 0","group": "group1", "value": "100000" },
{"suffix" : " 1","group": "group1", "value" : "200000" }
{"suffix":" 2","group": "group2", "value": "300000" },
{"suffix": " 3","group": "group2", "value": null}
}
}
}such , When accessing the virtual table name t _order when , Will press its ID“value” Scope oriented real table name , Such as “t _order. 0”、“t_ _order_ 1” Access data in the bisection table .
Divide the database and table according to the value list
When sorting databases and tables by value list , In fact, it is to specify... In the virtual table name - A field for partitioning 、 Type of field 、 And the method of partitioning . meanwhile , For each partition , Can be used to add suffixes ( Suffix) Set the independent table name in the same way , And specify the cluster group where the partition is located , And the list of values that can be obtained from the partition field . When - When a partition does not specify any partition value list , Indicates that all other values fall into this partition .
for example , The design of partitioning an order table by value list is as follows :
{
{
"table" : "t_order",
"pkey" : "id",
"type": "int",
"'method":"list",
"partitions":
{
{ "suffix" : "_0","group": "group1", "value" : ["1", "2", "3"] },
{ "suffix": "_1", "group": "group1", "value" : ["4","5","6"] },
{ "suffix" : "_2", "group": "group2", "value" : ["7", "8","9"] },
{ "suffix" : "_3", "group": "group2", "value" : ["10", "11", "12"] },
{ "suffix": " 4", "group": "group2", "value" :[] }
}
}
}among , The real table name consists of the virtual table name and its suffix , for example “t_order_0”, And the data will be accessed in the assigned cluster group .
Divide the database and table according to the hash algorithm
When the database and table are divided according to the hash algorithm , There has to be - A virtual table name for the application (Table) , And designate - A field for partitioning 、 Type of field , And the method of partitioning . meanwhile , For each - A division , You can set independent table names by adding suffixes , And specify the cluster group where the partition is located . It should be noted that , Partitioning by hash algorithm does not need to specify a value range or value list for each partition , They are made up of OneProxy The hash algorithm in is automatically calculated according to the number of partitions . When partitioning by hash algorithm , The number of partitions cannot be adjusted arbitrarily .
for example , The design of partitioning an order table according to the hash algorithm is as follows :
[
{
"table" : "t_ order",
"pkey" : "id",
"type" : "int",
"method" : "hash",
"partitions" :
[
{ "suffix" :" 0", "group": "group1" },
{ "suffix" :"group": "group2"},
{ "suffix" : " _2", "group": "group1" },
{ "suffix" :”3",, "group": "group2"}
}
}
}among , The real table name consists of the virtual table name and its suffix , And stored in different specified cluster groups . for example “t order_ 0” Store in group “group1” in ,“t _order_ 1” Store in group “group2" in .
For the above three partition methods , Readers can choose according to the actual situation . It is generally recommended to use hash algorithm for partition , In this way, the data distribution will be more reasonable .
The partition configuration is implemented using text files , If you want to - Use multiple partition configurations in one file , You can configure it in the format shown below :
{
{
"table" : "t_ goods",
"pkey" : "id",
"type" : "int",
"method"
"hash",
"partitions" :
{
{ "suffix" :" _0", "group": "group1" },
"suffix" :
"group": "group2" },
"suffix": " 2","group": "group1" },
"suffix" : "_3", "group": "group2"}
}
},
"table" : "t order",
"pkey" : "id",
"type" :
"char",
"method"
"hash",
"partitions" :
[
{"suffix" : " 0", "group": "group1" },
{"suffix":"group": "group2" }
{"suffix" :" 2", "group": "group1" },
{ "suffix" : "_3", "group": "group2"}
}
}
}After the file configuration is complete ( For example, save the configuration file as part1.txt) , You can add it to... In the following way OneProxy In the configuration of :
proxy-part-tables.1 = /usr/ 1ocal/ oneproxy/conf/part1.txtDual hot standby design
After using the database agent middleware , We have achieved a high-performance read-write separation configuration , however , For the proxy server itself , There is still - A single point of failure problem .
Want to solve the single point of failure problem , You need to use LVS and Keepalived Realize dual machine hot standby design .
LVS yes Linux Short for virtual server .Linux The kernel is completely built in LVS Each function module , It works in OSI (Open System Interconnect) In the network layer of the model , Load balancing and server cluster design can be carried out .
Keepalived Is a switch software , Working in OSI The network layer of the model 、 Transport layer and application layer , It mainly provides functions such as load balancing and high availability . The implementation of load balancing depends on LVS Kernel IPVS modular , And high availability is through VRRP (Virtual Router Redundancy Protocol, Virtual routing redundancy protocol ) Implementation of failover services between multiple machines .
Use LVS+Keepalived Can be in OnePoxy Build on the server - A dual hot standby design , Pictured 13-3 Shown .

In this architectural design , contain - A broadcast IP Address and four servers , As shown below :
VIP: 10.10.10.100
LVS Master: 10.10.10.28
LVS Back: 10.10.10.38
Real Server1: 10.10.10.24
Real Server2: 10.10. 10.34among ,VIP It's a virtual broadcast IP Address ,LVS_ Master and LVS_ _Back It is a dual machine hot standby host server and a standby server ,Real Server It's a real server , That is, you have installed OneProxy Database proxy server . The settings of each server are described in detail below .
Real Server To configure
reference 13.6 section OneProxy How to install and configure , On the other side - - Servers ( If IP The address is 10.10.10.34) Install a with the same configuration OneProxy proxy server , Then configure on both machines Real_ Server.
Use the following command to create - A startup file :
vi /etc/init. d/ realserverEnter the following :
SNS VIP=10.10.10.100
/etc/rc.d/init. d/ functions
case "$1" in
start)
ifconfig lo:0 $SNS_ VIP netmask 255.255.255.255 broadcast $SNS_ VIP
/sbin/route add -host $SNS_ VIP dev 1o:0
echo "1" >/proc/sys/net/ipv4/conf/1o/arp_ ignore
echo "2" >/proc/sys/net/ ipv4/conf/1o/arp_ announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_ announce
sysctl -p >/dev/null 2>&1
echo "RealServer Start OK"
;;
stop)
ifconfig 1o:0 down
route del $SNS_ VIP > /dev/null 2>&1
echo "0" >/proc/sys/net/ ipv4/conf/1o/arp_ ignore
echo "0" >/proc/sys/net/ ipv4/conf/lo/arp_ announce
echo "0" >/proc/sys/net/ ipv4/conf/all/arp_ ignore
echo "0" >/proc/sys/net/ ipv4/conf/all/arp announce
echo "RealServer Stoped"
;
*)
echo "Usage: $0 {startIstop}"
exit 1
I esac
exit0Save and exit the editing state .
Make the file executable :
chmod 755 /etc/ init.d/ realserverUse the following command to start the service :
service realserver start
LVS Host configuration
Use the following command , install Keepalived:
yum install -y keepalivedEnter the installation directory :
cd /etc/keepalivedClear the original configuration :
> keepal ived. confRe edit the configuration :
vi keepalived. conf Set as follows :
global defs {
noti fication email {
[email protected] cn
}
notification email_ from [email protected] cn
smtp_ server 10.10.10.1
smtp_ connection timeout 30
router_ id LVS_ DEVEL # LVS Of ID, It should be unique within a network
}
vrrp_ instance VI 1 {
state MASTER
# Appoint Keepalived Role ,MASTER For the host ,BACKUP Standby machine
interface ens192 # Name of local network card , You can use ifconfig Command view
virtual_ router_ id 51 # Virtual route number , Main engine and standby engine shall be one - Cause
priority 100 # Define priority , The greater the number , The higher the priority , host DR It must be larger than the standby unit DR
advert_ int 1 # Check interval , The default is 1s
authentication {
auth_ type PASS
auth pass 1111
}
virtual ipaddress {
10.10.10.100 # Definition VIP Address , Multiple can be set , Each row of a
}
}
# Define the LVS Of VIP Address and port
virtual server 10.10.10.100 3306 {
delay loop 6 # Set health check time , The unit is seconds
1b_ algo wrr # Set the load scheduling algorithm as WRR
1b kind DR # Set up IVS Mechanism to implement load , Yes NAT、TUN and DR Three modes
nat_ mask 255.255.255.0
persistence_ timeout 0
protocol TCP
real_ server 10.10.10.26 3306 { # Appoint Real_ Serverl Of IP Address
weight 3
# Configure node weights , The greater the number , Higher weight
TCP CHECK {
connect timeout 10
nb_ get_ retry 3
delay_ before_ retry 3
connect_ port 3306
}
}
real_ server 10.10.10.36 3306 { # Appoint Real_ Server2 Of IP Address
weight 3 # Configure node weights , The greater the number , Higher weight
TCP CHECK {
connect timeout 10
nb _get_ retry 3
delay before_ retry 3
connect port 3306
}
}
}start-up Keepalived:
service keepalived startstart-up VIP Address :
ifconfig ens192:0 10.10.10.100 broadcast 10.10.10.100 netmask 255.255.255.255
upUse the following command to view VIP Start result of address :
ip addrIf you can see in the output VIP The address is “10.10.10.100”, Indicates that the system has been started successfully , The code is as follows :
1: 1o: <LOOPBACK,UP,LOWER _UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host 1o
valid lft forever preferred lft forever
inet6: :1/128 scope host
valid lft forever preferred_ lft forever
2: ens192: <BROADCAST ,MULTICAST,UP, LOWER UP> mtu 1500 qdisc pfifo_ fast state UP
qlen
1000
link/ether 00:0c:29:81:6a:85 brd ff:ff:ff:ff:ff:ff
inet 10.10.10.26/24 brd 10.10.10.255 scope global ens192
valid_lft forever preferred_ lft forever
inet 10.10.10.100/32 brd 10.10.10.100 scope global ens192:0
valid lft forever preferred lft forever
inet6 fe80: :20c:29ff:fe81:6a85/64 scope link
valid lft forever preferred lft foreverIf you want to shut down VIP Address , You can use the following command :
I i fconfig ens192:0 downLVS Standby machine configuration
The installation method and process of the standby machine are basically the same as that of the host , Only "keepalived.conf" The configuration content of is slightly different ,
The code is as follows :
global defs
noti fication email {
[email protected] cn
}
notification email_ from [email protected] cn
smtp_ server 10.10.10.1
smtp connection timeout 30
router_ _id LVS_ DEVEL # LVS Of ID, It should be unique within a network
}
vrrp instance VI 1 {
state BACKUP
# Appoint Keepalived Role ,MASTER For the host ,BACKUP Standby machine
interface ens192 # Name of local network card , You can use ifconfig Command view
virtual_ router_ id 51 # Virtual route number , The main machine and standby machine should be consistent
priority 99 # Define priority , The greater the number , The higher the priority , host DR It must be larger than the standby unit DR
advert_ int. 1 # Check interval , The default is 1s
authentication {
auth_ type PASS
auth pass 1111
virtual ipaddress {
10.10.10.100 # Definition VIP Address , Multiple can be set , Each row of a
}
}
# Define the LVS Of VIP Address and port
virtual_ server 10.10.10.100 3306 {
delay_ loop 6 # Set health check time , The unit is seconds
1b_ algo wrr # Set the load scheduling algorithm as WRR
lb kind DR # Set up LVS Mechanism to implement load , Yes NAT、 TUN and DR Three modes
nat_ mask 255.255.255.0
persistence_ timeout (
protocol TCP
real server 10.10.10.26 3306 { # Appoint Real_ Serverl Of IP Address
weight 3 # Configure node weights , The greater the number , Higher weight
rCP CHECK {
connect_ timeout 10
nb_ get_ retry 3
delay_ before_ retry 3
connect_ port 3306
}
}
real server 10.10.10.36 3306 { # Appoint Real Server2 Of IP Address
weight 3 # Configure node weights , The greater the number , Higher weight
TCP CHECK {
connect_ timeout 10
nb_ get_ retry 3
delay_ before_ retry 3
connect_ port 3306
}
}
}among , stay “rp jinstance" Configuration in progress , hold “state” Change it to “BACKUP”, hold “priority" Change it to “99”, Other configurations are basically the same .
After starting the database agent service of dual machine hot standby , In microservice applications , You can change the address of the connection server in the data source to VIP Address .
Summary
This chapter introduces CentOS 7 Install in MySQL A simple way to solve this problem , And use master-slave design to build a distributed database cluster , Set the - A high performance 、 Scalable database cluster system , meanwhile , The design of high availability cluster is realized by grouping . In database access design , Use OneProxy The middleware implements the configurable read-write separation calling method , Combined with the function of sub database and sub table, the access efficiency of the database is improved . Last , Use dual hot standby design , It provides a more secure and reliable guarantee for the use of database agent middleware and its cluster .
Through the introduction of database cluster design in this chapter , Readers can deeply understand the working principle of database clustering . If you use a cloud service database , It is recommended to use the distributed database provided by the cloud service provider , This will make it more cost-effective .
The content of this article is the actual combat of microservice architecture : Construction of scalable distributed database cluster ,OneProxy Sub warehouse and partition design 、 Dual hot standby design
- The next article will explain the actual combat of microservice architecture : The construction of high availability distributed file system ;
- Friends who think the article is good can forward this article and pay attention to Xiaobian ;
- Thank you for your support !
边栏推荐
- Deep learning image data automatic annotation [easy to understand]
- executescalar mysql_ExecuteScalar()
- Vscode + eslint configuration
- 七张图,学会做有价值的经营分析
- What if the default browser cannot be set?
- QWebEngineView崩溃及替代方案
- traceroute命令讲解
- Sword finger offer 27 Image of binary tree
- Linux Installation PostgreSQL + Patroni cluster problem
- Geoserver: publishing PostGIS data sources
猜你喜欢

A case study of college entrance examination prediction based on multivariate time series
![[web technology] 1233 seconds understand web component](/img/42/c98d8112dc4ece0a92dda97647be5c.jpg)
[web technology] 1233 seconds understand web component

如何与博格华纳BorgWarner通过EDI传输业务数据?

13、Darknet YOLO3

The difference of message mechanism between MFC and QT

宝宝巴士创业板IPO被终止:曾拟募资18亿 唐光宇控制47%股权

Listing of chaozhuo Aviation Technology Co., Ltd.: raising 900million yuan, with a market value of more than 6billion yuan, becoming the first science and technology innovation board enterprise in Xia

Use the API port of the bridge of knowledge and action to provide resources for partners to access

Weili holdings listed on the Hong Kong Stock Exchange: with a market value of HK $500million, it contributed an IPO to Hubei

使用知行之桥的API端口,提供资源供合作伙伴访问
随机推荐
Visibilitychange – refresh the page data when the specified tab is visible
Sword finger offer 27 Image of binary tree
[web technology] 1233 seconds understand web component
ssb门限_SSB调制「建议收藏」
简单介绍BASE64Encoder的使用
智能垃圾桶(五)——点亮OLED
class和getClass()的区别
LeetCode:1380. Lucky number in matrix -- simple
Goodbye, shucang. Alibaba's data Lake construction strategy is really awesome!
IP地址转换地址段
超卓航科上市:募资9亿市值超60亿 成襄阳首家科创板企业
TCP congestion control details | 2 background
SSB threshold_ SSB modulation "suggestions collection"
Nexus簡介及小白使用IDEA打包上傳到Nexus3私服詳細教程
Chapter 3 of hands on deep learning - (1) linear regression is realized from scratch_ Learning thinking and exercise answers
linux安装postgresql + patroni 集群问题
Interpretation of key parameters in MOSFET device manual
[fluent] dart data type map type (create map set | initialize map set | traverse map set)
Jiuxian's IPO was terminated: Sequoia and Dongfang Fuhai were shareholders who had planned to raise 1billion yuan
Chmod command principle and usage details [easy to understand]