Redis+Tomcat+Nginx集群实现Session共享,Tomcat Session共享

=============================

蕃薯耀 2017年11月27日

http://www.cnblogs.com/fanshuyao/

代码美化版或附件下载见:http://fanshuyao.iteye.com/blog/2400975

一、Session共享使用tomcat-cluster-redis-session-manager插件实现

插件地址见:https://github.com/ran-jit/tomcat-cluster-redis-session-manager

该插件支持Tomcat7、Tomcat8、Tomcat9

或者直接在附件中下载(版本为2.0.2,2017-11-27日前最新版本)

这里有是一个只支持Tomcat7的,不支持tomcat8,暂时不见新的维护:

https://github.com/jcoleman/tomcat-redis-session-manager

二、tomcat-cluster-redis-session-manager详解

1、解压后的文件如下:


 conf目录下有一个redis-data-cache.properties :Redis的配置文件

  1. #-- Redis data-cache configuration
  2. #- redis hosts ex: 127.0.0.1:6379, 127.0.0.2:6379, 127.0.0.2:6380, ....
  3. redis.hosts=127.0.0.1:6379
  4. #- redis password (for stand-alone mode)
  5. #redis.password=
  6. #- set true to enable redis cluster mode
  7. redis.cluster.enabled=false
  8. #- redis database (default 0)
  9. #redis.database=0
  10. #- redis connection timeout (default 2000)
  11. #redis.timeout=2000

lib目录下有4个jar包,如下:

commons-logging-1.2.jar

commons-pool2-2.4.2.jar

jedis-2.9.0.jar

tomcat-cluster-redis-session-manager-2.0.1.jar

三、使用方法:

压缩文件中有使用方法,见readMe.txt 文件:

第一步:

  1. 1. Move the downloaded jars to tomcat/lib directory
  2. * tomcat/lib/

就是把lib目录下的Jar包全复制到tomcat/lib目录下

(一般来说tomcat是集群,至少有2个tomcat,所以先配置好一个tomcat,复制完文件后,再将tomcat文件重新复制一份,这样省事,但需要修改tomcat相应的端口)

第二步:

  1. 2. Add tomcat system property "catalina.base"
  2. * catalina.base="TOMCAT_LOCATION"

就是配置一个环境变量,和Jdk配置的环境变量一样,需要配置一个catalina.base的环境变量,值为TOMCAT_LOCATION

如下:

第三步:

  1. 3. Extract downloaded package (tomcat-cluster-redis-session-manager.zip) to configure Redis credentials in redis-data-cache.properties file and move the file to tomcat/conf directory
  2. * tomcat/conf/redis-data-cache.properties

把conf目录下的配置文件redis-data-cache.properties复制到tomcat/conf/目录下

第四步:

  1. 4. Add the below two lines in tomcat/conf/context.xml
  2. <Valve className="tomcat.request.session.redis.SessionHandlerValve" />
  3. <Manager className="tomcat.request.session.redis.SessionManager" />

在tomcat/conf/目录下的context.xml文件,加上相应的配置,如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. Licensed to the Apache Software Foundation (ASF) under one or more
  4. contributor license agreements.  See the NOTICE file distributed with
  5. this work for additional information regarding copyright ownership.
  6. The ASF licenses this file to You under the Apache License, Version 2.0
  7. (the "License"); you may not use this file except in compliance with
  8. the License.  You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. --><!-- The contents of this file will be loaded for each web application --><Context>
  16. <!-- Default set of monitored resources. If one of these changes, the    -->
  17. <!-- web application will be reloaded.                                   -->
  18. <WatchedResource>WEB-INF/web.xml</WatchedResource>
  19. <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>
  20. <!-- Uncomment this to disable session persistence across Tomcat restarts -->
  21. <!--
  22. <Manager pathname="" />
  23. -->
  24. <!-- Uncomment this to enable Comet connection tacking (provides events
  25. on session expiration as well as webapp lifecycle) -->
  26. <!--
  27. <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
  28. -->
  29. <Valve className="tomcat.request.session.redis.SessionHandlerValve"/>
  30. <Manager className="tomcat.request.session.redis.SessionManager"/>
  31. </Context>

第五步:

  1. 5. Verify the session expiration time (minutes) in tomcat/conf/web.xml
  2. <session-config>
  3. <session-timeout>60<session-timeout>
  4. <session-config>

修改session的过期时间,默认是30分钟,可以不需要此步骤。

session集群的配置至此结束。

四、Nginx集群

1、下载Nignx:http://nginx.org/en/download.html

本人练习时使用windows,所以下载的windows版本:http://nginx.org/download/nginx-1.13.7.zip

2、下载后解压:D:\soft\nginx-1.12.2 (之前使用的是1.12.2的版本,现在最新版是1.13.7,但都一样,附件中有1.12.2版本提供下载)

3、修改Nginx配置文件nginx.conf

进入conf目录(D:\soft\nginx-1.12.2\conf),找到nginx.conf配置文件,打开编辑:

