天天看點

Nginx(Openresty)+Tomcat+Memcache實作負載均衡和Session存儲共享避免單點故障

杩欑瘒鏂囩珷鏄熀浜庡墠闈笁绡囧崥瀹㈠啀鍔犱笂tomcat瀹炵幇鐨勶紝鏈€鍚庡姞涓妔ession瀹炵幇浜ゅ弶瀛樺偍

Lnmp锛歨ttps://blog.csdn.net/weixin_43287982/article/details/87806322

MemCache锛歨ttps://blog.csdn.net/weixin_43287982/article/details/88399502

OpenResty锛歨ttps://blog.csdn.net/weixin_43287982/article/details/88421662

鏈枃娑夊強鍒扮殑杞歡鏂囨湯鏂囨。涓湁涓嬭澆閾炬帴

Tomcat绠€浠嬶細

  • Tomcat 鏈嶅姟鍣ㄦ槸涓€涓厤璐圭殑寮€鏀炬簮浠g爜鐨刉eb 搴旂敤鏈嶅姟鍣紝灞炰簬杞婚噺绾у簲鐢ㄦ湇鍔″櫒锛屽湪涓皬鍨嬬郴缁熷拰骞跺彂璁塊棶鐢ㄦ埛涓嶆槸寰堝鐨勫満鍚堜笅琚櫘閬嶄嬌鐢紝鏄紑鍙戝拰璋冭瘯JSP 绋嬪簭鐨勯閫夈€傚浜庝竴涓垵瀛﹁€呮潵璇達紝鍙互杩欐牱璁や負锛屽綋鍦ㄤ竴鍙版満鍣ㄤ笂閰嶇疆濂紸pache 鏈嶅姟鍣紝鍙埄鐢ㄥ畠鍝嶅簲HTML锛堟爣鍑嗛€氱敤鏍囪璇█涓嬬殑涓€涓簲鐢級椤甸潰鐨勮闂姹傘€傚疄闄呬笂Tomcat鏄疉pache 鏈嶅姟鍣ㄧ殑鎵╁睍锛屼絾杩愯鏃跺畠鏄嫭绔嬭繍琛岀殑锛屾墍浠ュ綋浣犺繍琛宼omcat 鏃訛紝瀹冨疄闄呬笂浣滀負涓€涓笌Apache 鐙珛鐨勮繘绋嬪崟鐙繍琛岀殑銆?/li>
  • 璇€绐嶆槸锛屽綋閰嶇疆姝g‘鏃訛紝Apache 涓篐TML椤甸潰鏈嶅姟锛岃€孴omcat 瀹為檯涓婅繍琛孞SP 椤甸潰鍜孲ervlet銆傚彟澶栵紝Tomcat鍜孖IS绛塛eb鏈嶅姟鍣ㄤ竴鏍鳳紝鍏鋒湁澶勭悊HTML椤甸潰鐨勫姛鑳斤紝鍙﹀瀹冭繕鏄竴涓猄ervlet鍜孞SP瀹瑰櫒锛岀嫭绔嬬殑Servlet瀹瑰櫒鏄疶omcat鐨勯粯璁ゆā寮忥紝Tomcat涓€鑸鐞嗗姩鎬侀〉闈㈣€屽鐞嗛潤鎬丠TML鐨勮兘鍔涗笉濡侫pache鏈嶅姟鍣ㄣ€?/li>

涓€銆乯dk鐨勫畨瑁呬笌閰嶇疆

  1. 棣栧厛浠庡畼缃戜笅杞絡dk鍖咃紝瑙e帇鍒版寚瀹氱洰褰?usr/local/
[[email聽protected] ~]# tar zxf jdk-7u79-linux-x64.tar.gz -C /usr/local/
[[email聽protected] ~]# cd /usr/local/
[[email聽protected] local]# ln -s jdk1.7.0_79 java	##鍋氳蔣杩炴帴锛屽崌绾х殑鏃跺€欏彧鍗囩駭杞繛鎺ワ紝杈冧負鏂逛究
           
  1. 閰嶇疆java鐨勭幆澧冨彉閲?/li>
[[email聽protected] ~]# vim /etc/profile
鍦ㄦ枃绔犳渶鍚庢坊鍔狅細
 80 export JAVA_HOME=/usr/local/java
 81 export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
 82 export PATH=$PATH:$JAVA_HOME/bin
[[email聽protected] ~]# source /etc/profile
           
  1. 妫€娴媕dk鐜鏄惁姝g‘
