基於Java IO 序列化方案的memcached-session-manager多memcached節點配置


在公司項目里想要在前端通過nginx將請求負載均衡,而后台的幾組tomcat的session通過memcached(non-sticky模式)進行統一管理,這幾組tomcat部署的web app是同一應用,session的變化要統一,項目組最后采用memcached-session-manager來對tomcat的session進行管理。

 

  session的序列化方案官方推薦的有4種

  1. java serialization
  2. msm-kryo-serializer
  3. msm-javolution-serializer
  4. msm-xstream-serializer

關於這幾種,官方也給出了比較:

 

  • 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。

 

  網上有一些例子都是關於Javolution的,我個人傾向於使用Kryo,至於序列化出現的問題還可以自己解決,不過先使用java IO來配置,先用起來,以后再慢慢優化,換成Kryo。

 

  直接在$CATALINA_HOME/lib/下添加memcached-2.5.jar和memcached-session-manager-1.4.0.jar,然后對$CATALINA_HOME/conf/server.xml修改相應的配置

 

Xml代碼   收藏代碼
  1. <Context path="/webapp" docBase="D:\webapp\WebRoot" reloadable="false">  
  2.   <Manager   
  3.      className"de.javakaffee.web.msm.MemcachedBackupSessionManager"  
  4.      memcachedNodes"n1:192.168.112.1:11211,n2:192.168.112.2:11211"   
  5.      sticky="false"   
  6.      lockingMode="auto"   
  7.      requestUriIgnorePattern".*\.(png|gif|jpg|css|js)$"    
  8.      sessionBackupAsync"false"   
  9.      sessionBackupTimeout"0"   
  10.      memcachedProtocol="binary"  
  11.      transcoderFactoryClass"de.javakaffee.web.msm.JavaSerializationTranscoderFactory"    
  12.   />  
  13. </Context>  

 由於項目對session 管理模式是non-sticky的,所以不配置failoverNodes,任何一個web工程對session的修改要及時更新到memcache上,所以sessionBackupTimeout為0(不同的應用有不同的配置,這樣配置是符合我現在這個項目要求的)。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM