Linux系统——Session ID(负载均衡如何保持会话)

目录

一、实验环境搭建

二、部署Nginx代理服务器配置

三、部署后端真是服务器Tomcat配置

四、配置Tomcat的Session ID会话保持

五、测试


此次实验是Tomcat后端服务器如何做Session ID会话保持

一、实验环境搭建

[root@localhost ~]#systemctl stop firewalld
[root@localhost ~]#setenforce 0
setenforce: SELinux is disabled
[root@localhost ~]#yum install epel-release.noarch -y
#安装epel额外源
[root@localhost ~]#yum install nginx -y
#yum安装Nginx   作为代理服务器
[root@localhost ~]#systemctl start nginx
[root@localhost ~]#systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy serverLoaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)Active: active (running) since 六 2024-03-16 10:21:23 CST; 5s agoProcess: 2055 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)Process: 2051 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)Process: 2049 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)Main PID: 2058 (nginx)CGroup: /system.slice/nginx.service├─2058 nginx: master process /usr/sbin/nginx├─2059 nginx: worker process└─2060 nginx: worker process3月 16 10:21:23 localhost.localdomain systemd[1]: Starting The nginx HTTP ...
3月 16 10:21:23 localhost.localdomain nginx[2051]: nginx: the configuratio...
3月 16 10:21:23 localhost.localdomain nginx[2051]: nginx: configuration fi...
3月 16 10:21:23 localhost.localdomain systemd[1]: Started The nginx HTTP a...
Hint: Some lines were ellipsized, use -l to show in full.
[root@node2 ~]#systemctl stop firewalld
[root@node2 ~]#setenforce 0
[root@node2 ~]#rz -E
rz waiting to receive.
[root@node2 ~]#rz -E
rz waiting to receive.
[root@node2 ~]#ls
anaconda-ks.cfg              Downloads                   Pictures
apache-tomcat-9.0.16.tar.gz  initial-setup-ks.cfg        Public
Desktop                      jdk-8u291-linux-x64.tar.gz  Templates
Documents                    Music                       Videos
[root@node2 ~]#tar xf jdk-8u291-linux-x64.tar.gz  -C /usr/local/
[root@node2 ~]#cd /usr/local/
[root@node2 local]#ls
bin  etc  games  include  jdk1.8.0_291  lib  lib64  libexec  sbin  share  src
[root@node2 local]#ln -s jdk1.8.0_291/  jdk
[root@node2 local]#vim /etc/profile.d/jdk.sh
[root@node2 local]#cat /etc/profile.d/jdk.sh 
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=$JAVA_HOME/lib/:$JRE_HOME/lib/
export PATH=$JAVA_HOME/bin:$PATH
export JRE_HOME=$JAVA_HOME/jre
[root@node2 local]#. /etc/profile.d/jdk.sh 
[root@node2 local]#java -version
java version "1.8.0_291"
Java(TM) SE Runtime Environment (build 1.8.0_291-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.291-b10, mixed mode)
[root@node3 ~]#systemctl stop firewalld
[root@node3 ~]#setenforce 0
[root@node3 ~]#rz -E
rz waiting to receive.
[root@node3 ~]#rz -E
rz waiting to receive.
[root@node3 ~]#ls
anaconda-ks.cfg              jdk-8u291-linux-x64.tar.gz  视频  下载
apache-tomcat-9.0.16.tar.gz  公共                        图片  音乐
initial-setup-ks.cfg         模板                        文档  桌面
[root@node3 ~]#tar xf jdk-8u291-linux-x64.tar.gz  -C /usr/local/
[root@node3 ~]#cd /usr/local/
[root@node3 local]#ls
bin  etc  games  include  jdk1.8.0_291  lib  lib64  libexec  sbin  share  src
[root@node3 local]#ln -s jdk1.8.0_291/  jdk
[root@node3 local]#vim /etc/profile.d/jdk.sh
[root@node3 local]#cat /etc/profile.d/jdk.sh 
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=$JAVA_HOME/lib/:$JRE_HOME/lib/
export PATH=$JAVA_HOME/bin:$PATH
export JRE_HOME=$JAVA_HOME/jre
[root@node3 local]#. /etc/profile.d/jdk.sh 
[root@node3 local]#java -version
java version "1.8.0_291"
Java(TM) SE Runtime Environment (build 1.8.0_291-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.291-b10, mixed mode)

二、部署Nginx代理服务器配置

[root@localhost ~]#vim /etc/nginx/nginx.conf
[root@localhost ~]#sed -n '23,26p' /etc/nginx/nginx.confupstream tomcat {server 192.168.241.22:8080;server 192.168.241.23:8080;}
[root@localhost ~]#sed -n '50,52p' /etc/nginx/nginx.conflocation ~* \.jsp$ {proxy_pass http://tomcat;}
[root@localhost ~]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost ~]#nginx -s reload

