架構
Load Balancer
/ \
Tomcat1 Tomcat2
| \/ |
| /\ |
MemCached1 MemCached2
IP 配置
Load balancer :192.168.1.1
Tomcat1 : 192.168.1.2
Tomcat2 : 192.168.1.3
Memcached1 : 192.168.1.4
Memcached2 : 192.168.1.5
Load Balancer ( lighttpd ) 安裝
> mkdir -p /home/lighttpd/src
> cd /home/lighttpd/src/
> wget http://download.lighttpd.net/lighttpd/releases-1.4.x/lighttpd-1.4.28.tar.gz
> tar zxvf http://download.lighttpd.net/lighttpd/releases-1.4.x/lighttpd-1.4.28.tar.gz
> cd lighttpd-1.4.28
> ./configure --prefix=/home/lighttpd
> make; make install
> cp -R doc/config /home/lighttpd/etc
> vi /home/lighttpd/etc/lighttpd.conf
(加入或修正任何需要的設定,另外如果 proxy 沒設,加入 include "conf.d/proxy.conf")
> vi /home/lighttpd/etc/conf.d/proxy.conf
(加入 web application 的 proxy 設定,假設web application 叫 yw_test,你的第一台Application Server (Tomcat) IP 為 192.168.1.2 ,第二台為 192.168.1.3,都聽在 8080 埠上)
$HTTP["url"] =~ "^/yw_test/" { proxy.balance = "hash"
proxy.server = (
"" => (
("host" => "192.168.1.2", "port" => 8080),
("host" => "192.168.1.3", "port" => 8080)
)
)
}
Session Repository (Memcached) 安裝
> mkdir -p /home/memcached/src
> cd /home/memcached/src/
> wget http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz
> cd memcached-1.4.5
> ./configure --prefix=/home/memcached
> make; make install
啟動 Memcached
> /home/memcached/bin/memcached -m 32 -l 0.0.0.0 -p 11211 -d -u nobody
測試Application Server (Tomcat) 安裝
> telnet localhost 11211
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
stats <-- 需自行輸入指令
STAT pid 5148
STAT uptime 74567
STAT time 1308726049
STAT version 1.4.5
STAT pointer_size 64
STAT rusage_user 0.004999
STAT rusage_system 0.005999
STAT curr_connections 17
STAT total_connections 69
STAT connection_structures 18
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT get_hits 0
STAT get_misses 0
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 46
STAT bytes_written 2356
STAT limit_maxbytes 33554432
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT evictions 0
STAT reclaimed 0
END
quite <-- 需自行輸入的指令
Connection closed by foreign host.
> cd /home
> wget http://apache.stu.edu.tw/tomcat/tomcat-7/v7.0.16/bin/apache-tomcat-7.0.16.tar.gz
> mv apache-tomcat-7.0.14 tomcat
安裝 memcache-session-manager 相關 library
> cd /home/tomcat/lib
> wget http://memcached-session-manager.googlecode.com/files/memcached-session-manager-tc7-1.4.1.jar
> wget http://spymemcached.googlecode.com/files/spymemcached-2.7.jar
> wget http://memcached-session-manager.googlecode.com/files/kryo-1.03.jar
> wget http://memcached-session-manager.googlecode.com/files/minlog-1.2.jar
> wget http://memcached-session-manager.googlecode.com/files/reflectasm-0.9.jar
> wget http://mirrors.ibiblio.org/pub/mirrors/maven2/asm/asm/3.2/asm-3.2.jar
> wget --no-check-certificate http://github.com/downloads/magro/kryo-serializers/kryo-serializers-0.8.jar
> wget http://memcached-session-manager.googlecode.com/files/msm-kryo-serializer-1.4.0.jar
加入 Session 管理設定
> vi /home/tomcat/conf/context.xm
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:192.168.1.4:11211 n2:192.168.1.5:11211"
failoverNodes="n1"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" />
加入 Server 辨識
> vi /home/tomcat/conf/server.xml
(Engine的jvmRoute屬性,192.168.1.2 填入jvm1, 192.168.1.3 填入jvm2)
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
開啟Memcached-Session-Manager的log
> vi /home/tomcat/conf/logging.properties
加入以下設定
de.javakaffee.web.msm.level=FINE
重啟 Tomcat,並檢查 /home/tomcat/logs/catalina.out 是否有錯誤,有的話修正它
/home/tomcat/bin/shutdown.sh
/home/tomcat/bin/startup.sh
測試
建立一個 Web application (yw_test),並產生一個 JSP (test.jsp)內容如下
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ page import="java.net.InetAddress;" %>開啟你的網頁 http://192.168.1.1/yw_test/test.jsp,應該會看下樣這樣的結果
<%
String server_ip = "";
InetAddress inetAddress = InetAddress.getLocalHost();
server_ip = inetAddress.getHostAddress();
String abc = (String) session.getAttribute("abc");
String in_session = "false";
if (abc == null) {
session.setAttribute("abc", "value in session");
abc = "value not in session";
in_session = "false";
} else {
in_session = "true";
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
hello world! This is <%=server_ip%><br>
session id : <%=session.getId()%> <br>
abc value in session ? <%=in_session%><br>
abc value = <%=abc%>
</body>
</html>
hello world! This is 192.168.1.2因為proxy.balance 設定是 (IP) hash, 所以不一定一開始跳到的那台就會是 192.168.1.2,但一定會固定,而因為是第一次連,所以Session中沒值,再開一次應該就會變成
session id : FCA024212CE98ED51E1A1C626F1A3753-n2.jvm1
abc value in session ? false
abc value = value not in session
hello world! This is 192.168.1.2接下來將 192.168.1.2 的 Tomcat 關掉
session id : FCA024212CE98ED51E1A1C626F1A3753-n2.jvm1
abc value in session ? true
abc value = value in session
(> /home/tomcat/bin/shutdown.sh )
再開啟 http://192.168.1.1/yw_test/test.jsp 如果運作正常的話,應該會看到
hello world! This is 192.168.1.3可以看到跳到第二台Tomcat 了,但Session 還是在
session id : FCA024212CE98ED51E1A1C626F1A3753-n2.jvm2
abc value in session ? true
abc value = abc value in session
如果再把第一台Tomcat 打開,則又會顯示跳回第一台Tomcat
hello world! This is 192.168.1.2
session id : FCA024212CE98ED51E1A1C626F1A3753-n2.jvm1
abc value in session ? true
abc value = abc value in session
0 意見:
張貼留言