第一步:安装apache
编译的时候需要加入mod_proxy模块,具体可以参考这篇文章apache和tomcat的整合
第二步:配置tomcat
分别找到需要加入负载均衡或热备的tomcat的配置文件server.xml,修改engine行,添加jvmroute。例如
<Engine name="Catalina" defaultHost="localhost" jvmRoute="node1">
如果不需要session复制,其他默认保存即可。
第三步:配置ajp_proxy
不建议使用apache的http_proxy。
找到apache虚拟机主机配置文件/usr/local/apache/conf/extra/httpd-vhost.conf,添加如下内容。这个是负载均衡配置。
ProxyRequests off
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
<Proxy balancer://www.sijitao.net>
BalancerMember ajp://192.168.122.102:8009 route=node1 loadfactor=1
BalancerMember ajp://192.168.122.105:8009 route=node2 loadfactor=1
#ProxySet stickysession=JSESSIONID
ProxySet stickysession=ROUTEID
#byrequests(default),bytraffic,bybusyness
ProxySet lbmethod=bytraffic
ProxySet nofailover=On
</Proxy>
ProxyPass /upload !
ProxyPass /balancer !
ProxyPass / balancer://www.sijitao.net/
ProxyPassReverse / balancer://www.sijitao.net/
<Location /balancer>
SetHandler balancer-manager
Proxypass !
Order allow,deny
Allow from all
</Location>
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
扫描二维码关注公众号,回复:
541939 查看本文章
<Location />
Order allow,deny
Allow from all
</Location>
添加tomcat主要就是BalancerMember这行,loadfactor值越高,在负载均衡中权重就越大。
如果不需要负载均衡,使用仅仅需要热备,修改BalancerMember ajp://192.168.122.105:8009 route=node2 loadfactor=1成BalancerMember ajp://192.168.122.105:8009 route=node2 status=+H 。
参考网址:
http://httpd.apache.org/docs/2.2/mod/mod_proxy.html
http://www.ibm.com/developerworks/cn/opensource/os-lo-apache-tomcat/