Apache2.2+Tomcat6配置集群、负载均衡、session

终于搞定的;

详细的配置过程,我把配置过程中几个不同和关键的地方重点提出来。

软件版本有所不同,罗列如下:

Apache:apache_2.2.4-win32-x86-no_ssl.msi1个

Tomcat:apache-tomcat-6.0.29-windows-x86.zip2个

mod_jk:mod_jk-1.2.26-httpd-2.2.4.so1个

我在一台机器(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 -->

<Listenerclassname="org.apache.catalina.core.AprLifecycleListener"SSLEngine="on"/>

<!--InitializeJasperpriortowebappsareloaded.Documentationat/docs/jasper-howto.html-->

<Listenerclassname="org.apache.catalina.core.JasperListener"/>

<!--Preventmemoryleaksduetouseofparticularjava/javaxAPIs-->

<Listenerclassname="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>

<!--JMXSupportfortheTomcatserver.Documentationat/docs/non-existent.html-->

<Listenerclassname="org.apache.catalina.mbeans.ServerLifecycleListener"/>

  <Listener classname="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />

  <!-- Global JNDI resources

Documentationat/docs/jndi-resources-howto.html

-->

<GlobalNamingResources>

<!--Editableuserdatabasethatcanalsobeusedby

UserDatabaseRealmtoauthenticateusers

-->

<Resourcename="UserDatabase"auth="Container"

type="org.apache.catalina.UserDatabase"

description="Userdatabasethatcanbeupdatedandsaved"

factory="org.apache.catalina.users.MemoryUserDatabaseFactory"

pathname="conf/tomcat-users.xml"/>

  </GlobalNamingResources>

  <!-- A "Service" is a collection of one or more "Connectors" that share

asingle"Container"Note:A"Service"isnotitselfa"Container",

soyoumaynotdefinesubcomponentssuchas"Valves"atthislevel.

Documentationat/docs/config/service.html

-->

<Servicename="Catalina">

<!--Theconnectorscanuseasharedexecutor,youcandefineoneormorenamedthreadpools-->

<!--

<Executorname="tomcatThreadPool"namePrefix="catalina-exec-"

maxThreads="150"minSpareThreads="4"/>

-->

<!--A"Connector"representsanendpointbywhichrequestsarereceived

andresponsesarereturned.Documentationat:

JavaHTTPConnector:/docs/config/http.html(blocking&non-blocking)

JavaAJPConnector:/docs/config/ajp.html

APR(HTTP/AJP)Connector:/docs/apr.html

Defineanon-SSLHTTP/1.1Connectoronport8080

-->

<Connectorport="12080"protocol="HTTP/1.1"

connectionTimeout="20000"

redirectPort="8443"/>

<!--A"Connector"usingthesharedthreadpool-->

<!--

<Connectorexecutor="tomcatThreadPool"

port="8080"protocol="HTTP/1.1"

connectionTimeout="20000"

redirectPort="8443"/>

-->

<!--DefineaSSLHTTP/1.1Connectoronport8443

ThisconnectorusestheJSSEconfiguration,whenusingAPR,the

connectorshouldbeusingtheOpenSSLstyleconfiguration

describedintheAPRdocumentation-->

<!--

<Connectorport="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"   />

<!--AnEnginerepresentstheentrypoint(withinCatalina)thatprocesses

everyrequest.TheEngineimplementationforTomcatstandalone

analyzestheHTTPheadersincludedwiththerequest,andpassesthem

ontotheappropriateHost(virtualhost).

         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :

<Enginename="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(simplehowto)

/docs/config/cluster.html(referencedocumentation)-->

<Clusterclassname="org.apache.catalina.ha.tcp.SimpleTcpCluster"

               channelSendOptions="6">

        <Manager classname="org.apache.catalina.ha.session.BackupManager"

expireSessionsOnShutdown="false"

notifyListenersOnReplication="true"

mapSendOptions="6"/>

<!--

<Managerclassname="org.apache.catalina.ha.session.DeltaManager"

expireSessionsOnShutdown="false"

notifyListenersOnReplication="true"/>

-->

<Channelclassname="org.apache.catalina.tribes.group.GroupChannel">

<Membershipclassname="org.apache.catalina.tribes.membership.McastService"

bind="127.0.0.1"

address="228.0.0.4"

port="45564"

frequency="500"

dropTime="3000"/>

<Receiverclassname="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">

<Transportclassname="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>

</Sender>

<Interceptorclassname="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>

<Interceptorclassname="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>

<Interceptorclassname="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;

  Apache2.2+Tomcat6配置集群、负载均衡、session

然后在加上tomcat的端口去访问,所得值一样的.

Apache2.2+Tomcat6配置集群、负载均衡、session

后在加上tomcat的端口去访问,所得值一样的.

 Apache2.2+Tomcat6配置集群、负载均衡、session

从上面测试可以看到session复制是正确的..

相关推荐