[[email聽protected] ~]# vim test.java
public class test{
    public static void main(String[] args)
        {
            System.out.println("Hello World!");
        }
}
[[email聽protected] ~]# javac test.java 	#javac缂栬瘧.java鑴氭湰
[[email聽protected] ~]# java test		#杩愯鑴氭湰
Hello World!
           

浜屻€乀omcat鐨勫畨瑁呬笌閰嶇疆

  1. 涓嬭澆瀹夎鍖咃紝骞惰В鍘嬪埌鎸囧畾鐩綍/usr/local锛屾墦寮€tomcat骞舵煡鐪嬪叾绔彛
[[email聽protected] ~]# tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local/
[[email聽protected] ~]# cd /usr/local/
[[email聽protected] local]# ln -s apache-tomcat-7.0.37 tomcat
[[email聽protected] local]# pwd
/usr/local
[[email聽protected] local]# tomcat/bin/startup.sh 
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/java
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[[email聽protected] local]# netstat -atnlp
           
  1. 缂栬緫閰嶇疆鏂囦歡
[[email聽protected] ~]# vim /usr/local/openresty/nginx/conf/nginx.conf
 53         location ~ \.jsp$ {
 54             proxy_pass http://127.0.0.1:8080;
 55         }
[[email聽protected] ~]# /usr/local/openresty/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
[[email聽protected] ~]# /usr/local/openresty/nginx/sbin/nginx -s reload
           

鍦╤ttp妯″潡涓嬬殑server涓啓鍏?

  1. 杩涘叆tomcat鐨勫彂甯冪洰褰曪紝娴嬭瘯tomcat鏄惁瀹夎姝g‘
[[email聽protected] ~]# vim /usr/local/openresty/nginx/html/index.jsp
  1 the time is:<%=new java.util.Date() %>
           
  • 娴忚鍣ㄨ緭鍏ワ細http://172.25.254.1/index.jsp 鏄劇ず椤甸潰锛屼絾鏄笉鏄痶omcat鐨勫浘鍍?/li>
Nginx(Openresty)+Tomcat+Memcache實作負載均衡和Session存儲共享避免單點故障
  1. 灏唖erver1涓婇厤缃殑濂界殑tomcat锛屽彂閫佸埌server2涓?/li>
[[email聽protected] ~]# scp -r /usr/local/tomcat [email聽protected]:/usr/local/
[[email聽protected] ~]# scp -r /usr/local/java/ [email聽protected]:/usr/local/
           
  1. 鍦╯erver2涓婂悓鏍烽厤缃甹ava鐜鍙橀噺锛屽苟涓斿惎鍔╰omcat
[[email聽protected] ~]# vim /etc/profile
 80 export JAVA_HOME=/usr/local/java
 81 export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
 82 export PATH=$PATH:$JAVA_HOME/bin
[[email聽protected] ~]# source /etc/profile
[[email聽protected] ~]# /usr/local/tomcat/bin/startup.sh 
[[email聽protected] ~]# netstat -atnlp
           
  1. 瀹炵幇server1鍜宻erver2鐨則omcat璐熻澆鍧囪 
  • 淇敼nginx鐨勯厤缃枃浠?/li>
