3 ways of session sharing in tomcat cluster

The first two need to use memcached or redis to store sessions, and the last one uses terracotta server sharing. 
It is recommended to use redis, not only because it can persist the cached content, but also because the single object it supports is relatively large, and the data types are rich, 
not only for caching sessions, but also for other purposes, killing two birds with one stone. 

1. It is recommended to use the filter method to store 
this method, because its server usage range is more, not only limited to tomcat, and the principle of implementation is relatively simple and easy to control. 
You can use the official website of memcached-session-filter 
: http://code.google.com/p/memcached-session-filter/Official 
introduction: To solve the java web container session sharing in a cluster environment, use filter interceptor and memcached to achieve. It has passed the test on tomcat 6 and websphere 8. The current network has 2,000 concurrent transactions and 11 million daily PVs. 
Session events including create destory and attribute change are not supported for 
the time being. It is very good and small in size, but this stuff needs to be used with spring, and the objects stored in memcached are required to implement the serialization interface of java. As 
you all know, java itself has Serialization performance is also mediocre. 
I simply extended it, no longer relying on spring, and using javolution to achieve serialization, the cached objects are no longer limited. 
I haven't found the implementation of redis for the time being. I will use redis to store my own implementation and use kyro for serialization. The details will be written out separately when I have time. 


2. Use the tomcat session manager method to store 
this method The server can only use tomcat, but there are implementations for memcached and redis on the Internet, just configure it directly. 
Memcached implementation: 
URL: http://code.google.com/p/memcached-session-manager/ 
Modify the context.xml file in the conf directory of tomcat: 
  <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager "    
  memcachedNodes="n1:localhost:11211 n2:localhost:11212"    
  failoverNodes="n2"    
  requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"    
  sessionBackupAsync="false"    
  sessionBackupTimeout="100"    
  transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"    
  copyCollectionsForSerialization="false" 


memcached-session-manager-1.3.0.jar 
msm-javolution-serializer-1.3.0.jar 
javolution-5.4.3.1.jar 
memcached-2.4.2.jar 

redis 实现: 
网址:https://github.com/jcoleman/tomcat-redis-session-manager 
同样修改 tomcat 的 conf 目录下的 context.xml 文件: 
&lt;Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" /&gt; 
&lt;Manager className="com.radiadesign.catalina.session.RedisSessionManager" 
         host="localhost" 
         port="6379" 
         database="0" 
         maxInactiveInterval="60"/&gt; 
以上是以 1.2 版为例子,需要用的 jar 包: 
tomcat-redis-session-manager-1.2-tomcat-6.jar 
jedis-2.1.0.jar 
commons-pool-1.6.jar 


3、使用 terracotta 服务器共享 
这种方式配置有点复杂,大家到网上搜索一下吧。 

以上配置成功后,前端使用 nginx 进行负载均衡就行了,同时使用 Gzip 压缩 和 静态文件缓存。 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326831106&siteId=291194637