在公司項目裏想要在前端經過nginx將請求負載均衡,然後臺的幾組tomcat的session經過memcached(non-sticky模式)進行統一管理,這幾組tomcat部署的web app是同一應用,session的變化要統一,項目組最後採用memcached-session-manager來對tomcat的session進行管理。css
session的序列化方案官方推薦的有4種前端
- java serialization
- msm-kryo-serializer
- msm-javolution-serializer
- msm-xstream-serializer
關於這幾種,官方也給出了比較:java
- Java serialization is very robust and a proven technology. The biggest disadvantage IMHO is that different class versions cannot be handled.
- Kryo is an extremely fast binary serialization library. In the popular thrift-protobuf-compare benchmark it's one of the fastest serialization toolkits - and it differs from the fastest in that it does NOT need a schema definition of serialized data, which is a requirement for serialization arbitrary session data. A disadvantage of using kryo based serialization is that it's binary - you just cannot look how the serialized object graph looks like. This is my favorite serialization strategy, just because of its great performance.
- Javolution is a very good and fast xml binding toolkit. The reflection part is written by me and adds the bits that are actually binding POJOs to xml. It is covered well with unit tests, however I cannot guarantee that there's no issue left to solve (actually this serialization strategy was in use in my own projects, now replaced by kryo based serialization).
- XStream based serialization should be very robust as this is an often used java object binding library. The biggest disadvantage IMHO is the relatively bad performance.
要注意的是javolution是惟一支持copyCollectionsForSerialization="true"時對能對線程不安全的collection進行序列化特性的,其餘的方案要對序列化線程不安全的collection時特別處理,性能最好的序列化方案是Kryo。nginx
網上有一些例子都是關於Javolution的,我我的傾向於使用Kryo,至於序列化出現的問題還能夠本身解決,不過先使用java IO來配置,先用起來,之後再慢慢優化,換成Kryo。web
直接在$CATALINA_HOME/lib/下添加memcached-2.5.jar和memcached-session-manager-1.4.0.jar,而後對$CATALINA_HOME/conf/server.xml修改相應的配置tomcat
- <Context path="/webapp" docBase="D:\webapp\WebRoot" reloadable="false">
- <Manager
- className= "de.javakaffee.web.msm.MemcachedBackupSessionManager"
- memcachedNodes= "n1:192.168.112.1:11211,n2:192.168.112.2:11211"
- sticky="false"
- lockingMode="auto"
- requestUriIgnorePattern= ".*\.(png|gif|jpg|css|js)$"
- sessionBackupAsync= "false"
- sessionBackupTimeout= "0"
- memcachedProtocol="binary"
- transcoderFactoryClass= "de.javakaffee.web.msm.JavaSerializationTranscoderFactory"
- />
- </Context>
因爲項目對session 管理模式是non-sticky的,因此不配置failoverNodes,任何一個web工程對session的修改要及時更新到memcache上,因此sessionBackupTimeout爲0(不一樣的應用有不一樣的配置,這樣配置是符合我如今這個項目要求的)。安全