tomcat集群redis配置session共享
?
針對之前的nginx+tomcat的負載均衡機制,因為會出現session丟失的問題,特研究了下redis的session共享;下載JDK7、tomcat7以備后續測試;
一、下載tomcat-redis-session的源碼自行打包:https://github.com/jcoleman/tomcat-redis-session-manager
這里要對tomcat-redis-session打包做一下特殊記錄:
上圖中的構建pom.xml。
<?xml version="1.0" encoding="GB2312"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.hik</groupId><artifactId>tomcat-redis-session</artifactId><!--<packaging>war</packaging>--><version>0.0.1-SNAPSHOT</version><dependencies><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><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> </project>
由于我們的環境都是基于7系列來運行的,所以必須保持一致;
二、下載安裝redis(linxu版本)
wget?http://redis.googlecode.com/files/redis-2.4.6.tar.gz
解壓:
tar –zxvfredis-2.4.6.tar.gz
編譯
需要說明的事,redis的安裝非常簡單,已經有現成的Makefile文件,直接運行make命令即可。
make
make install
安裝成功后直接啟動redis:redis-server/etc/redis.conf,如圖
新開窗口redis-cli測試:
Set a 123
Get a?得到123?安裝成功可以使用
?
三、上邊源碼自己打包生成tomcat-redis-session.jar包,從網上下載jedis-2.7.2.jar?? commons-pool2-2.4.1.jar;將這3個包放入tomcat\lib目錄下;
?
四、配置tomcat\conf下的context.xml文件
將以下代碼添加進context.xml里邊
<!-- tomcat-redis-session共享配置 --> <ValveclassName="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve"/> <ManagerclassName="com.orangefunction.tomcat.redissessions.RedisSessionManager" host="localhost" port="6379" database="0" maxInactiveInterval="60" />?
五、tomcat測試session共享配置
準備兩個tomcat模擬,每個配置都跟上邊一樣,然后
?????? Tomcat18080 webapp/index.jsp修改:<% request.getSession().setAttribute(“a”,”123”);%>
?????? Tomcat28090 webapp/index.jsp?修改:<% out.print(request.getSession().getAttribute(“a”))%>
六、測試整個環境:
服務器記得先啟動redis!
啟動tomcat1,訪問http://localhost:8080/index.jsp
啟動tomcat2,訪問?http://localhost:8090/index.jsp,可看到頁面顯示123,證明session共享成功。
?
七、可能會出現的錯誤
Tomcat可能啟動不正常,最大的原因是jar包沒用對應的版本,本人測試可以用的相關jar版本是commons-pool2-2.3,jedis-2.7.2。 tomcat-redis-session-manager-master.jar如果不能用就按文檔自己重新打包。如果有其他錯誤(比如java.lang.UnsupportedClassVersionError) 的話就去檢查下啟動指定jdk是否為1.7, 如果報錯xml文件如果讀取錯誤(Invalid byte 1 of 1-byte UTF-8 sequence)就修改context.xml中? encoding="GB2312"?>, 其他錯誤基本上都跟jar版本和運行環境相關,請認真檢查。?
總結
以上是生活随笔為你收集整理的tomcat集群redis配置session共享的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android5.1远程升级,Andro
- 下一篇: Docker下使用daocloud镜像加