天天看點

使用apache+tomcat 負載均衡 session共享

鎴戠殑鐜锛歝entos6.4

聽聽 浣跨敤杞歡锛氾紙apache鐩稿叧锛塧pr-1.5.1.tar.gz锛宎pr-util-1.5.4.tar.gz锛宲cre-8.36.tar.gz锛宧ttpd-2.2.29.tar.gz

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 锛坱omcat鐩稿叧锛塲re-7u71-linux-i586.tar.gz锛宎pache-tomcat-7.0.57.tar.gz聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 锛堟祴璇晄ession锛塼estsession.war

聽 涓嬭澆鍦闆潃锛歨ttp://pan.baidu.com/s/1ntupCA9

聽聽聽 瑁卆pache

瀹夎apache闇€瑕乤pr锛宎pr-util锛宲cre缁勪歡

聽聽聽聽聽聽聽瀹夎apr

聽聽聽聽聽聽聽聽聽聽聽聽聽 瑙e帇锛歵ar聽 -zxvf聽聽 apr-1.5.1.tar.gz

聽聽聽聽聽聽聽聽聽cd apr-1.5.1

聽聽聽聽聽./configure

聽聽聽聽聽聽聽聽聽聽make聽 &&聽 make install

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 瀹夎apr-util

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 tar -zxvf apr-util-1.5.4.tar.gz

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽

cd聽 apr-util-1.5.4聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽

./configure聽--with-apr=/usr/loca/apr聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽

make && make install聽聽聽聽聽聽聽聽

瀹夎pcre

聽聽聽聽聽聽聽聽聽聽tar -zxvf聽聽 pcre-8.36.tar.gz聽聽聽聽聽聽聽聽聽聽

聽聽聽聽聽聽聽聽聽聽聽聽cd聽聽 pcre-8.36

聽聽聽聽聽聽聽聽聽聽聽聽聽 ./configure聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽

聽聽聽聽聽聽聽聽聽聽聽聽聽make && makeinstall聽聽聽聽聽

聽聽聽聽聽聽聽聽聽聽聽聽聽 瀹夎apache聽聽聽聽聽聽聽聽聽聽聽聽聽

聽聽聽聽聽聽聽聽聽聽聽聽聽 tar -zxvfhttpd-2.2.29.tar.gz

聽聽聽聽聽聽聽聽聽聽聽聽cd httpd-2.2.29

聽聽聽聽聽聽聽聽聽./configure --prefix=/usr/local/httpd --enable-mods-shared='proxy proxy_ajp proxy_balancer'聽聽聽聽聽聽聽聽聽聽 聽聽

聽聽聽聽聽聽聽聽聽make聽 && make install聽聽聽

聽聽聽聽聽聽聽聽聽鈶¢厤缃畉omcat聽聽聽聽聽聽聽聽聽聽聽聽

聽聽聽聽聽聽聽聽聽閰嶇疆jre 鐜聽聽聽聽聽聽聽聽聽聽聽聽

聽聽聽聽聽聽聽聽聽瑙e帇 tar -zxvf聽 jre-7u71-linux-i586.tar.gz聽聽聽聽聽聽聽聽聽聽聽聽

聽聽聽聽聽聽聽聽聽灏唈re鏂囦歡澶規斁鍒?銆?usr/local 涓?

聽聽聽聽聽聽聽聽聽聽聽mv jre1.7.0_71/聽聽聽聽聽 /usr/local/jre1.7聽聽聽聽聽聽聽聽聽聽聽聽聽

聽聽聽聽聽聽聽聽聽聽聽瑙e帇tomcat聽聽聽聽聽聽聽聽聽聽聽

聽聽聽聽聽聽聽聽聽聽聽tar -zxvf聽聽 apache-tomcat-7.0.57.tar.gz

聽聽聽聽聽聽聽聽聽聽聽mv apache-tomcat-7.0.57聽聽聽聽/usr/local/tomcat701聽聽聽聽聽聽聽聽聽

聽聽聽聽聽聽聽聽聽聽聽涓簍omcat閰嶇疆jre鐜聽聽聽聽聽 cd聽 /usr/local/tocmat701/bin聽聽聽聽

聽聽聽聽聽聽聽聽聽聽聽vi startup.sh聽 鍐嶇浜岃娣誨姞聽聽 export JRE_HOME=/usr/local/jre1.7聽聽聽聽

聽 聽 聽 聽 聽聽

使用apache+tomcat 負載均衡 session共享