[[email聽protected] ~]# vim /usr/local/openresty/nginx/conf/nginx.conf

 17 http {
 18     upstream tomcat {
 19         server 172.25.254.1:8080;
 20         server 172.25.254.2:8080;
 21         }
 22 

 53         location ~ \.jsp$ {
 54             proxy_pass http://tomcat;
 55         }

 57         #location /memc {
 58         #    internal;
 59         #    memc_connect_timeout 100ms;
 60         #    memc_send_timeout 100ms;
 61         #    memc_read_timeout 100ms;
 62         #    set $memc_key $query_string;
 63         #    set $memc_exptime 300;
 64         #    memc_pass memcache;
 65         #}

[[email聽protected] ~]# /usr/local/openresty/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
[[email聽protected] ~]# /usr/local/openresty/nginx/sbin/nginx -s reload
           

杩欓噷鐨刪ttp妯″潡閲岄潰绗竴涓槸璁﹕erver1鍜宻erver2鍧囪 璐熻澆锛岀浜屼釜鏄垰鎵嶉厤缃殑鍙渶瑕佸皢IP璇ヤ負tomcat鍗沖彲锛岀涓変釜闇€瑕佹敞閲婃帀锛屼篃鏄互鍓嶅啓鐨勬ā鍧楋紝鍚庨潰鐨刾hp妯″潡涓嶅共鎵闆疄楠屽彲浠ヤ笉鐢ㄦ敞閲?

  • 缂栧啓娴嬭瘯椤靛唴瀹?/li>
[[email聽protected] ~]# vim /usr/local/tomcat/webapps/ROOT/test.jsp
server1 the time is:<%=new java.util.Date() %>

[[email聽protected] ~]# vim /usr/local/tomcat/webapps/ROOT/test.jsp
server2 the time is:<%=new java.util.Date() %>
           
  1. 娴嬭瘯锛氭祻瑙堝櫒杈撳叆http://172.25.79.1:8080/ 鏄劇ずtomcat鐚浘褰㈢晫闈?/li>
Nginx(Openresty)+Tomcat+Memcache實作負載均衡和Session存儲共享避免單點故障
  • 娴忚鍣ㄨ緭鍏ワ細http://172.25.19.1/test.jsp 锛屽埛鏂幫紝鏄劇ずserver1 server2浜掓崲
Nginx(Openresty)+Tomcat+Memcache實作負載均衡和Session存儲共享避免單點故障
Nginx(Openresty)+Tomcat+Memcache實作負載均衡和Session存儲共享避免單點故障

涓夈€佸湪tomcat涓疄鐜皊ession鍏變韓

  • 浠€涔堟槸session锛?
  • 鍦ㄨ绠楁満涓紝灏ゅ叾鏄湪缃戠粶搴旂敤涓紝绉頒負鈥滀細璇濇帶鍒垛€濄€?
  • Session瀵矽薄瀛樺偍鐗瑰畾鐢ㄦ埛浼氳瘽鎵€闇€鐨勫睘鎬у強閰嶇疆淇℃伅銆傝繖鏍鳳紝褰撶敤鎴峰湪搴旂敤绋嬪簭鐨?Web 椤典箣闂磋煩杞椂锛屽瓨鍌ㄥ湪 Session瀵矽薄
  • 涓殑鍙橀噺灏嗕笉浼氫涪澶憋紝鑰屾槸鍦ㄦ暣涓敤鎴蜂細璇濅腑涓€鐩村瓨鍦ㄤ笅鍘彙€傚綋鐢ㄦ埛璇鋒眰鏉ヨ嚜搴旂敤绋嬪簭鐨?Web 椤墊椂锛屽鏋滆鐢ㄦ埛杩樻病鏈変細璇濓紝鍒?Web鏈嶅姟鍣ㄥ皢鑷姩鍒涘緩涓€涓?Session 瀵矽薄銆?
  • 褰撲細璇濊繃鏈熸垨琚斁寮冨悗锛屾湇鍔″櫒灏嗙粓姝㈣浼氳瘽銆?
  • Session瀵矽薄鏈€甯歌鐨勪竴涓敤娉曞氨鏄瓨鍌ㄧ敤鎴風殑棣栭€夐」銆備緥濡傦紝濡傛灉鐢ㄦ埛鎸囨槑涓嶅枩娆㈡煡鐪嬪浘褰紝灏卞彲浠ュ皢璇ヤ俊鎭瓨鍌ㄥ湪 Session 瀵矽薄涓€?
  • 鍦ㄨ绠楁満涓撲笟鏈閲岋細session鏄寚涓€涓粓绔敤鎴蜂笌浜や簰绯葷粺杩涜閫氫俊鐨勬椂闂撮棿闅旓紝閫氬父鎸囦粠娉ㄥ唽鍏ョ郴缁熷埌娉ㄩ攢绯葷粺涔嬮棿鎵€缁忚繃鐨勬椂闂翠互鍙婂鏋滈渶瑕佺殑璇濓紝鍙兘杩樻湁涓€瀹氭搷浣滅┖闂淬€?
  • session鎶€鏈垯鏄湇鍔$鐨勮В鍐蟲柟妗堬紝瀹冩槸閫氳繃鏈嶅姟鍣ㄦ潵淇濇寔鐘舵€佺殑銆傜敱浜嶴ession杩欎釜璇嶆眹鍖呭惈鐨勮涔夊緢澶氾紝鍥犳闇€瑕佸湪杩欓噷鏄庣‘涓€涓?Session鐨勫惈涔夈€傞鍏堬紝鎴戜滑閫氬父閮戒細鎶奡ession缈昏瘧鎴愪細璇濓紝鍥犳鎴戜滑鍙互鎶婂鎴風娴忚鍣ㄤ笌鏈嶅姟鍣ㄤ箣闂翠竴绯誨垪浜や簰鐨勫姩浣滅О涓轟竴涓?Session銆備粠杩欎釜璇箟鍑哄彂锛屾垜浠細鎻愬埌Session鎸佺畫鐨勬椂闂達紝浼氭彁鍒闆湪Session杩囩▼涓繘琛屼簡浠€涔堟搷浣滅瓑绛夛紱鍏舵锛孲ession鎸囩殑鏄湇鍔″櫒绔負瀹㈡埛绔墍寮€杈熺殑瀛樺偍绌洪棿锛屽湪鍏朵腑淇濆瓨鐨勪俊鎭氨鏄敤浜庝繚鎸佺姸鎬併€備粠杩欎釜璇箟鍑哄彂锛屾垜浠垯浼氭彁鍒闆線Session涓瓨鏀句粈涔堝唴瀹癸紝濡備綍鏍規嵁閿€間粠 Session涓幏鍙栧尮閰嶇殑鍐呭绛夈€傝浣跨敤Session锛岀涓€姝ュ綋鐒舵槸鍒涘緩Session浜嗐€傞偅涔圫ession鍦ㄤ綍鏃跺垱寤哄憿锛熷綋鐒惰繕鏄湪鏈嶅姟鍣ㄧ绋嬪簭杩愯鐨勮繃绋嬩腑鍒涘緩鐨勶紝涓嶅悓璇█瀹炵幇鐨勫簲鐢ㄧ▼搴忔湁涓嶅悓鍒涘緩Session鐨勬柟娉曪紝鑰屽湪Java涓槸閫氳繃璋冪敤HttpServletRequest鐨刧etSession鏂規硶锛堜嬌鐢╰rue浣滀負鍙傛暟锛夊垱寤虹殑銆傚湪鍒涘緩浜哠ession鐨勫悓鏃訛紝鏈嶅姟鍣ㄤ細涓鴻Session鐢熸垚鍞竴鐨凷ession id锛岃€岃繖涓猄ession id鍦ㄩ殢鍚庣殑璇鋒眰涓細琚敤鏉ラ噸鏂拌幏寰楀凡缁忓垱寤虹殑Session锛涘湪Session琚垱寤轟箣鍚庯紝灏卞彲浠ヨ皟鐢⊿ession鐩稿叧鐨勬柟娉曞線Session涓鍔犲唴瀹逛簡锛岃€岃繖浜涘唴瀹瑰彧浼氫繚瀛樺湪鏈嶅姟鍣ㄤ腑锛屽彂鍒闆鎴風鐨勫彧鏈塖ession id锛涘綋瀹㈡埛绔啀娆″彂閫佽姹傜殑鏃跺€欙紝浼氬皢杩欎釜Session id甯︿笂锛屾湇鍔″櫒鎺ュ彈鍒拌姹備箣鍚庡氨浼氫緷鎹甋ession id鎵懼埌鐩稿簲鐨凷ession锛屼粠鑰屽啀娆′嬌鐢ㄤ箣銆傛寮忚繖鏍蜂竴涓繃绋嬶紝鐢ㄦ埛鐨勭姸鎬佷篃灏卞緱浠ヤ繚鎸佷簡銆?
  • 涓轟粈涔堣浣跨敤session锛?
  • 涓轟簡瀵規暟鎹殑淇濆瓨鏇村姞鐗㈤潬锛屾垜浠€夋嫨浜ゅ弶瀛樻斁session鐨勬柟娉曪紝鍗沖皢璁塊棶tomcat1涓婄殑鏁版嵁瀛樻斁鍦╩emcache2涓紝灏嗚闂畉omcat2涓婄殑鏁版嵁瀛樻斁鍦╩emcache1涓紝杩欐牱瀛樻斁锛屽綋鏌愪釜tomcat鏈嶅姟鏂簡涔嬪悗锛岃闂畠鐨勫鎴穝ession骞朵笉浼氭秷澶憋紝鑰屾槸瀛樻斁鍦ㄤ簡瀵圭珛鐨刴emcache锛屽鏋滃瓨鏀緎ession鐨刴emcache鍧忎簡锛岄偅涔堝畠鐨勬暟鎹細绔嬪嵆鍒囨崲鍒闆彟涓€涓猰emcached涓?
Nginx(Openresty)+Tomcat+Memcache實作負載均衡和Session存儲共享避免單點故障

鍥涖€侀厤缃畇ession

  1. server1鍜宻erver2涓婄浉鍚岀殑鎿嶄綔锛岄噸鏂扮紪鍐欐祴璇曢〉鍐呭锛堜俊鎭彁浜ら〉闈級
[[email聽protected] ~]# vim /usr/local/tomcat/webapps/ROOT/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>");
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
}
out.print("<b>Session list</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="test.jsp" method="POST">
name:<input type=text size=20 name="dataName">
<br>
key:<input type=text size=20 name="dataValue">
<br>
<input type=submit>
</form>
</body>
</html>

[[email聽protected] ~]# vim /usr/local/tomcat/webapps/ROOT/test.jsp
           
  • 娴忚鍣ㄨ緭鍏ttp://172.25.254.1/test.jsp 鏄劇ず鏂扮殑娴嬭瘯椤甸潰锛屽紑濮嬫祴璇曪細
Nginx(Openresty)+Tomcat+Memcache實作負載均衡和Session存儲共享避免單點故障
Nginx(Openresty)+Tomcat+Memcache實作負載均衡和Session存儲共享避免單點故障
Nginx(Openresty)+Tomcat+Memcache實作負載均衡和Session存儲共享避免單點故障

鏍規嵁涓婇潰鐨勪笁寮犲浘鐗囷紝鍙互鐪嬪嚭姣忔鎻愪氦鏈嶅姟鍣ㄧ殑IP浼氬彉鍖栵紝杩欐槸鎴戜滑鐢ㄤ簡璐熻澆鍧囪 涔嬪悗鍑虹幇鐨勶紝灞炰簬姝e父鎯呭喌锛屼絾鏄繖閲岋紝鍚庨潰鐨勫唴瀹逛細瑕嗙洊鍓嶉潰鐨勫唴瀹癸紝杩欐樉鐒舵槸涓嶅悎鐞嗙殑

  1. 鏀矽繘涓婇潰鐨勮缃紝瀹炵幇鏁版嵁涓嶄氦鎹紝涓婁紶淇℃伅鍒闆悓涓€涓湇鍔″櫒
  • 瑙e帇nginx-1.10.1 鐗堟湰鐨勫帇缂╁寘锛宯ginx鐨剆ticky瀹夎鍖咃紝鍘繪帀nginx鐨勭増鏈彿浠ュ強鍏抽棴debug鏃ュ織锛岀紪璇戯紝娣誨姞sticky妯″潡锛岀劧鍚庡畨瑁咃紝鍏蟲帀openresty涓嬬殑nginx锛岄噸鏂扮紪杈憂ginx鐨勯厤缃枃浠訛紝鎶婁互鍓嶇殑openresty涓嬬殑nginx鏂囦歡澶嶅埗杩囨潵鍗沖彲锛屾煡鐪嬪苟娣誨姞涓€涓猻ticky妯″潡锛屾鏌ヨ娉曟槸鍚︽湁閿欒锛屽苟寮€鍚痭ginx銆? 杩欓噷鐨勬搷浣滄垜閮藉啓閬撲簡涓€璧鳳紝瀹為檯鎿嶄綔鐨勬椂鍊欎竴瀹氳璁よ鐪熺湡涓€姝ヤ竴姝ユ潵鍐?
[[email聽protected] ~]# tar zxf nginx-1.10.1.tar.gz
[[email聽protected] ~]# tar zxf nginx-sticky-module-ng.tar.gz
[[email聽protected] ~]# cd nginx-1.10.1
[[email聽protected] nginx-1.10.1]# vim src/core/nginx.h
[[email聽protected] nginx-1.10.1]# vim auto/cc/gcc 
[[email聽protected] nginx-1.10.1]# ./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --add-module=/root/nginx-sticky-module-ng
[[email聽protected] nginx-1.10.1]# make && make install
[[email聽protected] nginx-1.10.1]# /usr/local/openresty/nginx/sbin/nginx -s stop
[[email聽protected] nginx-1.10.1]# cp /usr/local/openresty/nginx/conf/nginx.conf /usr/local/lnmp/nginx/conf/
cp: overwrite `/usr/local/lnmp/nginx/conf/nginx.conf'? y
[[email聽protected] nginx-1.10.1]# vim /usr/local/lnmp/nginx/conf/nginx.conf

 17 http {
 18     upstream tomcat {
 19         sticky;		##娣誨姞
 20         server 172.25.254.1:8080;
 21         server 172.25.254.2:8080;
 22         }
濡傛灉鏈夎繖涓夎锛屾敞閲婁簡灏辮浜? 87         #    set $key $uri$args;
 88         #    srcache_fetch GET /memc $key;
 89         #    srcache_store PUT /memc $key;

[[email聽protected] nginx-1.10.1]# /usr/local/lnmp/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful
[[email聽protected] nginx-1.10.1]# /usr/local/lnmp/nginx/sbin/nginx
           
  • 鍦ㄦ祻瑙堝櫒杈撳叆http://172.25.19.1/test.jsp
Nginx(Openresty)+Tomcat+Memcache實作負載均衡和Session存儲共享避免單點故障
Nginx(Openresty)+Tomcat+Memcache實作負載均衡和Session存儲共享避免單點故障
Nginx(Openresty)+Tomcat+Memcache實作負載均衡和Session存儲共享避免單點故障

杩欓噷鎴戜滑瀹炵幇浜嗘暟鎹笉瑕嗙洊鍓嶉潰鐨勮€屾槸杩藉姞鍒闆悗闈紝浣嗘槸鍙戠幇鏁版嵁缁戝畾鐨勪富鏈洪兘鏄痵erver1

  • 閫氳繃鏌ョ湅鏃ュ織鍙互纭畾鏁版嵁鍐欏叆鍒扮殑鏄痵erver1鑰屼笉鏄痵erver2
[[email聽protected] nginx-1.10.1]# cat /usr/local/tomcat/logs/catalina.out    
Mar 12, 2019 8:54:44 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Mar 12, 2019 8:54:44 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Mar 12, 2019 8:54:44 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Mar 12, 2019 8:54:44 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 579 ms
Mar 12, 2019 8:54:44 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Mar 12, 2019 8:54:44 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.37
Mar 12, 2019 8:54:44 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.37/webapps/docs
Mar 12, 2019 8:54:44 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.37/webapps/host-manager
Mar 12, 2019 8:54:45 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.37/webapps/examples
Mar 12, 2019 8:54:45 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.37/webapps/manager
Mar 12, 2019 8:54:45 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.37/webapps/ROOT
Mar 12, 2019 8:54:45 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Mar 12, 2019 8:54:45 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Mar 12, 2019 8:54:45 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 566 ms
user2 = 222
user2 = 222
user2 = 222
user2 = 222
user3 = 333
user2 = 222
user4 = 444
user3 = 333
user2 = 222
user4 = 444
user3 = 333
user5 = 555

[[email聽protected] ~]# cat /usr/local/tomcat/logs/catalina.out  
Mar 12, 2019 8:54:44 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Mar 12, 2019 8:54:44 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Mar 12, 2019 8:54:44 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Mar 12, 2019 8:54:44 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 579 ms
Mar 12, 2019 8:54:44 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Mar 12, 2019 8:54:44 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.37
Mar 12, 2019 8:54:44 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.37/webapps/docs
Mar 12, 2019 8:54:44 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.37/webapps/host-manager
Mar 12, 2019 8:54:45 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.37/webapps/examples
Mar 12, 2019 8:54:45 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.37/webapps/manager
Mar 12, 2019 8:54:45 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/apache-tomcat-7.0.37/webapps/ROOT
Mar 12, 2019 8:54:45 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Mar 12, 2019 8:54:45 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Mar 12, 2019 8:54:45 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 566 ms
Mar 12, 2019 9:10:07 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Mar 12, 2019 9:10:07 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Mar 12, 2019 9:10:07 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Mar 12, 2019 9:10:07 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 942 ms
Mar 12, 2019 9:10:08 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Mar 12, 2019 9:10:08 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.37
Mar 12, 2019 9:10:08 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/tomcat/webapps/docs
Mar 12, 2019 9:10:44 PM org.apache.catalina.util.SessionIdGenerator createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [36,158] milliseconds.
Mar 12, 2019 9:10:44 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/tomcat/webapps/host-manager
Mar 12, 2019 9:10:44 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/tomcat/webapps/examples
Mar 12, 2019 9:10:45 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/tomcat/webapps/manager
Mar 12, 2019 9:10:45 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /usr/local/tomcat/webapps/ROOT
Mar 12, 2019 9:10:45 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Mar 12, 2019 9:10:45 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Mar 12, 2019 9:10:45 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 37165 ms
user1 = 111
           

杩欓噷鍙渶瑕佺湅鍚庨潰鐨勫氨鍙互浜?

  1. 瀹炵幇浜ゅ弶瀛樺偍
  • 涓轟簡鏂逛究鏌ョ湅瀹為獙缁撴灉锛屽厛娓呯┖鏃ュ織淇℃伅
[[email聽protected] nginx-1.10.1]# > /usr/local/tomcat/logs/catalina.out 

[[email聽protected] ~]# > /usr/local/tomcat/logs/catalina.out
           
  • server2涓婂畨瑁卪emcache锛屽苟鎵撳紑
[[email聽protected] ~]# yum install -y memcached
[[email聽protected] ~]# /etc/init.d/memcached start
           
  • 灏嗚繖浜涘畨瑁呭寘鏀懼湪server1鍜宻erver2鐨?usr/local/tomcat/lib鐩綍涓?/li>
Nginx(Openresty)+Tomcat+Memcache實作負載均衡和Session存儲共享避免單點故障
  • 鐒跺悗缂栬緫tomcat鐨勯厤缃枃浠?/li>
[[email聽protected] ~]# vim /usr/local/tomcat/conf/context.xml
  1 <?xml version='1.0' encoding='utf-8'?>
  2 <!--
  3   Licensed to the Apache Software Foundation (ASF) under one or more
  4   contributor license agreements.  See the NOTICE file distributed with
  5   this work for additional information regarding copyright ownership.
  6   The ASF licenses this file to You under the Apache License, Version 2.0
  7   (the "License"); you may not use this file except in compliance with
  8   the License.  You may obtain a copy of the License at
  9 
 10       http://www.apache.org/licenses/LICENSE-2.0
 11 
 12   Unless required by applicable law or agreed to in writing, software
 13   distributed under the License is distributed on an "AS IS" BASIS,
 14   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15   See the License for the specific language governing permissions and
 16   limitations under the License.
 17 -->
 18 <!-- The contents of this file will be loaded for each web application -->
 19 <Context>
 20 
 21     <!-- Default set of monitored resources -->
 22     <WatchedResource>WEB-INF/web.xml</WatchedResource>
 23 
 24     <!-- Uncomment this to disable session persistence across Tomcat restarts -->
 25     <!--
 26     <Manager pathname="" />
 27     -->
 28 
 29     <!-- Uncomment this to enable Comet connection tacking (provides events
 30          on session expiration as well as webapp lifecycle) -->
 31     <!--
 32     <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
 33     -->
 34 
 35 <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
 36 memcachedNodes="n1:172.25.254.1:11211,n2:172.25.254.2:11211"
 37 failoverNodes="n1"
 38 requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
 39 transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
 40 />
 41 
 42 </Context>
## 鍏跺疄鍙渶瑕佹坊鍔犲姞35-40琛?           
  • 灏嗛厤缃枃浠跺悓鏍峰彂閫佺粰server2
[[email聽protected] ~]# scp /usr/local/tomcat/conf/context.xml [email聽protected]:/usr/local/tomcat/conf/
## 鎷瘋礉鍒皊erver2涔嬪悗灏?7琛岀殑n1鏀逛負n2
## 杩欓噷闇€瑕侀噸鍚痶omcat
[[email聽protected] ~]# /usr/local/tomcat/bin/shutdown.sh 
[[email聽protected] ~]# /usr/local/tomcat/bin/startup.sh 

[[email聽protected] ~]# /usr/local/tomcat/bin/shutdown.sh 
[[email聽protected] ~]# /usr/local/tomcat/bin/startup.sh 
           
  • 鐒跺悗杩涘叆娴忚鍣ㄦ鏌ョ粨鏋滃氨OK浜?/li>

鎴戣繖閲岀粨鏋滃嚭浜嗛棶棰橈紝绗竴娆″仛娌¢棶棰橈紝鍐欒繖绡囧崥瀹㈤噸鏂闆仛浜嗕竴娆¤蔣浠跺寘缂哄け锛岄€犳垚鐨勭粨鏋滃嚭闂

杩欓噷璐翠竴鐗囨枃妗g粨鏋滄槸娌¢棶棰樼殑

nginx+tomcat+memcached

绯葷粺鐜:rhel6 x64 selinux and iptables disabled

涓繪満瑙掕壊:node1: 192.168.0.91:nginx tomcat memcached

node2: 192.168.0.92:tomcat memcached

杞歡涓嬭澆:http://www.nginx.org

http://code.google.com/p/memcached-session-manager/

Nginx(Openresty)+Tomcat+Memcache實作負載均衡和Session存儲共享避免單點故障

Tomcat-1 (T1) 灏?session 瀛樺偍鍦?memcached-2 (T2)涓娿€傚彧鏈夊綋 M2 涓嶅彲鐢ㄦ椂,T1 鎵嶅皢 session 瀛? 鍌ㄥ湪 memcached-1 涓?M1 鏄?T1 failoverNode)銆備嬌鐢ㄨ繖绉嶉厤缃殑濂藉鏄?褰?T1 鍜?M1 鍚屾椂宕? 婧冩椂涔熶笉浼氫涪澶?session 浼氳瘽,閬垮厤鍗曠偣鏁呴殰銆? 浠ヤ笅姝ラ鍦?node1 涓?node2 涓婂疄鏂?

tomcat 瀹夎

sh jdk-6u26-linux-x64.bin

mv jdk1.6.0_26/ /usr/local/jdk

vi /etc/profile

export JAVA_HOME=/usr/local/jdk

export CLASSPATH=: J A V A H O M E / l i b e x p o r t P A T H = JAVA_HOME/lib export PATH= JAVAH鈥婳ME/libexportPATH=PATH:$JAVA_HOME/bin

source /etc/profile

tar zxf apache-tomcat-7.0.8.tar.gz -C /usr/local

mv /usr/local/apache-tomcat-7.0.8 /usr/local/tomcat

session 鐨勫簭鍒楀寲鏂規瀹樻柟鎺ㄨ崘鐨勬湁 4 绉?

  1. java serialization
  2. msm-kryo-serializer
  3. msm-javolution-serializer
  4. msm-xstream-serializer

    鍏朵腑鎬ц兘鏈€濂界殑搴忓垪鍖栨柟妗堟槸 Kryo,姝ゅ疄楠屾垜浠噰鐢?kryo 鏂瑰紡銆? 鎶婂涓嬭蔣浠跺寘鏀劇疆鍒?usr/local/tomcat/lib 鐩綍涓? kryo-1.03.jar

    kryo-serializers-0.8.jar

    memcached-2.5.jar

    memcached-session-manager-1.5.1.jarmemcached-session-manager-tc7-1.5.1.jar

    minlog-1.2.jar

    msm-kryo-serializer-1.5.1.jar

    reflectasm-0.9.jar

    vi /usr/local/tomcat/conf/context.xml

    鈥? <Manager className=鈥渄e.javakaffee.web.msm.MemcachedBackupSessionManager鈥? memcachedNodes=鈥渘1:192.168.0.91:11211,n2:192.168.0.92:11211鈥? failoverNodes=鈥渘1鈥? #鍦?node2 涓婃椤矽缃負鈥渘2鈥? requestUriIgnorePattern="..(ico|png|gif|jpg|css|js)KaTeX parse error: Expected 'EOF', got '#' at position 145: 鈥.sh 鍚姩 tomcat #灘/usr/local/tomc鈥?{

    #鎵€鏈?jsp 椤甸潰浜ょ粰 tomcat 澶勭悊,鍔ㄩ潤鍒嗙

    proxy_pass

    http://desktop91.example.com;

    }

    }

    }

    鍒版閰嶇疆瀹屾垚,鍙互娴嬭瘯浜?

    浠ヤ笅涓烘祴璇曢〉闈?淇濆瓨鍒?usr/local/tomcat/webapps/ROOT/test.jsp<%@ page contentType=鈥渢ext/html; charset=GBK鈥?%>

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

Cluster App Test Server Info: <% out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"

");%> <% out.println("

ID " + session.getId()+"

"); String dataName = request.getParameter("dataName"); if (dataName != null && dataName.length() > 0) { String dataValue = request.getParameter("dataValue"); session.setAttribute(dataName, dataValue); } out.print("

Session list

"); Enumeration e = session.getAttributeNames(); while (e.hasMoreElements()) { String name = (String)e.nextElement(); String value = session.getAttribute(name).toString(); out.println( name + " = " + value+"

"); System.out.println( name + " = " + value); } %> name:

key:

璁塊棶 http://desktop91.example.com/test.jsp,涓嶅悓鐨勪富鏈鴻闂椂浼氳皟搴﹀埌涓嶅悓鐨?tomcat 瀹炰緥涓婂鐞?鏉ヨ嚜鍚屼竴涓繪満鐨勮姹備細浜ょ粰鍚屼竴涓?tomcat 瀹炰緥澶勭悊,姝ゆ椂浣?down 鎺夊綋鍓嶆鍦ㄥ搷搴旂殑 tomcat 瀹?渚?nginx 浼氳嚜鍔ㄦ妸鐢ㄦ埛鐨勮姹傝皟搴﹀埌鍙︿竴涓?tomcat 瀹炰緥涓?鍚屾椂 session 涔熸病鏈変涪鎺夈€?/body>