3.1在http{……}里加上upstream,如下:

  1. upstream myTomcatCluster{# tomcatCluster和proxy_pass保持一样
  2. #解决session的问题
  3. #ip_hash;#加上这个,解决Session每次访问页面都不一样,加上就一样了。
  4. #这里是tomcat的地址,weight越大,访问机率越大。
  5. server 127.0.0.1:9300 weight=1 fail_timeout=5s max_fails=1;
  6. server 127.0.0.1:9400 weight=1 fail_timeout=5s max_fails=1;
  7. }

server:配置tomcat服务器请求的地址,2台Tomcat服务就配置2个server,分别对应9300,9400端口

weight 表示权重,权重越大,访问到的机率越大。

3.2、修改location / {……}

默认是这个的:

  1. location / {
  2. root   html;
  3. index  index.html index.htm;
  4. }

修改成这样:

  1. location / {
  2. #root   html;
  3. proxy_pass http://myTomcatCluster;
  4. #index  index.html index.htm;
  5. proxy_set_header Host $host;
  6. proxy_set_header X-Real-IP $remote_addr;
  7. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  8. proxy_connect_timeout       1;
  9. proxy_read_timeout          1;
  10. proxy_send_timeout          1;
  11. }

最简单的配置就是:

  1. location / {
  2. proxy_pass http://myTomcatCluster;
  3. }

myTomcatCluster 对应upstream后的命名。

下面的配置可以解决2个Tomcat服务器集群,当一台服务器挂掉(宕机)后,请求变得很慢的问题。

(Tomcat集群一台服务器挂掉后请求变慢解决方案)

  1. proxy_connect_timeout       1;
  2. proxy_read_timeout          1;
  3. proxy_send_timeout          1;

3.3、启动Nginx服务器

使用Windows命令行启动

(1)进入D盘:d:

(2)进入D:\soft\nginx-1.12.2目录:

  1. cd D:\soft\nginx-1.12.2

(3)启动服务: (启动一闪而过,但打开进程管理器能看到是已经启动的)

  1. start nginx

关闭服务的命令:nginx -s stop

重新加载的命令:nginx -s reload,修改配置文件后,可以使用该命令直接加载,不需要重启。

五、测试集群:

1、tomcat准备

将已经配置好的一个tomcat复制一份,修改端口,然后再修改一下tomcat的配置文件(server.xml)

我的一个tomcat在:D:\soft\apache-tomcat-8.0.45-9300\conf

另一个是:D:\soft\apache-tomcat-8.0.45-9400\conf

修改:

  1. <Engine defaultHost="localhost" name="Catalina">

其中tomcat 9300端口的修改如下:

  1. <Engine defaultHost="localhost" jvmRoute="jvm9300" name="Catalina">

tomcat 9400端口的修改如下:

  1. <Engine defaultHost="localhost" jvmRoute="jvm9400" name="Catalina">

2、项目准备:

新建立一个web项目,然后新建立一个index.jsp的文件,如下:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>首页redis-session</title>
  8. </head>
  9. <body>
  10. <div>tomcat 集群测试</div>
  11. <div>
  12. <%
  13. //HttpSession session = request.getSession(true);
  14. System.out.println(session.getId());
  15. out.println("<br> SESSION ID:" + session.getId()+"<br>");
  16. %>
  17. </div>
  18. </body>
  19. </html>

主要是在打印页面输出sessionId的信息:

  1. out.println("<br> SESSION ID:" + session.getId()+"<br>");

然后把这个项目分别部署到9300、9400端口的2个tomcat中,分别启动,记得也启动Nginx和redis哦

然后打开浏览器通过地址访问项目:http://localhost/redis-session/ (使用Nginx集群分发,不需要端口号访问),显示如下:

  1. tomcat 集群测试
  2. SESSION ID:B837ECA85B47081EAA2FEFCD7E579CD2.jvm9400

无论怎么刷新访问(打开新的标签页也是(非新窗口))的都是jvm9400,也就是端口号为9400的tomcat

后缀.jvm9400就是前面配置的:

  1. <Engine defaultHost="localhost" jvmRoute="jvm9400" name="Catalina">

打开新的隐身窗口访问:

  1. tomcat 集群测试
  2. SESSION ID:83BBA58F4EB7B2EFF90AE05D4A0629FD.jvm9300

这时访问的是端口号为9300的tomcat,通过后缀.jvm9300判断知道。

新窗口每次访问的是都是tomcat9300,session也不会变。

在访问后缀为.jvm9400时,把端口9400的tomcat关掉,再次刷新访问,sessionId一样不变,由此可见,2个tomcat的sessionId是共享的。

使用Redis实现session共享的好处就是,把session管理放在redis中,如果服务器重启或挂机,sessionId保存在redis中,下次重启后一样生效,避免sessionId失效,同样redis最好也做集群,避免redis重启或挂机。

结束了。

=============================

蕃薯耀 2017年11月27日

http://www.cnblogs.com/fanshuyao/

