Original http://blog.csdn.net/caiwenfeng_for_23/article/details/45666831
PS: Until 2015-05-12 is not supported TOMCAT8, details see official website: Https://github.com/jcoleman/tomcat-redis-session-manager
Prerequisite: You have deployed Redis, have not learned, can be moved here: http://blog.csdn.net/caiwenfeng_for_23/article/details/45511007
My case Download: http://download.csdn.net/detail/caiwenfeng_for_23/8689847
Actually very simple, just a few steps:
1. Configure the context.xml file under Tomcat's conf directory:
1> Single Point Reids configuration
<!-- Jedis save session --> <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" /> <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager" host="localhost" port="6379" database="0" maxInactiveInterval="60"/>
2> Sentinel Cluster Configuration:
<!-- Sentinel 配置 --> <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. Add jar
3. Testing
1>
Storage session:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("hello"); //取得Session对象 HttpSession session=request.getSession(); //设置Session属性 for(int i=0;i<100000;i++){ session.setAttribute("name"+i, "Magci_"+i); } }
2> reboot Tomcat: If the session is saved under Tomcat, the session does not exist after the reboot, and if it is saved under Redis, the Tomcat restart has no effect on the session.
3> Remove Session:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("hello"); //取得Session对象 HttpSession session=request.getSession(); //取出Session属性 for(int i=0;i<100000;i++){ System.out.println(session.getAttribute("name"+i)); } }
Note: Starting from TOMCAT6, the session persistence setting is turned on by default, the local session persistence can be closed during testing, in fact it is very simple, in the context.xml file under Tomcat's Conf directory, uncomment the following configuration:
<!-- Uncomment this to disable session persistence across Tomcat restarts --> <!-- <Manager pathname="" /> -->
See this blog: example analysis of Session persistence
Go Tomcat7+redis Storage Session