apache+tomcat7.0实现负载均衡

一、开始安装Apache Http Server

点击这里下载装完后,在Apache2.2启动的前提下,打开一个ie输入 http://localhost ,你将会得到这样的一个页面,就说明你的Apache的安装是成功的。

二、配置Apache

1.找apache安装路径下的conf文件夹下的httpd.conf文件,在最下面加上include "D:/Apache2/conf/mod_jk.conf" 
2.http.conf 同目录下新建mod_jk.conf文件,内容如下:
LoadModule jk_module modules/mod_jk-1.2.31-httpd-2.2.3.so
JKWorkersFile conf/workers.properties
JkLogFile logs/mod_jk.log
<VirtualHost *>
DocumentRoot d:/www/
<Directory "d:/www/test">
    AllowOverride None
    Order allow,deny
          Allow from all
</Directory>

ServerAdmin localhost
ServerName localhost:80
DirectoryIndex index.html index.htm index.jsp index.action
ErrorLog logs/shsc-error_log.txt
CustomLog logs/shsc-access_log.txt common
 
JkMount /*WEB-INF controller
JkMount /*j_spring_security_check controller
JkMount /*.action controller
JkMount /servlet/* controller
JkMount /*.jsp controller
JkMount /*.do controller
JkMount /*.action controller
JkMount /*.html controller
JkMount /*fckeditor/editor/filemanager/connectors/*.* controller
JkMount /fckeditor/editor/filemanager/connectors/* controller
</VirtualHost>
 3.在http.conf同目录下新建 workers.properties文件,如下: 
 
worker.list = controller
#tomcat1
worker.tomcat1.port=8009       
worker.tomcat1.host=localhost
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor=1
#tomcat2
worker.tomcat2.port=9009     
worker.tomcat2.host=localhost
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1
#========controller========
worker.controller.type=lb
worker.controller.balance_workers=tomcat1,tomcat2
worker.lbcontroller.sticky_session=0
worker.controller.sticky_session_force=true
worker.connection_pool_size=3000
worker.connection_pool_minsize=50
worker.connection_pool_timeout=50000

这样apache的配置就结束了。

三、配置tomcat

将本地的tomcat拷贝两份,分别在 E:/tomcat7和E:/tomcat7-1,这样来模拟同时有两个服务的情况。
1.配置tomcat的service.xml文件:
1)
 <!-- 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="tomcat1">

先对engine 进行设置,定义为apache定义的相同就好,分别为tomcat1和tomcat2。
2)
<Server port="8005" shutdown="SHUTDOWN">
tomcat1和tomcat2的port进行区分。分别为8005和9005
3)
 <Connector port="8080" protocol="HTTP/1.1"
修改为
 <Connector port="8080" protocol="HTTP/1.1"
               URIEncoding="UTF-8"  minSpareThreads="25" maxSpareThreads="75"
               enableLookups="false" disableUploadTimeout="true" connectionTimeout="20000"
                   acceptCount="300"  maxThreads="300" maxProcessors="1000" minProcessors="5"
                   useURIValidationHack="false"
                             compression="on" compressionMinSize="2048"
                             compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"
               redirectPort="8443" />
同样port进行区分,注意redirectPort也需要进行区分。port分别为8080和9090,redirectPort分别为8443和9443。
4)
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" />
修改为
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" 
                  channelSendOptions="6"> 
              <Manager className="org.apache.catalina.ha.session.BackupManager" 
                    expireSessionsOnShutdown="false" 
                    notifyListenersOnReplication="true" 
                    mapSendOptions="6"/> 
           <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="4002" 
                       selectorTimeout="100" 
                       maxThreads="6"/> 
             <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter"> 
               <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender" timeout="60000"/>  
             </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中增加工程test,增加test.jsp
<%@ 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>

部署之后,启动两个tomcat,再启动apache服务。

大功告成了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sinom21

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值