三、部署后端真是服务器Tomcat配置

[root@node2 local]#cd
[root@node2 ~]#ls
anaconda-ks.cfg              Downloads                   Pictures
apache-tomcat-9.0.16.tar.gz  initial-setup-ks.cfg        Public
Desktop                      jdk-8u291-linux-x64.tar.gz  Templates
Documents                    Music                       Videos
[root@node2 ~]#tar xf apache-tomcat-9.0.16.tar.gz -C /usr/local/
[root@node2 ~]#cd /usr/local/
[root@node2 local]#ls
apache-tomcat-9.0.16  etc    include  jdk1.8.0_291  lib64    sbin   src
bin                   games  jdk      lib           libexec  share
[root@node2 local]#ln -s apache-tomcat-9.0.16/ tomcat
[root@node2 local]#ls
apache-tomcat-9.0.16  etc    include  jdk1.8.0_291  lib64    sbin   src
bin                   games  jdk      lib           libexec  share  tomcat
[root@node2 local]#cd tomcat/
[root@node2 tomcat]#useradd -s /sbin/nologin tomcat
[root@node2 tomcat]#chown tomcat:tomcat tomcat/  -R
chown: cannot access ‘tomcat/’: No such file or directory
[root@node2 tomcat]#chown tomcat:tomcat -R
chown: missing operand after ‘tomcat:tomcat’
Try 'chown --help' for more information.
[root@node2 tomcat]#cd ..
[root@node2 local]#chown tomcat:tomcat tomcat/  -R
[root@node2 local]#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@node2 local]#cd tomcat/webapps/ROOT/
[root@node2 ROOT]#ls
asf-logo-wide.svg  bg-upper.png       tomcat.css        tomcat.svg
bg-button.png      favicon.ico        tomcat.gif        WEB-INF
bg-middle.png      index.jsp          tomcat.png
bg-nav.png         RELEASE-NOTES.txt  tomcat-power.gif
[root@node2 ROOT]#mv index.jsp index.jsp.bak
[root@node2 ROOT]#rz -E
rz waiting to receive.
[root@node2 ROOT]#ls
asf-logo-wide.svg  bg-upper.png   RELEASE-NOTES.txt  tomcat-power.gif
bg-button.png      favicon.ico    tomcat.css         tomcat.svg
bg-middle.png      index.jsp      tomcat.gif         WEB-INF
bg-nav.png         index.jsp.bak  tomcat.png
[root@node2 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@node2 ROOT]#systemctl start tomcat
[root@node3 local]#cd
[root@node3 ~]#ls
anaconda-ks.cfg              jdk-8u291-linux-x64.tar.gz  视频  下载
apache-tomcat-9.0.16.tar.gz  公共                        图片  音乐
initial-setup-ks.cfg         模板                        文档  桌面
[root@node3 ~]#tar xf apache-tomcat-9.0.16.tar.gz -C /usr/local/
[root@node3 ~]#cd /usr/local/
[root@node3 local]#ls
apache-tomcat-9.0.16  etc    include  jdk1.8.0_291  lib64    sbin   src
bin                   games  jdk      lib           libexec  share
[root@node3 local]#ln -s apache-tomcat-9.0.16/ tomcat
[root@node3 local]#ls
apache-tomcat-9.0.16  etc    include  jdk1.8.0_291  lib64    sbin   src
bin                   games  jdk      lib           libexec  share  tomcat
[root@node3 local]#cd tomcat/
[root@node3 tomcat]#useradd -s /sbin/nologin tomcat
[root@node3 tomcat]#chown tomcat:tomcat tomcat/  -R
chown: 无法访问"tomcat/": 没有那个文件或目录
[root@node3 tomcat]#chown tomcat:tomcat -R
chown: "tomcat:tomcat" 后缺少操作数
Try 'chown --help' for more information.
[root@node3 tomcat]#cd ..
[root@node3 local]#chown tomcat:tomcat tomcat/  -R
[root@node3 local]#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@node3 local]#cd tomcat/webapps/ROOT/
[root@node3 ROOT]#ls
asf-logo-wide.svg  bg-upper.png       tomcat.css        tomcat.svg
bg-button.png      favicon.ico        tomcat.gif        WEB-INF
bg-middle.png      index.jsp          tomcat.png
bg-nav.png         RELEASE-NOTES.txt  tomcat-power.gif
[root@node3 ROOT]#mv index.jsp index.jsp.bak
[root@node3 ROOT]#rz -E
rz waiting to receive.
[root@node3 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@node3 ROOT]#systemctl start tomcat

