使用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>
c.也可以直接在httpd.conf后面加入这段xml
2. 配置tomcat
a.配置tomcat的启动和关闭端口
若是在同一台机器上同时运行多个tomcat,则需要修改tomcat 的启动端口和关闭端口。
启动端口修改:将每个tomcat的server.xml文件作如下修改
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
改为
<Connector port="xxxx" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="xxxx" />
<Connector port="xxxx" protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443" />
如果需要用到tomcat的共享线程池,其端口值也设置为xxxx,保持一致(线程池缺省是被注释掉的):
<Connector executor="tomcatThreadPool"port="xxxx" protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443" />
b.设置tomcat的关闭端口:
将<Server port="8005" shutdown="SHUTDOWN">修改为
<Server port="xxxx" shutdown="SHUTDOWN">每个tomcat的端口xxxx不同就行。
c.配置engine
tomcat 默认的engine配置为:
<Engine name="Catalina" defaultHost="localhost">
为了通过AJP来支持负载均衡,需要配置engine的jvmRoute属性。将server.xml的默认engine配置注释掉,按给出的 jvmRoute示例做如下配置:
分别将4个tomcat配置为
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm2">
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm3">
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm4">
d.配置 AJP Connector
这里是apache和tomcat链接的关键,前台apache就是通过AJP协议与tomcat进行通信的,以完成负载均衡的作用。
将server.xml中的缺省配置
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
改成与
port的值与apache中的文件httpd.conf中<proxy balancer://cluster></proxy>之间配置的均衡器成员的端口一致,一一对应;上面在配置的jvmRoute的值(如jvm1)应该与httpd.conf中<proxy balancer://cluster></proxy>之间配置的均衡器成员的route属性的值一致,且必须唯一。
例如 tomcatA就改成9001
e.配置Cluster
打开tomcat的server.xml,默认的Cluster配置如下,缺省被注释掉了:
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
- <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="<span style="color: #ff0000;">45562</span>"
- frequency="500"
- dropTime="3000"/>
- <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
- address="auto"
- port="<span style="color: #ff0000;">5002</span>"
- 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>
每个tomcat的端口唯一
全部配置全成后,如果SessionSticky粘性session无效的话,将<proxy balancer://cluster>放到<VirtualHost *:80>
- <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>