Redis+Tomcat+Nginx集群实现Session共享,Tomcat Session共享的更多相关文章

  1. Tomcat集群,Nginx集群,Tomcat+Nginx 负载均衡配置,Tomcat+Nginx集群

    Tomcat集群,Nginx集群,Tomcat+Nginx 负载均衡配置,Tomcat+Nginx集群 >>>>>>>>>>>> ...

  2. Tomcat学习总结(8)——Tomcat+Nginx集群解决均衡负载及生产环境热部署

    近日,为解决生产环境热部署问题,决定在服务器中增加一个tomcat组成集群,利用集群解决热部署问题. 这样既能解决高并发瓶颈问题,又能解决热部署(不影响用户使用的情况下平滑更新生产服务器)问题. 因为 ...

  3. windows Tomcat+Nginx 集群 迷你版

    一. 准备 两个Tomcat 加上Nginx 2. 创建一个公共的文件夹用于部署项目 3. Tomcat配置 配置内存 在catalina.bat 第一行增加 set JAVA_OPTS=-Xms51 ...

  4. 腾讯云CentOS 7搭建简单Tomcat+nginx集群

    1.安装Tomcat 进入 /usr/local/ 目录 cd /usr/local 下载 wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-9/v ...

  5. Nginx+Tomcat搭建集群,Spring Session+Redis实现Session共享

    小伙伴们好久不见!最近略忙,博客写的有点少,嗯,要加把劲.OK,今天给大家带来一个JavaWeb中常用的架构搭建,即Nginx+Tomcat搭建服务集群,然后通过Spring Session+Redi ...

  6. nginx+tomcat实现集群,redis实现session共享,软连接实现文件共享:http://blog.csdn.net/hua1586981/article/details/78132710

    转载 2017年02月08日 16:52:41 730 相信很多人都听过nginx,这个小巧的东西慢慢地在吞食apache和IIS的份额.那究竟它有什么作用呢?可能很多人未必了解. 说到反向代理,可能 ...

  7. Nginx集群配置与redis的session共享策略

    一.什么是Nginx? Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Ramb ...

  8. Spring Boot+redis存储session,满足集群部署、分布式系统的session共享

    本文讲述spring-boot工程中使用spring-session机制进行安全认证,并且通过redis存储session,满足集群部署.分布式系统的session共享. 原文链接:https://w ...

  9. 注意这几点,轻轻松松配置 Nginx + Tomcat 的集群和负载均衡

    Tomcat 集群是当单台服务器达到性能瓶颈,通过横向扩展的方式提高整体系统性能的有效手段.Nginx 是一个高性能的 HTTP 和反向代理 web 服务器,可以通过简单的配置实现 Tomcat 集群 ...

随机推荐

  1. Django实现简单分页功能

    使用django的第三方模块django-pure-pagination 安装模块: pip install django-pure-pagination 将'pure_pagination'添加到s ...

  2. 为选择屏幕的字段设置F4帮助

    在没有参考 数据元素,域和搜索帮助的情况下,自定义F4 帮助 1,PARAMETERS: p_bukrs(4) TYPE C MATCHCODE OBJECT H_T001. 2,AT SELECTI ...

  3. 框架应用 : Spring MVC - 开发详述

    软件开发中的MVC设计模式 软件开发的目标是减小耦合,让模块之前关系清晰. MVC模式在软件开发中经常和ORM模式一起应用,主要作用是将(数据抽象,数据实体传输和前台数据展示)分层,这样前台,后台,数 ...

  4. win10 uwp 如何让一个集合按照需要的顺序进行排序

    虽然这是 C# 的技术,但是我是用在 uwp ,于是就把标题写这个名.有一天,我的小伙伴让我优化一个列表.这个列表是 ListView 他绑定了一个 ObservableCollection 所以需要 ...

  5. 解决 Win10 UWP 无法使用 ss 连接

    一旦使用了 ss, 那么很多应用就无法连接网络. 本文提供一个方法可以简单使用ss提供的代理. 多谢 wtwsgs 提供方法:http://blog.csdn.net/wtwsgs/article/d ...

  6. Web Fragment在项目中的使用

    Web Fragment 是什么 - 它是在 servlet 3.0开始支持的,可以把一个dy web项目拆分为多个项目,解耦合,使其在项目中开发效率提高,下面我演示简单的项目创建过程 用eclips ...

  7. 解决Nginx+Tomcat时ContextPath不同的问题

    1        问题描述 项目前端模板使用Thymeleaf,在对各种URL进行格式化输出时,都使用@{uri}代码.它会自动读取项目部署的虚拟路径,添加到URI的前端输出. 真实测试和生产环境中, ...

  8. 【转】深度分析NandFlash—物理结构及地址传送(以TQ2440开发板上的K9F2G08U0A为例)

    K9F2G08U0A是三星公司生产的总容量为256M的NandFlash,常用于手持设备等消费电子产品.还是那句话,搞底层就得会看datasheet,我们就从它的datasheet看起. 这就是 K9 ...

  9. 项目总结一:响应式之CSS3 媒体查询

    1.<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scala ...

  10. JS中处理单个反斜杠(即转义字符的处理)

    问题来源:在表单的<input>标签中对输入的字符串进行大写转换.一不小心输入了反斜杠 \ 如下图所示: 输入 chn\  的时候,在  IE8  下弹出一个js错误.(在实际的项目的表单 ...