E-commerce system deployment

Chapter 2

MyCat & Nginx

The target

Goal 1: Understand MyCat sharding and be able to configure MyCat sharding

Goal 2: Master Nginx installation and static web site deployment

Goal 3: Master Nginx static web site deployment

Goal 4: Understand Nginx reverse proxies and load balancers and be able to configure reverse proxies and load balancers

Goal 5: Understand the overall deployment plan of Upbuy

1. Open source database middleware -MyCat

Nowadays, with the development of the Internet, the magnitude of data is also increasing exponentially, from GB to TB to PB. It is more and more difficult to operate the data, and the traditional relational database can not meet the needs of fast query and data insertion. The emergence of NoSQL at this point temporarily solved the crisis. It gains performance by reducing data security, reducing support for transactions, and reducing support for complex queries.

However, there are situations in which some of the trade-offs of NoSQL are not suitable for use scenarios, such as those in which transaction and security metrics are absolutely required. NoSQL is definitely not enough at this point, so you still need to use a relational database. What if you use a relational database to solve the problem of mass storage? At this time, we need to do database cluster, in order to improve the query performance of a database data distributed to different databases for storage.

1.1 introduction of MyCat

Mycat is the brainchild of Alibaba’s well-known open source product, Cobar. The core function and strength of Cobar is MySQL database sharding. This product was once widely spread. It is said that the original initiator was proficient in MySQL, and then jumped from Ali, which then opened source Cobar and maintained it until early 2013.

Cobar has a good idea and implementation path. Based on Java development, the implementation of MySQL public binary transfer protocol, cleverly disguised itself as a MySQL Server, currently on the market most MySQL client tools and applications are compatible. It’s smarter than implementing a new database protocol yourself, because the ecosystem is where the pendulum is.

Mycat is an evolution based on Cobar. It has carried out a thorough reconstruction of cobar code, used NIO to reconstruct the network module, optimized the Buffer kernel, enhanced the aggregation, Join and other basic features, and at the same time compatible with most databases to become a universal database middleware.

To put it simply, MyCAT is: a novel database middleware product that supports mysql clusters, or Mariadb clusters, for highly available data sharding clusters. You can use MyCat just like you use mysql. Mycat is invisible to developers.

 

MyCat supports:

 

1.2MyCat download and installation

1.2.1 installing and Starting MySQL

JDK: JDK 1.7 or later is required

MySQL: The recommended MySQL version is 5.5 or later

The procedure for installing and starting MySQL is as follows: step 1-5 is omitted.

(1) Upload the MySQL server and client installation packages (RPM) to the server

 

(2) Check whether MySQL has been installed

rpm -qa|grep -i mysql

(3) Uninstall the old version of MySQL

RPM -e –nodeps Software name
  1. Installing the Server
RPM — the ivh MySQL server – 5.5.49-1. There.. I386 RPM
  1. Installing the Client
RPM – the ivh MySQL – the client – 5.5.49-1 there.. I386 RPM

(6) Start MySQL service

service mysql start

(7) Log in to MySQL

mysql -u root
  1. Set the remote login permission
GRANT ALL PRIVILEGES ON *.* TO ‘root’@’%’IDENTIFIED BY ‘123456’  WITH GRANT OPTION;

Connect local SQLyog to remote MySQL for testing

1.2.2 MyCat installation and startup

MyCat:

MyCat’s official website:

http://www.mycat.org.cn/

Download address:

https://github.com/MyCATApache/Mycat-download

Step 1: Upload mycat-server-1.4-release-20151019230038-linux.tar. gz to the server

Step 2: Decompress the package. You are advised to save mycat in /usr/local/mycat.

Tar -xzvf mycat-server-1.4-release-20151019230038-linux.tar. GZMV Mycat /usr/local

Step 3: Go to the bin directory of mycat and start mycat

./mycat start

Stop:

./mycat stop

{the console of the commands supported by mycat | start | stop | restart | status | dump}

The default port number for Mycat is 8066

1.3MyCat Sharding – Mass data storage solution

1.3.1 What is Sharding

In simple terms, it means that the data stored in the same database is dispersed to multiple databases (hosts) under certain specific conditions, so as to achieve the effect of dispersing the load of a single device.

Data Sharding can be divided into two Sharding modes according to the types of Sharding rules.

