Apache+tomcat Server Cluster configuration

Source: Internet
Author: User

In real-world applications, if the site has a large number of accesses, in order to improve access speed, can be integrated with multiple Tomcat servers and Apache server, let them run servlet/jsp components together, multiple Tomcat servers constitute a cluster (Cluster) system, Together to provide services to customers. The cluster system has the following advantages:

High Reliability (HA): The use of cluster management software, when the primary server failure, the backup server can automatically take over the work of the primary server, and timely switch to the past in order to achieve uninterrupted service to users.
High-performance Computing (HP): The full use of the resources of each computer in the cluster to achieve parallel processing of complex operations, often used in scientific computing fields, such as genetic analysis, chemical analysis and so on.
Load balancing: The load pressure is allocated to each computer in the cluster according to some algorithm, in order to reduce the pressure on the primary server and reduce the hardware and software requirements of the primary server.

Principle: The JK Plugin's load balancer is responsible for assigning workloads to the Tomcat servers in the clustered system, based on the lbfactor (Load Balancing factor) configured in Worker.properties, for load balancing. Each tomcat server communicates with the Cluster Manager (simpletcpcluster) for HTTP reply replication, such as session.

Below we configure an Apache and two Tomcat server clusters on a single machine:

2. Install Apache, install two Tomcat, and put a test project into the two Tomcat WebApps directory for later testing.

3. Copy the mod_jk.so to the <apache_home>/modules.

4. Create: Workers.properties file in the <apache_home>/conf directory:



"PLN" >worker "pun";. "PLN" >list "pun" >= "PLN" > Worker1 "pun", "PLN" >worker2 "pun", "PLN" >loadbalancer "com" > #apa Che regards Tomcat as a worker, and LoadBalancer is a load balancer.

Worker.worker1.host=localhost #Tomcat worker1 Server
worker.worker1.port=8009 #Tomcat端口
WORKER.WORKER1.TYPE=AJP13 #协议
worker.worker1.lbfactor=100 #负载平衡因数

Worker.worker2.host=localhost #Tomcat worker2 Server
worker.worker2.port=8009 #因为在一台机器上所以端口不能一样
WORKER.WORKER2.TYPE=AJP13 #协议
worker.worker2.lbfactor=100 #设为一样代表两台机器的负载相同

worker.loadbalancer.type=1b
Worker.loadbalancer.balanced_workers=worker1,worker2
Worker.loadbalancer.sticky_seesion=false
Worker.loadbalancer.sticky_session_force=false

Description: 1.worker.loadbalancer.sticky_seesion If set to TRUE indicates that the session has "sticky" That is, if a user establishes a session in a tomcat, then all of the user's operations are assumed by the Tomcat server. The cluster system does not replicate session. If set to False then the following sticky_session_force is meaningless.

2.sticky_session_force: Assuming Sticky_session is set to True, the user session is sticky and when the current Tomcat server stops the service, if the Sticky_session_ Force is true, that is, forcing the session to be associated with the current tomcat, the 500 error is reported, and if set to false, it goes to the other Tomcat server.

5. Modify the <apache_home>/conf/httpd.conf file and add it after the file:



"com" > #Tomcat集群配置
"com" >loadmodule jk_module modules/mod_jk.so
Jkworkersfile conf/workers.properties
#我的工人们
Jklogfile Logs/mod_jk.log
#日志文件
Jkloglevel Debug
#tomcat运行模式
Jkmount/*.jsp LoadBalancer
#收到. JSP end of file to the Load balancer processing
jkmount/helloapp/* LoadBalancer
#收到helloapp/path to load balancer processing

6. Modify the two Tomcat conf/service.xml files.

6.1 First to modify the AJP port, make sure they are the same as configured in the Workers.properties

For example, according to our above configuration, only need to put the AJP port in Tomcat2 to 8109.

6.2 Also after using LoadBalancer, the name of the worker is required to match the Jvmroute attribute of the engine element in Tomcat's Service.xml,

For example Worker1 modified to: <engine name= "Catalina" defaulthost= "localhost" jvmroute= "worker1" >

6.3 In addition, if two Tomcat servers are installed on a single machine, you must ensure that their ports are not in conflict and that there are three ports configured in Tomcat:

<server port= "8005" shutdown= "Shutdown" >

<connector port= "8080" .../>

<connector port= "8109" protocol= "ajp/1.3" redirectport= "8443"/>

Just put one of them on the line and make them different.

Complete the above steps our cluster is basically complete, open Apache and two Tomcat browser into: localhost/demo/can access correctly.

To test, we write a JSP file: test.jsp



"Tag" ><title>test</title>
<body>
<%
SYSTEM.OUT.PRINTFLN ("Call test.jsp");
%>
Session:<%=session.getid ()%>
</body>

Put it in two Tomcat in the demo project, the browser accesses this page, each access to only one Tomcat console print statements.

However, the SessionID in the page will change. In this case, if a user is being accessed, if you jump to another Tomcat server, then his session will be gone and may cause an error.

7. Configure the Cluster Manager

If the reader is aware of the httpsession, the user's session state is saved in a session, and a browser accesses multiple web pages whose requests are always in the range of sessions, so the SessionID should be constant.

The SessionID in the browser we see above is different because the current session ends when you go to another tomcat, and a new session is opened on another server. So how do you get multiple Tomcat servers to share a single session?

To address these issues, we enabled the Tomcat Cluster Manager (Simpletcpcluster):

7.1 Modify the Servlet.xml file for TOMCAT1 and TOMCAT2 and add the following cluster elements to the engine element



"Tag" ><cluster "PLN" > "ATN" >classname "pun" >= "ATV" > "Org.apache.catalina.ha.tcp.SimpleTcpCluster "
channelsendoptions= "8" >

<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"
Bind= "127.0.0.1"
Address= "228.0.0.4"
Port= "45564"
Frequency= "500"
Droptime= "/>"
<receiver classname= "Org.apache.catalina.tribes.transport.nio.NioReceiver"
address= "Auto"
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"/>

<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.JvmRouteSessionIDBinderListener"/>
<clusterlistener classname= "Org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>

Related to cluster reference: <tomcat-home>\webapps\docs\cluster-howto.html <tomcat-home>\webapps\docs\config\ Cluster.html

7.2 Modify the Web. xml file for the TOMCAT1 and Tomcat2demo projects, and add the <distributable> elements later



"Tag" ><web-app>
"PLN";
"Tag" ><distributable/>
</web-app>

If the <distributable/> element is specified in the Web. xml file of a website project, the Tomcat server starts the Web app and creates a session manager that is specified by the <Cluster> element for it. Here we use Deltamanager, who copy the session from one Tomcat server to another Tomcat server in the cluster.

7.3 Restart two Tomcat and find the Tomcat console or print out the SessionID in the call test.jsp page in turn, but not the same. The test is complete.

Important Note: (1). If the project is to be published to the cluster, the session-related classes need to implement the Java.io.Serializable serialization interface.

(2). In a cluster, the use of multicast communication between Tomcat, if there are multiple network cards on the machine may cause multicast failure, the solution is the <Cluster> element of the <Membership> element configuration bind attribute, it is used to explicitly know the multicast address:

<membershipclassname= "Org.apache.catalina.tribes.membership.McastService" bind= "127.0.0.1" .../>

(3). If the cluster is small, the Deltamanager session Manager can be used, if more than recommended to use Backupmanager

(4) The address of the .<membership> is set to "228.0.0.4", and the runtime must ensure that the machine has access to the network, otherwise it may fail to run.

Reprint please specify the original address: http://www.server110.com/apache/201404/9499.html

Apache+tomcat Server Cluster configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.