文档章节

tomcat 集群配置

猪刚烈
 猪刚烈
发布于 2014/08/16 09:31
字数 1130
阅读 12
收藏 0

精选30+云产品,助力企业轻松上云!>>>

          长话短说,今天小配置了一个集群,是用apache做前段服务器,session管理方式用的session复制,然后把后缀名为 jsp和do的请求发送给后端tomcat

         首先的下载tomcat服务器 这里用的是:apache-tomcat-6.0.39-windows-x86  zip版,不需要配置环境变量,但必须设置好java环境变量

         Apache 服务器 httpd-2.0.65-win32-x86-openssl-0.9.8y 这个openssl和no_ssl都行

        插件:tomcat-connectors-1.2.40-windows-i386-httpd-2.0.x 注意tomcat 版本和apache版本

       上面三个东西都可以上网搜到,读者自行去搜索吧,可以锻炼一下读英文的能力

       配置集群,首先的让tomcat能在一台机器上运行,所以必须修改其端口号,使其不冲突其配置文件如下:


       

<?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="8006" shutdown="SHUTDOWN">

  <!--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" />
  <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->
  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />

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


  <Service name="Catalina">
  
   
    <Connector port="8081" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8445" />
    
         
  
    <Connector port="8010" protocol="AJP/1.3" redirectPort="8445" />


   

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

    
            
 <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
    
    

      <!-- 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"/>

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

        
       
     

      </Host>
    </Engine>
  </Service>
</Server>

tomcat2

<?xml version='1.0' encoding='utf-8'?>


<Server port="8007" shutdown="SHUTDOWN">

  <!--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" />
  <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->
  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />


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

  
  <Service name="Catalina">
  
    
    

    <Connector port="8083" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8446" />
   
           
  
   

   
    <Connector port="8011" protocol="AJP/1.3" redirectPort="8446" />


  

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

      
	 <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>

    

      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
             resourceName="UserDatabase"/>

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

     


      </Host>
    </Engine>
  </Service>
</Server>

然后配置apached

将tomcat-connectors-1.2.40-windows-i386-httpd-2.0.x解压,将里面的mod_jk.so 放到apached的modules下面

然后将conf 的httpd配置文件里面加上一句话:include "D:\Program Files (x86)\Apache Group\Apache2\conf\mod_jk.conf"

在conf里面 新建mod_jk.conf 将一下内容复制进去

LoadModule jk_module "D:/Program Files (x86)/Apache Group/Apache2/modules/mod_jk.so"
JkWorkersFile "conf/workers.properties"
JkMount /*.jsp controller
JkMount /*.do controller

上面内容是加载mod_jk模块,加载负均衡配置文件,将*.jsp 和*.do转发给tomcat

在conf里新建一个workers.properties 复制以下内容

#server
worker.list = controller
#========tomcat1========
worker.tomcat1.port=8010
worker.tomcat1.host=localhost
worker.tomcat1.type=ajp13
worker.tomcat1.lbfactor = 1
#========tomcat2========
worker.tomcat2.port=8011
worker.tomcat2.host=localhost
worker.tomcat2.type=ajp13
worker.tomcat2.lbfactor = 1
#========controller,负载均衡控制器========
worker.controller.type=lb
worker.controller.balanced_workers=tomcat1,tomcat2
worker.controller.sticky_session=false
worker.controller.sticky_session_force=1
#worker.controller.sticky_session=1

至此,集群就配置好了,启动tomcat和apache

可以写个测试类试试,这是到网上copy的:

<%@ page language="java" import="java.util.*" pageEncoding="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.println("<b>Session 列表</b><br>");
  System.out.println("============================");
  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="test.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>

如果没有web-inf 记得建立一个这样的文件夹,并且在web.xml里面的<web-aap>加上</description> 

如下效果图:




tomcat1和tomcat2都session的值,说明session复制成功了

当然这种集群方式只适合早起的企业应用系统,在集群之间session同步到其他机器,这种方案简单,但只适合较少量的web服务器,如果多了则服务器开销大都消耗在session的复制上去了,在用户很多的情况下,会出现服务器内存不够用的情况

猪刚烈

猪刚烈

粉丝 22
博文 708
码字总数 110
作品 1
海淀
程序员
私信 提问
加载中
请先登录后再评论。
基于tomcat8.5做web集群

概述 我们系统由于前期设计的问题,一直没有考虑集群、session共享的问题。上线后问题暴露,在保证应用正常,步进行大动的的情况下只能采用tomcat集群的方式来解决问题。 问题发现 刚开始的时...

ZongxingH
2018/08/24
33
0
CentOS7/RHEL7 pacemaker+corosync高可用集群搭建

一、集群信息... 4 二、集群搭建... 4 1、制作软件源... 4 2、主机基础配置... 5 3、集群基础环境准备... 7 4、集群资源准备... 9 5、资源配置... 11 6、constraint配置... 12 7、stonith配置...

osc_41dppmun
2018/06/28
4
0
Windows下配置Tomcat集群

为什么要用Tomcat集群 可以提高整体web服务器性能,将动态页面交给tomcat处理,将静态文件交给apache处理,可以大大提高服务器的静态文件处理性能。 可以实现web服务器的负载均衡,服务器可采...

风中倩影
2018/07/22
163
0
Apache + Tomcat集群配置详解 (2)

Apache + Tomcat集群配置详解 (2) 四、Tomcat配置 如果仅仅为了配置一个可用的集群,Tomcat的配置将会非常简单。分别打开t1和t2的server.xml配置文件,对于t1,尽量采用默认的设置,而对t2作...

liyanjun008
2011/04/17
0
0
linux下配置tomcat集群的负载均衡

linux下配置tomcat集群的负载均衡 一、首先了解下与集群相关的几个概念 集群: 集群是一组协同工作的服务实体,用以提供比单一服务实体更具扩展性与可用性的服务平台。在客户端看来,一个集群...

吴胜健
2014/10/28
0
0

没有更多内容

加载失败,请刷新页面

加载更多

使用CSS按比例调整图像大小? [重复] - Resize image proportionally with CSS? [duplicate]

问题: This question already has an answer here: 这个问题在这里已有答案: How do I auto-resize an image to fit a 'div' container? 如何自动调整图像大小以适合“div”容器? 31 ans......

fyin1314
今天
18
0
define()与const - define() vs. const

问题: In PHP, when do you use 在PHP中,何时使用 define('FOO', 1); and when do you use 以及何时使用 const FOO = 1; ? ? What are the main differences between those two? 两者之......

法国红酒甜
今天
26
0
将Node.js升级到最新版本 - Upgrading Node.js to latest version

问题: So, I have Node.js installed and now when I tried to install Mongoosejs I got an error telling me that I don't have the needed version of Node.js (I have v0.4.11 and v0.4......

javail
今天
17
0
等到所有jQuery Ajax请求都完成了吗? - Wait until all jQuery Ajax requests are done?

问题: How do I make a function wait until all jQuery Ajax requests are done inside another function? 我如何让一个函数等到所有jQuery Ajax请求都在另一个函数中完成之后? In short...

富含淀粉
今天
17
0
OSChina 周日乱弹 —— 那么长的绳子,你这是放风筝呢

Osc乱弹歌单(2020)请戳(这里) 【今日歌曲】 @ 巴拉迪维:黑豹乐队的单曲《无地自容》 耳畔突然响起旋律,是那首老歌。中国摇滚有了《一无所有》不再一无所有;中国摇滚有了《无地自容》不...

小小编辑
今天
79
1

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部