聽聽聽聽聽聽聽聽聽聽聽淇濆瓨锛岄€€鍑郝?鍚岀悊锛屽湪bin鐩綍涓嬬殑鍏朵粬.sh缁撳熬鏂囦歡涔熷姞涓婅繖涓€鍙?

聽 聽 聽 聽 聽 聽聽

使用apache+tomcat 負載均衡 session共享

聽聽聽聽聽聽聽聽聽聽聽聽鍚姩tomcat 锛?usr/local/tomcat701/bin/startup.sh聽

聽聽聽聽聽聽聽聽聽聽聽聽/usr/local/tomcat701/bin/catalina.shstart聽

聽聽聽聽聽聽聽聽聽聽聽聽鍏抽棴tomcat锛?usr/local/tomcat701/bin/shutdown.sh聽

聽聽聽聽聽聽聽聽聽聽聽聽/usr/local/tomcat701/bin/catalina.shstop聽

聽聽聽聽聽聽聽聽聽聽聽聽 鍚姩tomcat聽 娴忚鍣ㄨ闂甴ttp://ip:8080 鍑虹幇tomcat棣栭〉椤甸潰 鍗抽厤缃垚鍔熉犅犅犅?

聽聽聽聽聽聽聽聽聽聽聽聽

聽聽聽聽聽聽聽聽聽聽聽聽姝ゆ椂闂鍙兘鏈夛細 璁塊棶涓嶅埌锛堝叧闂槻鐏璇曡瘯銆?service iptables stop锛壜?

聽聽聽聽聽聽聽聽聽聽聽聽鏌ョ湅tomcat 杩愯鏃ュ織: /usr/local/tomcat701/bin/catalina.shstart ;tailf /usr/local/tomcat701/logs/catalina.out聽聽聽聽聽聽聽聽聽聽聽

聽聽聽聽聽聽聽聽聽聽聽聽

聽聽聽聽聽聽聽聽聽聽聽聽鍐嶅鍒朵竴涓猼omcat聽聽聽聽聽聽聽聽 cp聽聽 -R聽聽聽/usr/local/tomcat701聽聽聽 /usr/local/tomcat702聽

聽聽聽聽聽聽聽聽聽聽聽聽淇敼tomcat鐨勯厤缃枃浠訛紝 鍏堣鏄庝竴涓媡omcat 鐨勭洰褰曠粨鏋?

聽 聽 聽 聽 聽 聽 聽聽

使用apache+tomcat 負載均衡 session共享

聽聽聽聽聽聽聽聽聽聽聽聽聽 bin 锛氫富瑕佸瓨鏀総omcat鍚姩鎴栧叧闂?鐨勬枃浠堵?

聽聽聽聽聽聽聽聽聽聽聽聽聽 conf锛氬瓨鏀総omcat鐨勯厤缃枃浠堵?

聽聽聽聽聽聽聽聽聽聽聽聽聽 lib锛氬瓨鏀総omcat鎵€闇€鐨刯ar鏂囦歡聽

聽聽聽聽聽聽聽聽聽聽聽聽聽 logs锛氬瓨鏀総omcat杩愯鏃ュ織鏂囦歡聽

聽聽聽聽聽聽聽聽聽聽聽聽聽 temp锛氬瓨鏀句竴浜涗複鏃舵枃浠堵?

聽聽聽聽聽聽聽聽聽聽聽聽聽 webapps锛氶」鐩儴缃叉枃浠堵?

聽聽聽聽聽聽聽聽聽聽聽聽聽 work锛氶」鐩儴缃蹭箣鍚庣紦瀛樻枃浠堵犅犅?

聽聽聽聽聽聽聽聽聽聽聽聽聽

聽聽聽聽 conf/server.xml tomcat杩愯鏃剁殑涓昏閰嶇疆鏂囦歡聽 鍥犱負鏄湪鍚屼竴鍙版満鍣ㄤ笂鍋氭祴璇?锛屾墍浠ヤ袱涓猼omcat涓嶈兘浣跨敤鍚屼竴涓鍙o紝涓昏淇敼涓嬩慨鏀瑰叾涓竴涓猼omcat绔彛鍙仿?

聽聽聽聽聽聽聽聽聽vi /usr/local/tomcat701/conf/server.xml聽聽

聽聽聽聽聽<Connector port=8080 protocol="HTTP/1.1"聽聽聽聽聽聽聽聽聽聽聽聽聽聽

聽聽聽聽聽聽聽聽聽connectionTimeout="20000"聽聽聽聽聽聽聽聽聽聽聽聽聽聽

聽聽聽聽聽聽聽聽聽redirectPort="8443" />聽

