Apache负载均衡实战
含Session共享
2个或者多个tomcat
F:\JavaProgram\apache-tomcat-7.0.64-8088
F:\JavaProgram\apache-tomcat-7.0.64-8080
1个apache
F:\Apache24\bin
将此文件添加到目录F:\Apache24\modules
在最后一行添加
include "F:\Apache24\conf\mod_jk.conf"
在目录F:\Apache24\conf下添加文件,内容如下:
LoadModule jk_module modules/mod_jk.so
JkWorkersFile conf/workers.properties
#指定那些请求交给tomcat处理,"controller"为在workers.propertise里指定的负载分配控制器名
JkMount /* controller
在Server.xml文件中添加两端配置:
集群配置:
channelSendOptions= "6">
expireSessionsOnShutdown= "false"
notifyListenersOnReplication= "true"
mapSendOptions= "6"/>
address= "228.0.0.4"
port= "45564"
frequency= "500"
dropTime= "3000"/>
address= "auto"
port= "5000"
selectorTimeout= "100"
maxThreads= "6"/>
filter= ".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>
tempDir= "/tmp/war-temp/"
deployDir= "/tmp/war-deploy/"
watchDir= "/tmp/war-listen/"
watchEnabled= "false"/>
需要在应用的web.xml文件中添加如下配置,表示该应用将在分布式容器中部署:
在目录F:\Apache24\conf下添加文件,内容如下
#server
worker.list = controller
#========tomcat1========
worker.tomcat1.port=8009
worker.tomcat1.host=127.0.0.1
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor = 1
#========tomcat2========
worker.tomcat2.port=8019
worker.tomcat2.host=127.0.0.1
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor = 1
#========controller,负载均衡控制器========
worker.controller.type=lb
worker.controller.balanced_workers=tomcat1,tomcat2
worker.controller.sticky_session=false
worker.controller.sticky_session_force=1
#worker.controller.sticky_session=1
本文假设你的apahce安装目录为/usr/local/apache2,这些方法适合任何情况
apahce启动命令:
推荐/usr/local/apache2/bin/apachectl start apache启动
apache停止命令
/usr/local/apache2/bin/apachectl stop 停止
apache重新启动命令:
/usr/local/apache2/bin/apachectl restart重启
要在重启 Apache 服务器时不中断当前的连接,则应运行:
/usr/local/sbin/apachectl graceful
1、 session 复制的方式:BackupManager与DeltaManager
2、 Java 应用session的value需要序列化,否则无法实现session共享