(1) One is to segment different databases (hosts) according to different tables (or schemas), which can be called vertical (vertical) data segmentation

(2) The other is to split the data in the same table to multiple databases (hosts) according to certain conditions according to the logical relationship of the data in the table, which is called horizontal (horizontal) segmentation of data.

             

MyCat sharding strategy:

 

1.3.2 Concept of sharding correlation

Logical library (Schema) :

The previous section talked about database middleware. Usually, for practical applications, there is no need to know the existence of middleware. Business developers only need to know the concept of database, so database middleware can be regarded as a logical library composed of one or more database clusters.

Logical table:

Since there are logical libraries, there will be logical tables. In a distributed database, for applications, the tables that read and write data are logical tables. A logical table can be divided into one or more shard libraries after data is shard, or it can be composed of only one table without data shard.

Shard tables: These are tables with large data that need to be shelled into multiple databases, so that each shard has a portion of the data, and all the shards constitute the complete data. So this is a table that needs to be sharded.

Non-sharded tables: Not all tables in a database are large, and some tables may not need to be sharded. Non-sharded tables are tables that do not need to be sharded.

Fragment Node (dataNode)

After data is sharded, a large table is divided into different shard databases. The database where each table shard resides is the dataNode.

Node host (dataHost)

After data segmentation, each shard node (dataNode) may not have an exclusive machine. There can be multiple shard databases on the same machine, so that the machine where one or more shard nodes (Datanodes) reside is the dataHost. In order to avoid the limit on the number of concurrent hosts on a single node, As far as possible, slice nodes (Datanodes) with high read/write pressure should be evenly placed on different node hosts (datahosts).

Sharding rules

As mentioned above, when a large table is divided into several sharding tables, certain rules are required. In this way, the rules for dividing data into certain sharding tables according to certain business rules are sharding rules. It is very important to select appropriate sharding rules for data sharding, which will greatly avoid the difficulty of subsequent data processing.

1.3.3 MyCat Fragment Configuration

1. Configure schema.xml

Schema. XML, as one of the important configuration files in MyCat, manages the logical library, logical tables, sharding rules, Datanodes, and DataSource of MyCat. Understanding these configurations is a prerequisite for using MyCat correctly. Here the file is parsed layer by layer.

The SCHEMA tag is used to define the logical libraries in MyCat instances

The Table label defines the logical Table rule in MyCat, which is used to specify sharding rules. The sharding rule of auto-sharding is based on the ID value range: 1-5000000 is the first piece 5000001-10000000 is the second piece…. We’ll explain the Settings in section 5.

The dataNode tag defines the data nodes in MyCat, which are commonly referred to as data fragments.

DataHost tag also exists as the lowest level tag in myCAT logical library, which directly defines the specific database instance, read/write separation configuration and heartbeat statement.

Create three databases on the server, db1 DB2 DB3

Modify schema. XML as follows:

<? The XML version = “1.0”? > <! DOCTYPE mycat:schema SYSTEM “schema.dtd”><mycat:schema xmlns:mycat=”org.opencloudb/”><schemaname=”PINYOUGOUDB” checkSQLschema=”false” sqlMaxLimit=”100″><table name=”tb_test” dataNode=”dn1,dn2,dn3″ rule=”auto-sharding-long” /></schema><dataNode name=”dn1″ dataHost=”localhost1″ database=”db1″ /><dataNode name=”dn2″ dataHost=”localhost1″ database=”db2″ /><dataNode name=”dn3″ dataHost=”localhost1″ database=”db3″ /><dataHost name=”localhost1″ maxCon=”1000″ minCon=”10″ balance=”0″writeType=”0″ dbType=”mysql” dbDriver=”native” switchType=”1″ SlaveThreshold = “100” > < heartbeat > select the user () < / heartbeat > < writeHost host = “hostM1” url = “192.168.25.142:3306″ user=”root”password=”123456”></writeHost></dataHost></mycat:schema>

2. Configure server.xml

Server.xml holds almost all of the system configuration information required by MyCat. The most common is to configure the user name, password, and permissions here. Add UTF-8 character set Settings to system, otherwise there will be question marks for stored Chinese

<property name=”charset”>utf8</property>

Change the user Settings. Here we have two users for PINYOUGOUDB