聽聽聽聽聽聽聽聽聽8080绔彛涓簍omcat鏈嶅姟 http 鍗忚杩愯绔彛 鏇存敼姝ょ鍙d負8081聽

聽聽聽聽聽聽聽聽聽8443绔彛涓簍omcat鏈嶅姟 https 鍗忚鏃剁殑绔彛锛岄厤缃甴ttps鏃朵細浣跨敤鍒版绔彛銆備負閬垮厤閲嶅绔彛锛?鍙洿鏀逛負8444聽

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

聽 聽聽聽聽聽聽聽聽聽8009 涓簍omcat鏈嶅姟ajp 绔彛锛屽彲鏇存敼涓?010聽

聽聽聽聽聽聽聽聽聽聽8443 涓庝笂闈㈢鍙d繚鎸佷竴鑷達紝鏇存敼涓?444聽

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

聽聽聽聽聽聽8005 涓簍omcat鏈嶅姟鍏抽棴鏃剁鍙o紝鍙洿鏀逛負8006绔彛銆偮犅犅?

聽聽聽聽聽聽鏇存敼瀹屾垚鍚?淇濆瓨閫€鍑恒€偮?

聽聽聽聽聽聽娴嬭瘯锛?鍒嗗埆鍦ㄦ祻瑙堝櫒涓?杈撳叆 http://ip:8080聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽http://ip:8081聽聽聽 鏈塼omcat棣栭〉鍑虹幇銆傚嵆鎴愬姛銆偮犅犅?

聽聽聽聽聽聽鈶㈤厤缃礋杞藉潎琛÷犅犅犅犅?

聽聽聽聽聽聽閰嶇疆apache锛?淇敼apache 閰嶇疆鏂囦歡聽 vi /usr/local/httpd/conf/httpd.conf聽聽聽聽

聽聽聽聽聽聽淇敼ServerName聽 ServerName 127.0.0.1:80聽

聽 聽 聽聽

使用apache+tomcat 負載均衡 session共享

聽聽聽聽聽聽聽鍦╤ttpd.conf鏂囦歡鏈熬鍔犱笂

聽聽聽聽聽聽聽聽ProxyRequests Off聽聽聽聽 #鍏抽棴浠g悊璇鋒眰

聽聽聽聽聽聽聽聽ProxyPass / balancer://myCluster/聽聽聽聽

聽聽聽聽聽聽聽聽<Proxy balancer://myCluster/>

聽聽聽聽聽聽聽聽BalancerMember ajp://localhost:8009 route=Tomcat1聽聽聽聽聽聽 #璐熻澆鍧囪 tomcat1鐨勮闂湴鍧€聽 localhost涓烘湰鍦般€?

聽聽聽聽聽聽聽聽BalancerMember ajp://localhost:8010 route=Tomcat2聽聽聽聽聽聽 #璐熻澆鍧囪 tomcat2鐨勮闂湴鍧€聽 localhost涓烘湰鍦般€?

聽聽聽聽聽聽聽聽</Proxy>聽

聽聽聽聽聽聽聽聽淇濆瓨閫€鍑恒€偮?

聽聽聽聽聽聽聽聽閲嶅惎apache 鏈嶅姟聽 /usr/local/httpd/bin/httpd -krestart聽聽聽

聽聽聽聽聽聽聽聽

聽聽聽聽聽聽聽聽閰嶇疆tomcat聽聽

聽聽聽聽聽聽聽聽鍒嗗埆閰嶇疆涓や釜tomcat鐨剆erver.xml鏂囦歡

聽聽聽聽聽聽聽聽聽聽淇敼Engine 鏍囩 娣誨姞 jvmRoute = "Tomcat1"聽<Engine name = "Standalone" defaultHost ="localhost" jvmRoute = "Tomcat1">聽

聽聽聽聽聽聽聽聽聽聽濡傚浘锛?

使用apache+tomcat 負載均衡 session共享

聽聽聽聽聽聽聽聽聽聽娉ㄦ剰锛氭澶勭殑jvmRoute ="Tomcat1"聽聽 Tomcat1 瀵瑰簲apache閰嶇疆鏂囦歡httpd.conf鏂囦歡涓殑 BalancerMember ajp://localhost:8009 route=Tomcat1聽聽聽 鍏朵腑姝ゅlocalhost:8009涓殑8009涔熶負tomcat鐨刟jp 鏈嶅姟绔彛銆偮?

聽聽聽聽聽聽聽聽聽聽鍚岀悊锛屼慨鏀瑰彟澶栦竴涓猼omcat 閰嶇疆鏂囦歡锛屼慨鏀逛負 <Engine name = "Standalone" defaultHost ="localhost" jvmRoute = "Tomcat2"> 姝omcat鐨刟jp绔彛涓?010聽

