Connect a blog Nginx or httpd to achieve load Balancing Tomcat (iii)
Tomcat Realization of the principle and implementation of Session management:
The dedicated session management component used by the Tomcat management sessionhas 4 types of Session manager for Tomcat :
1. Standard Session Manager (Standerdmanager)
2. Persistent Session Manager (Persistentmanager can be based on file storage (Filestore) or JDBC storage (Jdbcstore) )
based on JDBC session cluster can be achieved with the high availability of Tomcat .
1. Deltamanager Session Manager
2. Backupmanager Session Manager
Standerdmanager the principle of Session management is :The Tomcat node hosts the session information of the client itself in the host file, which is saved by default in the $CATALINA _home/work/catalina/the sessions.ser file under >/<webapp-name>/ , when a tomcat instance fails, it can load session information from this file without causing the information to be lost.
advantages: To a certain extent to achieve session preservation,
Disadvantage: session sessions information can only be used by the current Tomcat instance and cannot be loaded by other Tomcat instances, so there is no high-availability feature.
Persistentmanager The persistent Session Manager principle isthat Tomcat can persist session information in a defined file or database, but this session management is still only able to load the session information of its own instance, Instead of being able to load session information from other instances, the Tomcat backup node when using the main standby mode can load the session information of this Tomcat instance from the shared file storage file or the database , enabling Tomcat to be highly available.
Cornsync+pacemaker+nfs(mysql) enables Tomcat to be highly available.
Deltamanager The Session Manager principle is :The Tomcat cluster uses Deltamanager to pass information through a dedicated multicast address in each node, and passes the multicast channel to each node's respective session to save it in memory , so each node has session information for each node in the cluster , and when one node is down, the user's request can be redirected to another node, thus enabling high-availability functionality. Is the way to implement the cluster of the session .
Advantage:session information is stored in each node with all the cluster nodes ' sessions, and the client's session information is not lost because of a problem with one node. The front end can use any scheduling algorithm to achieve load balancing.
disadvantage: Because to use multicast delivery, all this case the size of the cluster can not be too large, there is a number of restrictions, on the other hand, all the session information is also stored in memory, when a power outage will cause all node down is the machine, Restarting will still lose all session information. There are few scenarios for using large clusters.
3,Backupmanager Session Manager principle is : This kind of cluster needs two nodes each other to prepare the group,Backipmanager Manager will automatically manage to achieve the high availability of these two nodes, For the front-end scheduler, these two nodes are used as a node group, scheduler scheduling is dispatched to different groups, each set of internal implementation of the session of the high availability, so each group of external IP address within the group of two primary and standby nodes flow, This flow is achieved by this session manager. The front-end scheduler cannot use Nginxand can use httpd
Disadvantage: The deployment topology is difficult, so it is only occasionally used in small-scale clusters.
Configuration methods for the various session manager implementations:
Standard Session Manager (Standardmanager) :
<managerclassname= "Org.apache.catalina.session.StandardManager"
maxinactiveinterval= "7200"/>
The default is saved in the sessions.ser file under $CATALINA _home/work/catalina/.
Maxactivesessions: The maximum number of active sessions allowed, default is -1, which means no limit;
Maxinactiveinterval: Inactivity Session timeout length, default is 60s;
Pathname: Save directory for session files;
Persistent Session Manager (Persistentmanager) :
The session data is saved to the persistent storage and can be reloaded when the server is unexpectedly aborted and restarted. The persistent Session Manager supports saving a session to a file store (filestore) or JDBC store (jdbcstore) .
Examples saved to a file:
<managerclassname= "Org.apache.catalina.session.PersistentManager"
Saveonrestart= "true" >
<store classname= "Org.apache.catalina.session.FileStore"
directory= "/data/tomcat-sessions"/>
</Manager>
each user's session is saved to a file in a directory specified by directory, with the file name <session id>.session, and through the background thread every once in a while ( the Checkinterval parameter definition, which defaults to one second , checks a timeout session.
Save to examples in Jdbcstore:
<managerclassname= "Org.apache.catalina.session.PersistentManager"
Saveonrestart= "true" >
<storeclassname= "Org.apache.catalina.session.JDBCStore"
Drivername= "Com.mysql.jdbc.Driver"
Connectionurl= "JDBC:MYSQL://LOCALHOST:3306/MYDB?USER=JB;PASSWORD=PW"/>
</Manager>
whether it is a standard session management area or a persistent session manager, it can load the session information saved by its session, rather than loading other tomcat- saved session information, implementing the ability to load other The session information for the Tomcat host needs to use the session copy feature of Tomcat or use shared storage to keep all the tomcat hosts ' sessions.
Example Six: Set up a session cluster for the backend Tomcat server for sharing session.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/75/70/wKiom1Y47rWwHXdtAABomc438j8891.jpg "title=" 20.png "alt=" Wkiom1y47rwwhxdtaabomc438j8891.jpg "/>
Description: The front-end scheduler uses a session cluster that is based on the Http_proxy module reverse proxy back-end Tomcat .
The backend Tomcat host uses only the Deltamanager Session Manager.
The Load balancer configuration is based on the http_balance implementation above, using http-1.1 to communicate with back-end Tomcat .
The Session-cluster configuration is as follows:
Add the following configuration entry in the configuration file for each Tomcat node: (The following configuration can be defined within the Engin component or within the host component, mainly the difference in scope!) )
[Email protected] conf]# vim Server.xml
<clusterclassname= "Org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelsendoptions= "8" >
<managerclassname= "Org.apache.catalina.ha.session.DeltaManager"
Expiresessionsonshutdown= "false"
Notifylistenersonreplication= "true"/>
<channel classname= "Org.apache.catalina.tribes.group.GroupChannel" >
<membershipclassname= "Org.apache.catalina.tribes.membership.McastService"
Address= "228.0.1.9"
Port= "45564"
Frequency= "500"
Droptime= "/>"
<receiverclassname= "Org.apache.catalina.tribes.transport.nio.NioReceiver"
Address= "172.16.249.204"
port= "4000"
autobind= "100"
selectortimeout= "5000"
maxthreads= "6"/>
<senderclassname= "Org.apache.catalina.tribes.transport.ReplicationTransmitter" >
<transportclassname= "Org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<interceptorclassname= "Org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<interceptorclassname= "Org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
</Channel>
<valveclassname= "Org.apache.catalina.ha.tcp.ReplicationValve"
Filter= ""/>
<valveclassname= "Org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<deployerclassname= "Org.apache.catalina.ha.deploy.FarmWarDeployer"
Tempdir= "/tmp/war-temp/"
Deploydir= "/tmp/war-deploy/"
Watchdir= "/tmp/war-listen/"
Watchenabled= "false"/>
<clusterlistenerclassname= "Org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
then modify the Web-inf/web.xml file in the WebApp directory (you can also modify the default Web. XML file)
Added in Add
<distributable/>
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/75/6D/wKioL1Y47yjin1hMAAFYkqjBcMw792.jpg "title=" 1.png " alt= "Wkiol1y47yjin1hmaafykqjbcmw792.jpg"/>
Note : If you use the MOD_JK module, you need to add <engin name= "Catalina" jvmroute= "NOTE01" to the Tomcat configuration file ; The name of this Jvmroute is identical to the name of the JVM for this host .
The session cluster for Tomcat is actually a highly available feature.
Example Seven: using MSM (memcache-session-manager ) To implement Tomcat sessions are stored in session Server
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/75/70/wKiom1Y47wWSdubEAABrsPDVMfA278.jpg "title=" 2.png " alt= "Wkiom1y47wwsdubeaabrspdvmfa278.jpg"/>
first install the installation memcached service on two node hosts
[email protected] tomcat]# Yum install memcached
and both start the memcached service
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/75/70/wKiom1Y47yOg8QuWAANISPeQfhI191.jpg "style=" float: none; "title=" 3.png "alt=" Wkiom1y47yog8quwaanispeqfhi191.jpg "/>
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/75/6D/wKioL1Y472Hw299JAANeujXiorE699.jpg "style=" float: none; "title=" 4.png "alt=" Wkiol1y472hw299jaaneujxiore699.jpg "/>
Tomcat To configure the session service to use Memcached first requires the following packages to be installed:
Note: You will need to select the appropriate package version based on the version of Tomcat installed:
Javolution-5.4.3.1.jar
Memcached-session-manager-1.8.3.jar
Memcached-session-manager-tc8-1.8.3.jar
Msm-javolution-serializer-1.8.3.jar
Spymemcached-2.11.1.jar
copy these packages in Tomcat 's lib directory
then start configuring Tomcat
Modify The tomcat master configuration file server.xml file to add the following in the custom host component:
<context path= "WebApp" docbase= "/myweb/webapp/root" reloadable= "true" >
<managerclassname= "De.javakaffee.web.msm.MemcachedBackupSessionManager"
Memcachednodes= "n1:172.16.249.204:11211,n2:172.16.249.203:11211"
failovernodes= "N1"
Requesturiignorepattern= ". *\. (ICO|PNG|GIF|JPG|CSS|JS) $ "
transcoderfactoryclass= "De.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"/>
</Context>
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/75/70/wKiom1Y470KTGsudAAMXqYEwplA886.jpg "style=" float: none; "title=" 5.png "alt=" Wkiom1y470ktgsudaamxqyewpla886.jpg "/>
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/75/6D/wKioL1Y474CRdi6LAAMKJ7RWS-A028.jpg "style=" float: none; "title=" 6.png "alt=" Wkiol1y474crdi6laamkj7rws-a028.jpg "/>
The forward scheduler still uses nginx scheduling: The configuration file and the previous unchanged.
test: Input in the browser can be seen regardless of how the dispatch, the information returned is the information returned by the memcached node.
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/75/70/wKiom1Y4723xpm_iAAEcPhagALQ168.jpg "style=" float: none; "title=" 7.png "alt=" Wkiom1y4723xpm_iaaecphagalq168.jpg "/>
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/75/6D/wKioL1Y476rDT74eAAEEo-uR4Ug415.jpg "style=" float: none; "title=" 8.png "alt=" Wkiol1y476rdt74eaaeeo-ur4ug415.jpg "/>
This article is from the "Seven Stars" blog, please be sure to keep this source http://qikexing.blog.51cto.com/7948843/1709452
Tomcat implements session cluster and Tomcat+memcached shared session storage (IV)