Tomcat Cluster in the Cloud
Join the DZone community and get the full member experience.
Join For Freejelastic paas was specially designed for huge clustered application hosting. as we already demonstrated in our previous posts, jelastic provides session replication between pairs of server nodes with a the help of multicast and load balancer, which redirects requests to each server. such an approach guarantees session exchange between the nodes through the local net and eliminates the need to use additional software or memcached. as a result, session data is copied between servers providing high reliability, scalability and perfect fail-over capabilities.
today we will show you how to take a deeper look into clustering technology used in jelastic by the example of the tomcat application server.
on the above scheme you can see a tomcat cluster which consists of two servers and one load balancer, which handles all requests and distributes them to various nodes, according to availability and server load.
in case when one of the servers fails, the users, who were on that node get automatically switched to the other instance of this tomcat cluster. thanks to the replication, the other instance already has all the sessions of the failed node, so end-users never notice any change. also you can create multiple tomcat clusters in jelastic, so that in the highly unlikely event of whole cluster failing, users get redirected to another cluster.
to get a highly available tomcat cluster in jelastic, all you need is just click the create environment button, select tomcat 6 or 7 as the application server you want to use and switch on high availability for it.
note: don’t confuse horizontal scaling and ha mode in jelastic. in the first case you have multiple servers and the load is distributed evenly between the chosen number of instances with the help of load balancer. switching on of high availability mode sets replication between pair or few pairs of servers using multicast membership.
now let’s dive into the technology to see what happens when you switch on the ha button and how the tomcat cluster works on jelastic.
when you click ha a special tomcat config file is generated for each node by our system, which is called jelastic-ha.xml . here is an example:
<cluster classname="org.apache.catalina.ha.tcp.simpletcpcluster" channelsendoptions="4"> <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="${magicport}" frequency="500" droptime="3000"/> <receiver classname="org.apache.catalina.tribes.transport.nio.nioreceiver" address="${receiverip}" port="4000" autobind="100" selectortimeout="5000" 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"/> </channel> <valve classname="org.apache.catalina.ha.tcp.replicationvalve" filter=""/> <valve classname="org.apache.catalina.ha.session.jvmroutebindervalve"/> <clusterlistener classname="org.apache.catalina.ha.session.jvmroutesessionidbinderlistener"/> <clusterlistener classname="org.apache.catalina.ha.session.clustersessionlistener"/> </cluster>
let’s look at the details of this file:
1. here is the major element, inside which all the other cluster elements are configured.
<cluster classname="org.apache.catalina.ha.tcp.simpletcpcluster" channelsendoptions="4">
the channelsendoptions flag is attached to every message sent by simpletcpcluster class or any objects that are calling the simpletcpcluster.send method.
2. the deltamanager uses the simpletcpcluster.send method to send information though the channel.
<manager classname="org.apache.catalina.ha.session.deltamanager" expiresessionsonshutdown="false" notifylistenersonreplication="true"/>
3. the group communication framework used inside tomcat called tribes , is used as the channel element here. it encapsulates everything that has to do with membership, logic and communication.
<channel classname="org.apache.catalina.tribes.group.groupchannel">
4.
membership is made using
multicast
. tomcat cluster separation consists of a multicast address and port number. communication between nodes is realized over tcp.
<membership classname="org.apache.catalina.tribes.membership.mcastservice" address="228.0.0.4" port="${magicport}" frequency="500" droptime="3000"/>
{magicport} is a unique port number for the cluster, which is generatedon the fly from the java arguments.
5. tribes’ logic of sending and receiving data includes two components: sender and receiver. the receiver is responsible for data receiving. there is a thread pool in this element which has a maxthreads and minthreads setting. the address attribute is the host address that will be broadcasted by the membership component to the other nodes.
<receiver classname="org.apache.catalina.tribes.transport.nio.nioreceiver" address="${receiverip}" port="4000" autobind="100" selectortimeout="5000" maxthreads="6"/>
6. the sender component is responsible for sending messages to other nodes. the sender includes the replicationtransmitter , a special shell component, and transport sub component. messages can be sent concurrently with the nio sender and parallel with pool of senders.
<sender classname="org.apache.catalina.tribes.transport.replicationtransmitter"> <transport classname="org.apache.catalina.tribes.transport.nio.pooledparallelsender"/> </sender>
7. the elements of the tribes stack interceptors are:
- tcpfailuredetector – verifies crashed members via tcp
- messagedispatch15interceptor – dispatches messages to a thread pool to send message asynchronously
<interceptor classname="org.apache.catalina.tribes.group.interceptors.tcpfailuredetector"/> <interceptor classname="org.apache.catalina.tribes.group.interceptors.messagedispatch15interceptor"/> </channel>
8. the cluster uses valves to track requests to web applications:
- replicationvalve finds out when the request has been completed and initiates the replication
- jvmroutebindervalve is responsible for backing up your data
<valve classname="org.apache.catalina.ha.tcp.replicationvalve" filter=""/> <valve classname="org.apache.catalina.ha.session.jvmroutebindervalve"/>
9.
the
simpletcpcluster
is a sender and receiver of the channel object, so components are registered as listeners to this cluster.
<clusterlistener classname="org.apache.catalina.ha.session.jvmroutesessionidbinderlistener"/> <clusterlistener classname="org.apache.catalina.ha.session.clustersessionlistener"/>
as you can see, setting up high availability manually is possible, but is quite involved and needs additional research and knowledge. we automated the configuration for you and now you can set up high availability for every java application server that we support. from every one here at jelastic – enjoy!
Published at DZone with permission of Marina Sprava, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Integrating AWS With Salesforce Using Terraform
-
Structured Logging
-
Introduction To Git
-
Authorization: Get It Done Right, Get It Done Early
Comments