因爲項目須要,須要實現Tomcat8.0 + Redis 實現 Session 會話共享,以便於實現多應用集羣。後參考了開源項目: https://github.com/jcoleman/tomcat-redis-session-manager 進行調整後實現該需求。java
實現該需求須要進行三步操做:git
** 1.下載tomcat-redis-session-manager,調整代碼後進行打包。**github
修改1:修改tomcat-catalina的版本爲8.0.26redis
<dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-catalina</artifactId> <version>8.0.26</version> </dependency>
/修改2:修改 RedisSessionManager.java 文件startInternal方法apache
Boolean attachedToValve = false; // 這是原先的版本 // for (Valve valve :getContext().getPipeline().getValves()) { // 這是調整後的內容 for (Valve valve :getContext().getPipeline().getValves()) { if (valve instanceof RedisSessionHandlerValve) { this.handlerValve = (RedisSessionHandlerValve) valve; this.handlerValve.setRedisSessionManager(this); log.info("Attached to RedisSessionHandlerValve"); attachedToValve = true; break; } }
修改3:修改 RedisSessionManager.java 文件 initializeSerializer方法tomcat
Loader loader = null; /* // 這是原先的寫法 if (getContainer() != null) { loader = getContainer().getLoader(); } */ // 這是如今的寫法 if (getContext() != null) { loader = getContext().getLoader(); } ClassLoader classLoader = null;
以上內容都調整後,利用Maven打包成Jar包,譬如我本地叫:tomcat-redis-session.jarsession
2. 拷貝相關的Jar包到Tomcat8.0的Lib包下maven
分別拷貝commons-pool2-2.2.jar、jedis-2.5.2.jar、以及剛纔打包生成的 tomcat-redis-session.jar 到tomcat8.0 的lib下。gradle
3. 修改Tomcat8.0 的conf/Context.xml 文件this
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve"/> <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" database="0" host="redis IP" maxInactiveInterval="session過時時長,以秒爲單位" password="redis的密碼,若是沒有則置空" port="redis端口"/>
當上面三大步驟都處理完成後,重啓 Tomcat 就可見證奇蹟了。