Apache与Tomcat负载均衡

**************apache安装*********************************
1.官网下载httpd-2.0.64.tar.gz
2.解压文件 tar zxvf httpd-2.0.64.tar.gz,加压后文件夹为httpd-2.0.64
3.进入目录httpd-2.0.64,执行命令./configure --prefix=/usr/local/apache --enable-so
4.安装: 执行make和make install命令安装,安装完成之后会有一个apache目录

===apache启动
进入apache安装目录下的bin ./apachectl start
===apache停止服务         ./apachectl stop

*************mod_jk安装和配置****************************
从官网下载tomcat-connectors-1.2.36-src.tar.gz
解压并进入native目录,
./configure --with-apxs=/usr/local/apache/bin/apxs --with-java-home=/usr/local/jdk
make && make install后在/usr/local/apache/modules下面生成mod_jk.so

**************tomcat的安装和配置**************************
1.分别安装两个tomcat在目录/usr/local/tomcat1和/usr/local/tomcat2下面
2.修改server.xml中的内容
  2.1<Server port="8005" shutdown="SHUTDOWN"> (tomcat2中改为8006端口)
  2.2禁用http连接器,将如下内容注释
   <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" 
                                                 redirectPort="8443" />
  2.3分别启用AJP连接器
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
    <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
    
  2.4<Engine name="Catalina" defaultHost="localhost">
分别更改为<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
和 <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">

分别启动tomcat

****************httpd.conf下面的配置**********************
进入apache/conf目录下面,打开httpd.conf文件
在尾部加入
LoadModule  jk_module modules/mod_jk.so
JkWorkersFile conf/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel warn
#JkMount /jkmanager|/* jkstatus
#JkMount /*.jsp controller
JkMount /examples/jsp/* controller
JkMount /*.do controller
JkMount /*servlet controller

====编辑workers.properties
编辑如下内容
worker.list=controller,tomcat1,tomcat2

#================tomcat1==============
worker.tomcat1.port=8009 #ajp13 port
worker.tomcat1.host=localhost
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor =1

#============tomcat2================
worker.tomcat2.port=8010  #ajp13 port
worker.tomcat2.host=localhost
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor=1


#=============controller================
worker.controller.type =lb
worker.controller.balance_workers=tomcat1,tomcat2
worker.controller.sticky_session=1 

**********************测试程序*************************
编写测试界面index.jsp(内容如下)分别放在tomcat1和tomcat2的webapps/examples/jsp下面

<body>
      <h1><font color="red">Index Page Servered By Tomcat1</font></h1>
          <!--tomcat2 下面相应为Tomcat2-->
      
      <table align="center" border="1">
                <tr>
                        <td>Session ID</td>
                        <td><%=session.getId()%></td>
                </tr>

                <tr>
                        <td>Created on</td>
                        <td><%=session.getCreationTime()%></td>
                </tr>

                      
      </table>
  </body>
在浏览器访问:http://localhost/examples/jsp/index.jsp,会发现有时运行在tomcat1上有时会运行在tomcat2上,另外如果这时把tomcat1服务停掉,就会发现mod_jk这时只能连接到tomcat2上了,这时也就只能运行在tomcat2上了


















你可能感兴趣的:(apache,tomcat)