分类:
2011-11-19 20:35:55
原文地址:Apache+JK+Tomcat实现负载均衡 作者:tristan0720
注:基于应用的可用性需求,本文仅实现负载均衡,对于failover功能的实现,本文没有涉及。
目前,实现Tomcat集群的一般方法是采用Apache+JK+Tomcat的方式实现。具体原理是Apache使用JK native connector连接 Tomcat Server,JK native connector做为负载均衡器。其原理示意图如下:
Apache采用DNS轮询算法发送客户端请求到负载均衡器,负载均衡器再把请求转发到后端相应的Tomcat Server。JK native connector具有“会话保持”功能,同一会话后续发送的请求会发送到同一个Tomcat Server上。
2 软件安装 2.1 安装Apache
Apache安装在10.100.0.12上
安装目录:/ocs/ocsrun/billing/apache2
下载地址:
以下是安装步骤:
gzip -d httpd-2.2.21.tar.gz
tar -xvf httpd-2.2.21.tar
cd httpd-2.2.21
./configure --prefix=/ocs/ocsrun/billing/apache2 --enable-so
make
makeinstall
2.2 安装tomcatTomcat安装在10.100.0.12、10.100.0.14上
安装目录:/ocs/ocsrun/billing/tomcatCluster
下载地址:
以下是安装步骤:
gzip -d apache-tomcat-6.0.33.tar.gz
tar -xvf apache-tomcat-6.0.33.tar
mkdir -p /ocs/ocsrun/billing/tomcatCluster/ivr_query
10.100.0.12:
cp -R apache-tomcat-6.0.33 /ocs/ocsrun/billing/tomcatCluster/ivr_query/tomcat1
10.100.0.14:
cp -R apache-tomcat-6.0.33 /ocs/ocsrun/billing/tomcatCluster/ivr_query/tomcat2
2.3 安装JK 1.2.x native connectorJK 1.2.x native connector安装在10.100.0.12上。
下载地址:
以下是安装步骤:
gzip –d tomcat-connectors-1.2.32-src.tar.gz
tar –xvf tomcat-connectors-1.2.32-src.tar
cd tomcat-connectors-1.2.32-src/native
./configure --with-apxs=/ocs/ocsrun/billing/apache2/bin/apxs
gmake
gmake install
2.4 安装JDK在10.100.0.14上安装JDK
安装目录在/opt/jdk1.6.0_20,系统已自带。
3.1 配置tomcat注:tomcat不配置集群
编辑10.100.0.12上的/ocs/ocsrun/billing/tomcatCluster/tomcat1/conf/server.xml
|
编辑10.100.0.14上的/ocs/ocsrun/billing/tomcatCluster/tomcat2/conf/server.xml
|
3.2 配置JK 1.2.x native connector
编辑/ocs/ocsrun/billing/apache2/conf/workers.properties: #注:此路径和apache httpd.conf中配置要一致
注:以下红色是必要参数,蓝色是可选参数 # We define a load balancer worker # with name "balancer" worker.list=balancer worker.balancer.type=lb
worker.balancer.error_escalation_time=0 worker.balancer.max_reply_timeouts=10
# Now we add members to the load balancer # First member is "tomcat1", most # attributes are inherited from the # template "worker.template". worker.balancer.balance_workers=tomcat1,tomcat2 #worker.balancer.sticky_session=true --default #worker.balancer.sticky_session_force=true
#worker.tomcat1.reference=worker.template worker.tomcat1.host=10.100.0.12 worker.tomcat1.port=8109 worker.tomcat1.type=ajp13 # Activation allows to configure # whether this node should actually be used # A: active (use node fully) # D: disabled (only use, if sticky session needs this node) # S: stopped (do not use) # Since: 1.2.19 worker.tomcat1.activation=A
# Second member is "tomcat2", most # attributes are inherited from the # template "worker.template". #worker.tomcat2.reference=worker.template worker.tomcat2.host=10.100.0.14 worker.tomcat2.port=8209 worker.tomcat2.type=ajp13 # Activation allows to configure # whether this node should actually be used # A: active (use node fully) # D: disabled (only use, if sticky session needs this node) # S: stopped (do not use) # Since: 1.2.19 worker.tomcat2.activation=A
# Finally we put the parameters # which should apply to all our ajp13 # workers into the referenced template # - Type is ajp13 worker.template.type=ajp13
# - socket_connect_timeout: milliseconds, default=0 # Since: 1.2.27 worker.template.socket_connect_timeout=5000
# - socket_keepalive: boolean, default=false # Should we send TCP keepalive packets # when connection is idle (socket option)? worker.template.socket_keepalive=true
# - ping_mode: Character, default=none # When should we use cping/cpong connection probing? # C = directly after establishing a new connection # P = directly before sending each request # I = in regular intervals for idle connections # using the watchdog thread # A = all of the above # Since: 1.2.27 worker.template.ping_mode=A
# - ping_timeout: milliseconds, default=10000 # Wait timeout for cpong after cping # Can be overwritten for modes C and P # Using connect_timeout and prepost_timeout. # Since: 1.2.27 worker.template.ping_timeout=10000
# - connection_pool_minsize: number, default=connection_pool_size # Lower pool size when shrinking pool due # to idle connections # We want all connections to be closed when # idle for a long time in order to prevent # firewall problems. # Since: 1.2.16 worker.template.connection_pool_minsize=0
# - connection_pool_timeout: seconds, default=0 # Idle time, before a connection is eligible # for being closed (pool shrinking). # This should be the same value as connectionTimeout # in the Tomcat AJP connector, but there it is # milliseconds, here seconds. worker.template.connection_pool_timeout=600
# - reply_timeout: milliseconds, default=0 # Any pause longer than this timeout during waiting # for a part of the reply will abort handling the request # in mod_jk. The request will proceed running in # Tomcat, but the web server resources will be freed # and an error is send to the client. # For individual requests, the timeout can be overwritten # by the Apache environment variable JK_REPLY_TIMEOUT. # JK_REPLY_TIMEOUT since: 1.2.27 worker.template.reply_timeout=300000
# - recovery_options: number, default=0 # Bit mask to configure, if a request, which was send # to a backend successfully, should be retried on another backend # in case there's a problem with the response. # Value "3" disables retries, whenever a part of the request was # successfully send to the backend. worker.template.recovery_options=3 |
3.3 配置apache
编辑/ocs/ocsrun/billing/apache2/conf/httpd.conf
Listen 8888 #代理端口,集群上线时改为8010 LoadModule jk_module modules/mod_jk.so ServerName 10.100.0.12
JkWorkersFile /ocs/ocsrun/billing/apache2/conf/workers.properties #JK配置文件 JkShmFile logs/mod_jk.shm JkLogFile /proxy1/log/JK/logs/mod_jk.log #日志路径 JkLogLevel info #日志级别 JkLogStampFormat "[%a %b %d %H:%M:%S %Y] " #日志格式 JkMount /* balancer #发送所有请求到负载均衡器 |