nginx实现后端tomcat的负载均衡调度

发布时间 2023-10-07 15:24:16作者: 小糊涂90


1.负载均衡主机和网络地址规划
10.0.0.152 proxy.magedu.org nginx
10.0.0.150 t1.magedu.org tomcat1
10.0.0.160 t2.magedu.org tomcat2
#只需在10.0.0.52的nginx主机上实现域名解析
[root@localhost ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.152 proxy.magedu.org proxy
10.0.0.150 t1.magedu.org t1
10.0.0.160 t2.magedu.org t2

2.负载均衡tomcat主机准备
修改tomcat的虚拟机主机为自定义的主机名,并设为默认的虚拟主机
t1虚拟主机配置conf/server.xml
[root@centos8 ~]#vim /usr/local/tomcat/conf/server.xml
<Engine name="Catalina" defaultHost="t1.magedu.org">
   <Host name="t1.magedu.org" appBase="/data/webapps" autoDeploy="true" >
   </Host>
</Engine>
[root@centos8 ~]#systemctl restart tomcat

t2虚拟主机配置conf/server.xml
[root@centos8 ~]#vim /usr/local/tomcat/conf/server.xml
<Engine name="Catalina" defaultHost="t2.magedu.org">
   <Host name="t2.magedu.org" appBase="/data/webapps" autoDeploy="true" >
   </Host>
</Engine>
[root@centos8 ~]#systemctl restart tomcat

3.准备负载均衡规划测试用的jsp文件
#在t1和 t2节点创建相同的文件/data/webapps/ROOT/index.jsp
[root@centos8 ~]#mkdir -pv /data/webapps/ROOT
mkdir: created directory '/data/webapps'
mkdir: created directory '/data/webapps/ROOT'
[root@centos8 ~]#vim /data/webapps/ROOT/index.jsp
[root@centos8 ~]#cat /data/webapps/ROOT/index.jsp
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>tomcat test</title>
</head>
<body>
<div>On <%=request.getServerName() %></div>
<div><%=request.getLocalAddr() + ":" + request.getLocalPort() %></div>
<div>SessionID = <span style="color:blue"><%=session.getId() %></span></div>
<%=new Date()%>
</body>
</html>
[root@centos8 ~]#chown -R tomcat.tomcat /data/webapps/

4.Nginx 实现后端 tomcat 的负载均衡
[root@localhost ~]# yum install -y nginx
[root@localhost ~]# vim /etc/nginx/nginx.conf
#在http模块中添加一下几行
upstream tomcat {
server t1.magedu.org:8080;
server t2.magedu.org:8080;
}
server {
server_name proxy.magedu.org;
location ~* \.(jsp|do)$ {
proxy_pass http://tomcat;
}
}

[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# curl http://proxy.magedu.org/index.jsp

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>tomcat test</title>
</head>
<body>
<div>On tomcat</div>
<div>10.0.0.150:8080</div>
<div>SessionID = <span style="color:blue">23B0399E91F79E1F934E1AA087BB26D0</span></div>
Fri Dec 31 21:55:40 CST 2021
</body>
</html>
[root@localhost ~]# curl http://proxy.magedu.org/index.jsp

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>tomcat test</title>
</head>
<body>
<div>On tomcat</div>
<div>10.0.0.160:8080</div>
<div>SessionID = <span style="color:blue">306D4E2B1415782E799AE991EFA3286C</span></div>
Fri Dec 31 21:55:45 CST 2021
</body>
</html>

#可以看到轮询调度效果,每次刷新后端主机和SessionID都会变化

5.实现 session 黏性
#在upstream中使用ip_hash指令,使用客户端IP地址Hash。
[root@localhost ~]# vim /etc/nginx/nginx.conf
#在http模块中添加一下几行
upstream tomcat {
ip_hash;
server t1.magedu.org:8080;
server t2.magedu.org:8080;
}
server {
server_name proxy.magedu.org;
location ~* \.(jsp|do)$ {
proxy_pass http://tomcat;
}
}
[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# curl http://proxy.magedu.org/index.jsp

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>tomcat test</title>
</head>
<body>
<div>On tomcat</div>
<div>10.0.0.160:8080</div>
<div>SessionID = <span style="color:blue">36DA811FDAEFC13070FE522BC206E68D</span></div>
Fri Dec 31 21:58:53 CST 2021
</body>
</html>

[root@localhost ~]# curl http://proxy.magedu.org/index.jsp

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>tomcat test</title>
</head>
<body>
<div>On tomcat</div>
<div>10.0.0.160:8080</div>
<div>SessionID = <span style="color:blue">39CC514BF59EBC8244BF2CD510EE2419</span></div>
Fri Dec 31 21:58:55 CST 2021
</body>
</html>
#用curl访问每次都调度到10.0.0.160主机上,但因为curl每次请求不会自动携带之前获取的cookie,所有SessionID每次都在变化
#通过图形浏览器可看到主机不变,sessionID不变