tomcat8与redis4实现session共享

1、源码

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

2、修改代码RedisSessionManager.java

serializer = (Serializer) Class.forName(serializationStrategyClass).newInstance();
Loader loader =null;
Context context =this.getContext();
if (getContainer()!=null) {	
if (context!=null) {
loader =getContainer().getLoader();	
loader =context.getLoader();
}	
}

附件已修改好。。。

3、部署

下载附件打开:复制commons-pool2-2.3.jar、jedis-2.7.3.jar、tomcat-redis-session-manager-master-2.0.0.jar、tomcat-juli-8.0.46.jar(如果tomcat下没有该jar对应版本自己下载)复制到tomcat/lib下

4、redis配置

打开tomcat/conf/context.xml文件,添加如下配置:


    

4、redis键保存方式

开源项目是直接将SESSIONID作为key存储到redis中的,如下:

1) "6D5B0E0FD89E3A170B8BC5B8C112D3FD"  
2) "9546B26D78C99E8F0BF785535E319271"  
3) "839A35CFE17E900A81F50D629C104D2F"  
4) "1C287C797CF00C82BBBF37A617A3B55C"  
5) "FA2822C5021139641760754242F73393"  
6) "E904369E5B24D39B4E25515D50650EA6" 
可以通过配置 sessionCookieName开定义key,直接在tomcat7/conf/server.xml 的最下面的Context中增加 sessionCookieName 配置即可:

下载附件里面有:redis-修改通过sessionCookieName自定义redis文件夹修改源码RedisSessionManager.java查找如下代码:

private String getSessionRedisKey(String sessionId){
	  return getSessionCookieName() + ":" + sessionId;
  }
  
  private String getSessionCookieName(){
      Context context = (Context) this.getContainer();
       return SessionConfig.getSessionCookieName(context);
  }

  private String sessionIdWithJvmRoute(String sessionId, String jvmRoute) {
    if (jvmRoute != null) {
      String jvmRoutePrefix = '.' + jvmRoute;
      return sessionId.endsWith(jvmRoutePrefix) ? sessionId : sessionId + jvmRoutePrefix;
    }
    return sessionId;
  }

5、附件下载地址

http://download.csdn.net/download/fackyou200/10206410


参考:

http://blog.csdn.net/Jerome_s/article/details/52658946

http://blog.csdn.net/catoop/article/details/48603891

你可能感兴趣的:(redis)