配置Apache2.2.10负载平衡+Tomcat6集群(linux)

一.liunx下安装jdk安装完好.

不在累述

 

二,配置负载均衡服务器Apache2.2.10
1.下载apache2.2.10,安装相关组件
http://www.sunfreeware.com/indexintel10.html中下载httpd-2.2.10.tar.gz
安装方法:
解压缩: tar -zxvf httpd-2.2.10.tar.gz

./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 --with-ssl=/usr/local/openssl -enable-unique-id --enable-usertrack --enable-vhost-alias --enable-mods-shared=most

make

make install

 

按实际情况修改apache 配置文件
找到 #ServerName http://www.example.com/ 在其下设置 ServerName 如下
ServerName http://www.mysite.com/
基中 http://www.mysite.com/ 为你网站名,也可用IP代替
找到 DocumentRoot "/usr/local/apache/htdocs"
设置你的 WEB 服务器的根目录 如
DocumentRoot "/myweb"
找到 DirectoryIndex index.html index.html.var 改为
DirectoryIndex index.html index.php index.htm
用下面命令启动WEB服务器
# /usr/local/apache2/bin/apachectl start
查看自己的站点是否正常 http://www.mysite.com/ 也可用IP
用 # /usr/local/apache2/bin/apachectl stop 可停止服务


2.查看编译进apache的模块:
#cd /usr/local/apache2/bin
#./apachectl -l
Compiled in modules:
core.c
prefork.c
http_core.c
mod_so.c

3.修改/usr/local/apache2/conf/httpd.conf文件

将以下Module的注释去掉(其实已经去掉了)
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so

并在最后面,增加
<Location /server-status>
SetHandler server-status
Order Deny,Allow
Deny from all
Allow from all
</Location>
<Location /balancer-manager>
SetHandler balancer-manager
Order Deny,Allow
Deny from all
Allow from all
</Location>
ProxyRequests Off
ProxyPass / balancer://tomcatcluster/ stickysession=jsessionid nofailover=On
ProxyPass /temp http://10.8.1.8:8080/temp/
#ProxyPassReverse / balancer://tomcatcluster/
ProxyPassReverse /ccas balancer://tomcatcluster/ccas/
ProxyPassReverse /clpay balancer://tomcatcluster/clpay/

<Proxy balancer://tomcatcluster>
BalancerMember http://10.8.1.8:8080 loadfactor=1
BalancerMember http://10.8.1.8:8081 smax=1 loadfactor=1
# Less powerful server,don't send as many requests there
BalancerMember http://10.8.1.8:8082 smax=1 loadfactor=2
</Proxy>

 

ProxyPass为代理转发的Url,即将所有访问/的请求转发到群集balancer://tomcatcluster
BalancerMember为群集的成员,即群集服务器A或B,负载均衡服务器会根据均衡规则来将请求转发给BalancerMember。

配置好后,启动Apahce服务器,访问localhost就会看到群集服务器中应用返回的结果。恭喜你,负载均衡和群集已经配置成功了。
访问localhost/balancer-manager,显示负载均衡有关信息


4.进入/usr/local/apache2/bin目录,运行
#./apachectl -t
显示Syntax OK,说明配置正确
5.启动/关闭/重新启动apache
启动apache
# ./apachectl start

关闭apache
# ./apachectl stop
输入:http://10.8.1.8:8080 显示 it's work! 测试页面


三,安装配置tomcat6集群
1.下载apache-tomcat-6.0.18.tar.gz(http://archive.apache.org/dist/t ... omcat-6.0.18.tar.gz)
2.安装apache-tomcat-6.0.18.tar.gz

安装在目录/usr/apache/tomcat
# gzip -d apache-tomcat-6.0.18.tar.gz

3.解决在solaris下显示验证码出错(有这个 错误的情况下)
在/usr/apache/tomcat/bin/catalina.sh中的第266/278行加上-Djava.awt.headless=true /

4.启动tomcat服务器 测试是否正常
# cd /usr/apache/tomcat/bin/
# ./startup.sh

5.修改tomcat 的 conf/server.xml 的<Engine>(使用arp协议时配置)
去掉注释<Engine name="Standalone" defaultHost="localhost" jvmRoute="tomcat1">
jvmRoute是tomcat路由标示,由此区分两台tomcat主机,那么第二台就改为
<Engine name="Standalone" defaultHost="localhost" jvmRoute="tomcat2">
加上注释<Engine name="Catalina" defaultHost="localhost">
6.修改tomcat 的 conf/server.xml 的<Connector>(使用arp协议时配置)
去掉注释<Connector port="8009" enableLookups="false" redirectPort="8443" debug="0" protocol="AJP/1.3" />
7.修改tomcat 的 conf/server.xml 的<Cluster>
<!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
-->
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions="6">

<Manager className="org.apache.catalina.ha.session.BackupManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"
mapSendOptions="6"/>
<!--
<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="5000"
selectorTimeout="100"
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"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
</Channel>

<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter=".*/.gif;.*/.js;.*/.jpg;.*/.png;.*/.htm;.*/.html;.*/.css;.*/.txt;"/>

<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.ClusterSessionListener"/>
</Cluster>

8.在每个webapps应用中,修改配置文件web.xml文件 添加元素<distributable/>
在web.xml文件中<web-app>元素下增加以下内容:
<!--此应用将与群集服务器复制Session-->
<distributable/>
具体修改如下:
修改前:
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
</web-app>
修改后:
<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
    <!--此应用将与群集服务器复制Session-->
    <distributable/>
</web-app>

 

四.测试页面代码:

<%@ page contentType="text/html; charset=GBK" %>      
<%@ page import="java.util.*" %>      
<html><head><title>Cluster App Test</title></head>      
<body>      
<%        
    System.out.println("SessionID:"  + session.getId());       
%>      
Server Info:       
<%       
out.println(request.getServerName() + " : " + request.getServerPort()+"<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><br>");         
   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="test.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>   
你就发现你的session是复制了的! 这里就没有tomcat5 配置jk_conf那么麻烦!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值