前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >nginx完成session共享

nginx完成session共享

作者头像
故久
发布2019-09-29 14:28:58
1K0
发布2019-09-29 14:28:58
举报
文章被收录于专栏:故久故久

首先安装niginx,跟redis

然后准备两个tomcat,修改他们的端口号

修改nginx.conf加上:

[html]

upstream backend {

server 10.10.49.23:8080 max_fails=1 fail_timeout=10s;

server 10.10.49.15:8081 max_fails=1 fail_timeout=10s;

}

修改nginx.conf的location成

location / {

root html;

index index.html index.htm;

proxy_pass http://backend;

}

下载tomcat-redis-session-manager相应的jar包,主要有三个:

tomcat-redis-session-manager-1.2-tomcat-7-java-7.jar,jedis-2.5.2.jar,commons-pool2-2.0.jar下载完成后拷贝到$TOMCAT_HOME/lib中

修改两tomcat的context.xml:

<Context>

<!-- Default set of monitored resources -->

<WatchedResource>WEB-INF/web.xml</WatchedResource>

<!-- Uncomment this to disable session persistence across Tomcat restarts -->

<!--

<Manager pathname="" />

-->

<!-- Uncomment this to enable Comet connection tacking (provides events

on session expiration as well as webapp lifecycle) -->

<!--

<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />

-->

<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />

<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"

host="10.10.49.20"

port="6379"

database="0"

maxInactiveInterval="60" />

</Context>

在tomcat/webapps/test放一个index.jsp

<%@ page language="java" %>

  1. <html>
  2. <head><title>TomcatA</title></head>
  3. <body>
  4. <table align="centre" border="1">
  5. <tr>
  6. <td>Session ID</td>
  7. <td><%= session.getId() %></td>
  8. </tr>
  9. <tr>
  10. <td>Created on</td>
  11. <td><%= session.getCreationTime() %></td>
  12. </tr>
  13. </table>
  14. </body>
  15. </html>
  16. sessionID:<%=session.getId()%>
  17. <br>
  18. SessionIP:<%=request.getServerName()%>
  19. <br>
  20. SessionPort:<%=request.getServerPort()%>
  21. <%
  22. //为了区分,第二个可以是222
  23. out.println("This is Tomcat Server 1111");
  24. %>

开启niginx服务

通过访问http://端口号/test/

刷新

可以看到虽然Server从1111变为2222,但session的创建时间没有变化,这就完成了session共享。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档