apache2.4 + mod_proxy + tomcat7 配置集群和负载均衡

Posted node2017

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了apache2.4 + mod_proxy + tomcat7 配置集群和负载均衡相关的知识,希望对你有一定的参考价值。

集群和负载均衡好处自然不用说,概念不理解查百度,这里介绍下怎么利用mod_proxy配置集群和负载均衡。

  1. 参考文档
    https://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html
    http://httpd.apache.org/docs/2.4/en/mod/mod_proxy_balancer.html
    http://httpd.apache.org/docs/2.4/en/mod/mod_proxy.html

  2. 准备需要的需要工具
    a. apache2.4 安装部署见 http://blog.csdn.net/yingxiake/article/details/51408057
    b. mod_proxy代理连接器,apache2.4已经自带
    c. tomcat7官网下载,直接解压即可

  3. 实现的架构图

首先我们来实现tomcat的集群以及session的复杂

1.解压tomcat7到cluster的目录,并命名为apache-tomcat-7.0.55_64_1,打开conf/server.xml配置集群,在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="4001" <!-- 若在同一台机器上,必须修改这里的端口,避免端口冲突范围须在4000-4100 -->
              autoBind="100"
              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>

2.配置Engine节点的路由信息,session复制和负载均衡需要用

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat7_1">

3.复制webapp下的example项目并修改为mycluster,打开web.xml,并在web-app节点下添加<distributable/>节点,新建一个jsp页面作为测试,命名为testCluster.jsp

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

<h3>this is tomcat3!!</h3>
<%  
  out.println("<p> SESSION ID : " + session.getId()+"</p>"); 
  String name = request.getParameter("name");  
  if (name != null && name.length() > 0)   
     String value = request.getParameter("value");  
     session.setAttribute(name, value);  
      
    out.print("<table border = '1'>");    
    out.print("<tr><th>session key </th><th>session value </th></tr>"); 
    Enumeration<String> names = session.getAttributeNames();  
    while (names.hasMoreElements())   
        String key = names.nextElement();
        String value = session.getAttribute(key).toString();
        System.out.print(key + " --- " + value);
        out.print("<tr><td>"+key+"</td><td>"+value+"</td></tr>"); 
     
   out.print("</table>");
%> 
<br />
<form action="testCluster.jsp" method="post">  
session key :<input type=text name="name">  
session value:<input type=text name="value">  
<input type=submit value="添加">  
</form>  
</body>  
</html>

4.复制apache-tomcat-7.0.55_64_1,命名为apache-tomcat-7.0.55_64_2,由于在一条机器部署俩个tomcat需要修改server.xml中的三个端口

<Server port="8006" shutdown="SHUTDOWN"><!-- 服务端口 -->
    //...
    <Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" /><!-- http端口 -->
    //...
    <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" /><!-- AJP端口 --> 
   //...
</Server>

开始测试集群,启动tomcat1和tomcat2服务
在浏览器地址栏上输入http://localhost:8081/mycluster/testCluster.jsp
提交一个session key和value

集群和负载均衡好处自然不用说,概念不理解查百度,这里介绍下怎么利用mod_proxy配置集群和负载均衡。

  1. 参考文档
    https://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html
    http://httpd.apache.org/docs/2.4/en/mod/mod_proxy_balancer.html
    http://httpd.apache.org/docs/2.4/en/mod/mod_proxy.html

  2. 准备需要的需要工具
    a. apache2.4 安装部署见 http://blog.csdn.net/yingxiake/article/details/51408057
    b. mod_proxy代理连接器,apache2.4已经自带
    c. tomcat7官网下载,直接解压即可

  3. 实现的架构图

首先我们来实现tomcat的集群以及session的复杂

1.解压tomcat7到cluster的目录,并命名为apache-tomcat-7.0.55_64_1,打开conf/server.xml配置集群,在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="4001" <!-- 若在同一台机器上,必须修改这里的端口,避免端口冲突范围须在4000-4100 -->
              autoBind="100"
              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>

2.配置Engine节点的路由信息,session复制和负载均衡需要用

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat7_1">

3.复制webapp下的example项目并修改为mycluster,打开web.xml,并在web-app节点下添加<distributable/>节点,新建一个jsp页面作为测试,命名为testCluster.jsp

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