<user name=”test”><property name=”password”>test</property><property name=”schemas”>PINYOUGOUDB</property></user><user name=”root”><property name=”password”>123456</property><property name=”schemas”>PINYOUGOUDB</property></user>

1.3.4 MyCat fragment test

Enter mycat and execute the following statement to create a table:

CREATE TABLE tb_test (  id BIGINT(20) NOT NULL,  title VARCHAR(100) NOT NULL ,  PRIMARY KEY (id)) ENGINE=INNODB DEFAULT CHARSET=utf8

Once created, you’ll notice that MyCat will automatically convert your table to uppercase, similar to Oracle.

MySQL > create table (s); It’s amazing.

INSERT table (s); INSERT table (s);

Error code: 1064

partition table, insert must provide ColumnList

We try to insert some data:

INSERT INTO TB_TEST(ID,TITLE) VALUES(1,’goods1′); INSERT INTO TB_TEST(ID,TITLE) VALUES(2,’goods2′); INSERT INTO TB_TEST(ID,TITLE) VALUES(3,’goods3′);

We see that the data is written to the first node, but when is the data written to the second node?

We insert the following data to insert the second node

INSERT INTO TB_TEST(ID,TITLE) VALUES(5000001,’goods5000001′);

Since the sharding rule we use is to store 5 million data per node, when the ID is greater than 5000000, it will be stored on the second node.

Only two nodes are set up so what happens if the data is greater than 10 million? Execute the following statement to test it

INSERT INTO TB_TEST(ID,TITLE) VALUES(10000001,’goods10000001′);

1.3.5 MyCat Sharding rule

Rule-xml is used to define sharding rules, and we’ll cover the two most common sharding rules here

(1) Press primary key range-long

We find it in the configuration file

<tableRule name=”auto-sharding-long”><rule><columns>id</columns><algorithm>rang-long</algorithm></rule></tableRule>

TableRule is the name of a sharding rule that defines a table or a type of table. Columns Are the columns that define a sharding. The algorithm is the name of the algorithm

<function name=”rang-long”class=”org.opencloudb.route.function.AutoPartitionByLong”><property name=”mapFile”>autopartition-long.txt</property></function>

Function is used to define the algorithm mapFile is used to define the data required by the algorithm, we open autopartition-long.txt

# range start-end,data node index# K=1000,M= 10000.0-500m =0500M -1000m = 11000m-1500m =2

 

  1. It’s a hash tree

When we need to average data across several partitions, we need to use consistent hash rules

We found the definition of function whose name is Murmur and changed the count property to 3 because I want to split the data into three pieces

<function name=”murmur”class=”org.opencloudb.route.function.PartitionByMurmurHash”><property name=”seed”>0</property><! <property name=”count”>3</property><! <property name=”virtualBucketTimes”>160</property><! An actual database node is mapped to this number of virtual nodes, which is 160 times the number of virtual nodes than the number of physical nodes by default. — <property name=”weightMapFile”>weightMapFile</property> Specifies the weight of the node. Enter the value in the format of the properties file. The value is an integer from 0 to count-1, i.e. the node index is the key and the node weight is the value. The ownership weight must be a positive integer, otherwise 1 is substituted –><! — <property name=”bucketMapPath”> /etc/mycat/bucketmapPath </property> used to observe the distribution of physical and virtual nodes during testing. </function> Outputs the hash value of the virtual node. There is no default value.

Table rule definitions can be found in the configuration file

<tableRule name=”sharding-by-murmur”><rule><columns>id</columns><algorithm>murmur</algorithm></rule></tableRule>

If our table primary key is order_ID instead of ID, then we should redefine a tableRule:

<tableRule name=”sharding-by-murmur-order”><rule><columns>order_id</columns><algorithm>murmur</algorithm></rule></tableRule>

When configuring the logical table in schema. XML, specify the rule as sharding-by-mutation-order

<table name=”tb_order” dataNode=”dn1,dn2,dn3″ rule=”sharding-by-murmur-order” />

 

Let’s test it by creating a purchase order table and inserting data to test the effect of sharding.

 

1.4 Understanding Database Read/write Separation

Database read-write separation is an essential function for large systems or Internet applications with high traffic. For MySQL, the standard read/write separation is in master-slave mode, with a write node Master followed by multiple read nodes. The number of read nodes depends on the system pressure, usually 1-3 read nodes

 

