memcached-session-manager配置

文章是根据memcached-session-manager官方配置方法wiki页面翻译整理,

关于memcached-session-manager的介绍,

具体参见官网:http://code.google.com/p/memcached-session-manager/,也可以参考:http://gong1208.iteye.com/blog/1596120

 

Introduction

如果为了简单使用,你只需要安装一个tomcat(6或者7)和memcached,在生产环境中可能会有多台tomcat服务器以及多台可用的memcached节点,并安装在不同的机器上,我们可以使用黏性session(sticky sessions)或者非黏性session(non-sticky sessions),memcached-session-manager (msm) 对这两种操作模式都支持。

下面给出一个黏性session模式的设置示例,此实例中安装了2个tomcat以及2个memcached。

Tomcat-1(t1)的首要选择是把session存储在memcached-2 (m2)上(m2是t1的一个普通节点),而m2是运行在另外的一台机器上。只有当m2不可用(宕机或无法访问)时,t1才会把session存储到memcached-1(m1,m1是t1的故障转移节点)上。使用这种配置,即使机器1宕机了session也不会丢失。具体如下图所示:

memcached-session-manager with sticky sessions

我们如何设置才能实现呢?

 

Decide which serialization strategy to use

从1.1版开始,MSM就提供了多种可选的session序列化策略,默认的策略是使用java进行序列化,这种实现已经集成在memcached-session-manager.jar包中了,其它的策略则可以通过不同的jar包来提供实现。在下面的章节中,我们可以了解到每种策略所需要的jar包具体有哪些。

 

Configure tomcat

关于tomcat的配置主要包括两个方面,首先需要下载所需要的包,放到tomcat安装目录下的lib目录下(严格来说应该是$CATALINA_HOME/lib/)以及我们应用的WEB-INF/lib/ 目录下,同时还需要修改$CATALINA_HOME/conf/context.xml文件,并在<Context>元素中添加memcached session管理的配置信息。

 

Add memcached-session-manager jars to tomcat

不管你选择哪种序列化策略,你都需要 memcached-session-manager-${version}.jar ,如果你使用的是tomcat6,则还需要下载 memcached-session-manager-tc6-${version}.jar ,如果使用的是tomcat7则下载 memcached-session-manager-tc7-${version}.jar 。同时还需要下载 spymemcached-2.7.3.jar.下载这完这些jar包后把jar包放到 $CATALINA_HOME/lib/目录

 

Add custom serializers to your webapp (optional)

如果只是使用java序列化的话,那么需要的jar包就是以上所列出的那些了,但是如果想使用自定义的序列化策略(通常性能会更佳),我们还需要下载相应的jar包并放到我们webapp下的WEB-INF/lib/目录中。

如果你的应用使用了maven来进行jar包管理,那么你只需要在pom.xml中加入相应的序列化策略依赖定义就可以了,具体的maven依赖定义如下(任选一种就oK了):

kryo-serializer:

Xml代码  收藏代码
  1. <dependency>  
  2.     <groupId>de.javakaffee.msm</groupId>  
  3.     <artifactId>msm-kryo-serializer</artifactId>  
  4.     <version>1.6.0</version>  
  5.     <scope>runtime</scope>  
  6. </dependency>  

javolution:

Xml代码  收藏代码
  1. <dependency>  
  2.     <groupId>de.javakaffee.msm</groupId>  
  3.     <artifactId>msm-javolution-serializer</artifactId>  
  4.     <version>1.6.0</version>  
  5.     <scope>runtime</scope>  
  6. </dependency>  

xstream:

Xml代码  收藏代码
  1. <dependency>  
  2.     <groupId>de.javakaffee.msm</groupId>  
  3.     <artifactId>msm-xstream-serializer</artifactId>  
  4.     <version>1.6.0</version>  
  5.     <scope>runtime</scope>  
  6. </dependency>  

flexjson:

Xml代码  收藏代码
  1. <dependency>  
  2.     <groupId>de.javakaffee.msm</groupId>  
  3.     <artifactId>msm-flexjson-serializer</artifactId>  
  4.     <version>1.6.0</version>  
  5.     <scope>runtime</scope>  
  6. </dependency>  

 

