apache+tomcat集群部署总结

3 篇文章 0 订阅
2 篇文章 0 订阅

一、环境准备

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"
                 channelSendOptions="8">
          <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="45564"
                        frequency="500"
                        dropTime="3000"/>
            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
                      address="auto"
                      port="4000"
                     
                      selectorTimeout="5000"
                      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"/>
          </Channel>
          <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
                 filter=""/>
          <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>
          <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
                    tempDir="/tmp/war-temp/"
                    deployDir="/tmp/war-deploy/"
                    watchDir="/tmp/war-listen/"
                    watchEnabled="false"/>
          <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>
          <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
        </Cluster>


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">
<Connector port="28080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat7_b">
<Connector port="9009" protocol="AJP/1.3" redirectPort="8443" />
<Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
                      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.*" %>
<html><head><title>Cluster Test</title></head>
<body>
<%
  //HttpSession session = request.getSession(true);
  System.out.println(session.getId());
  out.println("<br> SESSION ID:" + session.getId()+"<br>");  
  // 如果有新的请求,则添加session属性
  String name = request.getParameter("name");
  if (name != null && name.length() > 0) {
     String value = request.getParameter("value");
     session.setAttribute(name, value);
  }  
    out.print("<b>Session List:</b>");  
    Enumeration<String> names = session.getAttributeNames();
    while (names.hasMoreElements()) {
        String sname = names.nextElement();
        String value = session.getAttribute(sname).toString();
        out.println( sname + " = " + value+"<br>");
        System.out.println( sname + " = " + value);
   }
%>
    <b>负载均衡测试:此为:Tomcat7_a上的文件,<font color=red>看到没有?看到没有?!?~?~</font><b>
  <form action="test.jsp" method="post">
    名称:<input type=text size=20 name="name">
     <br>
    值:<input type=text size=20 name="value">
     <br>
    <input type=submit value="提交">
   </form>
</body>
</html>

** 阴影部分请自行修改。。。。并分别发布到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

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值