The Mycat read-write separation and automatic switchover mechanism requires the master/slave replication mechanism of mysql.

For details, see the related extension documents.

 

2.Nginx installation and startup

2.1 What is Nginx

Nginx is a high-performance HTTP/reverse proxy server and E-mail (IMAP/POP3) proxy server. Developed by Russian programmer Igor Sysoev, Nginx was officially tested to support up to 50,000 concurrent links with very low CPU, memory and other resource consumption.

Nginx application scenarios:

1. HTTP server. Nginx is an HTTP service that can provide HTTP services independently. Can do web static server.

2. Virtual host. Can be realized in a server virtual out of multiple websites. For example, the virtual host used by personal websites.

3. Reverse proxy, load balancing. When the number of visits to the website reaches a certain level, a single server cannot meet the user’s request, you need to use multiple server cluster can use Nginx as a reverse proxy. In addition, multiple servers can evenly share the load, so that a server is not idle due to the high load of a server downtime.

2.2 Nginx installation in Linux

Prepare another VM as the server. For example, the IP address is 192.168.25.141

2.2.1 Environment Preparation

(1) The environment where GCC needs to be installed

yum install gcc-c++

(2) Third-party development kits. [This step omitted]

n PCRE

PCRE(Perl Compatible Regular Expressions) is a Perl library that includes the Perl-compatible Regular expression library. Nginx’s HTTP module uses PCRE to parse regular expressions, so you need to install the PCRE library on Linux.

yum install -y pcre pcre-devel

Note: Pcl-devel is a secondary development library developed using PCRE. Nginx also needs this library.

n  zlib

The Zlib library provides a variety of compression and decompression methods. Nginx uses Zlib to gzip the contents of HTTP packages, so you need to install zlib on Linux.

yum install -y zlib zlib-devel

n  OpenSSL

OpenSSL is a powerful Secure Socket layer cryptographic library that includes major cryptographic algorithms, common key and certificate encapsulation management capabilities, and SSL protocols, and provides rich applications for testing and other purposes. Nginx supports both HTTP and HTTPS (that is, HTTP over SSL), so you need to install the OpenSSL library on Linux.

yum install -y openssl openssl-devel

2.2.2 Nginx download

Official website: nginx:nginx.org/

The version we will use in this course is version 1.8.0.

2.2.3 Nginx installation

Step 1: Upload nginx source package nginx-1.8.0.tar.gz to Linux

Alt+p To start SFTP and upload nginx-1.8.0.tar.gz

Step 2: Decompress

The tar ZXVF nginx – 1.8.0 comes with. Tar. Gz

Step 3: Go to the nginx-1.8.0 directory and create a makeFile using the configure command.

./configure \–prefix=/usr/local/nginx \–pid-path=/var/run/nginx/nginx.pid \–lock-path=/var/lock/nginx.lock \–error-log-path=/var/log/nginx/error.log \–http-log-path=/var/log/nginx/access.log \–with-http_gzip_static_module \–http-client-body-temp-path=/var/temp/nginx/client \–http-proxy-temp-path=/var/temp/nginx/proxy \–http-fastcgi-temp-path=/var/temp/nginx/fastcgi \–http-uwsgi-temp-path=/var/temp/nginx/uwsgi \–http-scgi-temp-path=/var/temp/nginx/scgi

After execution, you can see the Makefile file