如果我们不是使用maven仓库来对依赖进行管理的话 ,我们需要针对每种策略下载单独需要的jar包,具体如下:

 

Configure memcached-session-manager as <Context> Manager

处理完jar包之后,我们还需要修改 $CATALINA_HOME/conf/context.xml文件中Context节点下的内容,添加memcached-session-manager配置。

下面将会讲解关于tomcat配置的具体的示例,主要包括使用memcached来管理黏性session和非黏性session以及使用membase来管理非黏性session。示例基于的前提是假设我们有2个memcached实例,一个运行在host1主机,另一个运行在host2主机,示例使用的序列化方式为kryo。

下面我们给出tomcat1的配置,假设tomcat1和memcached节点n1都是运行在host1主机上,其中属性failoverNodes="n1"的作用是告诉msm最好是把session保存在memcached "n2"节点上,只有在n2节点不可用的情况下才把session保存在n1节点。这样即使host1主机宕机,仍然可以通过host2上的tomcat2访问存放在memcached "n2" 节点中的session。

tomcat1 configuration:

Xml代码  收藏代码
  1. <Context>  
  2.   ...  
  3.   <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"  
  4.     memcachedNodes="n1:host1.yourdomain.com:11211,n2:host2.yourdomain.com:11211"  
  5.     failoverNodes="n1"  
  6.     requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"  
  7.     transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"  
  8.     />  
  9. </Context>  

以上就是tomcat1的配置信息,对于tomcat2,我们只需要修改一下failoverNodes属性的值为"n2" ,这样tomcat2就会优先把session存放到memcached "n1"节点,其余配置信息都一样。

 

下面演示一个非黏性session管理的配置示例,对于非黏性的session管理,我们不需要配置failoverNodes属性,因为所有sessions在tomcat集群中是循环可见的,并不会绑定到某一个单独的tomcat,对于非黏性session管理,集群中的所有tomcat都是用同一个配置,具体信息如下:

Xml代码  收藏代码
  1. <Context>  
  2.   ...  
  3.   <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"  
  4.     memcachedNodes="n1:host1.yourdomain.com:11211,n2:host2.yourdomain.com:11211"  
  5.     sticky="false"  
  6.     sessionBackupAsync="false"  
  7.     lockingMode="uriPattern:/path1|/path2"  
  8.     requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"  
  9.     transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"  
  10.     />  
  11. </Context>  

如果是使用membase来对session进行管理,那么则某一个节点的配置如下:

Xml代码  收藏代码
  1. <Context>  
  2.   ...  
  3.   <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"  
  4.     memcachedNodes="http://host1.yourdomain.com:8091/pools"  
  5.     username="bucket1"  
  6.     password="topsecret"  
  7.     memcachedProtocol="binary"  
  8.     sticky="false"  
  9.     sessionBackupAsync="false"  
  10.     requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"  
  11.     transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"  
  12.     />  
  13. </Context>  

在context.xml中配置完msm之后, 我们就可以启动我们的应用程序了,这样所有的session将会根据系统配置存储到指定的memcached节点或者membase中。

 

Overview over memcached-session-manager configuration attributes

className (required)

类名:de.javakaffee.web.msm.MemcachedBackupSessionManager

 

memcachedNodes (required)

memcached节点:此属性应该包含所有运行的memcached节点或者membase bucket的uri地址,每一个memcached节点的属性定义格式为<id>:<host>:<port>,多个节点定义直接使用空格或者逗号分隔,形如:memcachedNodes="n1:app01:11211,n2:app02:11211",如果只有单个的memcached节点,则<id>是可选项,只需配置<host>:<port>即可,形如:memcachedNodes="localhost:11211"。

如果我们配置的是membase,那么从1.6.0版本开始,我们可以配置指定一个或者多个membase bucket uris,形如:http://host1:8091/pools,http://host2:8091/pools。Bucket 名称和密码通过属性username,password来定义。membase buckets连接需要遵循memcached协议,传输数据通过二进制流方式。

 

