Linux下通过Apache搭建Tomcat集群

集群说明:

1. 负载均衡(Load Balance):当同一客户端发起一个请求时,apache始终将请求转发到同一个节点 (sticky session),当另一个客户端或同一客户端从一个新的浏览器窗口发起请求时,apache会把请求分发到另一节点 上,依次轮询,当然,可以在apache上设置后端tomcat的分发权重。从而达到负载均衡的效果。

        

2. 高可用(High availablity):当其中一台tomcat server 突然crash时,apache会将这在进行的请求分发到集群中其他tomcat server上,由于集群member 之间已经session replication,所以原来的session 会在另外一节点上继续进行,此时,请求已发生了无缝转移,在客户端完全感觉不到故障已发生。

Linux下通过Apache搭建Tomcat集群

一.准备工作:

系统:CentOS release 6.2(Final)

软件:httpd-2.2.24.tar.gz

      tomcat-connectors-1.2.37-src.tar.gz

      apache-tomcat-6.0.37.tar.gz

集群分配:192.168.1.103 安装apache  tomcat-connectors

          192.168.1.22 安装tomcat22

          192.168.1.23 安装tomcat23

二.安装

1.1安装Apache   192.168.1.103

[root@localhost soft]#wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.2.24.tar.gz [root@localhost soft]#tar -zxvf httpd-2.2.24.tar.gz    //解压apache的压缩包

[root@localhost soft]#cd httpd-2.2.24     //定位到httpd-2.2.24 文件夹下

[root@localhost httpd-2.2.24]#ls     //查看显示httpd-2.2.24 文件夹下内容

[root@localhost httpd-2.2.24]#./configure --help | more    //查看安装apache配置参数

[root@localhost httpd-2.2.24]#./configure  --prefix=/opt/apache  --enable-so --enable-mods-shared=all    //  配置apache路径

[root@localhost httpd-2.2.24]#make     //编译apache

[root@localhost httpd-2.2.24]#make install    //安装apache

[root@localhost httpd-2.2.24]#cd /opt/apache   //进入apache的目录   

[root@localhost apache]#  cd conf/

[root@localhost conf]#cp -a httpd.conf httpd.conf-     //备份apache配置文件

[root@localhost conf]#vi httpd.conf 

   修改#ServerName www.example.com:80为ServerName localhost:80

[root@localhost conf]#/opt/apache/bin/apachectl -k start    //linux启动apache命令              

[root@localhost conf]#netstat -an | grep :80    //查看linux80端口是否开启

[root@localhost conf]#ps -aux | grep httpd     //linux下查看apache进程

[root@localhost conf]#cd ../..

[root@localhost opt]#cp /opt/apache/bin/apachectl /etc/rc.d/init.d/apache //拷贝apache启动脚本

[root@localhost opt]#vi /etc/rc.d/init.d/apache    // 这里是编辑apache启动脚本

  在开头的#!/bin/sh  下面加上

    #chkconfig: 2345  85  15

[root@localhost opt]#chkconfig --add apache    //添加apache服务

[root@localhost opt]#chkconfig --list apache    //列出apache服务

[root@localhost opt]#service apache stop    //停止apache服务

[root@localhost opt]#netstat -an | grep :80     //查看linux的80端口是否关闭

[root@localhost opt]#ps -aux | grep httpd     //查看是否存在httpd服务,若果之前自带httpd服务启动的话会导致新添加的apache服务启动失败

[root@localhost opt]#service apache start    //启动apache服务

[root@localhost opt]#curl 127.0.0.1

 

附:安装proxy   (httpd-2.2.24版本)

httpd-2.4.9版本无需安装proxy,安装httpd-2.4.9版本后,会自动安装proxy相关文件,在modules目录下面就有mod_proxy_balancer.so、 mod_proxy_ftp.so等相关文件,无需再单独安装proxy了(2014.07.07 补充)

进入apache源代码的modules目录 

[root@localhost opt]# cd /opt/httpd-2.2.24/modules/proxy/

[root@www proxy]# ls

[root@www proxy]# /opt/apache/bin/apxs -c -i -a mod_proxy.c proxy_util.c

[root@www proxy]# /opt/apache/bin/apxs -c -i -a mod_proxy_balancer.c mod_proxy_connect.c mod_proxy_http.c

[root@www proxy]# /opt/apache/bin/apxs -c -i -a mod_proxy_ftp.c

重启apache。