****—- —-**** A Makefile is a configuration file that does not count the source files in a project. The Makefile is stored in several directories by type, function, and module. A Makefile defines a set of rules to specify which files need to be compiled first. Which files need to be postcompiled, which files need to be recompiled, and even more complex functional operations, because a Makefile is like a Shell script that can also execute operating system commands.
—- Tips —-The configure parameters./configure \–prefix=/usr \ points to the installation directory–sbin-path=/usr/sbin/nginx \ Point to (execute) program file (nginx)–conf-path=/etc/nginx/nginx.conf \ Point to the configuration file–error-log-path=/var/log/nginx/error.log \ Points to log* * * * – HTTP – log – path = / var/log/nginx/access. HTTP – log log \ to * * * * * * * * – pid – path = / var/run/nginx/nginx. Pid \ Point to pid********–lock-path= /var/locke/nginx. lock \–user=nginx \–group=nginx \–with-http_ssl_module \ Enable ngx_http_SSL_module support (to support HTTPS requests, openSSL must be installed)–with-http_flv_module \ enable ngx_http_FLv_module support (provides seeking memory using time-based offset files)–with-http_stub_status_module \ Enable ngx_http_stub_status_module support (get the working status of nginx since last startup)— with-http_gzip_STATIC_module \ Enable ngx_http_gzip_STATIC_module support (live compression of output data streams)–http-client-body-temp-path=/var/tmp/nginx/client/ \ Set the HTTP client request temporary file path–http-proxy-temp-path=/var/tmp/nginx/proxy/ \ Set the HTTP proxy temporary file path ********–http-fastcgi-temp-path=/var/tmp/nginx/ fcgi/\ Set HTTP fastcgi temporary file path ********–http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ Set HTTP fastcgi temporary file path ********–http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ Uwsgi temporary file path * * * * * * * * – HTTP – scgi – temp – path = / var/TMP/nginx/scgi \ HTTP scgi temporary file path set * * * * * * * * – with – pcre enabled pcre library * * * *

Step 4: Compile

make

Step 5: Install

make install

2.3 Nginx startup and access

/var/temp/nginx/client /var/temp/nginx/client

mkdir /var/temp/nginx/client -p

Go to the sbin directory under the Nginx directory

cd /usr/local/ngiux/sbin

Enter the command to start Nginx

./nginx

View the process after startup

ps aux|grep nginx

 

To access the vm, enter the VM IP address in the address bar (port 80 by default).

Close the nginx:

./nginx -s stop

or

./nginx -s quit

Restart the nginx:

1, first shut down and then start.

2. Refresh configuration file:

./nginx -s reload

3.Nginx static website deployment

3.1 Deployment of static Websites

Upload the static page (d:\item) to /usr/local/nginx/html

3.2 Configuring virtual Hosts

Virtual hosting, also known as “web space”, is a physical server running on the Internet divided into multiple “virtual” servers. Virtual host technology has greatly promoted the application and popularization of network technology. At the same time the virtual host rental service has become a new economic form in the network era.

3.2.1 Port Bonding

  1. Upload a static website:

Upload the front-end static page cart. HTML and image styles to /usr/local/nginx/cart

Upload the front-end static page search. HTML and image styles to /usr/local/nginx/search

(2) modify the Nginx configuration file: / usr/local/Nginx/conf/Nginx. Conf

server { listen 81; server_name localhost; location / { root cart; index cart.html; } } server { listen 82; server_name localhost; location / { root search; index search.html; }}
  1. Access tests:

Enter http://192.168.25.141:81 in the address bar to see the shopping cart page

Enter http://192.168.25.141:82 in the address bar to see the search page

3.2.2 Domain name Binding

What is a domain name?

A Domain Name is the Name of a computer or computer group on the Internet separated by dots. It is used to identify the computer’s electronic location during data transmission. A domain name is an IP address with a “mask” on it. The purpose of a domain name is to facilitate memorization and communication of the addresses of a group of servers (web sites, email, FTP, etc.). Domain names as memorable as the names of Internet participants. Domain names follow the rules and procedures of the Domain name System (DNS). Any name registered in DNS is a domain name. Domain names are used for various network environments and application-specific naming and addressing purposes. Typically, a domain name represents an Internet protocol (IP) resource, such as a personal computer used to access the Internet, a server computer that hosts a web site, or the web site itself or any other service delivered over the Internet. The world’s first domain name was registered in January 1985.

Domain name level:

(1) Top-level domain name

TLDS fall into two categories:

First, national top-level domainnames (nTLDs) have been assigned to more than 200 countries according to the ISO3166 country code. For example, China is cn, the United States is us, and Japan is jp.

International top-level domain names (iTDs), for example, com.top for business,.net for network providers,.org for nonprofit organizations,.edu for education, And unrestricted neutral domain names such as.xyz. Most domain name disputes occur under the com top-level domain name, because most companies go online to make money. However, due to the development of new top-level domain names since 2014, the number of domain name dispute cases has increased more and more [5]. In order to strengthen domain name management and solve the shortage of domain name resources, Internet Society, Internet Addressing Organization, world Intellectual Property Organization (WIPO) and other international organizations, through extensive consultation, have added 7 international generic top-level domain names (COM) on the basis of the original three: Firm, Store, Web, Arts, REC, INFO, nom, And select new registrars worldwide to handle domain name registrations.

