`

linux apache+tomcat集群+负载均衡

阅读更多

1 所需软件及版本:

apache:httpd-2.4.17.tar.gz(apr-1.5.2.tar.gz 、apr-util-1.5.4.tar.gz 、pcre-8.37.tar.gz)

tomcat:apache-tomcat-7.0.63.tar.gz

jk:tomcat-connectors-1.2.41-src.tar.gz

 

2 安装顺序(apr,apr-util,pcre,apache;tomcat、jk)

2.1 apr

./configure --prefix=/usr/local/apr
make && make install

 

2.2 apr-util

./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make && make install

 

2.3 pcre

./configure --prefix=/usr/local/pcre --with-apr=/usr/local/apr
make && make install

 

2.4 apache

./configure --prefix=/usr/local/apache2 --enable-so --enable-rewrite --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/bin --with-pcre=/usr/local/pcre

make && make install

2.5 tomcat

解压至/usr/local/。复制两份tomcat。一个配置成tomcat1,另一个配置成tomcat2。

 

2.6 jk

./configure --with-apxs=/usr/local/apache2/bin/apxs

make && make install

如果成功结束,可以在/usr/local/apache2/modules/下找到mod_jk.so文件

 

3 集群及负载均衡配置

3.1 配置tomcat

对于tomcat配置主要集中在conf/server.xml文件上。首先,鉴于是在同一机器上运行两个tomcat实例,因此,要对 server.xml中配置的所有端口进行修改,避免端口被占用,一种简单而稳妥的修改方法是将该文件中出现的所有端口号按一种简单的规则统一进行改动, 比如在原端口号基础上统一加1000或减1000。

3.2 开启tomcat集群支持

在conf/server.xml文件里,进行两处改动:

改动1:为<Engine>设置jvmRoute

改动2:在<Engine/>中添加关于集群的配置。这份默认配置可以满足大多数应用场景,因此,只需要将这份配置复制到<Engine/>中,tomcat的配置就全部完成了。

修改后,conf/server.xml的内容如下:

 

<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
 -->
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  <Listener className="org.apache.catalina.core.JasperListener" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">

    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->
    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->
    <!-- Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the BIO implementation that requires the JSSE
         style configuration. When using the APR/native implementation, the
         OpenSSL style configuration is required as described in the APR/native
         documentation -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

    <!-- An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :-->
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
    
   <!-- <Engine name="Catalina" defaultHost="localhost"> -->
      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->

      <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> 

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>
 3.2 配置apache

 

打开apache安装目录下的conf/httpd.conf文件

1 修改:

 

ServerName localhost:80

 

2 在文件最后追加以下内容:

 

# Load mod_jk module  
LoadModule jk_module modules/mod_jk.so
# Specify jk log file.  
JkLogFile /var/log/mod_jk.log
# Specify jk log level [debug/error/info]  
JkLogLevel info
# Specify workers.properties, this file tell jk:  
# how many nodes and where they are.  
JkWorkersFile conf/workers.properties
# Specify which requests should handled by which node. 
#指定哪些请求由tomcat处理,controller为workers.properties文件里指定的负载控制器. 
JkMount /* controller
 

 

3 在conf下创建 workers.properties文件,并添加一下内容:

#所有节点列表,其中controller是一个逻辑结点,负责负载均衡控制,  
#如果JkMount中的URL指定给了controller就表示这个请求会被自动散列到某个物理节点上。  
#注意:真正负责处理请求的tomcat的名称(这里就是tomcat1,tomcat2)必须于它们在conf/server.xml  
#文件中配置的jvmRout的属性值是一致的!  
worker.list = controller,tomcat1,tomcat2  
   
#========tomcat1========  
worker.tomcat1.port=8009       #ajp13 端口号,在tomcat下server.xml配置,默认8009  
worker.tomcat1.host=localhost  #tomcat的主机地址,如不为本机,请填写ip地址  
worker.tomcat1.type=ajp13  
worker.tomcat1.lbfactor = 1    #server的加权比重,值越高,分得的请求越多  
#========tomcat2========  
worker.tomcat2.port=9009       #ajp13 端口号,在tomcat下server.xml配置,默认8009  
worker.tomcat2.host=localhost  #tomcat的主机地址,如不为本机,请填写ip地址  
worker.tomcat2.type=ajp13  
worker.tomcat2.lbfactor = 1    #server的加权比重,值越高,分得的请求越多  
  
#========controller,负载均衡控制器========  
worker.controller.type=lb  
worker.controller.balance_workers=tomcat1,tomcat2   #指定分担请求的tomcat,旧版本中的balanced_workers,已不再推荐使用!  
worker.controller.sticky_session=1 #sticky_session为1表示,  
#当某一 client的session创建之后,后续由该客户端发起的请求,也就是这个session的所有请求都始终由第一次处理该请求的结点  
#负责处理(除非该结点挂掉)

 4 测试

4.1 可分别在tomcat1、tomcat2的webapps下创建mytest文件夹,在mytest里面放入测试页面index.jsp,其内容为:

<%@ page contentType="text/html; charset=utf-8" %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster App Test</title></head>
<body>
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
<%
  out.println("<br> ID " + session.getId()+"<br>");
  // 如果有新的 Session 属性设置
   String dataName = request.getParameter("dataName");
     if (dataName != null && dataName.length() > 0) {
          String dataValue = request.getParameter("dataValue");
          session.setAttribute(dataName, dataValue);
     }
     out.print("<b>Session 列表</b>");
     Enumeration e = session.getAttributeNames();
     while (e.hasMoreElements()) {
           String name = (String)e.nextElement();
           String value = session.getAttribute(name).toString();
           out.println( name + " = " + value+"<br>");
           System.out.println( name + " = " + value);
     }
%>
     <form action="index.jsp" method="POST">
            名称:<input type=text size=20 name="dataName">
             <br>
            值:<input type=text size=20 name="dataValue">
             <br>
            <input type=submit>
     </form>
  </body>
</html>

 先启动apache服务器,再分别启动两台tomcat。通过apache访问这个应用(假设apache服务器所在IP为192.168.6.121,那么访问地址为:http://192.168.6.121/mytest/index.jsp),观察tomcat后台打出的日志会发现,请求被随机分配给了两个tomcat中的一台。

4.2 以上实验不能体现session复制,可用以下步骤证明:

将以上index.jsp页面拷贝到tomcat自带的webapps下的examples文件夹下,并且修改webapps/examples/WEB-INF/web.xml文件,在里面添加元素:

<distributable/>

当关闭tomcat2(ctrl+c)后继续刷新url(http://192.168.6.121/examples/index.jsp)访问。 tomcat1控制台中输出的session 依然与之前tomcat2中的一致,并且在session中保存的属性值仍然有效。即:tomcat1接手了停掉的tomcat2中的session,继续为用户提供服务,而用户感觉不到背后的变化。

 

5 其他

session复制配置
tomcat集群中的 session管理,主要有两种方式:
1).粘性session
表示从同一窗口发来的请求都将有集群中的同一个tomcat进行处理。配 置方式是在上面workers.properties文件中
worker.lbcontroller.sticky_session=True   
粘 性session的好处在不会在不同的tomcat上来回跳动处理请求,但是坏处是如果处理该session的tomcat崩溃,那么之后 的请求将由其他tomcat处理,原有session失效而重新新建一个新的session,这样如果继续从session取值,会抛出 nullpointer的访问异常。
2).session复制
session复制是指tomcat彼此之间通过组播方式将session 发到各个tomcat实例上,如果其中一个访问出错,则另外tomcat仍然具有有效的session内容,从而能正常接管其session。坏处是当 tomcat实例很多,或者用户在session中有大量操作时,组播发送的信息量十分惊人。session复制配置则是在发布的web应用程序中的 web.xml中添加
<distributable/>
此外,session复制所需的JDK必须是JDK 5.0及其以上版本

 

 

 

分享到:
评论

相关推荐

    计算机二级公共基础知识模 拟试题及答案详解.pdf

    计算机二级公共基础知识模 拟试题及答案详解.pdf

    电子工程领域的语音发射机电路设计与实现

    内容概要:本文档详细介绍了语音发射机的设计与实现,涵盖了从硬件电路到具体元件的选择和连接方式。文档提供了详细的电路图,包括电源管理、信号处理、音频输入输出接口以及射频模块等关键部分。此外,还展示了各个引脚的功能定义及其与其他组件的连接关系,确保了系统的稳定性和高效性能。通过这份文档,读者可以全面了解语音发射机的工作原理和技术细节。 适合人群:对电子工程感兴趣的初学者、从事嵌入式系统开发的技术人员以及需要深入了解语音发射机制的专业人士。 使用场景及目标:适用于希望构建自己的语音发射设备的研究人员或爱好者,帮助他们掌握相关技术和实际操作技能。同时,也为教学机构提供了一个很好的案例研究材料。 其他说明:文档不仅限于理论讲解,还包括具体的实施步骤,使读者能够动手实践并验证所学知识。

    易语言注册机源码详解:单线程架构下的接码、滑块验证与IP代理实现

    内容概要:本文详细介绍了用易语言编写的单线程全功能注册机源码,涵盖了接码平台对接、滑块验证处理、IP代理管理以及料子导入等多个核心功能。文章首先展示了主框架的初始化配置和事件驱动逻辑,随后深入探讨了接码平台(如打码兔)的API调用及其返回数据的处理方法。对于滑块验证部分,作者分享了如何利用易语言的绘图功能模拟真实用户的操作轨迹,并提高了验证通过率。IP代理模块则实现了智能切换策略,确保代理的有效性和稳定性。此外,料子导入功能支持多种格式的数据解析和去重校验,防止脏数据污染。最后,文章提到了状态机设计用于控制注册流程的状态持久化。 适合人群:有一定编程基础,尤其是熟悉易语言的开发者和技术爱好者。 使用场景及目标:适用于希望深入了解易语言注册机开发的技术细节,掌握接码、滑块验证、IP代理等关键技术的应用场景。目标是帮助读者理解并优化现有注册机的功能,提高其稳定性和效率。 其他说明:文中提到的部分技术和实现方式可能存在一定的风险,请谨慎使用。同时,建议读者在合法合规的前提下进行相关开发和测试。

    计算机绘图实用教程 第三章.pdf

    计算机绘图实用教程 第三章.pdf

    计算机辅助设计—AutoCAD 2018中文版基础教程 各章CAD图纸及相关说明汇总.pdf

    计算机辅助设计—AutoCAD 2018中文版基础教程 各章CAD图纸及相关说明汇总.pdf

    计算机类电子书集合PDF

    C++相关书籍,计算机相关书籍,linux相关及http等计算机学习、面试书籍。

    计算机二级mysql数据库程序设计练习题(一).pdf

    计算机二级mysql数据库程序设计练习题(一).pdf

    计算机发展史.pdf

    计算机发展史.pdf

    计算机二级课件.pdf

    计算机二级课件.pdf

    计算机概论第三讲:计算机组成.pdf

    计算机概论第三讲:计算机组成.pdf

    端侧算力网络白皮书:6G时代终端算力资源高效利用与应用场景解析

    内容概要:本文档由中国移动通信集团终端有限公司、北京邮电大学、中国信息通信研究院和中国通信学会共同发布,旨在探讨端侧算力网络(TCAN)的概念、架构、关键技术及其应用场景。文中详细分析了终端的发展现状、基本特征和发展趋势,阐述了端侧算力网络的定义、体系架构、功能架构及其主要特征。端侧算力网络通过整合海量泛在异构终端的算力资源,实现分布式多级端侧算力资源的高效利用,提升网络整体资源利用率和服务质量。关键技术涵盖层次化端算力感知图模型、资源虚拟化、数据压缩、多粒度多层次算力调度、现场级AI推理和算力定价机制。此外,还探讨了端侧算力网络在智能家居、智能医疗、车联网、智慧教育和智慧农业等领域的潜在应用场景。 适合人群:从事通信网络、物联网、边缘计算等领域研究和开发的专业人士,以及对6G网络和端侧算力网络感兴趣的学者和从业者。 使用场景及目标:适用于希望深入了解端侧算力网络技术原理、架构设计和应用场景的读者。目标是帮助读者掌握端侧算力网络的核心技术,理解其在不同行业的应用潜力,推动端侧算力网络技术的商业化和产业化。 其他说明:本文档不仅提供了端侧算力网络的技术细节,还对其隐私与安全进行了深入探讨

    学习java的心得体会.docx

    学习java的心得体会.docx

    计算机二级考试(南开100题齐全).pdf

    计算机二级考试(南开100题齐全).pdf

    计算机二级C语言考试通关宝典:全面解析核心知识点与解题技巧

    内容概要:本文详细介绍了计算机二级C语言考试的内容和备考方法。首先概述了计算机二级考试的意义及其在计算机技能认证中的重要性,重点讲解了C语言的基础语法,包括程序结构、数据类型、运算符和表达式等。接着深入探讨了进阶知识,如函数、数组、指针、结构体和共用体的应用。最后分享了针对选择题、填空题和编程题的具体解题技巧,强调了复习方法和实战演练的重要性。 适合人群:准备参加计算机二级C语言考试的学生和技术爱好者。 使用场景及目标:①帮助考生系统地掌握C语言的核心知识点;②提供有效的解题策略,提高应试能力;③指导考生制定合理的复习计划,增强实战经验。 其他说明:本文不仅涵盖了理论知识,还提供了大量实例代码和详细的解释,有助于读者更好地理解和应用所学内容。此外,文中提到的解题技巧和复习建议对实际编程也有很大帮助。

    论文格式及要求.doc

    论文格式及要求.doc

    三菱FX3U与台达变频器RS485通信程序设置及应用实例

    内容概要:本文详细介绍了如何使用三菱FX3U PLC及其485BD通信板与四台台达VFD-M系列变频器进行通信的设置与应用。主要内容涵盖硬件连接注意事项、通信参数配置、RS指令的应用、CRC校验算法的实现以及频率给定和状态读取的具体方法。文中提供了多个实用的编程示例,展示了如何通过梯形图和结构化文本编写通信程序,并讨论了常见的调试技巧和优化建议。此外,还提到了系统的扩展性和稳定性措施,如增加温度传感器通信功能和应对电磁干扰的方法。 适合人群:从事工业自动化领域的工程师和技术人员,尤其是那些熟悉三菱PLC和台达变频器的使用者。 使用场景及目标:适用于需要实现多台变频器联动控制的工业应用场景,旨在提高生产效率和系统可靠性。通过学习本文,读者可以掌握如何构建稳定的RS485通信网络,确保变频器之间的高效协同工作。 其他说明:本文不仅提供了详细的理论指导,还包括了许多来自实际项目的经验教训,帮助读者避免常见错误并提升编程技能。

    计算机服务规范.pdf

    计算机服务规范.pdf

    Discuz-X3.2-TC-UTF8.zip

    Discuz_X3.2_TC_UTF8.zip LNMP搭建安装包

    2023年房地产行业研究报告:缓解竣工下行加速的两大改革.pdf

    2023年房地产行业研究报告:缓解竣工下行加速的两大改革

    win32汇编环境,网络编程入门之十五

    win32汇编环境,网络编程入门之十五

Global site tag (gtag.js) - Google Analytics