linux tomcat 6配置session复制

配置完了之后才发现很简单,主要是配置tomcat目录下的conf/server.xml文件,只需要在<host>下加入以下代码:

xml片段

[xhtml]  view plain copy
  1. <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8">  
  2.           <Manager className="org.apache.catalina.ha.session.DeltaManager"  
  3.                    expireSessionsOnShutdown="false"  
  4.                    notifyListenersOnReplication="true"/>  
  5.           <Channel className="org.apache.catalina.tribes.group.GroupChannel">  
  6.             <Membership className="org.apache.catalina.tribes.membership.McastService"  
  7.                         address="228.0.0.4"  
  8.                         port="45564"  
  9.                         frequency="500"  
  10.                         dropTime="3000"/>  
  11.             <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"  
  12.                       address="192.168.7.153"  
  13.                       port="4000"  
  14.                       autoBind="100"  
  15.                       selectorTimeout="5000"  
  16.                       maxThreads="6"/>  
  17.             <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">  
  18.               <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>  
  19.             </Sender>  
  20.             <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>  
  21.             <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>  
  22.           </Channel>  
  23.           <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"  
  24.                  filter=""/>  
  25.           <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>  
  26.           <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"  
  27.                     tempDir="/tmp/war-temp/"  
  28.                     deployDir="/tmp/war-deploy/"  
  29.                     watchDir="/tmp/war-listen/"  
  30.                     watchEnabled="false"/>  
  31.           <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>  
  32.           <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>  
  33.         </Cluster>  

注意receiver节点下的port 端口,两个tomcat端口不能相同

插入后代码的完整server.xml