选项说明: 
-c 执行编译操作 
-i 安装操作,安装一个或多个动态共享对象到服务器的modules目录 
-a 自动增加一个LoadModule行到httpd.conf文件,以激活此模块,若此行存在则启用之 
-A 与-a类似,但是它增加的LoadModule行前有井号前缀(#) 
-e 需要执行编辑操作,可与-a和-A选项配合使用,与-i操作类似,修改httpd.conf文件,但并不安装此模块 

 

2.安装tomcat-connectors   192.168.1.103

[root@zqkj103 soft]# wget http://mirror.bjtu.edu.cn/apache/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.37-src.tar.gz

[root@zqkj103 soft]# tar -zxvf tomcat-connectors-1.2.37-src.tar.gz

 [root@zqkj103 soft]# cd tomcat-connectors-1.2.37-src

[root@zqkj103 tomcat-connectors-1.2.37-src]# ls

[root@zqkj103 tomcat-connectors-1.2.37-src]# cd native/

[root@zqkj103 native]# ls

[root@zqkj103 native]# ./buildconf.sh

如果提示:

buildconf: checking installation...

buildconf: autoconf not found.

           You need autoconf version 2.59 or newer installed

           to build mod_jk from SVN.

[root@zqkj103 native]# ./buildconf.sh

[root@zqkj103 native]# yum install autoconf

[root@zqkj103 native]# ./buildconf.sh  再次提示

buildconf: checking installation...

buildconf: autoconf version 2.63 (ok)

buildconf: libtool not found.

           You need libtool version 1.4 or newer installed

           to build mod_jk from SVN.

[root@zqkj103 native]#yum install libtool

[root@zqkj103 native]# ./buildconf.sh

[root@zqkj103 native]# ./configure --with-apxs=/usr/local/apache/bin/apxs

[root@zqkj103 native]# make

[root@zqkj103 native]# make install

 

如果成功结束,你可以在/usr/local/apache/modules/下找到mod_jk.so文件。

 

3.安装tomcat  192.168.1.22

[root@zqkj22 soft]# cd /opt

[root@zqkj22 opt]# ls

[root@zqkj22 opt]# wget http://mirror.bjtu.edu.cn/apache/tomcat/tomcat-6/v6.0.37/bin/apache-tomcat-6.0.37.tar.gz [root@zqkj22 opt]#tar -zxvf apache-tomcat-6.0.37.tar.gz

 

4.安装tomcat  192.168.1.23

[root@zqkj23 soft]# cd /opt

[root@zqkj23 opt]# ls

[root@zqkj23 opt]# wget http://mirror.bjtu.edu.cn/apache/tomcat/tomcat-6/v6.0.37/bin/apache-tomcat-6.0.37.tar.gz [root@zqkj22 opt]#tar -zxvf apache-tomcat-6.0.37.tar.gz

 

5.修改192.168.1.22机器上tomcat的配置

修改server.xml

<Engine name="Catalina" defaultHost="localhost">修改为

<Engine name="Catalina" defaultHost="localhost"  jvmRoute="tomcat22">

<!- -

<Cluster classname="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>

-- >

改成:

<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>

配置web.xml,增加<distributable/>元素,表示此应用将与群集服务器复制Session

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

   <distributable/>

 

 

6.修改192.168.1.23机器上tomcat的配置

修改server.xml

<Engine name="Catalina" defaultHost="localhost">修改为

<Engine name="Catalina" defaultHost="localhost"  jvmRoute="tomcat23">

把<!- -

<Cluster classname="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>

-- >

改成:

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

<!--

注释:此处刚开始我设置的4000,可是tomcat总启动后,总是无法访问项目,遂改成了4001,则可以了。具体原因不明,不知道跟23机器上的端口是否冲突的原因。但是在22机器上,设置4000是可以的。也不知是否不能跟22机器上的端口一样

-->

 

    <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>

 

配置web.xml,增加<distributable/>元素,表示此应用将与群集服务器复制Session

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

        <distributable/>

 

7.配置Apache中的httpd.conf

打开apache安装目录下的conf/httpd.conf文件,在文件最后追加以下内容:

# Load mod_jk module 

LoadModule jk_module modules/mod_jk.so 

# Specify jk log file. 

JkLogFile /var/log/mod_jk.log 

# Specify jk log level [debug/error/info] 

JkLogLevel info 

# Specify workers.properties, this file tell jk: 

# how many nodes and where they are. 

JkWorkersFile conf/workers.properties 

# Specify which requests should handled by which node.  

JkMount /*.jsp controller

 

8.配置workers.properties

新建workers.properties     用vi命令

上述配置中:JkWorkersFile conf/workers.properties 指明由一个workers.properties文件来描述集群结点的情况,因此,我们需要创建这个workers.properties文件,并放置于 conf文件夹下,这个文件的内容如下:

 

worker.list = controller,tomcat22,tomcat23 

#========tomcat1========   

worker.tomcat22.port=8009  

# 此处的端口是指<Connector port="8009" redirectPort="8443" protocol="AJP/1.3"/>处的

worker.tomcat22.host=192.168.1.22 

worker.tomcat22.type=ajp13 

worker.tomcat22.lbfactor = 1  

#========tomcat2======== 

worker.tomcat23.port=8009    

worker.tomcat23.host=192.168.1.23 

worker.tomcat23.type=ajp13 

worker.tomcat23.lbfactor = 1  

#========controller======== 

worker.controller.type=lb 

worker.controller.balance_workers=tomcat22,tomcat23 

worker.controller.sticky_session=false

worker.controller.sticky_session_force=1

#worker.controller.sticky_session=1

 

9.建立项目测试

建立test项目,需要在项目的web.xml中添加<distributable/>

建立test2.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>

上面的测试页面就不解释了,很好理解。

 

注:关闭22和23的防火墙,不然访问不了,或者tomcat无法正常启动,或者能访问,但是每次重新请求session都会丢失,更不能共享

service iptables stop                                 

 

原本打算用tomcat8 ,但是添加< Cluster >标签后,无法启动tomcat,遂只能改用tomcat6进行配置  ;此情况可能是路由组播造成的。当不使用默认网络eth0时,tomcat6也会出现此情况。至于tomcat8没有再具体测试。或者也有可能是防火墙未关闭(2014.07.07 补充)

 

linux 下tomcat开机自启动
修改Tomcat/bin/startup.sh 为:

export JAVA_HOME=/usr/java/jdk1.8.0_05

export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:.
export PATH=$PATH:$JAVA_HOME/bin
export CATALINA_HOME=/opt/tomcat01
/opt/tomcat01/bin/catalina.sh start

 

在/etc/rc.d/rc.local中加入:
/opt/tomcat01/bin/startup.sh

相关推荐