Tomcat cluster and Session replication application introduction

  • 2020-04-01 01:08:23
  • OfStack

A configuration file:

 
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" 
channelSendOptions="6"> 
<Manager className="org.apache.catalina.ha.session.BackupManager" 
expireSessionsOnShutdown="false" 
notifyListenersOnReplication="true" 
mapSendOptions="6"/> 
<!-- 
<Manager className="org.apache.catalina.ha.session.DeltaManager" 
expireSessionsOnShutdown="false" 
notifyListenersOnReplication="true"/> 
--> 
<Channel className="org.apache.catalina.tribes.group.GroupChannel"> 
<Membership className="org.apache.catalina.tribes.membership.McastService" 
address="228.0.0.4" 
port="45564" 
frequency="500" 
dropTime="3000"/> 
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" 
address="auto" 
port="5000" 
selectorTimeout="100" 
maxThreads="6"/> 
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter"> 
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/> 
</Sender> 
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/> 
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/> 
<Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/> 
</Channel> 
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve" 
filter=".*.gif|.*.js|.*.jpeg|.*.jpg|.*.png|.*.htm|.*.html|.*.css|.*.txt"/> 
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer" 
tempDir="/tmp/war-temp/" 
deployDir="/tmp/war-deploy/" 
watchDir="/tmp/war-listen/" 
watchEnabled="false"/> 
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/> 
</Cluster> 

The explanation is as follows:
1, the Cluster
 
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" 
channelSendOptions="6"> 

1) Tomcat cluster main element, in which all details of the cluster can be configured
2) the className main cluster type, the current provided only org. Apache. Catalina. Ha. TCP. SimpleTcpCluste as implementation class
3) channelSendOptionssession way to send, the default value is 8, this identity to determine by SimpleTcpCluste how to send the message
4) channel. SEND_OPTIONS_SYNCHRONIZED_ACK = 0x0004
Channel. The SEND_OPTIONS_ASYNCHRONOUS = 0 x0008
Channel. The SEND_OPTIONS_USE_ACK = 0 x0002
If the message is sent using (ASYNCHRONOUS) plus (USE_ACK), the value should be 10(8+2) or 0x000B
If the message is sent using (SYNCHRONIZED_ACK) plus (USE_ACK), the value should be 6(4+2) or 0x0006
2, the Manager
 
<Manager className="org.apache.catalina.ha.session.BackupManager" 
expireSessionsOnShutdown="false" 
notifyListenersOnReplication="true" 
mapSendOptions="6"/> 
<!-- 
<Manager className="org.apache.catalina.ha.session.DeltaManager" 
expireSessionsOnShutdown="false" 
notifyListenersOnReplication="true"/> 
--> 

1) manage session replication between tomcat
2) the className the current implementation there are two classes: org. Apache. Catalina. Ha. The session. The DeltaManager and org., apache. Catalina. Ha. The session. The BackupManager
3) DeltaManager copies and sends Session data to all the nodes under the cluster, and this implementation class has proved to be very reliable and works very well. One limitation, however, is that the cluster has to have the same node types and the same applications deployed
4) expireSessionsOnShutdown when a web program is terminated, tomcat distributes the destroy command to each Session and notifies all Session listeners to execute. When a node under the cluster is stopped, if you want to destroy sessions under all nodes, set this to true and default to false
5) notifyListenersOnReplication if set to true, when the session attributes are copied and mobile session listener is notified
3, the Channel
 
<Channel className="org.apache.catalina.tribes.group.GroupChannel"> 

Channels are the main component of Apache Tribes, and channels manage a set of subcomponents that together form the communication framework between tomcat instances. In the tomcat cluster, DeltaManager calls the channel through the SimpleTcpCluster to deliver the information, while the BackupManager itself calls the channel and its child components to deliver the information. The ReplicatedContext also calls the channel to pass the context property.
4, the Membership
 
<Membership className="org.apache.catalina.tribes.membership.McastService" 
address="228.0.0.4" 
port="45564" 
frequency="500" 
dropTime="3000"/> 

The MemberShip component automatically retrieves new nodes in the discovery cluster or nodes that have stopped working and issues corresponding notifications. The default implementation is Multicast.
5, the Receiver
 
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" 
address="auto" 
port="5000" 
selectorTimeout="100" 
maxThreads="6"/> 

Listen for incoming data from other nodes. Non-blocking TCP Server sockets are used by default.
6, the Sender
 
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter"> 
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/> 
</Sender> 

Manages outbound connections and data information sent from one node to another, allowing information to be sent in parallel. TCP Client Sockets are used by default.
7, Interceptor
 
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/> 
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/> 
<Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/> 

A Channel delivers messages through an Interceptor stack, where you can customize how messages are sent and received, and even how MemberShip is handled.
8, the Value
 
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve" 
filter=".*.gif|.*.js|.*.jpeg|.*.jpg|.*.png|.*.htm|.*.html|.*.css|.*.txt"/> 

1) Value plays the role of an interceptor in invoking the Http Request chain to determine when data needs to be copied.
Org. Apache. Catalina. Ha. TCP. ReplicationValve, ReplicationValue at the end of the Http Request to determine whether the current data need to be copied.
2) the content of the Filter is the url or the end of the file. When accessing the link to configure the Filter, no matter whether the actual session has changed or not, the cluster will consider that the session has not changed, so it will not copy and send the changed session attribute.
9, Deployer
 
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer" 
tempDir="/tmp/war-temp/" 
deployDir="/tmp/war-deploy/" 
watchDir="/tmp/war-listen/" 
watchEnabled="false"/> 

To support cluster farmed deployment
10, ClusterListener
 
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/> 

The Clusterlistener is used to track messages sent and received.
The ClusterSessionListener is used to listen to the cluster component receiving information. When DeltaManager is used, the information is received by the cluster and passed to the Session Manager through the ClusterSessionListener.


Related articles: