原创

使用开源插件TomcatRedisSessionManager实现tomcat7session共享

 资源下载地址:

https://gameboys.oss-cn-shenzhen.aliyuncs.com/blog/%E4%BD%BF%E7%94%A8%E5%BC%80%E6%BA%90%E6%8F%92%E4%BB%B6%E5%AE%9E%E7%8E%B0tomcat7session%E5%85%B1%E4%BA%AB.zip

 

使用开源插件实现tomcat集群共享session

一、环境准备

一台linux服务器

已安装好nginx

已配置好两个tomcat服务

已安装好Redis服务

 

二、操作流程

1. 新建springmvc测试项目,项目结构如图


pom.xml

<dependencies>

<!-- 加入spring mvc依赖包 -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>3.2.5.RELEASE</version>

</dependency>

<!-- 加入jstl依赖包 -->

<dependency>

<groupId>jstl</groupId>

<artifactId>jstl</artifactId>

<version>1.2</version>

</dependency>

<dependency>

<groupId>javax.servlet.jsp</groupId>

<artifactId>jsp-api</artifactId>

<version>2.2.1-b03</version>

<scope>provided</scope>

</dependency>

<!-- servlet -->

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>servlet-api</artifactId>

<version>2.5</version>

<scope>provided</scope>

</dependency>

</dependencies>

 

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()

+ path + "/";

%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<%-- <%@ page isELIgnored ="false" %> --%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="<%=basePath%>">

<title>My JSP 'TestUpload.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

</head>

<body>

我的session${id}

<br> sessionid=<%=session.getId()%>

</body>

</html>

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

id="WebApp_ID" version="3.0">

<display-name>test</display-name>

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:spring.xml</param-value>

</context-param>

<filter>

<description>字符集过滤器</description>

<filter-name>encodingFilter</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

<init-param>

<description>字符集编码</description>

<param-name>encoding</param-name>

<param-value>UTF-8</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>encodingFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<listener>

<description>spring监听器</description>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<listener>

<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>

</listener>

<servlet>

<description>spring mvc servlet</description>

<servlet-name>springMvc</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<description>spring mvc 配置文件</description>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:spring.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>springMvc</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>/index.jsp</welcome-file>

</welcome-file-list>

<session-config>

<session-timeout>15</session-timeout>

</session-config>

</web-app>

 

配置好打包发布,访问http://localhost:8080/TestTomcatSession/index.jsp

 

2. 配置tomcat

下载安装tomcat7.0.94,分为两个目录,tomcat7080tomcat9080

分别改掉tomcat7080/conf/server.xml里面的端口号改成7打头和9打头

3. 配置nginx

upstream wwwproxy {

    server 127.0.0.1:7080 weight=50; #7080 监听端口

    server 127.0.0.1:9080 weight=50; #9080 监听端口

}

server {

                listen       80;

                server_name  www.666.com;

                charset utf-8;

                access_log  /data/log/nginx/test.log main;

                add_header Access-Control-Allow-Origin *;

                add_header Access-Control-Allow-Headers Origin,X-Requested-Width,Content-Type,Accept;

                location / {

                        proxy_pass http://wwwproxy; #反向代理

                }

        }

4. session非共享测试

http://47.107.177.203:7080/index.jsp

http://47.107.177.203:9080/index.jsp

他们的响应是不一样的

5. 下载资源

 

5.1配置 redis.properties , 集群中的 IP:端口用逗号隔开,打开 Redis 集群模式

# redis hosts ex: 127.0.0.1:6379, 127.0.0.2:6379, 127.0.0.2:6380, ....

redis.hosts=192.168.1.111:7111,192.168.1.112:7112,192.168.1.113:7113,192.168.1.114:7114,192.168.1.115:7115,192.168.1.116:7116

# Redis Password

redis.password=

# set true to enable redis cluster mode

redis.cluster.enabled=true

配置好之后把 redis.properties 上传到 Tomcat7 conf 目录

注意:此插件支持单节点的 Redis,也支持 Redis 集群,只需要在 redis.properties 中配置则可。

5.2 Tomcat7 中的 conf/context.xml 中增加如下两行配置:

<Valve className="com.r.tomcat.session.management.commons.SessionHandlerValve" />

<Manager className="com.r.tomcat.session.management.redis.RedisSessionManager" />

5.3 Tomcat conf/web.xml 中核对确认 Tomcat Session 超时时间,默认为 30 分钟。

<!-- ==================== Default Session Configuration ================= -->

<!-- You can set the default session timeout (in minutes) for all newly -->

<!-- created sessions by modifying the value below. -->

<session-config>

<session-timeout>30</session-timeout>

</session-config>

可按需修改。

5.4commons-logging-1.1.jar commons-pool2-2.2.jar jedis-3.0.0-SNAPSHOT.jar TomcatRedisSessionManager-1.0.jar

上传到tomcat7lib目录下

5.5重启tomcat7080  tomcat9080

5.6测试session共享

http://47.107.177.203:7080/index.jsp

http://47.107.177.203:9080/index.jsp

http://47.107.177.203/index.jsp

他们的结果是一样的!


 

 

 

 

 

 

 




正文到此结束
本文目录