linux环境下apache2与tomcat6的负载配置

同样是环境搭建,以前也配置过,但是通过jk或者jk2之类的东西,仿过来的。这段时间,因为svn的配置过程体验及用户和我们公司的要求,也来准备了一下负载的配置。希望能有些用处。

配置过程从以下几个方面进行:

1.服务器的准备:

   用一台电脑(以下称为服务器一)做apache负载,另外一台(服务器二)做tomcat服务器。

   初步设想,在服务器一上搭建apache及其相关配置,在服务器二上搭建多个(我测试了两个)tomcat节点。

  服务器一对应地址:192.168.0.12

  服务器一对应地址:192.168.0.13

2.所需软件准备:

   httpd-2.2.16.tar.gz,给服务器一

   下面两个给服务器二:

   jdk-6u22-linux-i586-rpm.bin

   apache-tomcat-6.0.29.tar.gz

  就这些东西了.

3.开始安装:

  192.168.0.12

   在终端里,执行命令:

#tar zxvf  httpd-2.2.16.tar.gz

#cd httpd-2.2.16

#rm -f configure

#./buildconf

#./configure --prefix=/usr/local/apache2 \

                  --enable-auth-anon \

                  --enable-auth-dbm \

                  --enable-cache \

                  --enable-disk-cache \

                  --enable-expires \

                  --enable-file-cache \

                  --enable-headers \

                  --enable-info \

                  --enable-logio \

                  --enable-mem-cache \

                  --enable-proxy \

                  --enable-rewrite \

                  --enable-ssl \

                  --enable-unique-id \

                  --enable-usertrack \

                  --enable-vhost-alias \

                  --enable-mods-shared=most

#make && make install

以上过程如果都没有问题,那基本说明apache装好了,但是先不要急着配置httpd.conf,先把服务器二配置好了再说。

 

192.168.0.13:

先装jdk:这个比较简单,我就简略一些。

#chmod 777 jdk-6u22-linux-i586-rpm.bin

#./jdk-6u22-linux-i586-rpm.bin

安装到最后会提示让你输入enter,也就完成了安装。

配置jdk的环境变量:

#vi /etc/profile

然后启用配置后的文件,执行命令:

#source /etc/profile

查看java版本

#java -version

 

安装tomcat:

#tar zxvf apache-tomcat-6.0.29.tar.gz

#cp -fR apache-tomcat-6.0.29 /usr/local/tomcat-node1

#cp -fR apache-tomcat-6.0.29 /usr/local/tomcat-node2

创建了两个节点。

修改tomcat-node1中的server.xml配置如下(我的文件全部内容,注意红色部分):

<?xml version='1.0' encoding='utf-8'?>

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

<!--APR library loader. Documentation at /docs/apr.html -->
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine
="on" />
<!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jas
per-howto.html -->
<Listener className="org.apache.catalina.core.JasperListener" />
<!-- Prevent memory leaks due to use of particular java/javax APIs-->
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener
" />
<!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.h
tml -->
<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListe
ner" />

<!-- Global JNDI resources
Documentation at /docs/jndi-resources-howto.html
-->
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>

<!-- A "Service" is a collection of one or more "Connectors" that share
a single "Container" Note: A "Service" is not itself a "Container",
so you may not define subcomponents such as "Valves" at this level.
Documentation at /docs/config/service.html
-->
<Service name="Catalina">

<!--The connectors can use a shared executor, you can define one or more na
med thread pools-->
<!--
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="150" minSpareThreads="4"/>
-->


<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
    
<!-- Define a SSL HTTP/1.1 Connector on port 8443
This connector uses the JSSE configuration, when using APR, the
connector should be using the OpenSSL style configuration
described in the APR documentation -->
<!--
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
-->

<!-- Define an AJP 1.3 Connector on port 8009

这里是使用ajp连接时的配置

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


<!-- 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="node1">


<!--
<Engine name="Catalina" defaultHost="localhost">
-->
<!--For clustering, please take a look at documentation at:
/docs/cluster-howto.html (simple how to)
/docs/config/cluster.html (reference documentation) -->
<!--

关于集群的配置,对应原来文档中的部分,去掉注释,加上相应的配置信息

下面的配置基本上通用,没有特殊性

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

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


   
    <!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>

<!-- Define the default virtual host
Note: XML Schema validation will not work with Xerces 2.2.
-->
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
   
</Host>
</Engine>
</Service>
</Server>

tomcat-node2中要做同样的配置,但是有几点注意:

1)端口冲突:connector端口(有两个,一个是http,一个是ajp),shutdown端口

2)<Engine name="Catalina" defaultHost="localhost" jvmRoute="node2">换成了node2

上面这些配置好了,两个就应该独立了。

接下来配置应用项目的指向,我这里是将两个tomcat指向同一个应用test,位置/home/test

所以,在/usr/local/tomcat-node1/conf/Catalina/localhost/中创建文件test.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/test" docBase="/home/test" />

然后执行命令:

#cp /usr/local/tomcat-node1/conf/Catalina/localhost/test.xml  /usr/local/tomcat-node2/conf/Catalina/localhost/

这样两个tomcat就指向了同一个应用路径。

test应用内容很简单:

test.jsp

-WEB-INF

     web.xml


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.print("<b>Session 脕卤铆b>");
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="index.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>

 

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2
001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee htt
p://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
  <display-name>demo</display-name>


  <distributable /> <!--为了搭建集群-->


</web-app>

好了,tomcat服务器配置完成。

开始配置apache集群:

#vi /usr/local/apache2/conf/httpd.conf

在最后加上下面的东西:

ProxyRequests Off

ProxyPass / balancer://cluster/ stickysession=jsessionid nofailover=On
ProxyPass /temp http://192.168.0.12:80/temp/

<Proxy balancer://cluster>
BalancerMember http://192.168.0.12 loadfactor=1
BalancerMember http://192.168.0.13:8080 smax=1 loadfactor=1 route=node1
# Less powerful server,don't send as many requests there
BalancerMember http://192.168.0.13:7070 smax=1 loadfactor=2 route=node2
</Proxy>

上面的8080,7070是配置的两个tomcat节点的http端口。

 

然后按照,先启动tomcat,再启动apache的顺序启动就可以了,访问http://192.168.0.12/test/test.jsp

看看session是不是共享了?

 

over

 

 

posted on 2010-10-22 10:29  eric_chen  阅读(512)  评论(0编辑  收藏  举报