apache+tomcat集群部署总结

一、环境准备

Jdk1.6/apache 2.2.17/tomcat6.0.39

二、部署步骤

1、  建立一个j2ee webproject ,起名"cluster"。

2、修改项目web.xml,添加""元素节点(红色所示,作用是用于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 Applicationdisplay-name>

 

    <filter>

       <filter-name>cookieDomainFilterfilter-name>   <filter-class>filters.CookieDomainFilterfilter-class>

    filter>

    <filter-mapping>

       <filter-name>cookieDomainFilterfilter-name>

       <url-pattern>/*url-pattern>

    filter-mapping>

   

    <welcome-file-list>

       <welcome-file>/index.jspwelcome-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、先在节点下添加以下内容。

                   channelSendOptions="8">
                             expireSessionsOnShutdown="false"
                   notifyListenersOnReplication="true"/>
         
                                    address="228.0.0.4"
                        port="45564"
                        frequency="500"
                        dropTime="3000"/>
                                  address="auto"
                      port="4000"
                     
                      selectorTimeout="5000"
                      maxThreads="6"/>
           
             
           

           
           
         

                           filter=""/>
         
                              tempDir="/tmp/war-temp/"
                    deployDir="/tmp/war-deploy/"
                    watchDir="/tmp/war-listen/"
                    watchEnabled="false"/>
         
         
       


3.2、修改 port="18080" protocol="HTTP/1.1"connectionTimeout="20000" redirectPort="8443" />
3.3、修改 jvmRoute="tomcat7_a">
3.4、修改 port="8005"shutdown="SHUTDOWN">
3.4、修改 port="8009" protocol="AJP/1.3"redirectPort="8443" />

4、复制并拷贝一份当前tomcat,重命名tomcat-7b,并修改server.xml 内容

port="8006" shutdown="SHUTDOWN">
port="28080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
jvmRoute="tomcat7_b">
port="9009" protocol="AJP/1.3" redirectPort="8443" />
                      address="auto"
                      port="4001"
                     
                      selectorTimeout="5000"
                      maxThreads="6"/>


**步骤4中的修改项,应该和步骤3中的对应上,只是端口号有略微修改,原因是在同一台机器上端口冲突,如果不是同一台机器,则无需修改。

到此我们tomcat集群配置结束。

三、 集群测试:
1、cluster工程新建test.jsp 内容如下:

 <%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
Cluster Test

<%
  //HttpSession session = request.getSession(true);
  System.out.println(session.getId());
  out.println("
SESSION ID:" + session.getId()+"
");  
  // 如果有新的请求,则添加session属性
  String name = request.getParameter("name");
  if (name != null && name.length() > 0) {
     String value = request.getParameter("value");
     session.setAttribute(name, value);
  }  
    out.print("Session List:");  
    Enumeration names = session.getAttributeNames();
    while (names.hasMoreElements()) {
        String sname = names.nextElement();
        String value = session.getAttribute(sname).toString();
        out.println( sname + " = " + value+"
");
        System.out.println( sname + " = " + value);
   }
%>
    负载均衡测试:此为:Tomcat7_a上的文件,看到没有?看到没有?!?~?~
 


    名称:
    

    值:
    

   
  

** 阴影部分请自行修改。。。。并分别发布到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 
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so 
LoadModule proxy_balancer_module modules/mod_proxy_balancer.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

3、添加以下内容到httpd.conf(care spaces:P

 

    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

   BalancerMember ajp://127.0.0.1:8009 loadfactor=1 route=tomcat7_a

BalancerMember ajp://127.0.0.1:9009 loadfactor=1 route=tomcat7_b

阴影部分应和两个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的配置注释:

 #

  #  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

#

  BalancerMember ajp://127.0.0.1:8009 loadfactor=1 route=tomcat7_a

BalancerMember ajp://127.0.0.1:9009 loadfactor=1 route=tomcat7_b

3、  修改httpd-vhosts.conf文件配置,添加以下配置:

    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

  

      Options Indexes FollowSymLinks

      AllowOverride All

      Order allow,deny

      Allow from all

RewriteEngine On

RewriteRule ^/$  http://192.168.1.102:18080/cluster/index.jsp [P]

 

    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/

  

      Options Indexes FollowSymLinks

      AllowOverride All

      Order allow,deny

      Allow from all

RewriteEngine On

RewriteRule ^/$  http://192.168.1.102:28080/cluster/index.jsp [P]

 

    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/

  

      Options Indexes FollowSymLinks

      AllowOverride All

      Order allow,deny

      Allow from all

RewriteEngine On

RewriteRule ^/$  http://192.168.1.102:28080/cluster/index.jsp [P]

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添加过滤器配置:

 

      cookieDomainFilter

      filters.CookieDomainFilter

  

  

      cookieDomainFilter

      /*

  

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共享方式的设置



你可能感兴趣的:(apache,tomcat,集群,服务器,tomcat,集群)