配置Apache2.2.10Server Load Balancer+Tomcat6叢集(linux)

來源:互聯網
上載者:User

一.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那麼麻煩!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.