Example for non-sticky sessions with couchbase + kryo
Example for multiple contexts sharing the same session>
kryo:
Decide which serialization strategy to use 决定使用哪一个序列化的策略
There are several session serialization strategies available, as they are described on SerializationStrategies. The default strategy uses java serialization and is already provided by the memcached-session-manager jar. Other strategies are provided by separate jars, in the section below you'll see which jars are required for which strategy.
序列化指的就是数据流式化,Tomcat是对象式编程语言而且是完全面向对象的,所以内部的所有的数据结构,都是对象,包括会话,而对象通常都有特定结构和元数据的,在内存中有结构,利用内存中存储系统专门去存储的。这种数据有特别诡异的地方是:
在内存中存好,同步到磁盘上去保存,都得流式化,只要通过网络传输,就一定要拆成0101代码发送出去,都需要流式化,往磁盘上发送也是一样,内部总线也是0101发出去的,往磁盘上存储都需要流式化,网络发送也要流式化。因此,像这种会话信息,想保存到memcached中,memcached本来就只能存储流式化数据,第一步就需要把会话流式化,然后才能存下来。流式化就需要流式化的工具,进行辅助实现。对memcached而言,流式化工具有很多
Add custom serializers to your webapp (optional) 定制使用serializers
Add custom serializers to your webapp (optional) 添加自动以流式化工具
If you want to use java's built in serialization nothing more has to be done. If you want to use a custom serialization strategy (e.g. because of better performance) this has to be deployed with your webapp so that they're available in WEB-INF/lib/. #类库的存放位置,把下载的类库放进去
As msm is available in maven central (under groupId de.javakaffee.msm) you can just pull it in using the dependency management of your build system. With maven you can use this dependency definition for the kryo-serializer:
For javolution the artifactId is msm-javolution-serializer, for xstream msm-xstream-serializerand for flexjson it's msm-flexjson-serializer.
If you're not using a dependency management based on maven repositories these are the jars you need for the different serializers:
不同的流式化工具
Example for non-sticky sessions + kryo + Redis 非粘性会话 + Redis
这里通过使用Example for sticky sessions + kryo方式进行构建
Example for non-sticky sessions + kryo
The following example shows a configuration for non-sticky sessions. In this case there's no need for failoverNodes, as sessions are served by all tomcats round-robin and they're not bound to a single tomcat. For non-sticky sessions the configuration (for both/all tomcats) would look like this:
准备Tomcat7专用的类
Add memcached-session-manager jars to tomcat
Independent of the chosen serialization strategy you always need the memcached-session-manager-${version}.jar and either memcached-session-manager-tc6-${version}.jar for tomcat6, memcached-session-manager-tc7-${version}.jar for tomcat7 (attention: tomcat 7.0.23+), memcached-session-manager-tc8-${version}.jar for tomcat8 or memcached-session-manager-tc9-${version}.jar for tomcat9.
If you're using memcached, you also need the spymemcached-${version}.jar. Tested up to v2.12.3.
If you're using couchbase, you need additionally these jars: couchbase-client-1.4.0.jar jettison-1.3.jar, commons-codec-1.5.jar, httpcore-4.3.jar, httpcore-nio-4.3.jar, netty-3.5.5.Final.jar.
If you're using Redis, you need the jedis-2.9.0.jar.
Please download the appropriate jars and put them in $CATALINA_HOME/lib/.
下载和序列化相关的jar文件,因为示例写的是keyo,不再进行修改,直接使用kryo,也可以修改为其他的流式化工具
kryo-serializer: msm-kryo-serializer, kryo-serializers-0.34+, kryo-3.x, minlog, reflectasm, asm-5.x, objenesis-2.x
要把下载的jar包放到WEB-INI/lib目录下
Configure memcached-session-manager as Manager
Example for sticky sessions + kryo
The following example shows the configuration of the first tomcat, assuming that it runs on host1, together with memcached "n1". The attribute failoverNodes="n1" tells msm to store sessions preferably in memcached "n2" and only store sessions in "n1" (running on the same host/machine) if no other memcached node (here only n2) is available (even if host1 goes down completely, the session is still available in memcached "n2" and could be served by the tomcat on host2). For the second tomcat (on host2) you just need to change the failover node to "n2", so that it prefers the memcached "n1". Everything else should be left unchanged.
所以,可以实现故障转移了
现在把memcached启动起来
[root@server2 /app]# systemctl start memcached
每一次转移,换一个节点,都是有成本的,所以是不会移来移去的,除非定义了failed back
故障转移有两个盖面,fill lower,自己故障了,转到备用节点上去,原来的节点重新上线以后,无论备用节点坏还是不坏,都要回来,这叫fill back。如果没有定义fill back,就意味着转过去之后,至于对方坏了才会转回来。过去之后,自己就是备用的了,重新转回来的时候自己才是主。
这就是讲到的session server
memcached没有持久功能,如果停电,所有的session就全没了。这是很悲剧的。所以,建议使用Redis,做session server
If you're using Redis, you need the jedis-2.9.0.jar.
Example for non-sticky sessions + kryo + Redis
The following example shows a configuration which uses a Redis server at the URL "redis.example.com" for session storage for non-sticky sessions. Here the configuration (for both/all tomcats) would look like this: