-
目标
无标题.png
1.准备
1.1springboot 项目-node01
- pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.itbin</groupId>
<artifactId>nginx</artifactId>
<version>1.0-SNAPSHOT</version>
<!--引入父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>
<dependencies>
<!--springboot的web启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!--加上这个插件,不然打jar包放到linux服务器上启动时会报no main manifest attribute, in nginx-1.0-SNAPSHOT.jar-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- application.yaml
server:
port: 9092
-
项目结构
springboot9092.jpg - 部署项目:将项目打成jar包放在服务器上启动,ip为:192.168.52.22
root@es-node02 jar]# java -jar nginx-1.0-SNAPSHOT.jar
此时浏览器输入:http://192.168.52.22:9092/ 即可访问项目
1.2springboot项目-node02
- 将步骤1.1做如下改变
- application.yaml
server:
port: 9093
- index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>我是9093</h1>
</body>
</html>
- 部署项目:将项目打成jar包放在服务器上启动,ip为:192.168.52.23
root@es-node03 jar]# java -jar nginx-1.0-SNAPSHOT.jar
此时浏览器输入:http://192.168.52.23:9093/ 即可访问项目
2.配置nginx负载均衡
[root@es-node02 conf]# pwd
/opt/nginx/conf
[root@es-node02 conf]# ls -rlt
total 72
-rw-r--r-- 1 root root 2223 Nov 7 14:27 koi-win
-rw-r--r-- 1 root root 2837 Nov 7 14:27 koi-utf
-rw-r--r-- 1 root root 3610 Nov 7 14:27 win-utf
-rw-r--r-- 1 root root 5231 Nov 7 14:27 mime.types
-rw-r--r-- 1 root root 5231 Nov 7 14:27 mime.types.default
-rw-r--r-- 1 root root 1007 Nov 7 14:27 fastcgi_params.default
-rw-r--r-- 1 root root 1007 Nov 7 14:27 fastcgi_params
-rw-r--r-- 1 root root 1077 Nov 7 14:27 fastcgi.conf
-rw-r--r-- 1 root root 1077 Nov 7 14:27 fastcgi.conf.default
-rw-r--r-- 1 root root 664 Nov 7 14:27 uwsgi_params.default
-rw-r--r-- 1 root root 664 Nov 7 14:27 uwsgi_params
-rw-r--r-- 1 root root 636 Nov 7 14:27 scgi_params.default
-rw-r--r-- 1 root root 636 Nov 7 14:27 scgi_params
-rw-r--r-- 1 root root 2656 Nov 7 14:27 nginx.conf.default
-rw-r--r-- 1 root root 2687 Nov 7 15:52 nginx.conf
drwxr-xr-x 2 root root 4096 Nov 7 16:38 vhost
[root@es-node02 conf]# cd vhost/
[root@es-node02 vhost]# ls -rlt
total 4
-rw-r--r-- 1 root root 492 Nov 7 16:38 balance.conf
2.1修改es-node02配置文件
- nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#include:读取当前nginx.conf同级目录下的vhost目录下的以.conf结尾的文件
include vhost/*.conf;
sendfile on;
keepalive_timeout 65;
}
}
- balance.conf
upstream web {
server 192.168.52.22:9092;
server 192.168.52.23:9093;
}
server {
listen 8082;
server_name es-node02;
location / {
proxy_pass http://web;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
此配置为访问es-node02的8082端口时,请求会轮询分发到192.168.52.22:9092,192.168.52.23:9093,但是在使用谷歌浏览器,火狐浏览器,edge浏览器负载均衡配置均无效,请求的一直是同一个地址,但在服务器上curl http://localhost:8082/则实现了负载均衡,还未找到什么原因;
此时修改springboot项目
添加一个Controller,注意修改相应的返回值,即在服务器192.168.52.22上的jar包返回我是9092
2020-11-07_173652.jpg
重启jar包,此时访问http://192.168.52.22:8082/demo/test,刷新链接谷歌浏览器仍然无效,使用火狐和edge均实现了负载均衡
2.2修改es-node03配置文件
- nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#include:读取当前nginx.conf同级目录下的vhost目录下的以.conf结尾的文件
include vhost/*.conf;
sendfile on;
keepalive_timeout 65;
}
}
- balance.conf
upstream web {
server 192.168.52.22:9092;
server 192.168.52.23:9093;
}
server {
listen 8083;
server_name es-node03;
location / {
proxy_pass http://web;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
重新加载nginx配置文件
[root@es-node03 nginx]# ./sbin/nginx -s reload
此时访问http://192.168.52.23:8083/demo/test也可以实现负载均衡
3.实现前端负载均衡
- 对于前后端分离部署的项目上述配置可实现后端的负载均衡,如果要实现前端的负载均衡则需要再配置一个nginx,即在es-node01上监听8081,使请求负载到es-node02:8082和es-node03:8083上
- es-node01 balance.conf
upstream web{
server 192.168.52.22:8082;
server 192.168.52.23:8083;
}
server {
listen 8081;
server_name es-node01;
location / {
proxy_pass http://web/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
此时访问http:192.168.52.21:8081即可实现前端的负载均衡
4.测试es-node02的nginx挂掉
此时停掉es-node02的nginx
[root@es-node02 nginx]# ./sbin/nginx -s stop
发现http://192.168.52.22:8082/demo/test无法访问
http://192.168.52.21:8081/demo/test仍可以访问
5.测试es-node02的springboot项目挂掉
此时停掉es-node02的springboot项目
那么此时访问http://192.168.52.21:8081/demo/test则返回的一直是我是9093
网友评论