The last time I talked about Alibaba Cloud Linux Nginx integrates Tomcat to realize load balancing cluster just simply realizes load balancing and does not realize session sharing.
1. Description
Our system often saves user login information. There is a COOKIE and Session mechanism. The COOKIE client stores user information, and the Session is in The server saves user information. If the browser does not support COOKIE or the user disables the COOKIE, the COOKIE will not be used. There are also different browsers that save the COOKIE in different ways, so we use the Session server to save it. In the previous section we We have introduced the deployment of Tomcat cluster. How can Tomcat in the cluster obtain the user information stored in the Session for the same user request, and use Memcached to manage the Session. Memcached is a high-performance distributed memory object caching system. Next we Introduce Nginx+Tomcat+Memcached to realize Session sharing.
two. Tomcat, Nginx, Memcached configuration
The first step: Memcached installation and deployment Memcached deployment Check out this article
Step 2: Installation and deployment of Nginx Check out this article
Step 3: Tomcat and JDK installation and environment configuration Tomcat and JDK installation and deployment Check out this article
three. achieve
We use Memcached to manage sessions, mainly because the memcached-session-manager open source tomcat plug-in changes Tomcat’s original session storage mechanism, and puts session storage in Memcached, a distributed cache, so as to realize session sharing.
The first step: Modify the conf/context.xml files under tomcat1 and tomcat2, and modify the session storage method.
-
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:localhost:12000” sessiOnBackupAsync=”false” sessiOnBackupTimeout=”100″ transcoderFactoryClass=”de .javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory” copyCollectiOnsForSerialization=”false” requestUriIgnorePattern=”.*\.(png|gif|jpg|css|js)$”/>
Description:
1. requestUriIgnorePatter: Filter static files such as pictures to trigger Session backup to Memcached.
2.sessionBackupAsync: Specifies whether the Session should be saved to Memcached asynchronously.
3.backupThreadCount: The number of threads used to asynchronously save the Session.
4.sessionBackupTimeout: The default is 100 milliseconds. If the operation exceeds the time, the save will fail.
Step 2: To introduce the jar package required by memcached-session-manager
couchbase-client-1.0.3.jar
javolution-5.4.3.1.jar
memcached-2.6.jar
memcached-session-manager-1.5.1.jar
memcached-session-manager-tc7-1.5.1.jar
msm-javolution-serializer-1.5.1.jar
msm-kryo-serializer-1.5.1.jar
msm-xstream-serializer-1.5.1.jar
JAR package download address: http://pan.baidu.com/s/1nuf5l7z
Restart tomcat and Nginx