文档章节

tomcat 集群配置

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

          长话短说,今天小配置了一个集群,是用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的复制上去了,在用户很多的情况下,会出现服务器内存不够用的情况

本文转载自:http://blog.csdn.net/xh199110/article/details/38025791

共有 人打赏支持
猪刚烈
粉丝 22
博文 708
码字总数 110
作品 1
海淀
程序员
私信 提问
linux下配置tomcat集群的负载均衡

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

吴胜健
2014/10/28
0
0
Apache+Tomcat集群配置+session共享

本文Apache+Tomcat集群配置 基于最新的Apache和Tomcat,具体是2011年4月20日最新的Tomcat和Apache集群和负载均衡配置。 准备环境 Apache Apache是http服务器,我们利用其对Tomcat进行负载均衡...

涂孟超
2013/07/31
0
5
Nginx在Windows平台的配置

1.1.1 课程目标: 课程目标: 能够使用Nginx搭建Tomcat集群,并完成负载均衡. 1.1.2 Nginx的概述: 什么是Nginx: 为什么使用Nginx: 度娘的解释: 背景: 互联网飞速发展的今天,大用户量高并发已经...

pxk
2017/10/29
0
1
Nginx--Session共享问题

在Nginx做反向代理时,面对集群,如何保证Session的一致性。 第一种是以负载均衡服务器本身提供的session共享策略,每种服务期的配置是不一样的并且nginx本身是没有的。 第二种是利用web容器...

btwangzhi
2017/11/26
0
0
结合Apache和Tomcat实现集群和负载均衡

一:软件环境 1. Apache: apache 2.0.55 (由http://httpd.apache.org/进入下载)(点击下载apache 2.0.55) 2. Tomcat: Tomcat 5.5.25 (由http://tomcat.apache.org/进入下载)(点击下载Tomcat ......

晨曦之光
2012/03/09
0
0

没有更多内容

加载失败,请刷新页面

加载更多

Alibaba Java诊断利器Arthas实践--使用redefine排查应用奇怪的日志来源

背景 随着应用越来越复杂,依赖越来越多,日志系统越来越混乱,有时会出现一些奇怪的日志,比如: [] [] [] No credential found 那么怎样排查这些奇怪的日志从哪里打印出来的呢?因为搞不清...

hengyunabc
24分钟前
0
0
home hosts

home hosts lwk@qwfys:~$ cat /etc/hosts127.0.0.1 localhost127.0.1.1 qwfys192.168.56.101vm600.qwfys.com39.108.212.91alpha1.ppy.com39.108.117.122alpha2.p......

qwfys
30分钟前
0
0
大数据教程(6.1)hadoop生态圈介绍及就业前景

1. HADOOP背景介绍 1.1、什么是HADOOP 1.HADOOP是apache旗下的一套开源软件平台 2.HADOOP提供的功能:利用服务器集群,根据用户的自定义业务逻辑,对海量数据进行分布式处理 3.HADOOP的核心组...

em_aaron
今天
2
0
hadoop垃圾回收站

在生产生,hdfs回收站必须是开启的,一般设置为7天。 fs.trash.interval 为垃圾回收站保留时间,如果为0则禁用回收站功能。 fs.trash.checkpoint.interval 回收站检查点时间,一般设置为小于...

hnairdb
昨天
1
0
腾讯与Github的魔幻会面背后的故事…

10月22日,腾讯开源管理办公室有幸邀请到Github新晋CEO Nat Friedman,前来鹅厂参观交流。目前腾讯已经有近70个项目在Github上开源,共获得17w stars,世界排名11位。Github是腾讯开源的主阵...

腾讯开源
昨天
12
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部