<h3>this is tomcat3!!</h3>
<%  
  out.println("<p> SESSION ID : " + session.getId()+"</p>"); 
  String name = request.getParameter("name");  
  if (name != null && name.length() > 0)   
     String value = request.getParameter("value");  
     session.setAttribute(name, value);  
      
    out.print("<table border = '1'>");    
    out.print("<tr><th>session key </th><th>session value </th></tr>"); 
    Enumeration<String> names = session.getAttributeNames();  
    while (names.hasMoreElements())   
        String key = names.nextElement();
        String value = session.getAttribute(key).toString();
        System.out.print(key + " --- " + value);
        out.print("<tr><td>"+key+"</td><td>"+value+"</td></tr>"); 
     
   out.print("</table>");
%> 
<br />
<form action="testCluster.jsp" method="post">  
session key :<input type=text name="name">  
session value:<input type=text name="value">  
<input type=submit value="添加">  
</form>  
</body>  
</html>

4.复制apache-tomcat-7.0.55_64_1,命名为apache-tomcat-7.0.55_64_2,由于在一条机器部署俩个tomcat需要修改server.xml中的三个端口

<Server port="8006" shutdown="SHUTDOWN"><!-- 服务端口 -->
    //...
    <Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" /><!-- http端口 -->
    //...
    <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" /><!-- AJP端口 --> 
   //...
</Server>

开始测试集群,启动tomcat1和tomcat2服务
在浏览器地址栏上输入http://localhost:8081/mycluster/testCluster.jsp
提交一个session key和value

再打开一个浏览器窗口,在地址栏上输入http://localhost:8082/mycluster/testCluster.jsp

发现8082端口的服务把8081服务的session同步过来了,并且,俩个服务的sessionid都一样,除了后缀不一样,后缀是我们配置的jvmRoute

到这里就测试成功了,这里要注意几个地方,要实现session复制,所有session属性必需实现 java.io.Serializable

集群配置好之后,我们来整合apache处理负载均衡,这里利用http代理处理tomcat和apache的链接

1.我们在apache conf的目录下新建一个balance.conf配置文件,内容如下


#提供基础的代理功能
LoadModule proxy_module modules/mod_proxy.so
#提供负载均衡的功能
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
#代理http协议
LoadModule proxy_http_module modules/mod_proxy_http.so

#负载均衡的算法模块
LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
#兼容低版本访问
LoadModule access_compat_module modules/mod_access_compat.so

ProxyRequests Off 
ProxyPass / balancer://mycluster/  

#设置代理的算法
#ProxySet lbmethod=bytraffic 

#代理关联配置loadfactor可以分发请求权重,loadfactor越大,权重越大
<Proxy balancer://mycluster> 
  BalancerMember http://localhost:8081 loadfactor=1 route=tomcat7_1
  BalancerMember http://localhost:8082 loadfactor=1 route=tomcat7_2

  #热部署,当着备份服务,当tomcat7_1和tomcat7_2死掉的时候,就自动访问tomcat7_3 
  #BalancerMember http://localhost:9080 loadfactor=1 route=tomcat7_3  status=+H 
</Proxy> 

#负载均衡控制台,通过http://localhost/balancer-manager 访问
<Location /balancer-manager> 
    SetHandler balancer-manager 
    Order Deny,Allow 
    Allow from all 
    #Allow from localhost 
</Location> 

2.打开conf/httpd.conf江balance.conf引进去,在httpd.conf最下面通过下面命令引入

include conf\\balancer.conf

配置好之后,启动apache,在浏览器的地址栏输入http://localhost

发现它已经路由到tomcat的主页上了,说明apache路径分发成功,这里我们在地址栏输入http://localhost/mycluster/testCluster.jsp,发现他能路由到相应的页面,并在tomcat7_1和tomcat7_2进行切换

若想在控制台上查看监控情况,可以在地址栏上输入http://localhost/balancer-manager

到这里就全部配置了,感觉还挺简单!!

以上是关于apache2.4 + mod_proxy + tomcat7 配置集群和负载均衡的主要内容,如果未能解决你的问题,请参考以下文章

centos7下apache2.4反向代理

使用 Apache 2.4 mod_proxy 和基本身份验证在 Docker 容器中运行 Portainer

centos 配置apache和tomcat共同使用https

Apache 2.4 + PHP-FPM 和授权标头

apache2.4 You don‘t have permission to access / on

Apache 2.4权限设置( you don't have permission to access / on this server Apache2.4)