实际生产环境不允许Session ID不断变动的。

  • 客户第一次访问被调度到后端真实服务器Tomcat1上,由于是第一次访问,客户度的Cookie缓存会记录,不会有SessionID,Tomcat1服务器会生成新的Session ID传输给客户端,客户端会将这个新的Session ID记录下来;
  • 当第二次访问的时候,Nginx代理服务器又可能将服务请求调度到后端真实服务器Tomcat2上,Tomcat2服务器是没有客户端传输来的Session ID1的,Tomcat2服务器认为客户端要生成新的SessionID,于是Tomcat2服务器将新的Session ID2传输给客户端,客户端会更新Session ID,将刚刚的SessionID更改为SessionID2;
  • 当第三次访问的时候,客户端带着SessionID2去Nginx代理服务器,Nginx代理服务器可能将客户端又调度到Tomcat1服务器上,Tomcat1服务器并没有SessionID2,于是又将新生成的SessionID3传输给客户端,这样会有很不好的客户端体验,所以我们可以根据修改Tomcat配置文件来进行会话保持 

四、配置Tomcat的Session ID会话保持

Tomcat会话保持官方说明https://tomcat.apache.org/tomcat-9.0-doc/cluster-howto.html

[root@node2 ROOT]#vim /usr/local/tomcat/conf/server.xml 
[root@node2 ROOT]#sed -n '163,201p' /usr/local/tomcat/conf/server.xml 
<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="192.168.241.22"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>
[root@node3 ROOT]#vim /usr/local/tomcat/conf/server.xml 
[root@node3 ROOT]#sed -n '163,201p' /usr/local/tomcat/conf/server.xml 
<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="192.168.241.23"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>
[root@node2 ROOT]#vim /usr/local/tomcat/webapps/ROOT/WEB-INF/web.xml 
[root@node2 ROOT]#sed -n 29p /usr/local/tomcat/webapps/ROOT/WEB-INF/web.xml <distributable/>
[root@node2 ROOT]#systemctl restart tomcat
[root@node2 ROOT]#systemctl status tomcat.service 
● tomcat.service - TomcatLoaded: loaded (/usr/lib/systemd/system/tomcat.service; disabled; vendor preset: disabled)Active: active (running) since Fri 2024-03-15 22:42:31 EDT; 5s agoProcess: 2566 ExecStop=/usr/local/tomcat/bin/shutdown.sh (code=exited, status=0/SUCCESS)Process: 2601 ExecStart=/usr/local/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)Main PID: 2617 (catalina.sh)CGroup: /system.slice/tomcat.service├─2617 /bin/sh /usr/local/tomcat/bin/catalina.sh start└─2618 /usr/bin/java -Djava.util.logging.config.file=/usr/local/...Mar 15 22:42:31 node2.localdomain systemd[1]: Starting Tomcat...
Mar 15 22:42:31 node2.localdomain systemd[1]: Started Tomcat.
[root@node3 ROOT]#vim /usr/local/tomcat/webapps/ROOT/WEB-INF/web.xml 
[root@node3 ROOT]#sed -n 29p /usr/local/tomcat/webapps/ROOT/WEB-INF/web.xml <distributable/>
[root@node3 ROOT]#systemctl restart tomcat
[root@node3 ROOT]#systemctl status tomcat.service 
● tomcat.service - TomcatLoaded: loaded (/usr/lib/systemd/system/tomcat.service; disabled; vendor preset: disabled)Active: active (running) since 六 2024-03-16 18:42:30 CST; 5s agoProcess: 2480 ExecStop=/usr/local/tomcat/bin/shutdown.sh (code=exited, status=0/SUCCESS)Process: 2518 ExecStart=/usr/local/tomcat/bin/startup.sh (code=exited, status=0/SUCCESS)Main PID: 2533 (catalina.sh)CGroup: /system.slice/tomcat.service├─2533 /bin/sh /usr/local/tomcat/bin/catalina.sh start└─2534 /usr/bin/java -Djava.util.logging.config.file=/usr/local/...3月 16 18:42:30 node3.node3 systemd[1]: Starting Tomcat...
3月 16 18:42:30 node3.node3 startup.sh[2518]: Using CATALINA_BASE:   /usr...t
3月 16 18:42:30 node3.node3 startup.sh[2518]: Using CATALINA_HOME:   /usr...t
3月 16 18:42:30 node3.node3 startup.sh[2518]: Using CATALINA_TMPDIR: /usr...p
3月 16 18:42:30 node3.node3 startup.sh[2518]: Using JRE_HOME:        /usr
3月 16 18:42:30 node3.node3 startup.sh[2518]: Using CLASSPATH:       /usr...r
3月 16 18:42:30 node3.node3 systemd[1]: Started Tomcat.
Hint: Some lines were ellipsized, use -l to show in full.

五、测试

此时后端真实服务器Tomcat调优完成,SessionID会话保持完成,客户端体验绝佳!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/544339.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Ubuntu Desktop - gnome-calculator (计算器)

