一、环境准备
Jdk1.6/apache 2.2.17/tomcat6.0.39
二、部署步骤
1、 建立一个j2ee webproject ,起名"cluster"。
2、修改项目web.xml,添加"<distributable/>"元素节点(红色所示,作用是用于session共享,下面有总结的)。如下:
<!DOCTYPEweb-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app> <display-name>Archetype Created Web Application</display-name>
<filter> <filter-name>cookieDomainFilter</filter-name> <filter-class>filters.CookieDomainFilter</filter-class> </filter> <filter-mapping> <filter-name>cookieDomainFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <distributable /> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list> </web-app> |
3、 tomcat重命名为tomcat-7a。修改conf/server.xml,具体可参见官方说明:http://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html
3.1、先在<Engine>节点下添加以下内容。
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" |
3.2、修改 <Connector port="18080" protocol="HTTP/1.1"connectionTimeout="20000" redirectPort="8443" />
3.3、修改 <Engine name="Catalina"defaultHost="localhost" jvmRoute="tomcat7_a">
3.4、修改 <Server port="8005"shutdown="SHUTDOWN">
3.4、修改 <Connector port="8009" protocol="AJP/1.3"redirectPort="8443" />
4、复制并拷贝一份当前tomcat,重命名tomcat-7b,并修改server.xml 内容
<Server port="8006" shutdown="SHUTDOWN"> |
**步骤4中的修改项,应该和步骤3中的对应上,只是端口号有略微修改,原因是在同一台机器上端口冲突,如果不是同一台机器,则无需修改。
到此我们tomcat集群配置结束。
三、 集群测试:
1、cluster工程新建test.jsp 内容如下:
<%@ page contentType="text/html; charset=GBK" %> |
** 阴影部分请自行修改。。。。并分别发布到2个tomcat 下。
2、分别启动tomcat7-a和tomcat7-b,无异常,表示启动成功。
查看页面:
以上SESSION已经同步。表示集群成功~~ HOHO
四、apache 负载均衡配置
### 在Apache安装目录下找到conf/httpd.conf文件
1、修改监听端口 :Listen 8000
2、打开以下注释,以便让Apache在启动时自动加载代理(proxy)模块
LoadModule proxy_module modules/mod_proxy.so |
3、添加以下内容到httpd.conf(care spaces:P)
<VirtualHost *:80> DocumentRoot D:/tools/tomcat-7a/webapps/cluster ServerName hostb.test.com ServerAlias hostb.test.com ProxyPreserveHost On ProxyPass / balancer://proxy/ stickysession=jsessionid nofailover=On ProxyPassReverse / balancer://proxy/ ProxyRequests Off </VirtualHost> <Proxy balancer://proxy> BalancerMember ajp://127.0.0.1:8009 loadfactor=1 route=tomcat7_a BalancerMember ajp://127.0.0.1:9009 loadfactor=1 route=tomcat7_b </Proxy> |
阴影部分应和两个tomcat的 ajp 对应上。
至此 apache 配置完成
分别启动apache,tomcat7-a,tomcat7-b,
在浏览器中键入"http://localhost:8000/cluster/test.jsp"刷新并查看控制台结果。
以上以集群部署已经成功,以IP方式访问,session共享。现在实现以域名访问集群的配置:
1、取消httpd.conf文件中Includeconf/extra/httpd-vhosts.conf的注释。
2、 http.conf文件只保留分发的配置,VirtualHost的配置注释:
#<VirtualHost *:80> # DocumentRoot D:/tools/tomcat-7a/webapps/cluster # ServerName hostb.test.com # ServerAlias hostb.test.com #ProxyPreserveHost On #ProxyPass / balancer://proxy/ stickysession=jsessionid #nofailover=On # ProxyPassReverse / balancer://proxy/ #ProxyRequests Off #</VirtualHost> <Proxy balancer://proxy> BalancerMember ajp://127.0.0.1:8009 loadfactor=1 route=tomcat7_a BalancerMember ajp://127.0.0.1:9009 loadfactor=1 route=tomcat7_b </Proxy> |
3、 修改httpd-vhosts.conf文件配置,添加以下配置:
<VirtualHost *:80> DocumentRoot D:/tools/tomcat-7a/webapps/cluster ServerName hosta.test.com ServerAlias hosta.test.com ProxyPreserveHost On ProxyPass / http://192.168.1.102:18080/cluster/ ProxyPassReverse / http://192.168.1.102:18080/cluster/ ProxyPassReverseCookieDomain localhost .24pay.net <Directory "D:/tools/tomcat-7a/webapps/cluster/WEB-INF"> Options Indexes FollowSymLinks AllowOverride All Order allow,deny Allow from all </Directory> RewriteEngine On RewriteRule ^/$ http://192.168.1.102:18080/cluster/index.jsp [P] </VirtualHost>
<VirtualHost *:80> DocumentRoot D:/tools/tomcat-7a/webapps/cluster ServerName hostb.test.com ServerAlias hostb.test.com ProxyPreserveHost On ProxyPass / http://192.168.1.102:28080/cluster/ ProxyPassReverse / http://192.168.1.102:28080/cluster/ <Directory "D:/tools/tomcat-7a/webapps/cluster/WEB-INF"> Options Indexes FollowSymLinks AllowOverride All Order allow,deny Allow from all </Directory> RewriteEngine On RewriteRule ^/$ http://192.168.1.102:28080/cluster/index.jsp [P] </VirtualHost>
<VirtualHost *:80> DocumentRoot D:/tools/tomcat-7a/webapps/cluster ServerName proxy.test.com ServerAlias proxy.test.com ProxyPreserveHost On ProxyPass / balancer://proxy/ stickysession=jsessionid nofailover=On ProxyPassReverse / balancer://proxy/ <Directory "D:/tools/tomcat-7a/webapps/cluster/WEB-INF"> Options Indexes FollowSymLinks AllowOverride All Order allow,deny Allow from all </Directory> RewriteEngine On RewriteRule ^/$ http://192.168.1.102:28080/cluster/index.jsp [P] </VirtualHost> |
4、 修改hosts文件,添加域名-ip映射:
192.168.1.102 hosta.test.com
192.168.1.102 hostb.test.com
192.168.1.102 proxy.test.com
5、由于域名不一致,导致session不能共享,现在暂时通过项目过滤器实现,后续可以研究能不能通过配置方式实现:
5.1、web.xml添加过滤器配置:
<filter> <filter-name>cookieDomainFilter</filter-name> <filter-class>filters.CookieDomainFilter</filter-class> </filter> <filter-mapping> <filter-name>cookieDomainFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> |
5.2、cookieDomainFilter过滤器的内容:
publicvoid doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest)request; HttpServletResponse res = (HttpServletResponse)response; Cookie cookie = new Cookie("JSESSIONID", req.getSession().getId()); cookie.setDomain(".test.com"); res.addCookie(cookie);
chain.doFilter(req, res); } |
最后,网上看到的session共享方式:Apache+Tomcat负载均衡两种session共享方式的设置