`
cuijiemin
  • 浏览: 264137 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

nginx负载均衡 tomcat集群 memcache共享session

阅读更多
要集群tomcat主要是解决SESSION共享的问题,因此我利用memcached来保存session,多台TOMCAT服务器即可共享SESSION了。
你可以自己写tomcat的扩展来保存SESSION到memcached。
这里推荐使用memcached-session-manager这个开源项目
[url]http://code.google.com/p/memcached-session-manager/ [/url],下面简称msm。
如何安装nginx、memcached、tomcat这些就不多说了。
先说明一下测试环境:
tomcat1、nginx、memcached安装在192.168.1.11
tomcat2安装在192.168.1.101

下面分步实现基于nginx的tomcat负载均衡和集群配置
一,tomcat集群
    1,先下载msm及其依赖包
   [url] http://memcached-session-manager.googlecode.com/files/memcached-session-manager-1.3.0.jar [/url]
  [url]   http://memcached-session-manager.googlecode.com/files/msm-javolution-serializer-jodatime-1.3.0.jar[/url]

[url] http://memcached-session-manager.googlecode.com/files/msm-javolution-serializer-cglib-1.3.0.jar [/url]

[url] http://spymemcached.googlecode.com/files/memcached-2.4.2.jar [/url]

[url] http://memcached-session-manager.googlecode.com/files/javolution-5.4.3.1.jar [/url]

2,将这5个包放到$TOMCAT_HOME/lib目录下
3,修改$TOMCAT_HOME/conf/server.xml

Xml代码1.


]<Context docBase="E:/java_codes/TestSession/WebContent" path="" reloadable="true" > 
2.<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" 
3.    memcachedNodes="n1:localhost:11211" 
4.    requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$" 
5.    sessionBackupAsync="false" 
6.    sessionBackupTimeout="100" 
7.    transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory" 
8.    copyCollectionsForSerialization="false" 
9.    /> 
10.</Context> 
 
<Context docBase="E:/java_codes/TestSession/WebContent" path="" reloadable="true" >
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
    memcachedNodes="n1:localhost:11211"
    requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"
    sessionBackupAsync="false"
    sessionBackupTimeout="100"
    transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
    copyCollectionsForSerialization="false"
    />
</Context>



这里的memcachedNodes是填写memcached节点,多个节点时可以以空隔分开,如:
n1:localhost:11211 n2:localhost:11212

sessionBackupTimeout的单位为分钟
  E:/java_codes/TestSession/WebContent 替换成你的WEB目录
   修改后重启两个TOMCAT即可,这个时候已经解决SESSION的共享问题.
二,配置nginx实现负载均衡
   以我的nginx.conf为例
Xml代码1.#user  nobody; 
2.worker_processes  1; 
3.
4.error_log  logs/error.log; 
5.
6.events { 
7.    worker_connections  1024; 
8.} 
9.
10.
11.http { 
12.    include       mime.types; 
13.    default_type  application/octet-stream; 
14.
15.    sendfile        on; 
16.    keepalive_timeout  65; 
17.
18.    #gzip  on; 
19.    upstream  www.yk2008.com   { 
20.              server   192.168.1.11:8080; 
21.              server   192.168.1.101:8080; 
22.    } 
23.    server { 
24.        listen       80; 
25.        server_name  www.yk2008.com; 
26.        charset utf-8; 
27.        location / { 
28.            root   html; 
29.            index  index.html index.htm; 
30.            proxy_pass        http://www.yk2008.com; 
31.            proxy_set_header  X-Real-IP  $remote_addr; 
32.            client_max_body_size  100m; 
33.        } 
34.
35.
36.        location ~ ^/(WEB-INF)/ {  
37.        deny all;  
38.        }  
39.
40.        error_page   500 502 503 504  /50x.html; 
41.        location = /50x.html { 
42.            root   html; 
43.        } 
44.
45.    } 
46.}
#user  nobody;
worker_processes  1;
error_log  logs/error.log;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    #gzip  on;
    upstream  www.yk2008.com   {
              server   192.168.1.11:8080;
              server   192.168.1.101:8080;
    }
    server {
        listen       80;
        server_name  www.yk2008.com;
        charset utf-8;
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass        http://www.yk2008.com;
            proxy_set_header  X-Real-IP  $remote_addr;
            client_max_body_size  100m;
        }

        location ~ ^/(WEB-INF)/ {
     deny all;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
将www.yk2008.com替换成你的域名
192.168.1.11和192.168.1.101替换成你服务器的IP
OK,已经完成。启动nginx即可。





以下是wiki上面的原话:

SetupAndConfiguration  
This page shows what's necessary to get the memcached-session-manager up and running.
Introduction¶
For the most simple integration you just need to have a tomcat (6 or 7) and a memcached installed (or s.th. supporting the memcached protocol). In your production environment you probably will have several tomcats and you should also have several memcached nodes available, on different pieces of hardware. You can use sticky sessions or non-sticky sessions, memcached-session-manager supports both operation modes.
The following description shows an example for a setup with sticky sessions, with two instances of tomcat and two instances of memcached installed.
Tomcat-1 (t1) will primarily store it's sessions in memcached-2 (m2) which is running on another machine (m2 is a regular node for t1). Only if m2 is not available, t1 will store it's sessions in memcached-1 (m1, m1 is the failoverNode for t1). With this configuration, sessions won't be lost when machine 1 (serving t1 and m1) crashes. The following really nice ASCII art shows this setup.
<t1>   <t2>   . \ / .   .  X  .   . / \ . <m1>   <m2>Details¶
So what needs to be done for this?
Decide which serialization strategy to use¶
Starting with release 1.1 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.
Configure tomcat¶
The configuration of tomcat requires two things: you need to drop some jars in your $CATALINA_HOME/lib/ directory and you have to configure the memcached session manager in the server.xml.
Add jars to $CATALINA_HOME/lib/¶
Independent of the chosen serialization strategy you always need the memcached-session-manager-1.4.0-RC1.jar (for tomcat6, or memcached-session-manager-tc7-1.4.0-RC1.jar for tomcat7) and the memcached-2.5.jar (spymemcached). Just download and put them in $CATALINA_HOME/lib/.
Add jars to WEB-INF/lib/¶
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) you need to drop some jars into your projects WEB-INF/lib/ directory. In the following the jars are listed by serialization strategy.
For kryo based serialization (recommended) these jars are required additionally (in your WEB-INF/lib):
kryo-1.03.jar
minlog-1.2.jar
reflectasm-0.9.jar
asm-3.2.jar
kryo-serializers-0.8.jar
msm-kryo-serializer-1.3.6.jar
For javolution based serialization these jars are required additionally:
javolution-5.4.3.1.jar (a patched version of the latest javolution release, the patch is required to support serialization of jdk proxies)
msm-javolution-serializer-1.3.6.jar
For xstream based serialization these jars are required additionally:
xstream-1.3.1.jar
xpp3_min-1.1.3.4.O.jar (xml pull parser used by xstream)
msm-xstream-serializer-1.3.6.jar

Update server.xml¶
Configure the appropriate context in your $CATALINA_HOME/conf/server.xml so that it contains the Manager configuration for the memcached-session-manager, like this example for sticky sessions:
<Context path="" docBase="ROOT">   <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"     memcachedNodes="n1:localhost:11211 n2:localhost:11212"     failoverNodes="n1"     requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"     transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"     /> </Context>For non-sticky sessions the configuration would look like this:
<Context path="" docBase="ROOT">   <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"     memcachedNodes="n1:localhost:11211 n2:localhost:11212"     sticky="false"     lockingMode="uriPattern:/path1|/path2"     requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"     transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"     /> </Context>These example configurations assume, that you have running two memcached nodes on localhost, one on port 11211 and another one on port 11212, and that you want to use kryo based serialization. More details to configuration attributes are provided in the section below.
Now you have finished the configuration of your first tomcat. For the second tomcat you just need to change the failover node when you're using sticky sessions.
After this is done, you can just start your application and sessions will be stored in the configured memcached nodes as configured. Now you should do some tests with a simulated tomcat failure, a restart of a memcached node etc. - have fun! :-)
MemcachedBackupSessionManager configuration attributes¶
className (required)
This should be set to de.javakaffee.web.msm.MemcachedBackupSessionManager to get sessions stored in memcached. However, since version 1.3.6 there's also a de.javakaffee.web.msm.DummyMemcachedBackupSessionManager that can be used for development purposes: it simply serializes sessions to a special in memory map and deserializes the serialized data at the next request when a session is requested (without really using this session) - just to see if deserialization is working for each request. Your application is still using sessions as if the memcached-session-manager (or DummyMemcachedBackupSessionManager) would not be existing. Session serialization/deserialization is done asynchronously. The configuration attributes memcachedNodes and failoverNode are not used to create a memcached client, so serialized session data will not be sent to memcached - and therefore no running memcacheds are required.
memcachedNodes (required)
This attribute must contain all memcached nodes you have running. Each memcached node is defined as <id>:<host>:<port>. Several definitions are separated by space or comma

详情请见:
http://code.google.com/p/memcached-session-manager/wiki/SetupAndConfiguration
1
0
分享到:
评论
1 楼 sunlightcs 2011-03-23  
写得不错了。

相关推荐

    tomcat整合nginx负载均衡+memcache共享session全部程序包

    在构建高性能、高可用性的...总之,这个“tomcat7+nginx+memcached”程序包提供了一种高效、可靠的Web应用架构,通过Nginx实现负载均衡,借助Memcache实现跨服务器的Session共享,极大地提升了系统的可用性和扩展性。

    nginx,tomcat集群,session共享

    nginx,tomcat集群,session共享,基于memcache 包含nginx配置,tomcat配置,以及tomcat所需要的jar 不包含nginx,tomcat,jdk,memcache的安装

    Nginx+tomcat6+memcache配置集群session共享所需jar包

    这里我们关注的是如何通过Nginx、Tomcat6和Memcached实现集群中的session共享。这个压缩包“Nginx+tomcat6+memcache所需jar包”提供了实现这一目标所需的组件。 首先,Nginx是一个高性能的反向代理服务器,常用于...

    Nginx+Memcache+Tomcat集群(session共享)

    【Nginx+Memcache+Tomcat集群(session共享)】是一种常见的高可用性和负载均衡解决方案,主要用于提升Web应用的性能和可扩展性。这个配置利用Nginx作为反向代理和负载均衡器,Memcache作为分布式session存储,而...

    Nginx+Tomcat7+Memcached集群Session共享

    Nginx+Tomcat7+Memcached集群Session共享 完整例子 主要是利用memcached-session-manager(下面简称msm)开源tomcat插件改变tomcat原始的session存储机制,将session的存储放到分布式缓存memcache中,从而实现对...

    性能调优 海量并发 系统架构

    Nginx+tomcat集群Memcached+Session复制 高性能高并发服务器架构 基于nginx的tomcat负载均衡和集群 实现多服务器负载均衡 系统性能优化 数据库 Nginx+Squid负载均衡 配置好的集群 总共三十个文档"&gt;Apache+Tomcat+...

    Memcache Session Manager Tomcat8.5.6

    标题 "Memcache Session Manager Tomcat8.5.6" 指的是在Tomcat 8.5.6版本中使用Memcache作为会话管理器的一种配置。这种配置旨在提高Web应用在集群环境下的性能和可扩展性,通过将用户的会话数据存储在分布式缓存...

    Memcache Session Manager + Tomcat8.5.6

    总结起来,这个架构结合了Nginx的负载均衡、Tomcat的Web应用处理、Memcache的高效缓存和Kryo的序列化技术,实现了高可用、高性能的Web服务,同时解决了session共享的问题。在实际部署时,还需要根据具体需求进行优化...

    tomcat集群安装配置_session.doc

    ### Tomcat集群安装配置_session知识点概述 #### 一、项目背景及环境介绍 根据文档提供的信息,本项目是由西安辉盛科技发展有限责任公司发起的,旨在实现Tomcat集群的搭建,以便于支持高并发访问场景下的应用服务...

    tomcat8 +memcached session 共享jar包

    `Tomcat8`提供了与`Memcached`集成的插件,通过这个插件,session可以被序列化并存储在`Memcached`服务器上,所有集群中的`Tomcat`实例都能访问到这些session数据,从而实现跨服务器的session共享。 在标题和描述中...

    Ngxin+双tomcat负载+mem做session共享

    在构建高可用和高性能的Web应用环境中,...通过以上步骤,你已经成功搭建了一个使用Nginx+双Tomcat负载均衡和Memcache进行会话共享的Web应用环境。这种架构提高了系统的可扩展性和可用性,同时保证了用户会话的一致性。

    nginx配置session+memcached所需jar包

    总结来说,实现Nginx、Tomcat集群与Memcached的Session共享,需要正确配置Nginx的反向代理设置,引入并配置Tomcat的Memcached Session Manager,以及确保所有必要的jar包已经正确安装。这一过程虽然涉及多个环节,但...

    apache+jk+memcache+nginx分布式网站建设笔记

    #### Session共享机制 在分布式环境中,为确保用户会话的一致性,通常采用Memcached等缓存系统来存储session数据。这涉及到对Kryo序列化库、Memcached Session Manager等组件的配置,以实现session的跨服务器共享。 ...

    tomcat9+memcached+memcachedSessionManagerjar.zip

    1. **tomcat集群**:在高并发或者需要高可用性的场景下,通过将多个Tomcat实例组成集群,可以实现负载均衡和故障转移。 2. **tomcat9**:这是Apache Tomcat的一个特定版本,提供了Java EE 8的支持,包括Web应用...

    tomcat 集群

    couchbase-client-1.2.2.jar javolution-5.4.3.1.jar kryo-1.03.jar kryo-serializers-0.10.jar memcached-session-manager-1.6.5.jar memcached-session-manager-tc6-1.6.5....Nginx+Tomcat+Memcached集群 收集,共享

    tomcat 配置宝典

    为了实现Tomcat集群间的Session共享,可以利用Memcached作为分布式缓存解决方案。 **Memcache简介** Memcached是一种高性能、分布式内存对象缓存系统,旨在减少数据库负载、加速动态Web应用的访问速度。 **...

    memcache + tomcat + tengin所用到的jar包

    本文将详细介绍如何在Tomcat中集成Memcached,并通过Tengin进行负载均衡,以及涉及的jar包信息。 1. **Memcached与Java的交互** 要在Java应用中使用Memcached,首先需要一个Java客户端库。这个压缩包中的"lib"目录...

Global site tag (gtag.js) - Google Analytics