nginx + spring boot microservices to achieve load balancing

2020/11/2210:52:03 technology 830

environment preparation

project JDK1.8 or above version

ready nginx environment

nginx configuration

nginx configuration file is in the /usr/local/nginx/conf directory, configuration file nginx.conf

configuration information is as follows:

3zz web_server 8089 max_fails=2 fail_timeout=30s;

}

upstream web_app: define a name, choose any name

server + ip: port or domain name

add the following configuration to the virtual host

virtual host configuration

server {

listen 3;zzz0 localhost;zzz0 localhost; root html; Define the default root directory location of the server

index index.html index.htm;

Reverse proxy configuration

If the back-end server returns 502,504, execution timeout and other errors, the request will be automatically forwarded to one of the upstream load balancers Server

Realize failover

proxy_next_upstream http_502 http_504 error timeout invalid_header;

Pass the user's information received by the proxy server to the real server

Proxy_add_header Host Proxy_set_header_Forward_head_zero_proxy_3zz0-z_proxy_host;

z0 For $proxy_add_x_forwarded_for;

is the same as the forwarded name configured above

proxy_pass http://web_app;

}

Description:

server: the name of the virtual host, multiple servers can be configured in one HTTP;

listen: nginx default The port

server_name: the address of the nginx service, you can use the domain name, multiple separated by spaces

proxy_pass: proxy path, generally configure the name behind upstream to achieve load balancing, you can directly configure ip to jump

nginx.conf Simple complete configuration:

events {

worker_connections 1024;

}

error_log nginx-error.log info;

http {

include mime.types;

default_type application/octet-stream;

sendfile on;

stream = 808.zzzserver 192.168.150;

keepalive_app_timeout3 65;0zz server 192.168.150.

}

server {

listen 80;

server_name 127.0.0.1;

location / {

root html;

proxy_pass http://web_app;

proxy_connect_timeout 3s;

proxy_read_timeout 5s;

503 zzzproxy_send_timeout 3s; zzz0 503 zzzproxy_send_timeout 3s; zzz0 502 503 zzzproxy_50 html ;

location = /50x.html {

root html;

}

}

}

Load balancing test

After completing the above Nginx configuration, restart Nginx

[root@localhost ~] /usr/sbin/nginx- s reload

microservice start:

[root@pertest bin] sh server.sh restart

access interface, through nginx ip: port + interface suffix address

nginx + spring boot microservices to achieve load balancing - DayDayNews

through nginx log, you can also see that the access is successful

nginx + spring boot microservices to achieve load balancing - DayDayNews

when there are multiple nodes At the time, add the corresponding ip: port in the nginx configuration file.

author: Lemon class software testing (lemonban) - focus on the latest and most cutting-edge technology software testing, testing technology to solve your troubles, software testing interested friends quickly concern us!

technology Category Latest News