使用apache做为控制转发器,后端使用4个tomcat做集群,此次选择的配置方式为SessionSticky(粘性Session),这种方式将同一用户的请求转发到特定的Tomcat服务器上,避免了集群中Session的复制。
apache以前配置apache和tomcat可能需要用到mod_jk.so进行apache和tomcat的链接。apache2.2中集成了mod_jk.so的功能,只需简单的把上面6个module的注释打开。
1、apache 配置
修改Apache配置文件\Apache2.2\conf\httpd.conf
a.将以下6个Module的注释去掉:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
再找到<IfModule dir_module></IfModule>加上index.jsp
<IfModule dir_module> DirectoryIndex index.html index.jsp </IfModule>
在文件的最下面加入
ProxyRequests Off <proxy balancer://cluster> BalancerMember ajp://127.0.0.1:9001 loadfactor=1 route=jvm1 BalancerMember ajp://127.0.0.1:9002 loadfactor=1 route=jvm2 BalancerMember ajp://127.0.0.1:9003 loadfactor=1 route=jvm3 BalancerMember ajp://127.0.0.1:9004 loadfactor=1 route=jvm4 <proxy>
设置apache2.2虚拟主机
a.先在conf\httpd.conf中引入配置虚拟主机的控制文件,将配置文件中默认注释掉的配置信息放开就可以了:
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
b.修改conf/extra/httpd-vhosts.conf文件,在最下面加入虚拟主机的配置信息
<VirtualHost *:80> ServerAdmin [email protected] ServerName localhost(域名) ServerAlias localhost(别名) ProxyPass / balancer://cluster/ stickysession=jsessionid nofailover=On ProxyPassReverse / balancer://cluster/ </VirtualHost>
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="6"> <Manager className="org.apache.catalina.ha.session.BackupManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true" mapSendOptions="6"/> <!-- <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="45562" frequency="500" dropTime="3000"/> <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="auto" port="5002" selectorTimeout="100" 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"/> <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/> </Channel> <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/> <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/> </Cluster>
<VirtualHost *:80> ServerAdmin [email protected] ServerName localhost(域名) ProxyPass / balancer://cluster/ lbmethod=byrequests stickysession=JSESSIONID nofailover=Off timeout=5 maxattempts=3 ProxyPassReverse / balancer://cluster/ ErrorLog "logs/lbtest-error.log" CustomLog "logs/lbtest-access.log" common <proxy balancer://cluster> BalancerMember ajp://127.0.0.1:9001 route=jvm1 BalancerMember ajp://127.0.0.1:9002 route=jvm2 BalancerMember ajp://127.0.0.1:9003 route=jvm3 </proxy> </VirtualHost>