操作系统:Debian6.0 (192.168.225.129 虚拟机1台)

软件版本:Apache-2.2.16, Tomcat-6.0.35

1.安装软件

    >apt-get install update

    *安装Apache

    >apt-get install apache2

    *安装Tomcat

    >wget http://apache.etoak.com/tomcat/tomcat-6/v6.0.35/bin/apache-tomcat-6.0.35.tar.gz

    >tar zxvf apache-tomcat-6.0.35.tar.gz

    >mv apache-tomcat-6.0.35 /user/local/tomcat

    >cp /usr/local/tomcat  /usr/local/tomcat2

    *安装mod_jk

    >apt-get install libapache2-mod-jk

2.配置2个Tomcat

    现在/usr/local目录中已经有2个tomcat目录了,需要更改tomcat2的端口,防止端口冲突。

    >nano /usr/local/tomcat/conf/server.xml

    有3处的默认端口需要更改:

    1.<Server port="8004" shutdown="SHUTDOWN"> 我更改为8003

    2.<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

        更改为7080

    3.<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

        更改为7009,其中redirectPort 8443不需要更改

    o.另外还需要在<Engine>配置中加入<Cluster>配置

<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" 
			selectorTimeout="5000" 
			maxThreads="6"/>  
        <!-- timeout="60000"-->  
        <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"/>  
        <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>  
    </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>

    x.最后2个tomcat都需要改变<Engine的 jvmRoute属性分别为tomcat1和tomcat2,以对应之后worker.properties中的名字。


3.配置Apache以jk方式和tomcat集群

   安装完成后apache的主目录为/etc/apache2,安装完mod-jk之后,mods-enabled里面会多一个jk.load

   创建文件   /etc/apache2/mods-enabled/jk.conf

    >nano /etc/apache2/mods-enabled/jk.conf

       JkWorkersFile /etc/apache2/workers.properties
       JkShmFile     /var/log/apache2/mod_jk.shm
       JkLogFile     /var/log/apache2/mod_jk.log
       JkLogLevel    info

   创建文件  /etc/apache2/workers.properties

    >nano /etc/apache2/workers.properties

       worker.list=controller1

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

       worker.tomcat2.port=7009
       worker.tomcat2.host=localhost
       worker.tomcat2.type=ajp13
       worker.tomcat2.lbfactor=1

       worker.controller1.type=lb
       worker.controller1.sticky_session=1   #可选项0,1
       worker.controller1.balance_workers=tomcat1,tomcat2

    更改/etc/apache2/sites-enabled/000-default文件

      在</VirtualHost>之前添加

       JkMount /* controller1

       (controller1对应workers.properties中的名字)

配置完成之后先后启动tomcat和apache。

测试:

2个tomcat部署相同的应用:即首页index.jsp显示各自tomcat名称tomcat1和tomcat2

通过80端口访问应用,刷新几次,tomcat1和tomcat2轮流显示。






Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