[xhtml]  view plain copy
  1. <?xml version='1.0' encoding='utf-8'?>  
  2. <!--  
  3.   Licensed to the Apache Software Foundation (ASF) under one or more  
  4.   contributor license agreements.  See the NOTICE file distributed with  
  5.   this work for additional information regarding copyright ownership.  
  6.   The ASF licenses this file to You under the Apache License, Version 2.0  
  7.   (the "License"); you may not use this file except in compliance with  
  8.   the License.  You may obtain a copy of the License at  
  9.       http://www.apache.org/licenses/LICENSE-2.0  
  10.   Unless required by applicable law or agreed to in writing, software  
  11.   distributed under the License is distributed on an "AS IS" BASIS,  
  12.   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  13.   See the License for the specific language governing permissions and  
  14.   limitations under the License.  
  15. -->  
  16. <!-- Note:  A "Server" is not itself a "Container", so you may not  
  17.      define subcomponents such as "Valves" at this level.  
  18.      Documentation at /docs/config/server.html  
  19.  -->  
  20. <Server port="8005" shutdown="SHUTDOWN">  
  21.   <!--APR library loader. Documentation at /docs/apr.html -->  
  22.   <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />  
  23.   <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->  
  24.   <Listener className="org.apache.catalina.core.JasperListener" />  
  25.   <!-- Prevent memory leaks due to use of particular java/javax APIs-->  
  26.   <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />  
  27.   <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->  
  28.   <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />  
  29.   <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />  
  30.   <!-- Global JNDI resources  
  31.        Documentation at /docs/jndi-resources-howto.html  
  32.   -->  
  33.   <GlobalNamingResources>  
  34.     <!-- Editable user database that can also be used by  
  35.          UserDatabaseRealm to authenticate users  
  36.     -->  
  37.     <Resource name="UserDatabase" auth="Container"  
  38.               type="org.apache.catalina.UserDatabase"  
  39.               description="User database that can be updated and saved"  
  40.               factory="org.apache.catalina.users.MemoryUserDatabaseFactory"  
  41.               pathname="conf/tomcat-users.xml" />  
  42.   </GlobalNamingResources>  
  43.   <!-- A "Service" is a collection of one or more "Connectors" that share  
  44.        a single "Container" Note:  A "Service" is not itself a "Container",  
  45.        so you may not define subcomponents such as "Valves" at this level.  
  46.        Documentation at /docs/config/service.html  
  47.    -->  
  48.   <Service name="Catalina">  
  49.     <!--The connectors can use a shared executor, you can define one or more named thread pools-->  
  50.     <!--  
  51.     <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"  
  52.         maxThreads="150" minSpareThreads="4"/>  
  53.     -->  
  54.     <!-- A "Connector" represents an endpoint by which requests are received  
  55.          and responses are returned. Documentation at :  
  56.          Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)  
  57.          Java AJP  Connector: /docs/config/ajp.html  
  58.          APR (HTTP/AJP) Connector: /docs/apr.html  
  59.          Define a non-SSL HTTP/1.1 Connector on port 8080  
  60.     -->  
  61.     <Connector port="8080" protocol="HTTP/1.1"  
  62.                connectionTimeout="20000"  
  63.                redirectPort="8443" />  
  64.     <!-- A "Connector" using the shared thread pool-->  
  65.     <!--  
  66.     <Connector executor="tomcatThreadPool"  
  67.                port="8080" protocol="HTTP/1.1"  
  68.                connectionTimeout="20000"  
  69.                redirectPort="8443" />  
  70.     -->  
  71.     <!-- Define a SSL HTTP/1.1 Connector on port 8443  
  72.          This connector uses the JSSE configuration, when using APR, the  
  73.          connector should be using the OpenSSL style configuration  
  74.          described in the APR documentation -->  
  75.     <!--  
  76.     <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"  
  77.                maxThreads="150" scheme="https" secure="true"  
  78.                clientAuth="false" sslProtocol="TLS" />  
  79.     -->  
  80.     <!-- Define an AJP 1.3 Connector on port 8009 -->  
  81.     <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />  
  82.     <!-- An Engine represents the entry point (within Catalina) that processes  
  83.          every request.  The Engine implementation for Tomcat stand alone  
  84.          analyzes the HTTP headers included with the request, and passes them  
  85.          on to the appropriate Host (virtual host).  
  86.          Documentation at /docs/config/engine.html -->  
  87.     <!-- You should set jvmRoute to support load-balancing via AJP ie :  
  88.     <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">  
  89.     -->  
  90.     <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm3">  
  91.       <!--For clustering, please take a look at documentation at:  
  92.           /docs/cluster-howto.html  (simple how to)  
  93.           /docs/config/cluster.html (reference documentation) -->  
  94.       <!-- 
  95.       <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/> 
  96.       -->  
  97.       <!-- The request dumper valve dumps useful debugging information about  
  98.            the request and response data received and sent by Tomcat.  
  99.            Documentation at: /docs/config/valve.html -->  
  100.       <!-- 
  101.       <Valve className="org.apache.catalina.valves.RequestDumperValve"/> 
  102.       -->  
  103.       <!-- This Realm uses the UserDatabase configured in the global JNDI  
  104.            resources under the key "UserDatabase".  Any edits  
  105.            that are performed against this UserDatabase are immediately  
  106.            available for use by the Realm.  -->  
  107.       <Realm className="org.apache.catalina.realm.UserDatabaseRealm"  
  108.              resourceName="UserDatabase"/>  
  109.       <!-- Define the default virtual host  
  110.            Note: XML Schema validation will not work with Xerces 2.2.  
  111.        -->  
  112.       <Host name="localhost"  appBase="webapps"  
  113.             unpackWARs="true" autoDeploy="true"  
  114.             xmlValidation="false" xmlNamespaceAware="false">  
  115. <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8">  
  116.           <Manager className="org.apache.catalina.ha.session.DeltaManager"  
  117.                    expireSessionsOnShutdown="false"  
  118.                    notifyListenersOnReplication="true"/>  
  119.           <Channel className="org.apache.catalina.tribes.group.GroupChannel">  
  120.             <Membership className="org.apache.catalina.tribes.membership.McastService"  
  121.                         address="228.0.0.4"  
  122.                         port="45564"  
  123.                         frequency="500"  
  124.                         dropTime="3000"/>  
  125.             <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"  
  126.                       address="192.168.7.153"  
  127.                       port="4000"  
  128.                       autoBind="100"  
  129.                       selectorTimeout="5000"  
  130.                       maxThreads="6"/>  
  131.             <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">  
  132.               <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>  
  133.             </Sender>  
  134.             <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>  
  135.             <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>  
  136.           </Channel>  
  137.           <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"  
  138.                  filter=""/>  
  139.           <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/>  
  140.           <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"  
  141.                     tempDir="/tmp/war-temp/"  
  142.                     deployDir="/tmp/war-deploy/"  
  143.                     watchDir="/tmp/war-listen/"  
  144.                     watchEnabled="false"/>  
  145.           <ClusterListener className="org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener"/>  
  146.           <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>  
  147.         </Cluster>  
  148.         <!-- SingleSignOn valve, share authentication between web applications  
  149.              Documentation at: /docs/config/valve.html -->  
  150.         <!-- 
  151.         <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> 
  152.         -->  
  153.         <!-- Access log processes all example.  
  154.              Documentation at: /docs/config/valve.html -->  
  155.         <!--  
  156.         <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"  
  157.                prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>  
  158.         -->  
  159.       </Host>  
  160.     </Engine>  
  161.   </Service>  
  162. </Server>  

