【Session】Tomcat Session 集群

news2025/3/13 7:03:52

设备

nginx:192.168.67.11
tomcat1:192.168.67.12
tomcat2:192.168.67.13

安装nginx

(192.168.67.11)

#关闭防火墙和安全机制
[root@test1 ~]# systemctl stop firewalld
[root@test1 ~]# setenforce 0

#安装epel源
[root@test1 ~]# yum -y install epel-release.noarch

#安装nginx
[root@test1 ~]# yum -y install nginx

#开启nginx服务
[root@test1 ~]# systemctl start nginx

#修改配置文件;在22行下写upstream反向代理
[root@test1 ~]# vim /etc/nginx/nginx.conf
 22     access_log  /var/log/nginx/access.log  main;
 23         upstream tomcat {
 24         server   192.168.67.12:8080;
 25         server   192.168.67.13:8080;
 26         }
    #在49行下写
 48         # Load configuration files for the default server block.
 49         include /etc/nginx/default.d/*.conf;
 50         location ~* \.jsp$ {
 51         proxy_pass   http://tomcat;
 52         }

#检查配置文件
[root@test1 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
#重新加载配置文件
[root@test1 ~]# nginx -s reload

安装tomcat

(192.168.67.12、192.168.67.13)两者操作相同,下面只展示了tomcat1

#关闭防火墙和安全机制
[root@test2 ~]# systemctl stop firewalld
[root@test2 ~]# setenforce 0

#创建一个data目录
[root@test2 ~]# mkdir data
[root@test2 ~]# cd data/
[root@test2 data]# ls

#上传jdk和tomcat包
[root@test2 data]# rz -E
rz waiting to receive.
[root@test2 data]# rz -E
rz waiting to receive.
[root@test2 data]# ls
apache-tomcat-9.0.16.tar.gz  jdk-8u201-linux-x64.tar.gz

#解压安装jdk
[root@test2 data]# tar xvf jdk-8u201-linux-x64.tar.gz -C /usr/local

[root@test2 data]# cd /usr/local/
[root@test2 local]# ln -s jdk1.8.0_201/ jdk
[root@test2 local]# vim /etc/profile.d/jdk.sh
export JAVA_HOME=/usr/local/jdk
export PATH=$JAVA_HOME/bin:$PATH
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=$JAVA_HOME/lib/:$JRE_HOME/lib/

[root@test2 local]# . /etc/profile.d/jdk.sh 

[root@test2 local]# java -version
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)
解压安装tomcat
[root@test2 local]# cd 
[root@test2 ~]# cd data
#解压
[root@test2 data]# tar zxvf apache-tomcat-9.0.16.tar.gz
[root@test2 data]# ls
apache-tomcat-9.0.16         jdk-8u201-linux-x64.tar.gz
apache-tomcat-9.0.16.tar.gz
[root@test2 data]# cp -r apache-tomcat-9.0.16 /usr/local/tomcat
#创建tomcat用户
[root@test2 data]# useradd -s /sbin/nologin tomcat
[root@test2 data]# cd /usr/local/
[root@test2 local]# ls
bin  games    jdk           lib    libexec  share  tomcat
etc  include  jdk1.8.0_201  lib64  sbin     src
[root@test2 local]# chown tomcat:tomcat tomcat/ -R
[root@test2 local]# cat > /usr/lib/systemd/system/tomcat.service <<EOF
> [Unit]
> Description=Tomcat
> After=syslog.target netwaork.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=muti-user.target
> 
> EOF

#刷新配置文件;启动tomcat
[root@test2 local]# systemctl daemon-reload 
[root@test2 local]# systemctl start tomcat.service 
[root@test2 local]# systemctl status tomcat.service 

[root@test2 local]# ls
bin  games    jdk           lib    libexec  share  tomcat
etc  include  jdk1.8.0_201  lib64  sbin     src
[root@test2 webapps]# cd tomcat/webapps/ROOT/
[root@test2 ROOT]# ls
asf-logo-wide.svg  favicon.ico        tomcat.png
bg-button.png      index.jsp          tomcat-power.gif
bg-middle.png      RELEASE-NOTES.txt  tomcat.svg
bg-nav.png         tomcat.css         WEB-INF
bg-upper.png       tomcat.gif

[root@test2 ROOT]# mv index.jsp index.jsp.bak
[root@test2 ROOT]# rz -E
rz waiting to receive.
[root@test2 ROOT]# ls
asf-logo-wide.svg  favicon.ico        tomcat.gif
bg-button.png      index.jsp          tomcat.png
bg-middle.png      index.jsp.bak      tomcat-power.gif
bg-nav.png         RELEASE-NOTES.txt  tomcat.svg
bg-upper.png       tomcat.css         WEB-INF

[root@test2 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>

浏览器访问:

刷新浏览器可以看到sid一直在变化

sid变化的原因:

固定同一客户机的SID
[root@test1 ~]# vim /etc/nginx/nginx.conf
#添加第24行
 22     access_log  /var/log/nginx/access.log  main;
 23         upstream tomcat {
 24         hash     $remote_addr;
 25         server   192.168.67.12:8080;
 26         server   192.168.67.13:8080;
 27         }

#检查配置文件
[root@test1 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
#重新加载配置文件
[root@test1 ~]# nginx -s reload

可以看到SID不再改变,但同一客户机的请求也不会再被轮循

搭配共享服务

官网网址:Apache Tomcat 9 (9.0.87) - Clustering/Session Replication How-To

注意先删除上面固定SID的代码

[root@test2 ROOT]# cd /usr/local/tomcat
[root@test2 tomcat]# vim conf/server.xml
#在163行,</Host>上面  添加
        <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"    #这里最好将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>

#若要添加上面的内容,之后一定要添加下面的内容
[root@test2 ROOT]# pwd
/usr/local/tomcat/webapps/ROOT
[root@test2 ROOT]# cd WEB-INF/
[root@test2 WEB-INF]# ls
web.xml
[root@test2 WEB-INF]# vim web.xml 
#在29行  </description>下面 添加
 29 <distributable/>
浏览器访问:192.168.67.11/index.jsp

刷新可以看到SID不会改变,轮询正常

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

还在用ifelse来写业务?了解下Spring状态机

状态机之所以强大&#xff0c;是因为其行为在启动时就以固定的方式定义了操作规则&#xff0c;从而确保了一贯的连贯性和相对较高的可调试性。关键在于&#xff0c;应用程序处于且仅可能处于有限数量的状态中。然后&#xff0c;某些事件发生会使得应用从一个状态过渡到另一个状…

RuoYi-Vue使用RestTemplate无法通过@Autowired注入报错

A component required a bean of type org.springframework.web.client.RestTemplate that could not be found. 解决方法&#xff1a; 将ruoyi-framework模块下找到ApplicationConfig这个配置类使用Bean注入&#xff1a; /*** RestTemplate配置*/Beanpublic RestTemplate r…

