tomcat+redis实现session共享配置之路 支持Tomcat 8

1、网上查找资料,大部分从下面网址下载java代码,因是几年前实现的(大概2,3年前吧),不支持tomcat8 
https://github.com/jcoleman/tomcat-redis-session-manager

2、在myeclipse 新建一个maven项目【maven-archetype-quickstart】 
源文件新建包名com.orangefunction.tomcat.redissessions 
讲下载下来的java类拷贝到该包之下(这些java类只实现tomcat7,实现tomcat8需要做一些修改) 
JavaSerializer.java 
RedisSession.java 
RedisSessionHandlerValve.java 
RedisSessionManager.java 
Serializer.java 
SessionSerializationMetadata.java

tomcat7与tomcat8代码差异对比

3、tomcat8与tomcat7 有些实现代码不一样,需各自新建一个maven项目要各自打包,注意tomcat引得版本与jdk要保持一致 
pom打包注意点

====tomcat8 maven pom.xml====

<dependencies>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-catalina</artifactId>
        <version>8.0.33</version>
    </dependency>
      <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.7.2</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
        <plugin> <!-- 打jar包 -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <!-- 源代码编译版本 -->
                <source>1.8</source>
                <!-- 目标平台编译版本 -->
                <target>1.8</target>
                <!-- 字符集编码 -->
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
    </plugins>
  </build>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

====tomcat7 maven pom.xml====

<dependencies>
  <!-- tomcat7 source1.7  -->
      <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-catalina</artifactId>
        <version>7.0.27</version>
    </dependency>
      <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.7.2</version>
    </dependency>

  </dependencies>

  <build>
    <plugins>
        <plugin> <!-- 打jar包 -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <!-- 源代码编译版本 -->
                <source>1.7</source>
                <!-- 目标平台编译版本 -->
                <target>1.7</target>
                <!-- 字符集编码 -->
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
    </plugins>
</build>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

将打包出来的tomcat-redis的jar包和 
jedis-2.7.2.jar 
commons-pool2-2.3.jar 
拷贝到tomcat的lib文件夹下面

4、tomcat配置 
====tomcat context.xml====

<!-- 
com.orangefunction.tomcat.redissessions 是自定义maven项目的报名路径,切需要与maven 中 RedisSessionManager的serializationStrategyClass值一致
-->

<!-- redis session 共享配置 -->
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />  
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"  
    host="127.0.0.1"  
    port="6379"  
    database="0"  
    maxInactiveInterval="60" />


<!-- 集群配置 还未进行验证-->
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />        
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" 
    maxInactiveInterval="60"
    sentinelMaster="mymaster"
    sentinels="127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381,127.0.0.1:26382" />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

5、需要特别注意的是项目中要存入session的对象必须实现序列化,否知会出现序列化错误

public class Test implements Serializable {
    private static final long serialVersionUID = 5021582410009851677L;
    ......
}
  • 1
  • 2
  • 3
  • 4

配置截图:

tomcat lib

tomcat context.xml 配置

tomcat配置与java源码对应

配置实现redis包名路径的对应


参考网址:

redis + Tomcat 8 的session共享解决 
http://www.cnblogs.com/interdrp/p/4868740.html

Tomcat7+Redis存储Session 
http://blog.csdn.net/caiwenfeng_for_23/article/details/45666831

用Redis存储Tomcat集群的Session 
http://blog.csdn.net/chszs/article/details/42610365

分布式集群系统下的高可用session解决方案 
http://tendyming.iteye.com/blog/1815136

猜你喜欢

转载自blog.csdn.net/luoww1/article/details/78602832