美文网首页
第五课 tomcat集群使用redis实现session共享

第五课 tomcat集群使用redis实现session共享

作者: Arroganter | 来源:发表于2018-08-27 08:39 被阅读1次

    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

    image.png

    3、maven设置

    ====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>
    
    image.png

    image.png
    将打包出来的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" />
    
    

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

    public class Test implements Serializable {
        private static final long serialVersionUID = 5021582410009851677L;
        ......
    }
    tomcat209
    <%@ page import="com.neuedu.bean.Person" %><%--
      Created by IntelliJ IDEA.
      User: Administrator
      Date: 2018/7/17
      Time: 
    13:57
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page
            contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
    
      <title>$Title$</title>
    </head>
    <body>
    <%
      Person person = new Person();
      person.setId(3);
      person.setName("zhangsan");
      session.setAttribute("person",person);
      session.setAttribute("msg","hello");
    %>
    
    <%=session.getId()%>
    tomcat141
    
    </body>
    </html>
    

    tomcat209

    <%@ page import="com.neuedu.bean.Person" %><%--
      Created by IntelliJ IDEA.
      User: Administrator
      Date: 2018/7/17
      Time:
    13:57
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page
            contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
    
      <title>$Title$</title>
    </head>
    <body>
    <%
      if(session.getAttribute("person") != null)
      {
        Person person = (Person)session.getAttribute("person");
        out.print(person.getName());
      }
      else
      {
        out.print("null");
      }
    %>
    <%=session.getId()%>
    tomcat209
    
    </body>
    </html>  
    

    相关文章

      网友评论

          本文标题:第五课 tomcat集群使用redis实现session共享

          本文链接:https://www.haomeiwen.com/subject/vrheiftx.html