簡介
本文將介紹tomcat7、tomcat8、spring多種方式如何實現單臺redis和集羣redis如何配置session共享(其中tomcat8只能實現單臺redis共享)
第一步:環境準備
1、所需lib包如下:
tomcat7依賴包如下:
tomcat8依賴包如下:
2、測試war包如下:
- war包:my_project.war
- 源碼:my_project.zip
3、Tomcat如下:
第二步:Tomcat8配置方式實現session共享
1、====tomcat conf/context.xml <Context>標籤下====
<!-- com.orangefunction.tomcat.redissessions 是自定義maven項目的報名路徑,切需要與maven 中 RedisSessionManager的serializationStrategyClass值一致 --> <!-- 單臺redis session 共享配置,已驗證(如果redis沒有設置密碼,需將password一行去掉)--> <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" host="127.0.0.1" port="6379" database="0" password="123456" maxInactiveInterval="60" /> <!-- 集羣redis session 共享配置,未驗證 --> <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" maxInactiveInterval="60" sentinelMaster="mymaster" sentinels="127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381,127.0.0.1:26382" /> |
2、將lib包和war包分別拷貝至至少兩個tomcat的lib和webapps目錄
3、啓動tomcat,瀏覽器訪問Nginx負載均衡地址:http://10.100.160.135/my_project/indexServlet,觀察返回的session值是否一致
第三步:Tomcat7配置方式實現session共享
1、====tomcat conf/context.xml <Context>標籤下====
<!-- com.orangefunction.tomcat.redissessions 是自定義maven項目的報名路徑,切需要與maven 中 RedisSessionManager的serializationStrategyClass值一致 --> <!-- 單臺redis session 共享配置,已驗證(如果redis沒有設置密碼,需將password一行去掉)--> <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve"/> <!-- 集羣redis session 共享配置,已驗證 --> <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve"/> |
2、將lib包和war包分別拷貝至至少兩個tomcat的lib和webapps目錄
3、啓動tomcat,瀏覽器訪問Nginx負載均衡地址:http://10.100.160.135/my_project/indexServlet,觀察返回的session值是否一致
第三步: spring-session-data-redis實現session共享
補丁:ssm.war
直接降war包發佈至tomcat,conf/content.xml無需任何修改
啓動tomcat,瀏覽器訪問Nginx負載均衡地址:http://10.100.160.135/my_project/indexServlet,觀察返回的session值是否一致
問題總結
1、要存入session的對象必須實現序列化,否知會出現序列化錯誤
public class Test implements Serializable { private static final long serialVersionUID = 5021582410009851677L; ...... } |
2、待續