linux下apache tomcat mod_jk 负载均衡+集群+session 复制

时间:2022-02-04 16:55:49

一、系统简介

Linux AS 5.1(其他Linux版本的安装方法大致相同)

1个apache 2个tomcat在同一台电脑上的配置

二、本实例所用软件:

1、apache安装软件

httpd-2.4.2.tar.gz 点此下载:

apr-1.4.6.tar.gz 点此下载:

apr-util-1.4.1.tar.gz 点此下载:

pcre-8.30.tar.gz 点此下载:

2、JDK安装软件

安装JDK要根据自己电脑系统去官方网站选择合适的版本下载。

本例下载的是:jdk-7u3-linux-x64.tar.gz

3、Tomcat安装软件

apache-tomcat-7.0.29.tar.gz 点此下载:

4、JK模块

tomcat-connectors-1.2.35-src.tar.gz 点此下载:

软件准备完毕

三、安装软件

注:为了避免权限限制,本例所有操作都是以root用户操作。

软件安装目录: /usr/local

1、安装并测试apache

首先先安装apr、apr-util以及pcre,这个在安装apache的时候会用到。(ps:安装apache时系统会检测有没有这些文件支持。)

  • 安装apr

tar -zxvf apr-1.4.6.tar.gz # 解压安装文件

cd apr-1.46 # 进入解压出来的文件夹:apr-1.46

./configure --prefix=/usr/local/apr # 设置apr的安装目录 本例设置为: /usr/local/apr

make && make install # 编译并安装apr

apr安装完成

  • 安装apr-util

tar -zxvf apr-util-1.4.1.tar.gz

cd apr-util-1.4.1

./configure --prefix=/usr/local/apr-util--with-apr=/usr/local/apr

make && make install

apr-util安装完成

  • 安装pcre

tar -zxvf pcre-8.30.tar.gz

cd pcre-8.30

./configure --prefix=/usr/local/pcre

make && make install

pcre安装完成

  • 安装apache

tar -zxvf httpd-2.4.2.tar.gz

cd httpd-2.4.2

./configure --prefix=/usr/local/apache2--with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util--with-pcre=/usr/local/pcre --enable-so

make && make install

安装apache 需要指定apr ,apr-util ,pcre 。问题在于指定的目录pcre是安装后的目录而apr和apr-util不是安装后的目录而是源文件目录

至此,apache安装完成。

  • 测试

/usr/local/apache2/bin/apachectl start

在地址栏输入:127.0.0.1 出现 IT works!页面 apache安装成功

如果LINUX 原来安装了APACHE请先删除,然后安装新APACHE新版本。

2、安装并测试JDK

  • 安装

tar -zxvf jdk-7u3-linux-x64.tar.gz

mv jdk1.7.0_03 ./jdk #这个只是改变JDK的目录,也可以不改,在后边设置环境变量要注意路径设置

下载的这个版本的JDK是免安装软件,解压缩之后就安装完成

  • 配置环境变量

vi /etc/profile (新手对vi编辑器不熟悉的话,可以在图形化界面用 gedit /etc/profile )

在文件最后加上

export JAVA_HOME=/usr/local/jdk

export CLASSPATH=$JAVA_HOME/lib

export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH

保存退出

source /etc/profile #上载刚才对profile文件的更改

  • 测试

在终端输入命令:

java -version

出现以下信息表示java运行环境配置成功

java version "1.7.0_03"

Java(TM) SE Runtime Environment (build 1.7.0_03-b04)

Java HotSpot(TM) 64-Bit Server VM (build 22.1-b02,mixed mode)

##如果提示JAVA_HOME找不到,可尝试把JAVA_HOME在 /etc/enviroment 文件中做定义##

vi /etc/enviroment

在空白处增加:

export JAVA_HOME=/usr/local/jdk

:wq 保存退出

source /etc/enviroment

再进行测试。

3、安装并测试tomcat

  • 安装

tar -zxvf apache-tomcat-7.0.29.tar.gz

