Linux 下tomcat的session共享(概要和配置文件)

准备条件:

测试服务器192.168.0.2、192.168.0.3、负载均衡设备或配置好的apache、nginx软件等。

1、修改tomcat的conf/server.xml

在<Engine>节点下增加配置,这里直接贴出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="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" />

  <!-- 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="8081" 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 JSSE configuration, when using APR, the
         connector should be using the OpenSSL style configuration
         described in the APR documentation -->
    <!--
    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8010" 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="jvm1">         
    -->
    <Engine name="Catalina" defaultHost="localhost">

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

          <Manager className="org.apache.catalina.ha.session.BackupManager"
                   expireSessionsOnShutdown="false"
                   notifyListenersOnReplication="true"
                   mapSendOptions="6"/>
          <!--
          <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="5000"
                      selectorTimeout="100"
                      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"/>
            <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
          </Channel>

          <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
                 filter=".*\.gif;.*\.js;.*\.jpg;.*\.png;.*\.htm;.*\.html;.*\.css;.*\.txt;"/>

          <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.ClusterSessionListener"/>
        </Cluster>

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

      <!-- The request dumper valve dumps useful debugging information about
           the request and response data received and sent by Tomcat.
           Documentation at: /docs/config/valve.html -->
      <!--
      <Valve className="org.apache.catalina.valves.RequestDumperValve"/>
      -->

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

      <!-- Define the default virtual host
           Note: XML Schema validation will not work with Xerces 2.2.
       -->
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">

        <!-- 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 -->
        <!--
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"  
               prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>
        -->

    <Context path="" docBase="D:\apache-tomcat-6.0.35\logintest1" reloadable="true" distributable="true" />

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

2、修改web工程的WEB-INF下的web.xml

对于要进行负载和集群的的应用中的WEB-INF中的web.xml文件要添加如下一句配置
<distributable/>

如下(不过我一般就在tomcat conf下的server.xml的Contex接口中增加属性distributable="true"——上面server.xml中倒数第5行):

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <distributable/>
</web-app>

3、分别启动tomcat,试用自己测试session的demo测试即可。

注意:因为sessionid与访问的域名地址有关,所以测试的时候不要试用2个ip地址访问测试,这样session肯定不一致。

一般我们使用负载均衡设备(F5、netscaler等)或者使用apache、nginx负载均衡软件加在我们集群的tomcat服务器外面,试用一个ip地址分发到不同的tomcat。

备注:

一、

tomcat集群和负载均衡的实现(session同步)补充

因 为tomcat的session同步功能需要用到组播,windows默认情况下是开通组播服务的,但是linux默认情况下并没有开通。

可以通过指令打 开route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0

如果需要服务器启动时即开通组播需在/etc/sysconfig/static-routes文件内加入eht0 net 224.0.0.0 netmask 240.0.0.0。

具体组播概念请查阅CCNP相关内容。

二、

我一般都是引用项目发布(热部署),在server.xml的Context中增加distributable="true",这样工程的web.xml中<distributable/>也可以不加了。

    <Context path="" docBase="D:\apache-tomcat-6.0.35\logintest1" reloadable="true" distributable="true" />

三、

server.xml元素中

            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
                      address="auto"
                      port="5000"
                      selectorTimeout="100"
                      maxThreads="6"/>

其中address写为auto时,很多时候服务器会自动使用127.0.0.1,这样就会产生问题,所以遇到服务器没有自动读取本地局域网ip的时候,我们手工设置一下,如:192.168.0.2,这样比试用auto保险。

四、

server.xml中我们新增的一些内容部分的具体含义和试用,官方都有说明,参考即可。

时间: 2024-08-16 04:04:32

Linux 下tomcat的session共享(概要和配置文件)的相关文章

解决Linux下tomcat连接MySQL数据库问题