For example: baidu.com

(2) Secondary domain name

The second level domain name refers to the domain name under the top-level domain name. Under the international top-level domain name, it refers to the online name of the domain name registrant, such as IBM, Yahoo, Microsoft, etc. Under a country top-level domain, it is a symbol for a registered business category, such as. Top, com, edu, gov, net, etc.

The top-level domain name officially registered and operated by China in the International Internet Network Information Center (Inter NIC) is CN, which is also the first-level domain name in China. Under the top-level domain name, China’s secondary domain name is divided into category domain name and administrative domain name. There are 7 category domain names, including AC for scientific research institutions; Com and TOP for industrial and commercial financial enterprises; Edu for educational institutions; Gov for government departments; Net for Internet information center and operation center; Org for non-profit organizations. There are 34 administrative domain names, respectively corresponding to China’s provinces, autonomous regions and municipalities.

For example: map.baidu.com

(3) Three-level domain name

A three-level domain name consists of letters (A to Z, A to Z, uppercase and lowercase), digits (0 to 9), and hyphens (-). Real dots (.) are used between each level of domain names. Connect. The length of a level 3 domain name cannot exceed 20 characters. If there is no special reason, it is recommended to use the applicant’s English name (or abbreviation) or Chinese pinyin name (or abbreviation) as the third-level domain name to keep the domain name clear and concise.

Such as:

item.map.baidu.com

Domain name and IP binding:

A domain name corresponds to an IP address, and an IP address can be bound by multiple domain names.

Local tests can modify hosts files (C:\Windows\System32\drivers\etc)

You can configure the mapping between domain names and IP addresses. If the mapping between domain names and IP addresses is configured in the hosts file, you do not need to use the DNS server.

We can change the domain name pointing by using a software called SwitchHosts

New scheme:

Specify the binding rule IP domain name and then confirm.

 

After the domain name pointing, modify the nginx configuration file

server { listen 80; server_name cart.pinyougou.com; location / { root cart; index cart.html; } } server { listen 80; server_name search.pinyougou.com; location / { root search; index search.html; }}

Run the following command to refresh the configuration

[root@localhost sbin]# ./nginx -s reload

Testing:

The address bar enter http://cart.pinyougou.com/

The address bar enter http://search.pinyougou.com/

 

4.Nginx reverse proxy and load balancing

4.1 Reverse Proxy

4.1.1 What is a Reverse proxy

In Reverse Proxy mode, a Proxy server receives Internet connection requests, forwards the requests to the Intranet server, and returns the results to the Internet client. In this case, the proxy server acts as a reverse proxy server.

First of all, let’s understand the forward proxy, as shown below:

A forward proxy is for your client, and a reverse proxy is for the server, as shown below

 

4.1.2 Configuring a Reverse Proxy – Preparations

(1) Deploy the homepage to Tomcat (ROOT directory) and upload it to the server.

(2) start TOMCAT, enter http://192.168.25.141:8080 can see the homepage

4.1.3 Configuring a Reverse proxy

(1) Modify the Nginx configuration file in Nginx host

