Using Redis to share the Tomcat multi-node session requires the installation of Redis, Nginx, Tomcat6 and the jar package for the Tomcat storage session to Redis: Tomcat-redis-session-manager-1.2-tomcat-6.jar,
Commons-pool-1.6.jar, Jedis-2.1.0.jar and so on. These jar packages, configuration files and demo programs can be downloaded in the final demo address.
Here is the configuration and operation of Tomcat and Nginx:
1. Copy the jar package from the demo package to the Tomcat Lib directory :
2. Configure the Tomcat context.xml file:
<valve classname= "Com.radiadesign.catalina.session.RedisSessionHandlerValve"/>; <manager classname= "Com.radiadesign.catalina.session.RedisSessionManager" host= "localhost" port= " 6379 " database=" 0 "
3. Configure Tomcat's Server.xml:
For different Tomcat, the following ports are configured with different values such as: Tomcat1:<server port= "8007" shutdown= "shutdown" ><connector port= "18080" Protocol= "Org.apache.coyote.http11.Http11Protocol" connectiontimeout= "20000" redirectport= "8444"/> <connector port= "8019" protocol= "ajp/1.3" redirectport= "8444"/><engine name= "Catalina" defaultHost= " localhost "jvmroute=" tomcat1 ">tomcat2:<server port=" 8008 "shutdown=" shutdown "><connector port=" 18081 " Protocol= "Org.apache.coyote.http11.Http11Protocol" connectiontimeout= "20000" redirectport= "8445"/> <connector port= "8029" protocol= "ajp/1.3" redirectport= "8445"/> <engine name= "Catalina" defaulthost= "localhost" jvmroute= "TOMCAT2" >
4. Configure the Nginx cluster:
Configure the nginx.conf: Upstream tomcat { server 127.0.0.1:18080; Server 127.0.0.1:18081; } server { listen 8080; server_name 127.0.0.1; Location/{ proxy_redirect off; Proxy_set_header Host $host; Proxy_set_header x-real-ip $remote _addr; Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for; Client_max_body_size 10m; Client_body_buffer_size 128k; Proxy_buffers 4k; Proxy_connect_timeout 3; Proxy_send_timeout ; Proxy_read_timeout ; Proxy_pass http://tomcat; } }
5. Put the Demo.war program into the TOMCATDE WebApps:
Modify the contents of the index.jsp in the demo, different Tomcat, the content is not the same. As follows:
Demo:tomcat2:<%=session.getid ()%> in Demo:tomcat1:<%=session.getid ()%> tomcat2 in TOMCAT1
6. Start Nginx,redis, TOMCAT1, TOMCAT2:
Open Browser, enter address: Http://localhost:8081/demo
7. Press F5 to refresh the page:
We can see the page, no matter how we refresh it, only the previous Tomcat's logo has changed, and the back SessionID is fixed as follows:
TOMCAT1:8EA77B42358EFC1CD1F5FBC7246A1317.TOMCAT1 TOMCAT2:8EA77B42358EFC1CD1F5FBC7246A1317.TOMCAT1
If the above results show that the shared session was successful, if you have any problems in the process of trying, you can leave me a message.
8.demo:
TOMCAT6 Multi-node sharing session with Redis under Nginx load
Using Redis to solve multi-node sharing session problem with TOMCAT6 under nginx load