基于数据库的全文检索实现

对于内容摘要&#xff0c;信件内容进行全文检索 基于SpringBoot 2.5.6Postgresqljpahibernate实现 依赖 <spring-boot.version>2.5.6</spring-boot.version> <hibernate-types-52.version>2.14.0</hibernate-types-52.version><dependency><…

Chrome浏览器滚动条样式优化

针对Chrome浏览器&#xff0c;可以全局设置滚动条样式&#xff0c;让你的项目更美观 ::-webkit-scrollbar-track-piece {background-color: transparent; } ::-webkit-scrollbar {width: 7px;height: 7px;background-color: transparent; }::-webkit-scrollbar-thumb {border-…

ctfshow(WEB AK赛)

目录 web-观己 web1-观字 web2-观星 web3-观图 web4-观心 过程and分析 web-观己 没啥难的有include 想着伪协议 但是过滤了php 那就是用file为协议读取本地文件 全靠猜 <?php if(isset($_GET[file])){$file $_GET[file];if(preg_match(/php/i, $file)){die(error);}…

【深度学习笔记】9_5 多尺度目标检测

注&#xff1a;本文为《动手学深度学习》开源内容&#xff0c;部分标注了个人理解&#xff0c;仅为个人学习记录&#xff0c;无抄袭搬运意图 9.5 多尺度目标检测 在9.4节&#xff08;锚框&#xff09;中&#xff0c;我们在实验中以输入图像的每个像素为中心生成多个锚框。这些…

opencv中的图像高斯双边模糊—bilateralFilter函数

高斯双边滤波&#xff08;Bilateral Filtering&#xff09;是一种非线性的滤波方法&#xff0c;用于平滑图像&#xff0c;同时保留边缘。与传统的高斯模糊不同&#xff0c;双边滤波在平滑图像的同时&#xff0c;能够避免模糊边缘。这是通过考虑像素值的差异来实现的&#xff1a…

把 Windows 装进 Docker 容器里

本篇文章聊聊如何在 Docker 里运行 Windows 操作系统&#xff0c; Windows in Docker Container&#xff08;WinD&#xff09;。 写在前面 我日常使用 macOS 和 Ubuntu 来学习和工作&#xff0c;但是时不时会有 Windows 使用的场景&#xff0c;不论是运行某个指定的软件&…

YOLOv8_pose-Openvino和ONNXRuntime推理【CPU】

纯检测系列&#xff1a; YOLOv5-Openvino和ONNXRuntime推理【CPU】 YOLOv6-Openvino和ONNXRuntime推理【CPU】 YOLOv8-Openvino和ONNXRuntime推理【CPU】 YOLOv7-Openvino和ONNXRuntime推理【CPU】 YOLOv9-Openvino和ONNXRuntime推理【CPU】 跟踪系列&#xff1a; YOLOv5/6/7-O…

Fix a Tree(树的遍历,判断是否有环 并连成一颗树 )

题意翻译 对于下图中的树&#xff0c; 可以用数组表示为 [2,3,3,2]。这种可以表示树的数组&#xff08;即有效&#xff09;需要符合以下条件&#xff1a; 有且只有一个索引 r &#xff0c;符合pr​r 。其中顶点 r 是树的根。对于所有剩下的 n−1 个顶点 i 一定要有在 i 和 pi…

怎么制作自己的微信小程序店铺?

移动互联网的迅猛发展&#xff0c;微信小程序已成为商家拓展线上业务的重要工具。它不仅能够提供便捷的用户访问体验&#xff0c;还能够帮助商家快速构建起一个功能齐全的在线商城。那么&#xff0c;商家怎么制作自己的微信小程序店铺&#xff1f; 一、准备工作&#xff1a; …

鸿蒙 Harmony 初体验

前言 看现在网上传得沸沸扬扬的鸿蒙&#xff0c;打算弄个 hello world 玩一下, 不然就跟不上时代的发展了 环境安装 我的环境 Windows 11 家庭中文版HarmonyOS SDK (API 9)DevEco Studio (3.1.1 Release)Node.js (16.19.1) 开发IDE下载 官方下载链接 配置 nodejs 这里帮…

MWC 2024|「Paraverse平行云」展示空间计算时代沉浸式交互体验

&#x1f389;当地时间2月26日&#xff0c;2024年世界移动通信大会&#xff08;MWC2024&#xff09;在巴塞罗那拉开帷幕。作为全球移动通信领域最大的技术展会之一&#xff0c;MWC被视为全球通信行业风向标。 &#x1f680;随着Vision Pro再次点燃全球空间计算技术热情&#xf…

电脑文件msvcr100.dll丢失的多种解决方法,快速修复dll报错问题

当计算机用户遇到“msvcr100.dll丢失”的问题时&#xff0c;可能会感到困扰并急于寻求解决方案。这个提示通常意味着系统中某个关键的动态链接库文件缺失&#xff0c;这可能导致某些应用程序无法正常启动或运行。msvcr100.dll是Microsoft Visual C Redistributable Package的一…

HarmonyOS应用开发-Stage模型开发概述

基本概念 UI框架 HarmonyOS提供了一套UI开发框架&#xff0c;即方舟开发框架&#xff08;ArkUI框架&#xff09;。提供了应用UI开发所必需的能力&#xff1a;多种组件、布局计算、动画能力、UI交互、绘制。 方舟开发框架针对开发者提供了两种开发范式&#xff1a; 基于ArkTS…

Java实现知乎热点小时榜爬虫

1.效果演示 1.1 热点问题列表 启动程序后&#xff0c;自动展示热点问题&#xff0c;并等待终端输入 1.2 根据序号选择想看的热点问题 输入问题序号&#xff0c;展示回答内容 1.3 退出 输入q即可退出程序 2.源码 2.1 pom.xml <?xml version"1.0" enco…

【机器学习】无监督学习算法之:层次聚类

层次聚类 1、引言2、层次聚类2.1 定义2.2 原理2.3 实现方式2.4 算法公式2.5 代码示例 3、总结 1、引言 小屌丝&#xff1a;鱼哥&#xff0c; 这周末过的滋润啊。 小鱼&#xff1a;… 每个周末都挺滋润的啊。 小屌丝&#xff1a;啊~ ~ 你这… 小鱼&#xff1a;周末加班&#xf…

从大厂到高校,鸿蒙人才“红透半边天”!

截至目前&#xff0c;继清华大学、北京航空航天大学、武汉大学等985高校开设鸿蒙相关课程后&#xff0c;已经或将要开设鸿蒙相关课程的985、211高校达到近百所&#xff0c;为鸿蒙人才培养提供沃土。 随着鸿蒙系统即将摒弃安卓&#xff0c;鸿蒙原生应用将全面启动的背景下&…

win10从Huggingface下载模型

这里写自定义目录标题 安装CLI工具设置环境变量下载 安装CLI工具 安装Huggingface CLI pip install -U huggingface_hub设置环境变量 设置好变量后&#xff0c;重新启动一个新的命令窗口&#xff0c;cmd或者powershell 下载 huggingface-cli download --resume-download fa…

基于YOLOv8/YOLOv7/YOLOv6/YOLOv5的安全帽检测系统(深度学习模型+UI界面代码+训练数据集)

摘要&#xff1a;开发先进的安全帽识别系统对提升工作场所的安全性至关重要。本文详细介绍了使用深度学习技术创建此类系统的方法&#xff0c;并分享了完整的实现代码。系统采用了强大的YOLOv8算法&#xff0c;并对其与YOLOv7、YOLOv6、YOLOv5的性能进行了详细比较&#xff0c;…