PDA

View Full Version : Cấu hình load phân tải(balancing) với tomcat.



tungt84
23-07-2010, 04:07 PM
Khi các ứng dụng web càng lúc càng nhiều người sử dụng thì tomcat của bạn có lẽ càng lúc càng chịu tải. Đến lúc đó chúng ta cần xem xét đến giải pháp phân tải(load balancing) xuống nhiều máy.
http://www.netdigix.com/images/load-balance.jpg

Để sử dụng cơ chế load balancing chúng ta cần 3 máy tính:

* Máy thứ nhất cài đặt apache làm nhiệm vụ chuyển request xuống máy 2 và 3.

* Máy thứ 2 là máy cài đặt tomcat xừ lý các logic cùa chương trình.

* Máy thứ 3 là máy cài đặt tomcat xừ lý các logic cùa chương trình.

Thực hiện lần lượt các bước sau để cấu hình load balancing với cấu hình mình chọn là cài đặt trên localhost có nghĩa là cài 3 thành phần trên lên cùng 1 máy(Nếu bạn không cài trên cùng 1 máy thì cấu hình lại các thuộc tính ip cho phù hợp):

Cài đặt và cấu hình mod_jk trong apache trên máy 1
Tải mod_jk tại http://www.apache.org/dist/tomcat/tomcat-c...rs/jk/binaries/ (http://www.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/)
Chọn và tải về bản mod_jk phù hợp(Mình dùng Apache 2.2 Window XP 32bit thì mình tải bản http://www.apache.org/dist/tomcat/tomcat-c...httpd-2.2.3.so) (http://www.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/win32/jk-1.2.28/mod_jk-1.2.28-httpd-2.2.3.so))
Chép mod_jk-*-so này vào thư modules của apache và đổi tên thành mod_jk.so.
Vào thư mục conf của Apache và thêm tập tin httpd.conf:

Include "conf/extra/jk_mod.conf"Trong thư mục conf tạo thư mục extra nếu chưa tồn tại.Tạo trong thư mục extra tập tin jk_mod.conf với nội dung sau:

<IfModule !mod_jk.c>
LoadModule jk_module modules/mod_jk.so
</IfModule>
JkWorkersFile "conf/workers.properties"
JkLogFile "logs/mod_jk.log"
JkLogLevel info
<VirtualHost localhost>
ServerName localhost
JkMount /myApp lb
JkMount /myApp/* lb
</VirtualHost>
Tạo tập tin workers.properties với nội dung sau:
# Define the path separator appropriate to the platform we are using
# For Windows Systems
ps=\
# For Linux /Unix Systems
#ps=/
# Define the load balancing worker only, and not other workers.
worker.list=lb
# ------------------------------------------------------------------------
# First Tomcatinstance running on local machine (localhost)
# ------------------------------------------------------------------------
# Set the port on which it will listen
worker.tomcat1.port=8009
# Set the host on which the Tomcat worker is running
worker.tomcat1.host=localhost
# Set the type of worker; here we are using ajp13
worker.tomcat1.type=ajp13
# Specify the load-balancing factor, any value greater than 0
worker.tomcat1.lbfactor=10
# Specify the size of the open connection cache.
worker.tomcat1.cachesize=5
# ------------------------------------------------------------------------
# Second Tomcat instance running on local machine (localhost)
# ------------------------------------------------------------------------
# Set the port on which it will listen
worker.tomcat2.port=8010
# Set the host on which the Tomcat worker is running
worker.tomcat2.host=localhost
# Set the type of worker; here we are using ajp13
worker.tomcat2.type=ajp13
# Specify the load-balancing factor, any value greater than 0
worker.tomcat2.lbfactor=10
# Specify the size of the open connection cache.
worker.tomcat2.cachesize=5
# ------------------------
# Load Balancer worker
# ------------------------
worker.lb.type=lb
# State the comma-separated name of workers that will form part of this
# load balancing mechanism
worker.lb.balanced_workers=tomcat1, tomcat2Cấu hình tomcat trên máy 2 và máy 3:
Cấu hình máy 2(tomcat1):
Mở tập tin server.xml trong thư mục conf của tomcat và thay đổi các thông tin được bôi đậm sau(port 8005,8009 và jvmRoute="tomcat1"):

<Server port="8005" shutdown="SHUTDOWN">
.................................................. ...............
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>
.................................................. ................
<Engine defaultHost="localhost" name="Catalina" jvmRoute="tomcat1">
.................................................. ...........
Cấu hình máy 3(tomcat2) tương tự làm với máy 2 chỉ khác biệt lần lượt là 8006,8010 và tomcat2:

<Server port="8006" shutdown="SHUTDOWN">
.................................................. ...............
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443"/>
.................................................. ................
<Engine defaultHost="localhost" name="Catalina" jvmRoute="tomcat2">
.................................................. ...........Như vậy là đã hoàn tất. Bạn có thể deploy ứng dụng vào hai máy 2 và 3(abc.war) và sau đó truy xuất vào máy 1(http://may1ip/abc/) để request phân bổ vài 2 máy 2 và 3.
Nguồn http://techs.box-idea.com/