2008年10月31日星期五

利用 mod_jk 或 mod_proxy 實現 Apache-2.2 + Tomcat-6 負載平衡(Load Balance) 和 集群技術 (Cluster)

Tomcat 是一個廣為人知的 JSP/Servlet 伺服器, 人們通常將它與 Apache 網頁服務器一同使用, 從而增加傳輸速度。

在這篇文章中, 我會示範如何利用 mod_jk 或 mod_proxy 設定 Apache-2.2 + Tomcat-6 平衡負載(Load Balance) 和 集群技術 (Cluster)。


請準備以下的軟件:
  1. Apache web server 2.2.x
  2. Tomcat-6.x
  3. mod_jk


※ 下載位址 :

- Apache web server
http://httpd.apache.org/

- Tomcat
http://tomcat.apache.org

- mod_jk 1.2
http://tomcat.apache.org/download-connectors.cgi



以下是各伺服器的 IP 設定:

Server (IP)
=========================
Apache-2.2.x (192.168.0.1)
Tomcat-6.x(1) (192.168.0.10)
Tomcat-6.x(2) (192.168.0.20)



首先介紹如何用 MOD_JK 1.2 實現平衡負載(Load Balance) 和 集群技術 (Cluster)

- 在 httpd.conf 下加入以下腳本

# -------- START HERE -----------
# Load mod_jk module
# Specify the filename of the mod_jk lib
LoadModule jk_module modules/mod_jk-apache-2.2.3.so

# Where to find workers.properties
JkWorkersFile conf/workers.properties

# Where to put jk logs
JkLogFile logs/mod_jk.log

# Set the jk log level [debug/error/info]
JkLogLevel info

# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"

# JkOptions indicates to send SSK KEY SIZE
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

# JkRequestLogFormat
JkRequestLogFormat "%w %V %T"

JkMount /* loadbalancer

# ------------ END HERE -----------------




接著介紹如何用 MOD_JK 1.2 實現平衡負載(Load Balance) 和 集群技術 (Cluster)

- 在 httpd.conf 下加入以下腳本


# ---------------- START HERE -----------------------

<Location /balancer-manager&rt;
SetHandler balancer-manager
</Location&rt;

<Proxy balancer://cluster&rt;
BalancerMember ajp://192.168.0.10:8009
BalancerMember ajp://192.168.0.20:8009
</Proxy&rt;

<Location /&rt;
ProxyPass balancer://cluster/ stickysession=JSESSIONID
</Location&rt;


# ---------------- END HERE ------------------



安裝完 Tomcat-6.x 後,在 $TOMCAT_HOME/conf 下開啟 server.xml 並 解除 Cluster 註釋



所有設定已完成, 請先啟動 Tomcat 後再啟動 Apache.