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

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

第五课 tomcat集群使用redis实现session共享_第1张图片
image.png

3、maven设置

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


    
        org.apache.tomcat
        tomcat-catalina
        8.0.33
    
      
        redis.clients
        jedis
        2.7.2
    
  

  
    
         
            org.apache.maven.plugins
            maven-compiler-plugin
            3.0
            
                
                1.8
                
                1.8
                
                UTF-8
            
        
    
  
第五课 tomcat集群使用redis实现session共享_第2张图片
image.png

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

4、tomcat配置

====tomcat context.xml====




  


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" %>



  $Title$


<%
  Person person = new Person();
  person.setId(3);
  person.setName("zhangsan");
  session.setAttribute("person",person);
  session.setAttribute("msg","hello");
%>

<%=session.getId()%>
tomcat141



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" %>



  $Title$


<%
  if(session.getAttribute("person") != null)
  {
    Person person = (Person)session.getAttribute("person");
    out.print(person.getName());
  }
  else
  {
    out.print("null");
  }
%>
<%=session.getId()%>
tomcat209


  

你可能感兴趣的:(第五课 tomcat集群使用redis实现session共享)