聽聽聽聽聽聽聽聽聽聽淇敼瀹屾瘯鍚?

聽聽聽聽聽聽聽聽聽聽灏唗estsession.war 鏂囦歡鏀懼埌涓や釜 toncat鐩綍/webapps聽 鏂囦歡澶逛笅聽

聽聽聽聽聽聽聽聽聽聽鍒嗗埆閲嶅惎涓や釜tomcat鏈嶅姟銆偮?

聽聽聽聽聽聽聽聽聽聽/usr/local/tomcat701/bin/catalina.sh stop

聽聽聽聽聽聽聽聽聽聽/usr/local/tomcat701/bin/catalina.sh start聽

聽聽聽聽聽聽聽聽聽聽/usr/local/tomcat702/bin/catalina.sh stop聽

聽聽聽聽聽聽聽聽聽聽/usr/local/tomcat702/bin/catalina.sh start聽聽聽

聽聽聽聽聽聽聽聽聽聽娴嬭瘯 娴忚鍣ㄨ緭鍏?http://ip/testsession/聽聽 鍒嗗埆鍒鋒柊椤甸潰锛屼細鍙戠幇鏄湪涓や釜tomcat涓婅疆璇紝鍗蟲垚鍔熴€?

聽聽聽聽聽聽聽聽聽聽濡傚浘锛毬犅犅?

聽聽聽聽聽聽聽聽聽聽

使用apache+tomcat 負載均衡 session共享
使用apache+tomcat 負載均衡 session共享

聽聽聽聽聽聽聽聽聽聽閰嶇疆session鍏變韓聽聽聽聽聽

聽聽聽聽聽聽聽聽聽聽涓昏鍙傝€冧簡tomcat瀹樼綉鐨勯厤缃細http://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html聽

聽聽聽聽聽聽聽聽聽聽 鍒嗗埆淇敼tomcat閰嶇疆鏂囦歡server.xml聽 鍦‥ngine 鏍囩涓嬫坊鍔?瀹樼綉涓婄殑閰嶇疆鎺ㄨ崘聽

<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="4000"
                      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>    
           

淇濆瓨锛岄€€鍑恒€?

鍒嗗埆閲嶅惎涓や釜tomcat

娴嬭瘯锛毬犳祻瑙堝櫒杈撳叆 http://ip/testsession/ 聽 鍒嗗埆鍒鋒柊椤甸潰锛屽彂鐜癷d鍊間笉鍙橈紝tomcat1锛宼omcat2鍒囨崲鏄劇ず锛屽嵆session鍏變韓鎴愬姛銆?

使用apache+tomcat 負載均衡 session共享
使用apache+tomcat 負載均衡 session共享

姝ら樁娈甸亣鍒扮殑闂锛?

閲嶅惎tomcat 璁塊棶搴旂敤 鎻愮ず503閿欒

使用apache+tomcat 負載均衡 session共享

淇敼閰嶇疆鏂囦歡涓殑address 鍜?hosts鏂囦歡

vi聽/etc/hosts

娣誨姞濡備笅锛?

192.168.1.65聽 聽 localhost

璇存槑锛?92.168.1.65涓烘垜鐨刬p鍦闆潃 聽鏀逛負浣犵殑ip鍦闆潃

使用apache+tomcat 負載均衡 session共享

淇敼tomcat 鐨剆erver.xml鏂囦歡

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

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 <spanstyle="color:#ff0000;">address="auto"</span>

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 port="5000"

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 聽聽聽selectorTimeout="100"

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽maxThreads="6"/>

聽address="auto"聽 鏀逛負聽 <spanstyle="font-family: Arial, Helvetica,sans-serif;">address="192.168.1.65"聽聽 鏈満鐨刬p鍦闆潃</span>

鍐嶆閲嶅惎tomcat 鍗沖彲銆?

鍙﹀闇€瑕佹敞鎰忕殑鏄?鎵€閮ㄧ講鐨勫簲鐢?web.xml 鏂囦歡涓渶娣誨姞聽 <distributable/> 聽鏀寔session 鍏變韓

鍙﹀ 鎵€浣跨敤鐨勬祴璇曟枃浠舵槸缃戜笂寰堝娴嬭瘯session鐢ㄧ殑渚嬪瓙

index.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.println("<b>Session 鍒楄〃</b><br>");
  System.out.println("============================");
  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="test2.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 version="2.5" 
	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">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <distributable/>
</web-app>
           

繼續閱讀