failoverNodes (optional, must not be used for non-sticky sessions)

故障转移节点:可选项,对非黏性session不可用,属性必须包含memcached节点集群的所有ids。节点id之间用空格或者逗号分隔。

 

username (since 1.6.0, optional)

从1.6.0版开始使用,并且是可选的。用来进行membase bucket或者SASL验证。

 

password (since 1.6.0, optional)

从1.6.0版开始使用,并且是可选的。用来进行membase bucket或者SASL验证,密码可以为空。

 

memcachedProtocol (since 1.3, optional, default text)

定义memcached协议,默认使用text文本

 

sticky (since 1.4.0, optional, default true)

定义session方式为黏性或非黏性,默认为true

 

lockingMode (since 1.4.0, optional, for non-sticky sessions only, default none)

只有非黏性session才使用,默认值为none

  • none: 从不对session进行锁定
  • all: session将一直被锁定,直到请求结束
  • auto: 对于只读请求,session将不会被锁定,如果是非只读请求,则session会被锁定
  • uriPattern:<regexp>: 通过正则表达式的方式来对请求uri以及查询字符串进行匹配,只有匹配上的才会被锁定。

requestUriIgnorePattern (optional)

 

sessionBackupAsync (optional, default true)

 

backupThreadCount (since 1.3, optional, default number-of-cpu-cores)

 

sessionBackupTimeout (optional, default 100)

 

operationTimeout (since 1.6.0, optional, default 1000)

 

sessionAttributeFilter (since 1.5.0, optional)

 

transcoderFactoryClass (since 1.1, optional, default de.javakaffee.web.msm.JavaSerializationTranscoderFactory)

序列化接口实现:

  • Java serialization: de.javakaffee.web.msm.JavaSerializationTranscoderFactory
  • Kryo based serialization: de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory
  • Javolution based serialization: de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory
  • XStream based serialization: de.javakaffee.web.msm.serializer.xstream.XStreamTranscoderFactory

 

copyCollectionsForSerialization (since 1.1, optional, default false)

 

customConverter (since 1.2, optional)

 

enableStatistics (since 1.2, optional, default true)

 