cp -r apache-tomcat-7.0.29 /usr/local/apache-tomcat-7.0.29-lb1

mv apache-tomcat-7.0.29/usr/local/apache-tomcat-7.0.29-lb2

  • 配置tomcat运行环境

修改tomcat运行环境

vi /etc/profile

在文件最后加入两行:

exportCATALINA_1_HOME=/usr/local/apache-tomcat-7.0.29-lb1

exportCATALINA_2_HOME=/usr/local/apache-tomcat-7.0.29-lb2

保存退出

·        修改tomcat1/bin/catalina.sh

vi /usr/local/tomcat1/bin/catalina.sh

在注释结束的地方,大概第99行加入一句:

CATALINA_HOME=$CATALINA_1_HOME

保存退出

·        修改tomcat2/bin/catalina.sh

vi /usr/local/tomcat2/bin/catalina.sh

位置与tomcat1相同,加入:

CATALINA_HOME=$CATALINA_2_HOME

保存退出

  • 配置tomcat端口信息

因为两台tomcat在同一台电脑上,修改端口避免冲突

vi /usr/local/tomcat1/conf/server.xml

共修改3处:

<Server port="8005"shutdown="SHUTDOWN"> 修改8005

<Connector port="8081"protocol="HTTP/1.1" maxThreads="150"minSpareThreads="25" maxSpareThreads="75"enableLookups="false" redirectPort="8443" acceptCount="100"debug="99" connectionTimeout="20000"disableUploadTimeout="true" URIEncoding="UTF-8" /> 修改8081

<Connector port="8009"protocol="AJP/1.3" redirectPort="8443" /> 修改为

<Connector port="8109"protocol="AJP/1.3" redirectPort="8443"URIEncoding="UTF-8"/>

修改8109,增加URIEncoding="UTF-8"

Tomcat2 修改如下:

<Server port="8006"shutdown="SHUTDOWN">

<Connector port="8082"protocol="HTTP/1.1" maxThreads="150"minSpareThreads="25" maxSpareThreads="75"enableLookups="false" redirectPort="8443"acceptCount="100" debug="99"connectionTimeout="20000" disableUploadTimeout="true"URIEncoding="UTF-8" />

<Connector port="8209"protocol="AJP/1.3" redirectPort="8443"URIEncoding="UTF-8"/>

#本端口在做整合的时候要用

  • 测试tomcat

分别运行:

/usr/local/ apache-tomcat-7.0.29-lb1/bin/startup.sh

/usr/local/ apache-tomcat-7.0.29-lb2/bin/startup.sh

打开浏览器输入地址:访问机器的IP和端口

192.168.1.101:8081

192.168.1.101:8082

出现tomcat 猫的界面,tomcat安装成功

4、生成mod_jk.so模块

·        安装JK模块

tar -zxvf tomcat-connectors-1.2.35-src.tar.gz

cd tomcat-connectors-1.2.35-src/native/

./configure --with-apxs=/usr/local/apache2/bin/apxs--with-java-home=/usr/local/jdk

make && make install

运行apache下的apxs,如果返回如下结果,证明正常。

cd/usr/local/tomcat-connectors-1.2.35-src/native/apache-2.0

/usr/local/apache2/bin/apxs -n jk2 -i mod_jk.so

/usr/local/apache2/build/instdso.shSH_LIBTOOL='/usr/local/apache2/build/libtool' mod_jk2.so/usr/local/apache2/modules

/usr/local/apache2/build/libtool--mode=install cp mod_jk.so /usr/local/apache2/modules/

cp mod_jk.so /usr/local/apache2/modules/mod_jk.so

Warning! dlname not found in/usr/local/apache2/modules/mod_jk.so.

Assuming installing a .so rather than alibtool archive.

cp mod_jk.so /usr/local/apache2/modules/

chmod 755/usr/local/apache/modules/mod_jk.so

ll /usr/local/apache/modules/mod_jk.so

-rwxr-xr-x 1 root root 709215 08-14 10:22/usr/local/apache/modules/mod_jk.so

说明mod_jk.so生成成功。

