首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 服务器 > Apache >

Apache2.2与tomcat7 集群配备

2012-10-17 
Apache2.2与tomcat7 集群配置以前就对集群比较感兴趣,今天在网上收集资料,自己也尝试配置了一个demo。准备

Apache2.2与tomcat7 集群配置
以前就对集群比较感兴趣,今天在网上收集资料,自己也尝试配置了一个demo。
准备环境:
安装Apache是http服务器,我们利用其对Tomcat进行负载均衡,我用的是apache2.2。下载Tocmat7,我这里用的是tomcat-7.0.21
集群主要有两种方式:
1.mod_proxy_blancer方式

我这里的demo是两个tomcat7的集群。
首先将apache安装目录下的conf目录下的httpd.conf文件的下图中的注释去掉

然后再这个文件最后添加下列文字
#虚拟机配置,负载均衡配置

<VirtualHost *:8000>ServerAdmin zengzhaoshuai@qq.comServerName localhostServerAlias localhost#小心,有些地方要有空格,要不然会出错哈哈。ProxyPass / balancer://cluster/ stickysession=JSESSIONID|jsessionid nofailover=OnProxyPassReverse / balancer://cluster/#ErrorLog "logs/error.log"#CustomLog "logs/access.log" common</VirtualHost> #The ProxyRequests directive should usually be set off when using ProxyPass.ProxyRequests Off<proxy balancer://cluster>BalancerMember ajp://localhost:8009 loadfactor=1 route=tomcat7_a  smax=5 max=20 ttl=120 retry=300 timeout=15BalancerMember ajp://localhost:9009 loadfactor=1 route=tomcat7_b  smax=5 max=20 ttl=120 retry=300 timeout=15# status=+H为配置热备,当所有机器都over时,才会请求该机器#BalancerMember http://192.168.1.218:8009 status=+HProxySet lbmethod=bytraffic</proxy>

在这里特别注意的是httpd.conf文件编码一定是utf-8 ,要不然apache启动不了。
<VirtualHost *:8000>,虚拟的主页,访问时:http://localhost:8000/项目名
ajp://localhost:8009 ,一个tomcat的,8009为ajp端口
ajp://localhost:9009,另外一个一个tomcat的,9009为ajp端口
route=tomcat7_a,其中的一个tomcat
route=tomcat7_a,另外一个tomcat

最后修改tomcat的confmuluxia的server.xml配置文件件
tomcat7_a的server.xml配置文件如下:
<?xml version='1.0' encoding='utf-8'?><Server port="8005" shutdown="SHUTDOWN">  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />  <Listener className="org.apache.catalina.core.JasperListener" />  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />  <GlobalNamingResources>    <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>  <Service name="Catalina">    <Connector port="8081" protocol="HTTP/1.1"                connectionTimeout="20000"                redirectPort="8443" />    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />    <Engine name="Catalina" defaultHost="localhost"   jvmRoute="tomcat7_a"> <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"/>            <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>      <Realm className="org.apache.catalina.realm.LockOutRealm">        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"               resourceName="UserDatabase"/>      </Realm>      <Host name="localhost"  appBase="webapps"            unpackWARs="true" autoDeploy="true">        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"               prefix="localhost_access_log." suffix=".txt"               pattern="%h %l %u %t &quot;%r&quot; %s %b" />      </Host>    </Engine>  </Service></Server>


由于我是在同一台机器上搭建的两个tomcat集群,故端口不能有冲突,如果不是的话就不需要修改了端口。
下面是tomcat7_b的配置文件

<?xml version='1.0' encoding='utf-8'?><Server port="8006" shutdown="SHUTDOWN">  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />  <Listener className="org.apache.catalina.core.JasperListener" />  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />  <GlobalNamingResources>    <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>  <Service name="Catalina">     <Connector port="8082" protocol="HTTP/1.1"                connectionTimeout="20000"                redirectPort="8443" />    <Connector port="9009" protocol="AJP/1.3" redirectPort="8443" />    <Engine name="Catalina" defaultHost="localhost"   jvmRoute="tomcat7_b"> <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"/>            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"                      address="auto"                      port="4001"                      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>      <Realm className="org.apache.catalina.realm.LockOutRealm">        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"               resourceName="UserDatabase"/>      </Realm>      <Host name="localhost"  appBase="webapps"            unpackWARs="true" autoDeploy="true">        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"               prefix="localhost_access_log." suffix=".txt"               pattern="%h %l %u %t &quot;%r&quot; %s %b" />      </Host>    </Engine>  </Service></Server>


配置就大告成功了,我发布的项目是cluster,访问只需要访问http://localhost:8000/cluster/testCluster.jsp.

二、利用mod_jk集群
首先要下载mod_jk.so,我附件中与apache2.2匹配的。
详细说我就不说了,我就贴出配置文件吧
apache安装目录的conf目录结构,没有就创建文件

在httpd.conf开头添加这句:
# 监听端口和监听地址Listen 8000

在httpd.conf最后添加以下文字:
Include conf/mod_jk.conf


mod_jk.conf文件内容:
# Load mod_jk moduleLoadModule jk_module modules/mod_jk.so# Where to find workers.propertiesJkWorkersFile conf/workers.properties# Where to put jk logsJkLogFile logs/mod_jk.log# Set the jk log level [debug/error/info]JkLogLevel info# Select the log formatJkLogStampFormat "[%a %b %d %H:%M:%S %Y] "# JkOptions indicate to send SSL KEY SIZE,JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories# JkRequestLogFormat set the request formatJkRequestLogFormat "%w %V %T"# Send servlet for context /examples to worker named ajp13#JkMount /servlet/* controller# Send JSPs for context /examples to worker named ajp13JkMount /* controller


workers.properties文件内容:
#========controller,负载均衡控制器========  worker.list=controller,tomcat7_a,tomcat7_b worker.controller.type=lb  #========tomcat7_a========  worker.tomcat7_a.port=8009        #ajp13 端口号,在tomcat下server.xml配  置,默认8009  worker.tomcat7_a.host=localhost  #tomcat的主机地址,如不为本机,请填写ip地址  worker.tomcat7_a.type=ajp13  worker.tomcat7_a.lbfactor = 1   #server的加权比重,值越高,分得的请求越多  #========tomcat7_b========  worker.tomcat7_b.port=9009       #ajp13 端口号,在tomcat下server.xml配置,  worker.tomcat7_b.host=localhost #tomcat的主机地址,如不为本机,请填写ip地址  worker.tomcat7_b.type=ajp13  worker.tomcat7_b.lbfactor = 1   #server的加权比重,值越高,分得的请求越多  worker.controller.balanced_workers=tomcat7_a,tomcat7_b #指定分担请求的tomcat  worker.controller.sticky_session=false #worker.controller.sticky_session=1  


tomcat那部分的与上面第一种方法配置一样就就行了,运行方式也一样。

热点排行