实验配置:
7-1安装Nginx
7-2和7-3安装Tomcat
1.配置7-1
1.做负载均衡,反向代理
[root@localhost ~]# vim /etc/nginx/nginx.conf
17 http {18 upstream tomcat {19 server 192.168.91.102:8080;20 server 192.168.91.103:8080;21 }
50 location ~* \.jsp$ {51 proxy_pass http://tomcat;52 }
[root@localhost ~]# nginx -t
[root@localhost ~]# nginx -s reload
2.配置7-2和7-3
1.改一下主页面
[root@localhost local]# cd tomcat
[root@localhost tomcat]# ls
bin conf lib logs README.md RUNNING.txt webapps
BUILDING.txt CONTRIBUTING.md LICENSE NOTICE RELEASE-NOTES temp work
[root@localhost tomcat]# cd webapps/ROOT
[root@localhost 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@localhost ROOT]# mv index.jsp index.jsp.bak
[root@localhost ROOT]# ls
asf-logo-wide.svg bg-nav.png index.jsp.bak
2.配置安装环境;替换主页面文件
这个脚本会产生sessionid
###每次调度7-2和7-3时,sessionID都会改变
###我们要让他保持会话连接(sessionID不会变)
3.原理
1.当客户端去访问代理服务器;代理服务器调度给Tomcat1;如果是第一次访问:客户端cookie里面没有sessionID,Tomcat1服务器就给给客户端生成一个sessionID并且发给客户端;客户端会将sessionID存在cookie中;当客户端第二次去访问Tomcat1时,会带着Tomcat1的sessionID,第二次,Tomcat1就不会给客户端生成新的sessionID
2.由于代理服务器调度的原因;不会每次都调度给Tomcat1;当调度给Tomcat2时;cookie带的是sessionID1;Tomcat2上要用sessionID2,他就会生成一个新的ID给客户端
3.所以,来回调度的时候,每次都是新的sessionID
4.会话绑定方法一:IP hash
1.去配置7-1
2.网页段查看结果
3.缺点:
服务器不会调度了
5.会话绑定方法二
https://tomcat.apache.org/tomcat-9.0-doc/cluster-howto.html
把官网中,蓝色页面内容全部复制到主配置文件中
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"channelSendOptions="6"><Manager className="org.apache.catalina.ha.session.BackupManager"expireSessionsOnShutdown="false"notifyListenersOnReplication="true"mapSendOptions="6"/><!--<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="5000"selectorTimeout="100"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"/><Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/></Channel><Valve className="org.apache.catalina.ha.tcp.ReplicationValve"filter=".*\.gif|.*\.js|.*\.jpeg|.*\.jpg|.*\.png|.*\.htm|.*\.html|.*\.css|.*\.txt"/><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>
1.先把ip hash删掉
2.配置7-2和7-3的主配置文件
[root@localhost tomcat]# vim conf/server.xml
2.修改7-2和7-3子配置文件
[root@localhost tomcat]# cd webapps/ROOT
[root@localhost ROOT]# ls
asf-logo-wide.svg bg-nav.png index.jsp tomcat.css tomcat-power.gif
bg-button.png bg-upper.png index.jsp.bak tomcat.gif tomcat.svg
bg-middle.png favicon.ico RELEASE-NOTES.txt tomcat.png WEB-INF
[root@localhost ROOT]# cd WEB-INF
[root@localhost WEB-INF]# LS
bash: LS: 未找到命令...
相似命令是: 'ls'
[root@localhost WEB-INF]# ls
web.xml
[root@localhost WEB-INF]# vim web.xml
3.访问浏览器看结果
结果:不管调度7-2还是7-3;sessionID都保持一致