基于tomcat集群做session共享
前端代理服务器nginx:192.168.223.136
tomcat服务器:采用的一台多实例192.168.223.146:8081,192.168.223.146:8082(如何构建多实例tomcat,请参考前面的文章)
首先查看下tomcat的webapps目录架构:
[root@wadeson tomcat-instance]# pwd
/usr/local/tomcat-instance
[root@wadeson tomcat-instance]# ll
总用量 24
-rwxr-xr-x. 1 root root 165 8月 9 15:31 start-tomcat1.sh
-rwxr-xr-x. 1 root root 165 8月 9 16:18 start-tomcat2.sh
-rwxr-xr-x. 1 root root 165 8月 9 15:38 stop-tomcat1.sh
-rwxr-xr-x. 1 root root 165 8月 9 16:18 stop-tomcat2.sh
drwxr-xr-x. 7 root root 4096 8月 9 15:45 tomcat1
drwxr-xr-x. 7 root root 4096 8月 9 16:12 tomcat2
[root@wadeson tomcat-instance]# cd tomcat1/webapps/
[root@wadeson webapps]# tree .
.
├── myapp
│ └── test.jsp
└── ROOT
├── classes
├── index.jsp
├── lib
├── META-INF
└── WEB-INF
由于安装的tomcat是8.5版本的,所以查看下官方文档:https://tomcat.apache.org/tomcat-8.5-doc/cluster-howto.html
构建tomcat集群只需要如下几步:
1、添加集群组件信息(在host或者engine组件下添加进去就行)
<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>
以上是直接摘自官网,需要稍微做下修改address="auto",
address="228.0.0.4"2、需要在各自的应用程序目录下面为web.xml添加一行<distributable/>元素
- Make sure your
web.xml
has the<distributable/>
element
现在直接贴自己的配置tomcat1:
<Host name="localhost" appBase="/usr/local/tomcat-instance/tomcat1/webapps" unpackWARs="true" autoDeploy="true">
<Context path="" docBase="ROOT" reloadable="true"/>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/usr/local/tomcat-instance/tomcat1/logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
<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.223.146" 由于是一台主机上的两个tomcat实例,所以这里的cluster添加的内容都一样
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>
</Host>
然后在应用程序ROOT下面的WEB-INF的下面添加自己的web.xml:
[root@wadeson tomcat-instance]# ll tomcat1/webapps/ROOT/WEB-INF/web.xml
-rw-------. 1 root root 168271 8月 10 16:50 tomcat1/webapps/ROOT/WEB-INF/web.xml
<distributable/> 这就是需要添加的一行内容
</web-app>
于是配置tomcat1实例完成,tomcat2的配置和tomcat1一致(由于是同一主机的不同实例)
于是进行访问:
可以看见session的id值两者相同,只是后面的tomcat不一致,于是session共享的功能就达成了,然后配置nginx进行代理转发:
upstream backserver {
server 192.168.223.146:8081 weight=1;
server 192.168.223.146:8082 weight=1;
}
server {
listen 80;
server_name 192.168.223.136;
location / {
root html;
index index.html index.htm;
proxy_pass http://backserver/;
}
测试效果:
转载于:https://www.cnblogs.com/jsonhc/p/7344241.html
基于tomcat集群做session共享相关推荐
- Nginx+Tomcat+Memcached实现tomcat集群和session共享
Nginx+Tomcat+Memcached实现tomcat集群和session共享 [http://blog.csdn.net/shimiso/article/details/8979044] 为什 ...
- Tomcat集群及Session共享
Tomcat集群 由于单台Tomcat的承载能力是有限的,当我们的业务系统用户量比较大,请求压力比较大时,单台Tomcat是扛不住的,这个时候,就需要搭建Tomcat的集群,而目前比较流行的做法就是通 ...
- MSM实现tomcat集群的session共享
本文首发于我的个人网站: https://hewanyue.com/ 本文作者: Hechao 本文链接: https://hewanyue.com/blog/6254cc16.html 会话保持起源 ...
- Nginx+tomcat集群的session共享问题
请求负载过程中会话信息不能丢失.那么需要在多个tomcat中session需要共享.所以需要进行相关问题的解决 配置Tomcat的session共享可以有三种解决方案: 第一种:是以负载均衡服务器本身 ...
- Tomcat集群和Session共享的配置方法
Tomcat集群配置其实很简单,在Tomcat自带的文档中有详细的说明( /docs/cluster-howto.html ),只不过是英语的,对我这样的人来说很难懂 . 下面根据说下怎么配置吧: 第 ...
- 关于 tomcat 集群中 session 共享的三种方法
前两种均需要使用 memcached 或 redis 存储 session ,最后一种使用 terracotta 服务器共享. 建议使用 redis ,不仅仅因为它可以将缓存的内容持久化,还因为它支持 ...
- nginx tomcat集群实现session共享
tomcat配置文件 项目中引入如下4个jar包,就搞定啦
- Redis + Tomcat + Nginx 集群实现 Session 共享
点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 作者 | 蕃薯耀 链接 | www.cnblogs.com/fan ...
- 通过memcached来实现对tomcat集群中Session的共享策略 .
近期在做一套集群的实现,实现的方案是在Linux下完成对Apache + Tomcat 负载均衡的功能. 但是实现了该集群后,发现登陆系统后,每次都会被拦截回登录页面,造成该现象的原因是Session ...
最新文章
- 图解负载均衡 LVS、Nginx及HAProxy--云平台技术栈14
- Codeforces 479【E】div3
- QT的QGraphicsLinearLayout类的使用
- java程序员选择多个offer时需要看重哪些?_对不起,我们公司不要本科以下的大学生,学历对于程序员重不重要...
- php ajax loading图片居中显示,PHP语言入门之PHP+ajax实现登录按钮加载loading效果
- 520 miix 小兵 黑苹果_【黑果小兵】macOS Big Sur 11.0.1 20B50 正式版 with Clover 5126 黑苹果系统镜像下载...
- Unity使用UniWebview插件内嵌H5游戏
- dnastar拼接反向互补序列_什么叫“反向互补序列”????????
- asp中的session使用方法详解
- ctfshow XXE web373-web378 wp
- 苹果手机smtp服务器没有响应,smtp服务器没有响应
- 我的一周re学习总结
- gensim学习之corpora.Dictionary
- Linux:WCP知识库安装及配置
- 单片机-硬件接口(UART/IIC/SPI/RS232/USB/CAN)异同
- Docker Swarm 健康检查
- 置换群的整幂运算【置换群】
- Android动态修改icon--让你的app浪起来
- 看雪版主、十年黑客“玩命”:我不作死,我只玩命 | 宅客故事
- 搭建图片加密平台,扫码支付后简单获取密码