Ubuntu Desktop - gnome-calculator [计算器] 1. Ubuntu Software -> gnome-calculator -> Install -> Continue2. Search your computer -> Calculator -> Lock to LauncherReferences 1. Ubuntu Software -> gnome-calculator -> Install -> Continu…

2024年3月退伍大学生士兵报名参加2024年天津专升本的通知

3月20日开始&#xff0c;2024年3月退伍的大学生士兵可报名参加2024年天津市高职升本科招生考试 为落实有关退役大学生士兵免试专升本工作的文件精神&#xff0c;按照《市高招办关于印发2024年天津市高职升本科招生实施办法的通知》&#xff08;津招办高发〔2023〕14号&#xf…

D 咖智能饮品机入驻万达,引领时尚饮品新潮流!

近日&#xff0c;D 咖智能饮品机正式入驻万达广场&#xff0c;为广大消费者带来全新的时尚饮品体验。作为国内领先的智能饮品设备品牌&#xff0c;D 咖智能饮品机以其多样化的口味选择、便捷的操作方式和个性化的定制服务&#xff0c;受到了众多消费者的喜爱。 D 咖智能饮品机提…

鸿蒙Harmony应用开发—ArkTS声明式开发(容器组件:RowSplit)

将子组件横向布局&#xff0c;并在每个子组件之间插入一根纵向的分割线。 说明&#xff1a; 该组件从API Version 7开始支持。后续版本如有新增内容&#xff0c;则采用上角标单独标记该内容的起始版本。 子组件 可以包含子组件。 RowSplit通过分割线限制子组件的宽度。初始化…

C语言 1000内完数、素数判断

一、一个数如果恰好等于它的因子之和&#xff0c;这个数就称为“完数”。例如&#xff0c;6旳因子为1&#xff0c;2&#xff0c;3&#xff0c;而6123&#xff0c;因此6是“完数”。编程序找出1000以内的所有“完数”&#xff0c;并按照下面格式输出其因子&#xff1a;6 its fac…

Centos7安装Clickhouse单节点部署

&#x1f388; 作者&#xff1a;互联网-小啊宇 &#x1f388; 简介&#xff1a; CSDN 运维领域创作者、阿里云专家博主。目前从事 Kubernetes运维相关工作&#xff0c;擅长Linux系统运维、开源监控软件维护、Kubernetes容器技术、CI/CD持续集成、自动化运维、开源软件部署维护…

Stargo 管理部署 Starrocks 集群

配置主机间 ssh 互信 ssh-copy-id hadoop02 ssh-copy-id hadoop03配置系统参数 ############################ Swap检查 ############################ echo 0 | sudo tee /proc/sys/vm/swappiness########################### 内核参数检查 ########################## echo…

Swift 面试题及答案整理,最新面试题

Swift 中如何实现单例模式&#xff1f; 在Swift中&#xff0c;单例模式的实现通常采用静态属性和私有初始化方法来确保一个类仅有一个实例。具体做法是&#xff1a;定义一个静态属性来存储这个单例实例&#xff0c;然后将类的初始化方法设为私有&#xff0c;以阻止外部通过构造…

鸿蒙Harmony应用开发—ArkTS声明式开发(容器组件:TabContent)

仅在Tabs中使用&#xff0c;对应一个切换页签的内容视图。 说明&#xff1a; 该组件从API Version 7开始支持。后续版本如有新增内容&#xff0c;则采用上角标单独标记该内容的起始版本。 子组件 支持单个子组件。 说明&#xff1a; 可内置系统组件和自定义组件&#xff0c;支…

Java基础 - 9 - 集合进阶(二)

一. Collection的其他相关知识 1.1 可变参数 可变参数就是一种特殊形参&#xff0c;定义在方法、构造器的形参列表里&#xff0c;格式是&#xff1a;数据类型…参数名称; 可变参数的特点和好处 特点&#xff1a;可以不传数据给它&#xff1b;可以传一个或者同时传多个数据给…

Spring Web MVC入门(2)

学习Spring MVC Postman介绍 在软件工程中, 我们需要具有前后端分离的思想, 以降低耦合性. 但是在测试后端代码时,我们还得写前端代码测试,这是个令人头疼的问题. 那么我们如何测试自己的后端程序呢, 这就用到了一个工具: Postman. 界面介绍: 传参的介绍 1.普通传参, 也就…

MongoDB——linux中yum命令安装及配置

一、创建mongodb-org-3.4.repo文件 vi /etc/yum.repos.d/mongodb-org-3.4.repo 将下面内容添加到创建的文件中 [mongodb-org-3.4] nameMongoDB Repository baseurlhttps://repo.mongodb.org/yum/amazon/2013.03/mongodb-org/3.4/x86_64/ gpgcheck1 enabled1 gpgkeyhttps://www…