Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize load balancing with Nginx+SpringBoot

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

The knowledge of this article "how to achieve load balancing in Nginx+SpringBoot" is not quite understood by most people, so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can gain something after reading this article. Let's take a look at this "how to achieve load balancing in Nginx+SpringBoot" article.

Introduction to load balancing

Before introducing the implementation of load balancing in nginx, let's briefly talk about the classification of load balancing, which is mainly divided into hardware load balancing and software load balancing. hardware load balancing is a device that uses a combination of special software and hardware. the equipment will provide a complete and mature solution, such as f5, which is very reliable in terms of data stability and security, but it will be more expensive than software. Software load balancing is mainly based on software such as nginx, which is a message queue distribution mechanism.

To put it simply, the so-called load balancing is to divert a lot of requests and assign them to different servers to process. For example, I have three servers, a, b, c, and then use nginx for load balancing and use polling strategy. If nine requests are received, the nine requests will be evenly distributed to a, b, and cf servers, and each server will handle 3 requests. In this way, we can take advantage of the feature of multiple machine clusters to reduce the pressure on a single server.

Example diagram of load balancing implemented by nginx:

Load balancing strategy

Nginx open source supports four load balancing methods, while nginx plus adds two.

1.round robin:

Polling all requests to send requests, the default allocation method.

Example nginx.conf configuration:

Note: the above domain name can also be replaced with ip.

2.least connections:

To send a request to the server with the minimum number of active connections, also consider the server weight.

Example nginx.conf configuration:

3.ip hash:

The server that sends the request is determined by the client ip address. In this case, the hash value is calculated using the first three bytes of the ipv4 address or the entire ipv6 address. This method ensures that requests from the same address arrive at the same server unless the server is unavailable.

4.generic hash:

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

5.least time (nginx plus only)

For each request, nginx plus selects the server with the lowest average latency and the lowest number of active connections, where the lowest average delay is calculated based on the following parameters that contain the least_time directive:

Header: the time the first byte was received from the server.

Last_byte: the time to receive the full response from the server.

Last_byte inflight: the time to receive the full response from the server.

6.random:

Each request is passed to a randomly selected server. If you specify two parameters, first, nginx randomly selects two servers based on server weight, and then selects one of them using the specified method.

Least_conn: minimum number of active connections

Least_time=header (nginx plus): the minimum average time to receive a response header from the server ($upstream_header_time).

Least_time=last_byte (nginx plus): the shortest average time to receive a full response from the server ($upstream_response_time).

Load balancing based on nginx+springboot

Environmental preparation

Rely on versions above jdk1.8

Dependent on nginx environment

The project here is based on my previous springboot project, the project address of springboot: https://github.com/xuwujing/springboot-study/tree/master/springboot-thymeleaf

First, we download the project, type: mvn clean package to package the project into a jar file, then put application.properties and the jar project in a folder, and then copy that folder (here for clarity, copy it, change the port restart without actually copying), and modify the port of the replication folder application.properties, such as 8086.

Nginx configuration

We find the configuration file nginx.conf of nginx, and the configuration is in the nginx/conf/nginx.conf directory, and then we modify the configuration by adding the following configuration:

Upstream pancm: define a name, whatever you want

Server + ip: Port or domain name

If you don't want to use the round robin strategy, you can switch to something else.

Then add / modify the following configuration in server:

Configuration instructions:

Server: the name of the virtual host. Multiple server can be configured in a http

Default port for listen:nginx

The address of the server_name:nginx service, you can use a domain name, and multiple addresses are separated by spaces.

Proxy_pass: proxy path. Generally, the name after upstream is configured to achieve load balancing. You can directly configure ip to jump.

Complete configuration of nginx.conf:

Load balancing test

After completing the nginx configuration, we start nginx.

Linux enter / usr/local/nginx/sbin/nginx-c / usr/local/nginx/conf/nginx.conf. If you have started, you can use the / usr/local/nginx/sbin/nginx-s reload command to hot load the configuration file. Windows can directly click nginx.exe or cmd under the nginx directory to run start nginx to start. If started, you can still use nginx-s reload for hot loading.

After the nginx startup is complete, we start the springboot we just downloaded and then copy the project that changes the port, and type: java-jar springboot-jsp-thymeleaf.jar to start.

After the startup is successful, we can enter the ip of the service in the browser to access it.

Example diagram:

Note: here I am using the windows system for testing, and the actual linux is the same.

Then we do the operation and check the console log!

From the above sample diagram, we made four interface refresh requests, and finally evenly distributed them to the two services. From the above test results, we achieved load balancing.

Here I would like to talk about the precautions of using nginx. When learning and testing, there is generally no problem with using the default port of nginx to achieve load balancing, but when we use it in the project, especially when there is a login interface and the port is not 80, the login interface will not jump, and errors such as net::err_name_not_resolved will occur when debugging. The reason for this is that the default port of nginx is 80, so this is also the default jump, so when this happens, you need to add the configuration of proxy_set_header host $host:port under location, so that the ports of port and listen are the same.

The above is the content of this article on "how to achieve load balancing in Nginx+SpringBoot". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to learn more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report