There are actually three connection methods for the apache server and tomcat: JK, http_proxy, and ajp_proxy.
This article mainly introduces the most common JK.
Based on CentOS 5.5 linux:
1. Install Tomcat 7 and apache2.2.22 servers and JK corresponding to the version
Note that the JK version must correspond to the apache Server version. Apache download
The corresponding linux compiled jk version can be downloaded using mod_jk.so.
Note: If your installed apache version is 2.0.63 or 2.0.X, choose mod_jk-1.2.26-httpd-2.0.61.so.
If it is the latest version 2.2.22, you can choose the most use mod_jk-1.2.31-httpd-2.2.x.so
In short, I tried so myself when I was not successful. At first, the machine was installed with an old version of apache 2.0.63, which made people crash.
Download the corresponding so file to the modules directory under apache.
Apache_home indicates the installation directory of the apache server.
2. Configure the apache server
2.1 httpd. conf file
Add complex configurations to the conf/httpd. conf file in the apache_home directory.
LoadModule jk_module modules/mod_jk-1.2.31-httpd-2.2.x.so
JkWorkersFile conf/workers. properties
JkMountFile conf/uriworkermap. properties # It is unnecessary to load the workers request and process the allocated file.
JkLogFile logs/mod_jk.log # you do not need to specify the log output file of jk.
JkLogLevel warn # The specified log level is optional
JkMount/* lbcontroller # you can configure multiple apache distributors as needed./* indicates that apache distributes all files by lbcontroller. You can set it by yourself *. jsp ,*. do and so on
The uriworkermap. properties file contains the following content:
/* = Lbcontroller # all requests are processed by the controller server.
/Jkstatus = status # all requests containing jkstatus are processed by the status server.
! /*. Gif = lbcontroller all requests ending with .gif cannot be processed by the controller server. The following are the same meanings:
! /*. Jpg = lbcontroller
! /*. Png = lbcontroller
! /*. Css = lbcontroller
! /*. Js = lbcontroller
! /*. Htm = lbcontroller
! /*. Html = lbcontroller
Simple configuration: add "/usr/local/apache/conf/mod. conf" to the apache file"
Create mod. conf in the/usr/local/apache/conf/directory:
LoadModule jk_module modules/mod_jk-1.2.31-httpd-2.2.x.so
JkWorkersFile conf/workers. properties
# JkMount/*. jsp lbcontroller
# JkMount/*. do lbcontroller
JkMount/* lbcontroller # you can configure multiple apache distributors as needed./* indicates that apache distributes all files by lbcontroller. You can set it by yourself *. jsp ,*. do and so on
2.2 workers. properties file
Create the workers. properties file in the conf directory under apache_home, and enter the following content:
Xml code
Worker. list = lbcontroller
# Tomcat1 instance configuration
Worker. tomcat1.host = localhost # this can be your IP address
Worker. tomcat1.port = 8009
Worker. tomcat1.type = ajp13
# Greater distribution weight, greater load
Worker. tomcat1.lbfactor = 1
# Tomcat2 instance configuration
Worker. tomcat2.host = localhost # this can be your IP address
Worker. tomcat2.port = 9009
Worker. tomcat2.type = ajp13
# Greater distribution weight, greater load
Worker. tomcat2.lbfactor = 1
# Server load balancer distribution controller
Worker. lbcontroller. type = lb
Worker. lbcontroller. balance_workers = worker AT1, tomcat2
Worker. lbcontroller. sticky_session = True
Here
Worker. list = lbcontroller
The name of the distributor in the httpd. conf file. Note that the tomcat instance name
Xml code
Worker. tomcat1.host = ....
...
Worker. tomcat2.host = ....
...
Here, the values of Route 1 and tomcat 2 must be consistent with those of jvmRoute in server. xml in tomcat.
<Pre>
For more work. properties, refer:
<Pre lang = "java" line = "1">
Worker. list = loadbalancer # defines the name of the server load balancer instance.
Worker. loadbalancer. type = lb # defines the load balancing type, which is lb
Worker. loadbalancer. error_escalation_time = 0 # time of switching error status
Worker. loadbalancer. max_reply_timeouts = 10
Worker. loadbalancer. sticky_session = 1 #1 indicates true, and 0 indicates false.
# Worker. loadbalancer. sticky_session_force = true
Worker. loadbalancer. balance_workers = lb1, lb2 # Name of the server load balancer worker
Worker. lb1.reference = worker. template # lb1 refer to worker. template work template
Worker. lb1.host = localhost # IP address of lb1
Worker. lb1.port = 8009 # lb1 port, which is the ajp port, not the website Port
Worker. lb1.activation = A # A: active mode D: sticky Mode S: not used
Worker. lb2.reference = worker. template
Worker. lb2.host = localhost
Worker. lb2.port = 9009
Worker. lb2.activation =
Worker. template. type = ajp13 # above lb1, lb2 refer to the following value worker. template, protocol ajp13
Worker. template. socket_connect_timeout = 5000 # The socket connection timeout value is 5000 milliseconds. The default value is 0.
Worker. template. socket_keepalive = true # enable to keep true. The default value is false.
Worker. template. ping_mode = A # Use the cping/cpong mode, which has four modes: C, P, I, and. Select all
Worker. template. ping_timeout = 10000 # default 10000 milliseconds for detecting cping/cpong supertime
Worker. template. connection_pool_minsize = 0 # minimum time for closing the connection pool
Worker. template. connection_pool_timeout = 600 # connection pool 600 seconds
Worker. template. reply_timeout = 300000 # Reply timeout 300000 milliseconds
Worker. template. recovery_options = 3 # Number of retries
3. tomcat cluster configuration
Configure the server. xml file under each tomcat Directory. The specific modification content is as follows:
3.1 Engine configuration
Before modification
Xml code
<Engine name = "Catalina" defaultHost = "localhost">
After modification
Xml code
<Engine name = "Catalina" defaultHost = "localhost" jvmRoute = "tomcat1">
The value of jvmRoute in tomcat depends on the configuration in workers. properties. Note that the IP address, tomcat instance name, and jvmRoute value must correspond one to one.
3.2 Connector configuration
Before modification
Xml code
<Connector port = "8009" protocol = "AJP/1.3" redirectPort = "8443"/>
After modification
Xml code
<Connector port = "9009" protocol = "AJP/1.3" redirectPort = "8443"/>
Here, the tomcat port value must be consistent with the port value in workers. properties.
3.3 Cluster configuration
Before modification
Xml code
<! --
<Cluster className = "org. apache. catalina. ha. tcp. SimpleTcpCluster">
-->
After modification, remove the comment and add the following content:
Xml code
<Cluster className = "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"
Address = "228.0.0.4"
Port = "45564"
Frequency = "500"
DropTime = "3000" type = "codeph" text = "codeph"/>
<Cycler className = "org. apache. catalina. tribes. transport. nio. NioReceiver"
Address = "auto"
Port = "4001"
AutoBind = "100"
SelectorTimeout = "5000"
MaxThreads = "6"/>
<! -- Timeout = "60000" -->
<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 = ""/>
<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>
The above configuration is removed from the configuration officially recommended by tomcat.
Xml code
<Deployer className = "org. apache. catalina. ha. deploy. FarmWarDeployer"
TempDir = "/tmp/war-temp /"
DeployDir = "/tmp/war-deploy /"
WatchDir = "/tmp/war-listen /"
WatchEnabled = "false"/>
If this part is not removed, tomcat startup reports "FarmWarDeployer can only work as host cluster subelement !. "
3.4 session replication configuration
Session management in a tomcat cluster can be implemented in two ways:
1). Sticky session
Indicates that all requests sent from the same window will be processed by the same tomcat in the cluster. The configuration method is in the preceding workers. properties file.
Xml code
Worker. lbcontroller. sticky_session = True
The advantage of a sticky session is that it does not throw requests on different tomcat servers, but the disadvantage is that if the tomcat server that processes the session crashes, subsequent requests will be processed by other tomcat servers, if the original session becomes invalid, a new session is created again. If the value of the session continues, the nullpointer access exception is thrown.
2). session replication
Session replication means that tomcat sends sessions to each tomcat instance through multicast. If an access error occurs, tomcat still has valid session content, in this way, the session can be taken over normally. The disadvantage is that when many tomcat instances or users have a large number of operations in the session, the amount of information sent by multicast is amazing. The session replication configuration is added to the web. xml file of the published web application.
Xml code
In addition, JDK 5.0 and later are required for session replication.
4. Test
1) start the apache server httpd first.
2) start the tomcat7 server called tomcat2 here, the new webapps function, that is, the test contains the WEB-INF/classes; otherwise, the startup reports an error;
Create a session. jsp file:
<% @ Page language = "java" contentType = "text/html; charset = ISO-8859-1"
PageEncoding = "ISO-8859-1" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = ISO-8859-1">
<Title> session-test </title>
</Head>
<Body>
<%
String s = session. getId ();
Out. println ("session id =" + s );
System. out. println ("session id =" + s );
// Log loger = LogFactory. getLog ("nice ");
// Loger.info ("session id =" + s );
%>
</Body>
</Html>
Enter the access address http: // localhost/test/session. jsp in the browser to view the output.
The request is forwarded to Tomcat 2 for processing.
3) start tomcat 1
You can find that tomcat2 instance is searched and session replication is performed after tomcat1 is started. You can also find the following output in the tomcat2 console:
4) disable tomcat 2 and refresh the url. The session output in the tomcat1 console is still consistent with that in the previous tomcat2, and the property values saved in the session are still valid.