enabled (since 1.4.0, optional, default true)

 

  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 
 10       http://www.apache.org/licenses/LICENSE-2.0
 11 
 12   Unless required by applicable law or agreed to in writing, software
 13   distributed under the License is distributed on an "AS IS" BASIS,
 14   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15   See the License for the specific language governing permissions and
 16   limitations under the License.
 17 -->
 18 <!-- Note:  A "Server" is not itself a "Container", so you may not
 19      define subcomponents such as "Valves" at this level.
 20      Documentation at /docs/config/server.html
 21  -->
 22 <Server port="8005" shutdown="SHUTDOWN">
 23   <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
 24   <!-- Security listener. Documentation at /docs/config/listeners.html
 25   <Listener className="org.apache.catalina.security.SecurityListener" />
 26   -->
 27   <!--APR library loader. Documentation at /docs/apr.html -->
 28   <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
 29   <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
 30   <Listener className="org.apache.catalina.core.JasperListener" />
 31   <!-- Prevent memory leaks due to use of particular java/javax APIs-->
 32   <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
 33   <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
 34   <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
 35 
 36   <!-- Global JNDI resources
 37        Documentation at /docs/jndi-resources-howto.html
 38   -->
 39   <GlobalNamingResources>
 40     <!-- Editable user database that can also be used by
 41          UserDatabaseRealm to authenticate users
 42     -->
 43     <Resource name="UserDatabase" auth="Container"
 44               type="org.apache.catalina.UserDatabase"
 45               description="User database that can be updated and saved"
 46               factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
 47               pathname="conf/tomcat-users.xml" />
 48   </GlobalNamingResources>
 49 
 50   <!-- A "Service" is a collection of one or more "Connectors" that share
 51        a single "Container" Note:  A "Service" is not itself a "Container",
 52        so you may not define subcomponents such as "Valves" at this level.
 53        Documentation at /docs/config/service.html
 54    -->
 55   <Service name="Catalina">
 56 
 57     <!--The connectors can use a shared executor, you can define one or more named thread pools-->
 58     <!--
 59     <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
 60         maxThreads="150" minSpareThreads="4"/>
 61     -->
 62 
 63 
 64     <!-- A "Connector" represents an endpoint by which requests are received
 65          and responses are returned. Documentation at :
 66          Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
 67          Java AJP  Connector: /docs/config/ajp.html
 68          APR (HTTP/AJP) Connector: /docs/apr.html
 69          Define a non-SSL HTTP/1.1 Connector on port 8080
 70     -->
 71     <Connector port="8080" protocol="HTTP/1.1"
 72                connectionTimeout="20000"
 73                redirectPort="8443"  URIEncoding="UTF-8" />
 74     <!-- A "Connector" using the shared thread pool-->
 75     <!--
 76     <Connector executor="tomcatThreadPool"
 77                port="8080" protocol="HTTP/1.1"
 78                connectionTimeout="20000"
 79                redirectPort="8443" />
 80     -->
 81     <!-- Define a SSL HTTP/1.1 Connector on port 8443
 82          This connector uses the BIO implementation that requires the JSSE
 83          style configuration. When using the APR/native implementation, the
 84          OpenSSL style configuration is required as described in the APR/native
 85          documentation -->
 86     <!--
 87     <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
 88                maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
 89                clientAuth="false" keystoreFile="C:/tomcat.keystore" keystorePass="password" sslProtocol="TLS" />
 90                -->
 91     <!-- Define an AJP 1.3 Connector on port 8009 -->
 92     <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
 93  
 94      
 95 
 96     <!-- An Engine represents the entry point (within Catalina) that processes
 97          every request.  The Engine implementation for Tomcat stand alone
 98          analyzes the HTTP headers included with the request, and passes them
 99          on to the appropriate Host (virtual host).
