Implementation of Tomcat clusters and Server Load balancer (Session synchronization)

Source: Internet
Author: User

 

(1) Environment Description
(1) four servers, one with Apache installed, and three with Tomcat installed
(2) apache2.0.55, tomcat5.5.15, jk2.0.4, jdk1.5.6, or jdk1.4.2
(3) IP configuration. The IP address for installing Apache is 192.168.0.88, And the IP addresses of the three servers for installing Tomcat are 192.168.0.1/2/4, respectively.
(2) Installation Process
(1) install JDK on three servers to install Tomcat
(2) configure the JDK installation path, add the JDK bin path to the environment variable path, and create the environment variable java_home to point to the JDK installation path.
(3) install Tomcat on the three servers where Tomcat is to be installed, and debug the three Tomcat servers to be started properly.
(4) Tomcat's default Web Service port is 8080, and the default mode is separate service. The Web Service port of three of my tomcat servers is changed to 7080/8888/9999.
Change the location to CONF/server. XML in the tomcat installation directory.
The configuration before modification is
<Connector Port = "8080" maxhttpheadersize = "8192"
Maxthreads = "150" minsparethreads = "25" maxsparethreads = "75"
Enablelookups = "false" redirectport = "8443" acceptcount = "100"
Connectiontimeout = "20000" disableuploadtimeout = "true"/>
The modified configuration is
<Connector Port = "7080" maxhttpheadersize = "8192"
Maxthreads = "150" minsparethreads = "25" maxsparethreads = "75"
Enablelookups = "false" redirectport = "8443" acceptcount = "100"
Connectiontimeout = "20000" disableuploadtimeout = "true"/>
Modify the listening port (7080/8888/9999) of each Tomcat)

(5) test whether the startup of each Tomcat is normal.
Http: // 192.168.0.1: 7080
Http: // 192.168.0.2: 8888
Http: // 192.168.0.4: 9999
(3) Server Load balancer configuration process
(1) install apache2.0.55 on the server where Apache is to be installed. The default installation path is C:/program files/Apache Group/apache2.
(2) After installation, test whether Apache can be started normally. After debugging, the system can start http: // 192.168.0.88 normally.
(3) download jk2.0.4 and decompress the file
(4) copy the mod_jk2.so file in the modules directory in the decompressed directory to the modules directory under the Apache installation directory. My files are C:/program files/Apache Group/apache2/modules.
(5) modify the configuration file httpd. conf In the conf directory of the Apache installation directory, add the loadmodule module configuration information to the file, and add the loadmodule jk2_module modules/mod_jk2.so
(6) modify the configuration files CONF/server. xml of three Tomcat servers respectively. The modified content is as follows:
Before Modification
<! -- An engine represents the entry point (within Catalina) that processes
Every request. The engine implementation for Tomcat stand alone
Analyzes the HTTP headers encoded with the request, and passes them
On to the appropriate host (virtual host). -->

<! -- You shoshould set jvmroute to support load-balancing via AJP ie:
<Engine name = "standalone" defaulthost = "localhost" jvmroute = "jvm1">
-->

<! -- Define the top level container in our container hierarchy -->
<Engine name = "Catalina" defaulthost = "localhost">
After modification
<! -- An engine represents the entry point (within Catalina) that processes
Every request. The engine implementation for Tomcat stand alone
Analyzes the HTTP headers encoded with the request, and passes them
On to the appropriate host (virtual host). -->

<! -- You shoshould set jvmroute to support load-balancing via AJP ie: -->
<Engine name = "standalone" defaulthost = "localhost" jvmroute = "tomcat1">


<! -- Define the top level container in our container hierarchy
<Engine name = "Catalina" defaulthost = "localhost">
-->
Modify jvmroute = "jvm1" to jvmroute = "cmdat1" and jvmroute = "tomcat2" and jvmroute = "tomcat3" respectively"