四、整合实现负载均衡

1、配置apache中mod_jk.so运行所需文件

在Apache的conf目录中创建文件workers.properties和mod_jk.conf

vi /usr/local/apache2/conf/workers.properties

加入如下内容

#server 列表
worker.list = controller,tomcat1,tomcat2

# tomcat1(ajp13 端口号,在tomcat下server.xml配置,默认8009)
worker.tomcat1.port=8109 ps:此处端口和tomcat中的配置需一致)
#tomcat的主机地址,如不为本机,请填写ip地址
worker.tomcat1.host=192.168.1.101
worker.tomcat1.type=ajp13
#server的加权比重,值越高,分得的请求越多
worker.tomcat1.lbfactor = 1

# tomcat2
worker.tomcat2.port=8209 ps:此处端口和tomcat中的配置需一致)
worker.tomcat2.host=192.168.1.101
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor = 1

# controller(负载均衡控制器)
worker.controller.type=lb
# 指定分担请求的tomcat
worker.controller.balanced_workers=tomcat1,tomcat2
#worker.controller.sticky_session=true

保存退出

 

vi /usr/local/apache2/conf/mod_jk.conf

加入如下内容:

#Load mod_jk2 module

LoadModule jk_module modules/mod_jk.so

 

# Where to find workers.properties(引用workers配置文件)

JkWorkersFile conf/workers.properties

 

# Where to put jk logs(log文件路径)

JkLogFile logs/mod_jk2.log

 

# Set the jk log level [debug/error/info](log级别)

JkLogLevel info

       

# Select the log format(log格式)

JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "

# JkOptions indicate to send SSL KEY SIZE,

JkOptions +ForwardKeySize +ForwardURICompat-ForwardDirectories

 

# JkRequestLogFormat set the request format

JkRequestLogFormat "%w %V %T"

 

# Send JSPs for context / to worker namedloadBalancer(URL转发配置,匹配的URL才转发到tomcat进行处理)

