部署:nginx服务器:11-11;tomcat1:11-3; tomcat2:11-6
nginx服务器11-11做搭建:
[root@mcb-11 ~]# systemctl stop firewalld
[root@mcb-11 ~]# setenforce 0
[root@mcb-11 ~]# yum install epel-release.noarch -y
[root@mcb-11 ~]# yum install nginx -y
[root@mcb-11 ~]# systemctl start nginx
[root@mcb-11 ~]# systemctl status nginx
[root@mcb-11 ~]# vim /etc/nginx/nginx.conf
[root@mcb-11 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@mcb-11 ~]# nginx -s reload
upstream tomcat {server 192.168.11.3:8080;server 192.168.11.6:8080;}
location ~* \.jsp$ {proxy_pass http://tomcat;}
11-3 tomcat 搭建
[root@mcb-11-3 ~]# mkdir /data
[root@mcb-11-3 ~]# cd /data
[root@mcb-11-3 data]# rz -E
rz waiting to receive.
[root@mcb-11-3 data]# rz -E
rz waiting to receive.
[root@mcb-11-3 data]# ls
apache-tomcat-8.5.16.tar.gz jdk-8u291-linux-x64.tar.gz
[root@mcb-11-3 data]# tar xf jdk-8u291-linux-x64.tar.gz -C /usr/local 版本不一致也不能解压
[root@mcb-11-3 data]# cd /usr/local/
[root@mcb-11-3 local]# ln -s jdk1.8.0_291/ jdk
[root@mcb-11-3 local]# vim /etc/profile.d/jdk.sh
[root@mcb-11-3 local]# . /etc/profile.d/jdk.sh
[root@mcb-11-3 local]# java -version
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-b12)
OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)
#此时jdk安装完成
[root@mcb-11-3 local]# cd -
/data
[root@mcb-11-3 data]# tar xf apache-tomcat-8.5.16.tar.gz
[root@mcb-11-3 data]# cp -r apache-tomcat-8.5.16 /usr/local/tomcat
[root@mcb-11-3 data]# cd /usr/local/
[root@mcb-1-3 data]# useradd -s /sbin/nologin tomcat
[root@mcb-11-3 data]# cd /usr/local/
[root@mcb-11-3 local]# chown tomcat:tomcat tomcat/ -R
[root@mcb-11-3 system]# cat > /usr/lib/systemd/system/tomcat.service <<EOF
> [Unit]
> Description=Tomcat
> After=syslog.target network.target
>
> [Service]
> Type=forking
> ExecStart=/usr/local/tomcat/bin/startup.sh
> ExecStop=/usr/local/tomcat/bin/shutdown.sh
> RestartSec=3
> PrivateTmp=true
> User=tomcat
> Group=tomcat
>
> [Install]
> WantedBy=multi-user.target
>
> EOF
[root@mcb-11-3 system]# systemctl daemon-reload
[root@mcb-11-3 system]# systemctl start tomcat
[root@mcb-11-3 system]# systemctl status tomcat.service
● tomcat.service - TomcatLoaded: loaded (/usr/lib/systemd/system/tomcat.service; disabled; vendor preset: disabled)Active: active (running) since 日 2024-03-17 00:28:10 CST; 19s agoProcess: 7560 ExecStart=/usr/local/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)Main PID: 7575 (catalina.sh)
[root@mcb-11-3 data]# cd /usr/local/tomcat
[root@mcb-11-3 tomcat]# ls
apache-tomcat-9.0.16 conf LICENSE README.md temp
bin CONTRIBUTING.md logs RELEASE-NOTES webapps
BUILDING.txt lib NOTICE RUNNING.txt work
[root@mcb-11-3 tomcat]# cd webapps/
[root@mcb-11-3 webapps]# ls
docs examples host-manager manager ROOT
[root@mcb-11-3 webapps]# cd /usr/local/tomcat/webapps/ROOT
[root@mcb-11-3 ROOT]# ls
asf-logo-wide.svg bg-nav.png index.jsp tomcat.gif tomcat.svg
bg-button.png bg-upper.png RELEASE-NOTES.txt tomcat.png WEB-INF
bg-middle.png favicon.ico tomcat.css tomcat-power.gif
[root@mcb-11-3 ROOT]# mv index.jsp index.jsp.bak
[root@mcb-11-3 ROOT]# ls
asf-logo-wide.svg bg-nav.png index.jsp.bak tomcat.gif tomcat.svg
bg-button.png bg-upper.png RELEASE-NOTES.txt tomcat.png WEB-INF
bg-middle.png favicon.ico tomcat.css tomcat-power.gif
[root@mcb-11-3 ROOT]# rz -E
rz waiting to receive.
[root@mcb-11-3 ROOT]# cat 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@mcb-11-3 ROOT]#
11-6tomcat 搭建
[root@mcb-11-6 local]# cd /usr/local/tomcat
[root@mcb-11-6 tomcat]# ls
bin conf lib logs README.md RUNNING.txt webapps
BUILDING.txt CONTRIBUTING.md LICENSE NOTICE RELEASE-NOTES temp work
[root@mcb-11-6 tomcat]# cd webapps/
[root@mcb-11-6 webapps]# ls
docs examples host-manager manager ROOT
[root@mcb-11-6 webapps]# cd /usr/local/tomcat/webapps/ROOT
[root@mcb-11-6 ROOT]# ls
asf-logo-wide.svg bg-nav.png index.jsp tomcat.gif tomcat.svg
bg-button.png bg-upper.png RELEASE-NOTES.txt tomcat.png WEB-INF
bg-middle.png favicon.ico tomcat.css tomcat-power.gif
[root@mcb-11-6 ROOT]# mv index.jsp index.jsp.bak
[root@mcb-11-6 ROOT]# ls
asf-logo-wide.svg bg-nav.png index.jsp.bak tomcat.gif tomcat.svg
bg-button.png bg-upper.png RELEASE-NOTES.txt tomcat.png WEB-INF
bg-middle.png favicon.ico tomcat.css tomcat-power.gif
[root@mcb-11-6 ROOT]# rz -E
rz waiting to receive.
[root@mcb-11-6 ROOT]# cat 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@mcb-11-6 ROOT]#
检测
问题:去访问代理服务器会发现,每次调度到192.168.11.3和192.168.11.4上面,sessionID都会改变,假如我们要让他保持会话连接(sessionID不会变)
二 会话绑定
①ip_ hash
[root@mcb-11-3 local]# vim /etc/nginx/nginx.conf
检测:
缺点:把服务器也停止了,不调度了
②调度服务器不停止
Apache Tomcat 9 (9.0.87) - Clustering/Session Replication How-To
把官网文本拷贝下来,ip_hash 删除,影响实验
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"channelSendOptions="8"><Manager className="org.apache.catalina.ha.session.DeltaManager"expireSessionsOnShutdown="false"notifyListenersOnReplication="true"/><Channel className="org.apache.catalina.tribes.group.GroupChannel"><Membership className="org.apache.catalina.tribes.membership.McastService"address="228.0.0.4"port="45564"frequency="500"dropTime="3000"/><Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"address="auto"port="4000"autoBind="100"selectorTimeout="5000"maxThreads="6"/><Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter"><Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/></Sender><Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/><Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatchInterceptor"/></Channel><Valve className="org.apache.catalina.ha.tcp.ReplicationValve"filter=""/><Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/><Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"tempDir="/tmp/war-temp/"deployDir="/tmp/war-deploy/"watchDir="/tmp/war-listen/"watchEnabled="false"/><ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/></Cluster>
配置11-3tomcat
[root@mcb-11-3 tomcat]# vim conf/server.xml
子配置文件:
配置11-6tomcat:
[root@mcb-11-6 tomcat]# vim conf/server.xml
子配置文件
[root@mcb-11-6 tomcat]# cd webapps/ROOT/WEB-INF/
[root@mcb-11-6 WEB-INF]# vim web.xml
测试:
结果:不管调度11-6还是11-3,不管调度到那个tomcat服务器,sessionID都保持一致,不变化了 (既IP地址在变,但sessionID不变)