(7) Restart three Tomcat servers, and the debugging will start properly.
(8) Create the workers2.propertie file under the conf directory in the Apache installation directory. The content of the written file is as follows:

# Fine the communication channel
[Channel. Socket: 192.168.0.1: 8009]
Info = ajp13 forwarding over socket
# Configure the first Server
Tomcatid = tomcat1 # It must be consistent with the jvmroute = "tomcat1" name in the tomcat configuration file server. xml.
DEBUG = 0
Lb_factor = 1 # load balancing factor. A larger number indicates a higher probability of request allocation.

# Define the communication channel
[Channel. Socket: 192.168.0.2: 8009]
Info = ajp13 forwarding over socket
Tomcatid = tomcat2
DEBUG = 0
Lb_factor = 1

# Define the communication channel
[Channel. Socket: 192.168.0.4: 8009]
Info = ajp13 forwarding over socket
Tomcatid = tomcat3
DEBUG = 0
Lb_factor = 1

[Status:]
Info = status worker, displays runtime information.

[URI:/jkstatus. jsp]
Info = display status information and checks the config file for changes.
Group = status:

[URI:/*]
Info = map the whole webapp
DEBUG = 0
(9) In the three tomcat installation directory of webapps to create the same application, I and the application directory named tomcatdemo, in the three application directory to create the same WEB-INF directory and page index. JSP, index. the content of the JSP page is as follows:
<% @ Page contenttype = "text/html; charset = GBK" %>
<% @ Page import = "Java. util. *" %>
<HTML> <Body>
Server info:
<%
Out. println (request. getlocaladdr () + ":" + request. getlocalport () + "<br>"); %>
<%
Out. println ("<br> ID" + session. GETID () + "<br> ");

// If a new session attribute is set
String dataname = request. getparameter ("dataname ");
If (dataname! = NULL & dataname. Length ()> 0 ){
String datavalue = request. getparameter ("datavalue ");
Session. setattribute (dataname, datavalue );
}

Out. Print ("<B> session List </B> ");

Enumeration E = session. getattributenames ();
While (E. hasmoreelements ()){
String name = (string) E. nextelement ();
String value = session. getattribute (name). tostring ();
Out. println (name + "=" + value + "<br> ");
System. Out. println (name + "=" + value );
}
%>
<Form action = "index. jsp" method = "Post">
Name: <input type = text size = 20 name = "dataname">
<Br>
Value: <input type = text size = 20 name = "datavalue">
<Br>
<Input type = submit>
</Form>
</Body>
</Html>
(10) restart the Apache server and three Tomcat servers. The Server Load balancer has been configured. Test Server Load balancer first test Apache and access http: // 192.168.0.88/jkstatus. jsp
Whether the service can be accessed normally and the content is queried. There are three tomcat configuration information and load descriptions, which can be run at http: // 192.168.0.88/atatdemo/index. jsp,
If the server Load balancer is enabled, a server Load balancer instance has been created.
(4) Tomcat cluster configuration
(1) configure the Tomcat cluster under the conditions of Server Load balancer Configuration
(2) modify the configuration files CONF/server. xml of three Tomcat servers respectively. The modified content is as follows:
Before Modification
<! --
<Cluster classname = "org. Apache. Catalina. Cluster. tcp. simpletcpcluster"
Managerclassname = "org. Apache. Catalina. Cluster. session. deltamanager"
Expiresessionsonshutdown = "false"
Usedirtyflag = "true"
Notifylistenersonreplication = "true">

<Membership
Classname = "org. Apache. Catalina. Cluster. MCAST. mcastservice"
Mcastaddr = "228.0.0.4"
Mcastport = "45564"
Mcastfrequency = "500"
Mcastdroptime = "3000"/>

<Cycler
Classname = "org. Apache. Catalina. Cluster. tcp. replicationlistener"
Tcplistenaddress = "Auto"
Tcplistenport = "4001"
Tcpselectortimeout = "100"
Tcpthreadcount = "6"/>

<Sender
Classname = "org. Apache. Catalina. Cluster. tcp. replicationtransmitter"
Replicationmode = "pooled"
Acktimeout = "5000"/>

<Valve classname = "org. Apache. Catalina. Cluster. tcp. replicationvalve"
Filter = ". */. GIF ;. */. JS ;. */. JPG ;. */. PNG ;. */. htm ;. */. HTML ;. */. CSS ;. */. txt; "/>

<Deployer classname = "org. Apache. Catalina. Cluster. Deploy. farmwardeployer"
Tempdir = "/tmp/war-temp /"
Deploydir = "/tmp/war-deploy /"
Watchdir = "/tmp/war-Listen /"
Watchenabled = "false"/>

<Clusterlistener classname = "org. Apache. Catalina. Cluster. session. clustersessionlistener"/>
</Cluster>
-->
After modification
<! -- Modify by whh -->
<Cluster classname = "org. Apache. Catalina. Cluster. tcp. simpletcpcluster"
Managerclassname = "org. Apache. Catalina. Cluster. session. deltamanager"
Expiresessionsonshutdown = "false"
Usedirtyflag = "true"
Notifylistenersonreplication = "true">

<Membership
Classname = "org. Apache. Catalina. Cluster. MCAST. mcastservice"
Mcastaddr = "228.0.0.4"
Mcastport = "45564"
Mcastfrequency = "500"
Mcastdroptime = "3000"/>

<Cycler
Classname = "org. Apache. Catalina. Cluster. tcp. replicationlistener"
Tcplistenaddress = "Auto"
Tcplistenport = "4001"
Tcpselectortimeout = "100"
Tcpthreadcount = "6"/>

<Sender
Classname = "org. Apache. Catalina. Cluster. tcp. replicationtransmitter"
Replicationmode = "pooled"
Acktimeout = "5000"/>

<Valve classname = "org. Apache. Catalina. Cluster. tcp. replicationvalve"
Filter = ". */. GIF ;. */. JS ;. */. JPG ;. */. PNG ;. */. htm ;. */. HTML ;. */. CSS ;. */. txt; "/>

<Deployer classname = "org. Apache. Catalina. Cluster. Deploy. farmwardeployer"
Tempdir = "/tmp/war-temp /"
Deploydir = "/tmp/war-deploy /"
Watchdir = "/tmp/war-Listen /"
Watchenabled = "false"/>

<Clusterlistener classname = "org. Apache. Catalina. Cluster. session. clustersessionlistener"/>
</Cluster>
<! -- Modify by whh -->
Just release the comments of the cluster configuration options, as shown above.
(3) Restart three Tomcat servers. The Tomcat cluster has been configured.

(5) application configuration
For the Web. xml file in the WEB-INF of the application in webapps under the Tomcat directory where you want to load and cluster, add the following configuration
<Distributable/>
Before Configuration
<? XML version = "1.0" encoding = "UTF-8"?>
<Web-app xmlns = "http://java.sun.com/xml/ns/j2ee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version = "2.4">
<Display-Name> tomcatdemo </display-Name>
</Web-app>
After configuration
<? XML version = "1.0" encoding = "UTF-8"?>
<Web-app xmlns = "http://java.sun.com/xml/ns/j2ee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version = "2.4">
<Display-Name> tomcatdemo </display-Name>
<Distributable/>
</Web-app>

Implementation of Tomcat clusters and Server Load balancer (Session synchronization) Supplement
Because the session synchronization function of Tomcat requires multicast, the multicast service is activated by default in Windows, but not in Linux by default, you can run the command to open route add-net 224.0.0.0 netmask 240.0.0.0 Dev eth0, to enable multicast when the server is started, add eht0 net 224.0.0.0 netmask 240.0.0.0 to the/etc/sysconfig/static-routes file. For more information about the concept of multicast, see ccnp.

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.