mysql|解决|数据|数据库|问题 解决Linux下tomcat连接MySQL数据库 在linux下,开发一个web应用,期间调用数据库时总是不成功,如果单独写一个类,并编译执行访问数据库,正常,但是用web应用来调这个类连接数据库,就会出现错误:com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: ** BEGIN NESTED EXCEPTION **

linux下tomcat如何配置类覆盖jar里的类

问题描述 linux下tomcat如何配置类覆盖jar里的类 windows下类是可以直接覆盖jar里的类的,但是在linux环境下无法覆盖,求解决方法

win8 映射到虚拟盘到 linux下samba设置的共享盘,为什么虚拟盘的大小会变动,而且变小??

问题描述 win8 映射到虚拟盘到 linux下samba设置的共享盘,为什么虚拟盘的大小会变动,而且变小?? win8 映射到虚拟盘到 linux下samba设置的共享盘,为什么虚拟盘的大小会变动,而且变小?? 解决方案 不知道变动大小是多少 很有可能是计量单位不同,df -h 和 df -H 分别是按1000和1024计算的 解决方案二: 文件大小计算不同OS会有一些区别.

linux下tomcat部署应用后如何通过url访问文件

问题描述 linux下tomcat部署应用后如何通过url访问文件 是这样的,我的tomcat是在linux的/opt/server/tomcat这个目录下,端口9080.tomcat里部署的应用有写文件的功能,将文件写在/opt/awr/这个目录下,文件是html格式,1.html.现在我想在tomcat启动后,在浏览器里输入url就访问到这个html文件,请问该怎么办

Linux下Tomcat catalina.out自动归档,以及logrotate 配置详解

Linux下Tomcat catalina.out自动归档 如果 catalina.out 日志达到 2GB 大小的时候,Tomcat 因为缓存问题,便没有办法继续输出日志了.  为了避免这种情况,你需要及时对 catalina.out  进行归档.  实际上 Tomcat 自身也会对日志内容进行归档,但是归档后,Tomcat 不会清理 catalina.out 的内容,这将会导致 catalina.out 越来越大.  下面的方法能够使用在 linux/unix 的机器上.  如果我们期望每天

linux下tomcat apr 安装问题

问题描述 linux下tomcat apr 安装问题 求高手帮忙: 按网上的文档安装apr一切顺利,但tomcat在启动的时候apr仍然找不到... 2015-7-7 15:17:31 org.apache.catalina.core.AprLifecycleListener init 信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments

Java中tomcat memecached session 共享同步问题的解决办法_java

事件缘由:一个主项目"图说美物",另外一个子功能是品牌商的入驻功能,是跟主项目分开的项目,为了共享登录的用户信息,而实现session共享,俩个tomcat,一个tomcat6,一个tomcat7 web项目windows系统下实现session的共享 第一个步: 在俩个tomcat的context.xml这个文件中配置如下代码: <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManage

Linux下Tomcat与Apache Web服务器整合

apache|web|web服务|web服务器 1.引言      基于Web技术的Internet/Intranet近年来已经得到了广泛的应用,Intranet是以TCP/IP协议为基础.以Web为核心的企业内部网,用户通过低成本.简单易用的客户浏览器就能随时随地到企业的Web站点上查阅自己所需的数据.浏览器客户端操作界面的一致性避免了C/S模式客户端程序的多样性,而服务器端的开放和基于标准的连接方案使企业很方便地通过Internet同外界联系:同时,Web信息动态的.交互式的发布方式从根本上

Linux下Tomcat与Apache服务器的整合之一

apache|服务器 引言 基于Web技术的Internet/Intranet近年来已经得到了广泛的应用,Intranet是以TCP/IP协议为基础.以Web为核心的企业内部网,用户通过低成本.简单易用的客户浏览器就能随时随地到企业的Web站点上查阅自己所需的数据. 浏览器客户端操作界面的一致性避免了C/S模式客户端程序的多样性,而服务器端的开放和基于标准的连接方案使企业很方便地通过Internet同外界联系:同时,Web信息动态的.交互式的发布方式从根本上改变了企业的服务质量,增加了企业的商业