100          Documentation at /docs/config/engine.html -->
101 
102     <!-- You should set jvmRoute to support load-balancing via AJP ie :
103     <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
104     -->
105     <Engine name="Catalina" defaultHost="localhost"  jvmRoute="worker1">
106 
107       <!--For clustering, please take a look at documentation at:
108           /docs/cluster-howto.html  (simple how to)
109           /docs/config/cluster.html (reference documentation) -->
110       <!--
111       <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
112       
113       <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8"></Cluster>
114 -->
115       <!-- Use the LockOutRealm to prevent attempts to guess user passwords
116            via a brute-force attack -->
117       <Realm className="org.apache.catalina.realm.LockOutRealm">
118         <!-- This Realm uses the UserDatabase configured in the global JNDI
119              resources under the key "UserDatabase".  Any edits
120              that are performed against this UserDatabase are immediately
121              available for use by the Realm.  -->
122         <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
123                resourceName="UserDatabase"/>
124       </Realm>
125 
126       <Host name="localhost"  appBase="webapps"
127             unpackWARs="true" autoDeploy="true">
128 
129         <!-- SingleSignOn valve, share authentication between web applications
130              Documentation at: /docs/config/valve.html -->
131         <!--
132         <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
133         -->
134           
135 
136         <!-- Access log processes all example.
137              Documentation at: /docs/config/valve.html
138              Note: The pattern used is equivalent to using pattern="common" -->
139         <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
140                prefix="localhost_access_log." suffix=".txt"
141                pattern="%h %l %u %t &quot;%r&quot; %s %b" />
142 
143         <Context path="" docBase="test">
144             <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"  
145                     memcachedNodes="n1:localhost:11211,n2:localhost:11311"  
146                     sticky="false"  
147                     requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"  
148                     sessionBackupAsync="false"  
149                     sessionBackupTimeout="100"  
150              transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"  
151              copyCollectionsForSerialization="false" />  
152 
153         </Context>
154 
155       </Host>
156     </Engine>
157   </Service>
158   
159    <Service name="Catalina1">
160 
161     <!--The connectors can use a shared executor, you can define one or more named thread pools-->
162     <!--
163     <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
164         maxThreads="150" minSpareThreads="4"/>
165     -->
166 
167 
168     <!-- A "Connector" represents an endpoint by which requests are received
169          and responses are returned. Documentation at :
170          Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
171          Java AJP  Connector: /docs/config/ajp.html
172          APR (HTTP/AJP) Connector: /docs/apr.html
173          Define a non-SSL HTTP/1.1 Connector on port 8080
174     -->
175     <Connector port="8081" protocol="HTTP/1.1"
176                connectionTimeout="20000"
177                redirectPort="8445"  URIEncoding="UTF-8" />
178     <!-- A "Connector" using the shared thread pool-->
179     <!--
180     <Connector executor="tomcatThreadPool"
181                port="8080" protocol="HTTP/1.1"
182                connectionTimeout="20000"
183                redirectPort="8443" />
184     -->
185     <!-- Define a SSL HTTP/1.1 Connector on port 8443
186          This connector uses the BIO implementation that requires the JSSE
187          style configuration. When using the APR/native implementation, the
188          OpenSSL style configuration is required as described in the APR/native
189          documentation -->
190     <!--
191     <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
192                maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
193                clientAuth="false" keystoreFile="C:/tomcat.keystore" keystorePass="password" sslProtocol="TLS" />
194                -->
195     <!-- Define an AJP 1.3 Connector on port 8009 -->
196     <Connector port="8010" protocol="AJP/1.3" redirectPort="8445" />
197  
198      
199 
200     <!-- An Engine represents the entry point (within Catalina) that processes
201          every request.  The Engine implementation for Tomcat stand alone
202          analyzes the HTTP headers included with the request, and passes them
203          on to the appropriate Host (virtual host).
204          Documentation at /docs/config/engine.html -->
205 
206     <!-- You should set jvmRoute to support load-balancing via AJP ie :
207     <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
208     -->
209     <Engine name="Catalina1" defaultHost="localhost"  jvmRoute="worker2">
210 
211       <!--For clustering, please take a look at documentation at:
212           /docs/cluster-howto.html  (simple how to)
213           /docs/config/cluster.html (reference documentation) -->
214       <!--
215       <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
216       
217       <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8"></Cluster>
218 -->
219       <!-- Use the LockOutRealm to prevent attempts to guess user passwords
220            via a brute-force attack -->
221       <Realm className="org.apache.catalina.realm.LockOutRealm">
222         <!-- This Realm uses the UserDatabase configured in the global JNDI
223              resources under the key "UserDatabase".  Any edits
224              that are performed against this UserDatabase are immediately
225              available for use by the Realm.  -->
226         <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
227                resourceName="UserDatabase"/>
228       </Realm>
229 
230       <Host name="localhost"  appBase="webapps"
231             unpackWARs="true" autoDeploy="true">
232 
233         <!-- SingleSignOn valve, share authentication between web applications
234              Documentation at: /docs/config/valve.html -->
235         <!--
236         <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
237       -->
238          
239   
240         <!-- Access log processes all example.
241              Documentation at: /docs/config/valve.html
242              Note: The pattern used is equivalent to using pattern="common" -->
243         <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
244                prefix="localhost_access_log." suffix=".txt"
245                pattern="%h %l %u %t &quot;%r&quot; %s %b" />
246             
247 
248         <Context path="" docBase="test">
249             <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"  
250                     memcachedNodes="n1:localhost:11211,n2:localhost:11311"  
251                     sticky="false"  
252                     requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"  
253                     sessionBackupAsync="false"  
254                     sessionBackupTimeout="100"  
255              transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"  
256              copyCollectionsForSerialization="false" />  
257 
258         </Context>
259 
260       </Host>
261     </Engine>
262   </Service>
263    
264 </Server>

 

posted @ 2015-08-14 10:03  wangjuneng  阅读(495)  评论(0编辑  收藏  举报