First, in the work because to use the Tomcat cluster deployment, this time involves the session sharing problem, mainly has three kinds of solutions:
1. Use database to store session
2. Use cookies to store session
3. Use Redis to store sesssion
Second, this article mainly talk about the 3rd scenario, that is, using Redis to store Session,github already have the Open Source component (Tomcat-redis-session-manager), the following is the steps to configure
1. Configuring the Tomcat configuration file Context.xml
<valve classname= "Com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve"/> <manager Classname= "Com.orangefunction.tomcat.redissessions.RedisSessionManager" host= "localhost" <!-- Redis Address-- port= "6379" <!--Redis Port-- password= "123456" <!--redis password -- database= "0" <!--storage session Redis Library number --maxinactiveinterval= "@" <!--session expiration interval (seconds)-- > />
Note:
1) The ClassName attribute in the manager node is required and all other options are optional
2) Maxinactiveinterval setting does not take effect, temporarily cannot find the reason, in the deployment found that the interval of the session failure is always read tomcat/conf/ The value of the Session-timeout attribute configured in the Session-config node in Web. XML, and all in seconds (if you know the cause of the problem, please tell me, thank you very much)
2, add the relevant jar package to the Tomcat/lib directory, the required jar package is as follows:
Commons-pool2-2.2.jar
Jedis-2.5.2.jar
Tomcat-redis-session-manage-tomcat7.jar
Note:
1) I upload these jar packages to the Baidu cloud disk, there is a need to click to download: Http://pan.baidu.com/s/1bokMOVH
3, configure load balancer in Nginx, configuration file is nginx.conf
HTTP {upstream MyServer {server 192.168.1.2:8080;server 192.168.1.3:8080;} server {Listen 80;server_name www.kolbe.cn;location/{Proxy_pass http://myServer;}}
Once configured, requests to access www.kolbe.cn will be forwarded to 192.168.1.2:8080 and 192.168.1.3:8080, which can be printed on the page to verify the session ID.
Session Id: <%= request.getsession (). GetId ()%>
Of course, you can also view it directly in the Redis library
Note:
1) Home page for this open source component (Tomcat-redis-session-manager) in GitHub: Https://github.com/jcoleman/tomcat-redis-session-manager
2) This component does not currently support TOMCAT8, I am using Tomcat 7.0. Version 68
3) When placing objects into Redis in the program, The object must implement the Java.io.Serializable interface, otherwise it will be an error, if the object has references to other objects, the reference object also implements the Java.io.Serializable interface, so use Request.getsession (). SetAttribute () method, be sure to look at this detail
Using Tomcat+redis to implement session sharing issues in cluster deployment