Apache2.2+Tomcat6配置集群、负载均衡、session
终于搞定的;
本文参照http://lxiaodao.iteye.com/blog/321000来修改的.
详细的配置过程,我把配置过程中几个不同和关键的地方重点提出来。
软件版本有所不同,罗列如下:
Apache? :apache_2.2.4-win32-x86-no_ssl.msi 1个
Tomcat:? apache-tomcat-6.0.29-windows-x86.zip ? 2个
mod_jk: mod_jk-1.2.26-httpd-2.2.4.so 1个?
? 我在一台机器(xp)上进行测试。
把apache和tomcat安装好以后,配置过程中有如下一些地方改动或者改进。
在mod_jk-1.2.26-httpd-2.2.4.so复制到Module目录下
在D:\Apache2.2\conf\httpd.conf增加
# JK module settings Include conf/mod_jk.conf
?
?
?
?在conf目录新增mod_jk.conf ,内容如下
# Load JK Module LoadModule jk_module modules/mod_jk-1.2.26-httpd-2.2.4.so # Configure mod_jk JkWorkersFile conf/workers.properties JkMountFile conf/uriworkermap.properties JkShmFile logs/mod_jk.shm JkLogFile logs/mod_jk.log JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories JkLogLevel warn JkLogStampFormat "[%a %b %d %H:%M:%S %Y]" HostnameLookups Off <Location /jkmanager/> JkMount jkstatus Order deny,allow Deny from all Allow from 127.0.0.1 </Location>
?
?
?
?分别在Module目录下:workers.properties文件
# environment slash for Windows ps=\ worker.retries=2 worker.list=loadbalancer,jkstatus worker.s1.type=ajp13 worker.s1.host=localhost worker.s1.port=11009 worker.s1.lbfactor=1 worker.s1.connection_pool_timeout=600 worker.s1.socket_keepalive=true worker.s1.socket_timeout=60 worker.s2.type=ajp13 worker.s2.host=localhost worker.s2.port=12009 worker.s2.lbfactor=1 worker.s2.connection_pool_timeout=600 worker.s2.socket_keepalive=true worker.s2.socket_timeout=60 worker.jkstatus.type=status worker.loadbalancer.type=lb worker.loadbalancer.balance_workers=s1,s2 worker.loadbalancer.sticky_session=true
?
?
?
?uriworkermap.properties文件
/*.jsp=loadbalancer /*.action=loadbalancer /jkmanager/*=jkstatus !*.gif=loadbalancer !*.jpg=loadbalancer !*.png=loadbalancer !*.bmp=loadbalancer !*.css=loadbalancer !*.js=loadbalancer !*.htm=loadbalancer !*.html=loadbalancer !*.swf=loadbalancer
?
?
?
?然后配置tomcat6.0修改server.xml
?
<Server port="12005" shutdown="SHUTDOWN">
? <!--APR library loader. Documentation at /docs/apr.html -->
? <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
? <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
? <Listener className="org.apache.catalina.core.JasperListener" />
? <!-- Prevent memory leaks due to use of particular java/javax APIs-->
? <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
? <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->
? <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
? <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
? <!-- Global JNDI resources
?????? Documentation at /docs/jndi-resources-howto.html
? -->
? <GlobalNamingResources>
??? <!-- Editable user database that can also be used by
???????? UserDatabaseRealm to authenticate users
??? -->
??? <Resource name="UserDatabase" auth="Container"
????????????? type="org.apache.catalina.UserDatabase"
????????????? description="User database that can be updated and saved"
????????????? factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
????????????? pathname="conf/tomcat-users.xml" />
? </GlobalNamingResources>
? <!-- A "Service" is a collection of one or more "Connectors" that share
?????? a single "Container" Note:? A "Service" is not itself a "Container",
?????? so you may not define subcomponents such as "Valves" at this level.
?????? Documentation at /docs/config/service.html
?? -->
? <Service name="Catalina">
?
??? <!--The connectors can use a shared executor, you can define one or more named thread pools-->
??? <!--
??? <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
??????? maxThreads="150" minSpareThreads="4"/>
??? -->
???
???
??? <!-- A "Connector" represents an endpoint by which requests are received
???????? and responses are returned. Documentation at :
???????? Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
???????? Java AJP? Connector: /docs/config/ajp.html
???????? APR (HTTP/AJP) Connector: /docs/apr.html
???????? Define a non-SSL HTTP/1.1 Connector on port 8080
??? -->
??? <Connector port="12080" protocol="HTTP/1.1"
?????????????? connectionTimeout="20000"
?????????????? redirectPort="8443" />
??? <!-- A "Connector" using the shared thread pool-->
??? <!--
??? <Connector executor="tomcatThreadPool"
?????????????? port="8080" protocol="HTTP/1.1"
?????????????? connectionTimeout="20000"
?????????????? redirectPort="8443" />
??? -->??????????
??? <!-- Define a SSL HTTP/1.1 Connector on port 8443
???????? This connector uses the JSSE configuration, when using APR, the
???????? connector should be using the OpenSSL style configuration
???????? described in the APR documentation -->
??? <!--
??? <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
?????????????? maxThreads="150" scheme="https" secure="true"
?????????????? clientAuth="false" sslProtocol="TLS" />
??? -->
??? <!-- Define an AJP 1.3 Connector on port 8009 -->
??? <Connector port="12009"? enableLookups="false" protocol="AJP/1.3" redirectPort="8443"?? />
??? <!-- An Engine represents the entry point (within Catalina) that processes
???????? every request.? The Engine implementation for Tomcat stand alone
???????? analyzes the HTTP headers included with the request, and passes them
???????? on to the appropriate Host (virtual host).
???????? Documentation at /docs/config/engine.html -->
??? <!-- You should set jvmRoute to support load-balancing via AJP ie :
??? <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">????????
??? -->
??? <Engine name="Catalina" defaultHost="localhost" jvmRoute="s2" >
????? <!--For clustering, please take a look at documentation at:
????????? /docs/cluster-howto.html? (simple how to)
????????? /docs/config/cluster.html (reference documentation) -->
?????
?? <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"
????????????????????? bind="127.0.0.1"
????????????????????? address="228.0.0.4"
????????????????????? port="45564"
????????????????????? frequency="500"
????????????????????? dropTime="3000"/>
????????? <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
??????????????????? address="127.0.0.1"
??????????????????? port="4001"
??????????????????? 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;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>
??????? <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
????? </Cluster>
?
?
?
?
?那红色部分的端口,两个tomcat必须不一样,前三个端口只要不冲突就可以了,而Receiver? port="4001"? 必须设置在4000-4010之间..网上别人说的,具体我也没做测试;
因在前面workers.properties文件定义s1,s2,所以两个server.xml分别定义为jvmRoute="s2",jvmRoute="s1" ,端口等;
然后在tomcat6.0目录下context.xml文件 修改19行为<Context distributable="true">
配置OK之后,而我测试时用的test2.jsp就直接放到jp工程下test目录下 <%@ page contentType="text/html; charset=GBK" %> <%@ page import="java.util.*" %> <html><head><title>Cluster App Test</title></head> <body> Server Info: <% out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%> <% out.println("<br> ID " + session.getId()+"<br>"); // 如果有新的 Session 属性设置 String dataName = request.getParameter("dataName"); if (dataName != null && dataName.length() > 0) { String dataValue = request.getParameter("dataValue"); session.setAttribute(dataName, dataValue); } out.println("<b>Session 列表</b><br>"); System.out.println("============================"); 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="test2.jsp" method="POST"> 名称:<input type=text size=20 name="dataName"> <br> 值:<input type=text size=20 name="dataValue"> <br> <input type=submit> </form> </body> </html>
?然后分别启动apache,tomcat1,tomcat2;在http://localhost/jp/test/test2.jsp设值为222,v222;
??
然后在加上tomcat的端口去访问,所得值一样的.
后在加上tomcat的端口去访问,所得值一样的.
?
?
从上面测试可以看到session复制是正确的..