JkMount /*.jsp controller

# JkMount /*.* controller

保存退出

2、配置tomcat中mod_jk.so运行所需文件

vi /usr/local/ apache-tomcat-7.0.29-lb1/conf/server.xml

在这个位置修改:

<Host name="localhost"appBase="webapps"
unpackWARs="true" autoDeploy="true">

修改后:

<Host name="localhost"appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="/usr/local/apache2/htdocs"debug="0"/>

然后在这个位置:

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

修改后:

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

(PS:注意此处是在tomcat1的server.xml文件中,指向tomcat2,在tomcat2的文件中指向tomcat1)

同样也修改tomcat2server.xml文件中

<Host name="localhost"appBase="webapps"
unpackWARs="true" autoDeploy="true">

tomcat1相同。

保存退出

至此,整合完成。

         3、修改配置tomcat1中conf/server.xml文件Cluster 节点修改为:

<ClusterclassName="org.apache.catalina.ha.tcp.SimpleTcpCluster"

                channelSendOptions="8">

 

         <Manager className="org.apache.catalina.ha.session.DeltaManager"

                  expireSessionsOnShutdown="false"

                  notifyListenersOnReplication="true"/>

 

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

           <ReceiverclassName="org.apache.catalina.tribes.transport.nio.NioReceiver"

                     address="192.168.1.101" #本机IP地址

                     port="5000"  #此处端口tomcat1和tomcat2 不要一致,这里tomcat1为5000

                     autoBind="100"

                     selectorTimeout="5000"

                      maxThreads="6"/>

 

           <SenderclassName="org.apache.catalina.tribes.transport.ReplicationTransmitter">

             <TransportclassName="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>

           </Sender>

            <InterceptorclassName="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>

           <InterceptorclassName="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>

         </Channel>

 

         <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"

                filter=""/>

         <ValveclassName="org.apache.catalina.ha.session.JvmRouteBinderValve"/>

 

         <!--

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

         <ClusterListenerclassName="org.apache.catalina.ha.session.ClusterSessionListener"/>

       </Cluster>

4、修改配置tomcat2中conf/server.xml文件Cluster 节点修改为:

       <ClusterclassName="org.apache.catalina.ha.tcp.SimpleTcpCluster"

                channelSendOptions="8">

 

         <ManagerclassName="org.apache.catalina.ha.session.DeltaManager"

                  expireSessionsOnShutdown="false"

                  notifyListenersOnReplication="true"/>

 

         <Channel className="org.apache.catalina.tribes.group.GroupChannel">

           <MembershipclassName="org.apache.catalina.tribes.membership.McastService"

                       address="228.0.0.4"

                       port="45564"

                       frequency="500"

                       dropTime="3000"/>

           <ReceiverclassName="org.apache.catalina.tribes.transport.nio.NioReceiver"

                     address="192.168.1.101"

                     port="5001"

                     autoBind="100"

                      selectorTimeout="5000"

                     maxThreads="6"/>

 

           <SenderclassName="org.apache.catalina.tribes.transport.ReplicationTransmitter">

             <TransportclassName="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>

           </Sender>

           <InterceptorclassName="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>

           <InterceptorclassName="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>

          </Channel>

 

         <ValveclassName="org.apache.catalina.ha.tcp.ReplicationValve"

                filter=""/>

         <ValveclassName="org.apache.catalina.ha.session.JvmRouteBinderValve"/>

 

         <!--

         <DeployerclassName="org.apache.catalina.ha.deploy.FarmWarDeployer"

                   tempDir="/tmp/war-temp/"

                   deployDir="/tmp/war-deploy/"

                   watchDir="/tmp/war-listen/"

                   watchEnabled="false"/>

          -->

 

         <ClusterListenerclassName="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>

         <ClusterListenerclassName="org.apache.catalina.ha.session.ClusterSessionListener"/>

       </Cluster>

5、修改/usr/local/apche2/conf/server.xml

在server.xml最后增加一行:Include conf/mod_jk.conf

6、在tomcat 的webapps发布应用中web.xml 新增一行:<distributable/>用于session复制。

7、在/usr/local/apache2/htdocs下创建测试文件test.jsp

vi /usr/local/apache2/htdocs/test.jsp

<%
System.out.println( "Evaluating date now" );
java.util.Date date = new java.util.Date();
%>
Hello! The time is now <%= date %>

8、在tomcat的应用目录,新增index.jsp

<%@ page contentType="text/html;charset=UTF-8" %>

<%@ page import="java.util.*" %>

<html><head><title>Cluster AppTest</title></head>

<body>

Server Info:

<%

out.println(request.getLocalAddr() + " : " +request.getLocalPort()+"<br>");%>

<%

 out.println("<br> ID " +session.getId()+"<br>");

 

  // 如果有新的 Session 属性设置

  StringdataName = request.getParameter("dataName");

  if (dataName!= null && dataName.length() > 0) {

     StringdataValue = request.getParameter("dataValue");

    session.setAttribute(dataName, dataValue);

  }

 

 out.print("<b>Session 列表</b>");

 

  Enumeration e= session.getAttributeNames();

  while(e.hasMoreElements()) {

     String name= (String)e.nextElement();

     Stringvalue = session.getAttribute(name).toString();

    out.println( name + " = " + value+"<br>");

        System.out.println( name + " = " + value);

   }

%>

  <formaction="index.jsp" method="POST">

    名称:<input type=text size=20name="dataName">

     <br>

    值:<input type=text size=20name="dataValue">

     <br>

    <inputtype=submit>

   </form>

</body>

</html>

 

五、测试成果

启动tomcat 和httpd 服务

/usr/local/ apache-tomcat-7.0.29-lb1//bin/startup.sh

/usr/local/ apache-tomcat-7.0.29-lb12/bin/startup.sh

/usr/local/apache2/bin/apachectl restart

测试

打开浏览器访问localhot/test.jsp 时输出

Hello! The time is now (当前时间)

访问 localhost可以查看状态,可以看到每次请求依次连接到每个tomcat上

 六、访问localhot/index.jsp可以测试session 复制