由于我做的session复制是在同一台机器上做的,所以需要改端口号,将两个tomcat里面的,Server,Connector,Connector AJP端口改为不一样的就行了,防止端口冲突。

在自己的项目中的web.xml文件</web-app>标签前面加上<distributable/>,

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相关内容。

可以通过netstate -g 来查看组播状态,也可以在route -e 命令中看到

下面开始测试:

写一个测试工程test,页面如下index.jsp

[xhtml]  view plain copy
  1. <%@ page contentType="text/html; charset=UTF-8" import="java.util.*"%>  
  2. <html>  
  3. <head>  
  4. <title>Cluster App Test</title>  
  5. </head>  
  6. <body>  
  7. Server Info:  
  8. <%  
  9.     out.print(request.getLocalAddr() + " : " + request.getLocalPort());  
  10. %>  
  11. <%  
  12.     out.println("<br> ID " + session.getId());  
  13.     // 如果有新的 Session 属性设置  
  14.     String dataName = request.getParameter("dataName");  
  15.     if (dataName != null && dataName.length() > 0) {  
  16.         String dataValue = request.getParameter("dataValue");  
  17.         session.setAttribute(dataName, dataValue);  
  18.     }  
  19.     out.print("<b>Session 列表</b>");  
  20.     Enumeration e = session.getAttributeNames();  
  21.     while (e.hasMoreElements()) {  
  22.         String name = (String) e.nextElement();  
  23.         String value = session.getAttribute(name).toString();  
  24.         out.println(name + " = " + value);  
  25.     }  
  26. %>  
  27. <form action="index.jsp" method="POST">名称:<input type=text  
  28.     size=20 name="dataName"> <br>  
  29. 值:<input type=text size=20 name="dataValue"> <br>  
  30. <input type=submit></form>  
  31. </body>  
  32. </html>  

1.在浏览器中输入http://192.168.7.153:8080/test/index.jsp

然后输入名称和值提交

2.在当前浏览器中输入http://192.168.7.153:8081/test/index.jsp

可以看到刚刚我们在8080这个tomcat提交的东西说明,已经配置成功。

(注意:必须是同一个窗口)

 

 

 

20110623补充:后来在项目中使用session的时候报异常如下:

java.lang.IllegalArgumentException: setAttribute: Non-serializable attribute



这是由于session存入的对象不可以序列化,只需要实现Serializable接口即可

 

 

利用msm+tomcat6管理session可以查看:http://code.google.com/p/testcq/wiki/MemcacheSessionShare

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值