Upstream tomcat-portal {server 192.168.25.141:8080; } server { listen 80; server_namewww.pinyougou.com; location / { proxy_pass http://tomcat-portal; index index.html; }}
  1. Restart Nginx and test with your browser: www.pinyougou.com (this domain name must be configured to point to the domain name)

4.2 Load Balancing

4.2.1 What is Load Balancing

Based on the existing network structure, load balancing provides a cheap, effective and transparent method to expand the bandwidth of network devices and servers, increase the throughput, strengthen the network data processing capacity, and improve the flexibility and availability of the network.

Load Balance is distributed among multiple operation units, such as the Web server, FTP server, enterprise critical application server, and other critical task servers, to jointly complete work tasks.

4.2.2 Configuring Load Balancing – Preparations

  1. Make two copies of Tomcat, with ports 8180 and 8280 respectively.
  2. Start the Tomcat service of the two Tomcat servers respectively.
  3. In order to distinguish which server you are visiting, you can put a tag in the title of your home page.

4.2.3 Configuring load Balancing

Modify Nginx configuration file:

Upstream tomcat – portal {server 192.168.25.141:8080; Server 192.168.25.141:8180; Server 192.168.25.141:8280; } server { listen 80; server_namewww.pinyougou.com;         location/ { proxy_pass http://tomcat-portal; index index.html; }}

Address bar enter http://www.pinyougou.com/ each of the title of the page refresh observation, to see if different.

After testing, the probability of the occurrence of three servers is 33.3333333%, alternating display.

If one of the servers is performing well and you want to put more pressure on it, you can set the weight.

For example, if you want NO.1 to appear twice as often as other servers, modify the configuration as follows:

Upstream tomcat-portal {server 192.168.25.141:8080; Server 192.168.25.141:8180 weight = 2; Server 192.168.25.141:8280; }

After testing, every four times the refresh, there are two 8180

4.3 Understanding HIGH availability

4.3.1 What is High availability

Nginx as a load balancer, all requests to nginx, can be seen in a very important position, if the Nginx server down the backend Web services will not be able to provide services, serious impact.

To protect the load balancing server from downtime, create a backup machine. Both the primary server and backup machine run High Availability monitors that monitor each other’s health by sending messages such as’ I am alive. ‘ If the backup machine cannot receive such information within a certain period of time, it takes over the service IP address of the primary server and continues to provide load balancing services. When the backup manager receives another message like “I am alive” from the master manager, it releases the service IP address and the master server starts to provide load balancing services again.

4.3.2 keepalived profile

Keepalived is a service software used in cluster management to ensure high availability of clusters and prevent single points of failure.

The purpose of Keepalived is to detect the state of the Web server. If a Web server crashes, or fails to work, Keepalived will detect it and remove the faulty web server from the system. When the web server works properly, Keepalived automatically adds the Web server to the server farm. All the work is done automatically, without human intervention. All you need to do is to repair the faulty Web server.

Keepalived is based on Virtual Router Redundancy Protocol (VRRP), which stands for Virtual Router Redundancy Protocol.

Virtual route redundancy protocol can be regarded as a protocol to implement high availability of routers, that is, N routers provide the same function

A router group consists of a router group. This group contains a master and multiple backup routers. The master has a VIP (VIP = Virtual IPAddress, Virtual IP address, Virtual IP address) that provides external services. The default route of other machines in the LAN where the router resides is this VIP. The master sends multicast packets. If the backup fails to receive VRRP packets, the master is considered to be down. This ensures that the router is highly available.

Keepalived has three main modules, namely core, Check and VRRP. The core module is the core of Keepalived, which is responsible for the startup and maintenance of the main process as well as the loading and parsing of the global configuration file. Check is responsible for health checks, including common checks. The VRRP module implements the VRRP protocol.

Initial state:

 

Host downtime:

 

Host recovery:

 

See the extension documentation for installation and configuration of Keepalived

5. Optimized purchase deployment plan

5.1 Upbuy network topology

 

5.2 Excellent Purchase Server list

use Number of Servers Install the software
Reverse proxy server 3 Nginx  Keepalived
subtotal 3  
Web portal 5 tomcat
Search the web 3 tomcat
Product detail Page Web + Product detail page generation service 3 Nginx  +  tomcat
Shopping cart web 3 tomcat
Down the channel 3 tomcat
The user center 2 tomcat
Comment on center 2 tomcat
Single sign-on (sso) 1 Tomcat
Business background 1 Tomcat
Operator Management Background 1 Tomcat
subtotal 25  
SMS sending gateway 2 Micro service
Merchant goods and Services 5 tomcat
Advertising content service 3 tomcat
Shopping cart service 3 tomcat
Order service 3 tomcat
Payment service 3 tomcat
Seconds kill service 3 tomcat
Review service 3 tomcat
Search service 3 tomcat
Customer service 3 tomcat
subtotal 31  
Dubbox registry 3 Zookeeper
Solr entrance 3 Zookeeper
Redis 6  
Solr 4  
Message middleware 3 ActiveMQ
subtotal 19  
MySQL 6  
MyCAT 1  
subtotal 7  
Tracker 3  
Storage 5  
subtotal 8  
A total of 93