最近在看负载均衡,以下是根据网络上的资料,加上自己的实践,小笔记录便于今后使用参考。多谢网上专家们的优贴!
一、所需软件安装
Apache HTTP Server:httpd-2.2.17-win32-x86-no_ssl.msi
Tomcat:tomcat_7
安装apache http server时注意端口冲突的问题;
二、使用JK实现负载均衡配置:
1.mod_jk: mod_jk-1.2.31-httpd-2.2.3.so 该文件需与tomcat版本对应否则报错
2.mod_jk.so文件复制到 %apache_home%/modules中;
3.在%apache_home%/conf/中新增文件mod_jk.conf,编辑文件:
# Load mod_jk module
LoadModule jk_module modules/mod_jk.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 indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"
# Send servlet for context /examples to worker named ajp13
#JkMount /servlet/* controller
# Send JSPs for context /examples to worker named ajp13
JkMount /* controller
4.在%apache_home%/conf/中新增文件workers.properties,编辑文件,其中注意文件中的worker.jvm1.port的值为该tomcat/conf/server.xml配置中的ajp13的端口编号:
#
# workers.properties
#
# list the workers by name
worker.list=controller
worker.controller.type=lb
worker.controller.sticky_session=1
worker.controller.error_escalation_time=0
worker.controller.max_reply_timeouts=10
# localhost server 1
# ------------------------
worker.jvm1.reference=worker.template
worker.jvm1.port=7009
worker.jvm1.host=localhost
worker.jvm1.lbfactor = 5 #数值大负载大
worker.jvm1.activation=A
# localhost server 2
# ------------------------
worker.jvm2.reference=worker.template
worker.jvm2.port=7019
worker.jvm2.host=localhost
worker.jvm2.lbfactor = 1
worker.jvm2.activation=A
worker.template.type=ajp13
worker.template.socket_connect_timeout=5000
worker.template.socket_keepalive=true
worker.template.ping_mode=A
worker.template.ping_timeout=10000
worker.template.connection_pool_minsize=0
worker.template.connection_pool_timeout=600
worker.template.reply_timeout=300000
worker.template.recovery_options=3
worker.controller.balance_workers=jvm1, jvm2
5.在%apache_home%/conf/httpd.conf文件末位引用mod_jk.conf:
Include conf/mod_jk.conf
即使用apache jk模式的负载均衡配置完成。
三、Tomcat集群session复制:
参考%tomcat_home%/webapps/docs/cluster-howto.html,并session对象必须序列化;
1.编辑%tomcat_home%/conf/server.xml文件在<Engine>属性中配置cluster信息及:
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8">
<Manager className="org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown="false"
notifyListenersOnReplication="true"/>
<Channel className="org.apache.catalina.tribes.group.GroupChannel">
<Membership className="org.apache.catalina.tribes.membership.McastService"
address="228.0.0.4"
port="45564"
frequency="500"
dropTime="3000"/>
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="192.168.105.4"
port="4001"
autoBind="100"
selectorTimeout="5000"
maxThreads="6"/>
<Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
<Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
</Sender>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
<Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
</Channel>
<Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
filter="/"/>
<Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
<Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir="/tmp/war-temp/"
deployDir="/tmp/war-deploy/"
watchDir="/tmp/war-listen/"
watchEnabled="false"/>
<ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
<ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
</Cluster>
2.将Engine节点增加jvmRoute属性,其值为workers.properties文件中worker.controller.balance_workers属性的其中一值
及 <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
3.如同一机器,配置好后保存复制tomcat,修改tomcat运行端口信息,及<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
address="192.168.105.4" port="4001" …/> 的port信息即可实现tomcat,session复制。
四、基于http_proxy的配置
http_proxy 模式是基于 HTTP 协议的代理,因此它要求 Tomcat 必须提供 HTTP 服务,也就是说必须启用 Tomcat 的 HTTP Connector。一个最简单的配置如下:
ProxyPass /images !
ProxyPass /css !
ProxyPass /js !
ProxyPass / balancer://example/
<Proxy balancer://example/>
BalancerMember http://server1:8080/
BalancerMember http://server2:8080/
BalancerMember http://server3:8080/
</Proxy>
采用 proxy 的连接方式,需要在 Apache 上加载所需的模块,mod_proxy 相关的模块有mod_proxy.so、mod_proxy_connect.so、mod_proxy_http.so、mod_proxy_ftp.so、mod_proxy_ajp.so, 其中mod_proxy_ajp.so 只在 Apache 2.2.x 中才有。如果是采用 http_proxy 方式则需要加载 mod_proxy.so 和mod_proxy_http.so;如果是 ajp_proxy 则需要加载 mod_proxy.so 和 mod_proxy_ajp.so这两个模块。