current position:Home>How to achieve load balancing gracefully by nginx( Recommended Collection), original Kafka learning notes

How to achieve load balancing gracefully by nginx( Recommended Collection), original Kafka learning notes

2021-08-26 12:32:27 Programmer LQS

Introducing Nginx Before the implementation of load balancing , Let's talk about the classification of load balancing , It is mainly divided into Hardware load balancing and software load balancing , Hardware load balancing is the use of specialized software and hardware combined equipment , Equipment chamber of commerce provides complete and mature solutions , such as F5, Very reliable in terms of data stability and security , But it's more expensive than software ; Software load balancing with Nginx This kind of software is mainly , The implementation of a message queue distribution mechanism .

In short, the so-called load balancing is to split many requests , Assign them to different servers to handle . For example, I have 3 Servers , Respectively A、B、C, And then use Nginx Load balancing , Use polling strategy , At this time, if received 9 A request , Then I'll even this 9 Requests are distributed to A、B、Cf The server , Every server handles 3 A request , In this way, we can take advantage of the characteristics of multiple machine clusters to reduce the pressure of a single server .

Nginx Example of load balancing :

 Insert picture description here

Load balancing strategy

NGINX Open source supports four load balancing methods , and NGINX Plus Two more ways .

1.Round Robin: Poll all requests to send requests , Default allocation method .

nginx.conf Configuration example :


upstream xuwujing {

   server www.panchengming.com;

   server www.panchengming2.com;

} 


     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

notes : The above domain name can also be used IP replace .

2.Least Connections: Send requests to the server with the minimum number of active connections , Also consider server weight .

nginx.conf Configuration example :


upstream xuwujing {

    least_conn;

    server www.panchengming.com;

    server www.panchengming2.com;

} 


     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

3.IP Hash : The server that sends the request is the client IP Address determination . under these circumstances , Use IPv4 The first three bytes or the whole of the address IPv6 Address to compute hash value . This method ensures that requests from the same address arrive at the same server , Unless the server is not available .


upstream xuwujing {

     ip_hash;

     server www.panchengming.com;

     server www.panchengming2.com;

} 


     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

4.Generic Hash: The server to which the request is sent is determined by the user-defined key , The key can be a text string 、 A variable or combination .


 upstream xuwujing {

     hash $request_uri consistent;

     server www.panchengming.com;

        server www.panchengming2.com;

 } 


     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

5.Least Time (NGINX Plus only) – For each request ,NGINX Plus Select the server with the lowest average latency and the lowest number of active connections , The lowest average delay is based on the inclusion of

least_time The following parameters of the command calculate :

  • header : Time to receive the first byte from the server .

  • last_byte: Time to receive a full response from the server .

  • last_byte inflight: Time to receive a full response from the server .

upstream xuwujing {least_time header;server www.panchengming.com;server www.panchengming2.com;}

6.Random: Each request will be delivered to a randomly selected server . If two parameters are specified , First ,NGINX Select two servers randomly according to the server weight , Then use the specified method to select one of .

  • least_conn : Minimum number of active connections

  • least_time=header (NGINX Plus): Minimum average time to receive response headers from server ($upstream_header_time).

  • least_time=last_byte (NGINX Plus) : The minimum average time to receive a full response from the server ($upstream_response_time).

  • upstream xuwujing {random two least_time=last_byte;server www.panchengming.com;server www.panchengming2.com;}

 Nginx+SpringBoot Load balancing


Environmental preparation

  • rely on JDK1.8 Version above ;

  • rely on Nginx Environmental Science ;

The project here uses one of my previous springboot project ,SpringBoot Project address of :  https://github.com/xuwujing/springBoot-study/tree/master/springboot-thymeleaf

First we download this project , Input :mvn clean package Package the project as jar file , And then application.properties And this jar Items in one folder , Then copy the folder ( This is a copy for clarity , It's OK to restart the port without copying and changing ), Modify the copy folder application.properties The port of , For example, change to 8086.

Nginx To configure

We find nginx Configuration file for nginx.conf, The configuration is in nginx/conf/nginx.conf Under the table of contents , Then let's change the configuration , Add the following configuration :


upstream pancm{

   server 127.0.0.1:8085;

   server 127.0.0.1:8086;

} 


     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • upstream pancm: Define a name , Just do it ;

  • server + ip: port or domain name ;

If you don't want to use it Round Robin Strategy , You can also change it to something else .

And then in server add to / Modify the following configuration :


 server {

        listen       80;

        server_name  127.0.0.1;





        location / {

            root   html;

            proxy_pass http://pancm;

            proxy_connect_timeout 3s;

            proxy_read_timeout 5s;

            proxy_send_timeout 3s; 

            index  index.html index.htm;

        }



        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    } 


     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.

Configuration instructions :

  • server: The name of the virtual host , One http You can configure multiple server;

  • listen:Nginx Default port ;

  • server_name:Nginx Address of service , You can use the domain name , Multiple separated by spaces .

  • proxy_pass: Proxy path , General configuration upstream The following name is used for load balancing , You can configure it directly ip To jump ;

nginx.conf Complete configuration :


events {

    worker_connections  1024;

}



error_log nginx-error.log info;

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;



      upstream pancm{

       server 127.0.0.1:8085;


#  Last 

 Interview is the most direct and effective way to change jobs and raise salary , Gold, silver and silver are coming soon , Everybody, do a good job in the interview and build the plane , Are you ready to work ?

 Master these knowledge points , Interview in the candidates can be a lot of eye-catching , crit 9999 spot . Opportunities are reserved for those who are prepared , Only enough preparation , To be able to stand out from the candidates .

 If you need this full version of ** Interview notes **, As long as you have more ** Support ** My article .

**—— Praise the article + Comment on , Pay attention to me ,[ Then click here to get it for free ](https://gitee.com/vip204888/java-p7)**

![](https://s2.51cto.com/images/20210823/1629669683876198.jpg)

![](https://s2.51cto.com/images/20210823/1629669684491878.jpg)
     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.

copyright notice
author[Programmer LQS],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2021/08/20210826123225451M.html

Random recommended