對之前的nginx+tomcat的負載平衡機制,因為會出現session丟失的問題,特研究了下redis的session共用;(如果想搭建本環境可從上一篇開始,涉及到nginx的環境搭建http://blog.csdn.net/qq584852076/article/details/46469141)
下載JDK7、tomcat7以備後續測試;
一、下載tomcat-redis-session的源碼自行打包:https://github.com/jcoleman/tomcat-redis-session-manager
這裡要對tomcat-redis-session打包做一下特殊記錄:
由於我們的環境都是基於7系列來啟動並執行,所以必須保持一致;
二、下載redis安裝並運行 http://redis.io/download
下載後進入redis-windows-master\src\msopentech\redis-64.2.8.17 目錄
在CMD中執行 redis-server.exe redis.windows.conf 如圖
三、打包產生tomcat-redis-session.jar包,從網上下載jedis-2.7.2.jar commons-pool2-2.4.1.jar;將這3個包放入tomcat\lib目錄下;
四、配置tomcat\conf下的context.xml檔案 [html] view plain copy <?xml version='1.0' encoding='utf-8'?> <Context> <WatchedResource>WEB-INF/web.xml</WatchedResource> <!-- tomcat-redis-session共用配置 --> <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" host="localhost" port="6379" database="0" maxInactiveInterval="60" /> <!----------------------------- --> </Context>
五、多個tomcat的配置都一致,所以把配置都copy到對應的其他tomcat下;如果在一台機器上進行測試,記得對各個tomcat的連接埠進行修改;個人的部署環境如下:
六、測試整個環境:(需要提前在tomcat放置一個簡單的web項目)
啟動redis
啟動nginx
啟動3個tomcat
訪問:http://localhost/ 不停的F5重新整理頁面 發現頁面的sessionid不變;關閉某個tomcat繼續F5,sessionid也不變,說明環境搭建成功; 個人資產庫中放置需要用到的設定檔以及相關jar包,redis伺服器等資源;