`

Nginx+Memcached+Tomcat 搭载负责均衡实现session共享

阅读更多

一环境:

windows 7-x64

tomcat版本:apache-tomcat-6.0.36-windows-x64

 

jar包下载地址:http://code.google.com/p/memcached-session-manager/downloads/list

本人使用:javolution-5.4.3.1;memcached-session-manager-1.6.1;memcached-session-manager-tc6-1.6.1;msm-javolution-serializer-1.6.1;spymemcached-2.7.3

 

项目路径:

nginx,memcached,tomcat0,tomcat1(两份tomcat)都放在:E:\nginx+memcached+tomcat目录下,

测试项目:E:\nginx+memcached+tomcat\test0 和 E:\nginx+memcached+tomcat\test1

 

二 tomcat配置:

 

copy两份tomcat,分别放到tomcat0,、tomcat1目录下,分别命名tomcat_0、tomcat_1:

 

配置tomcat0:

配置第一个$TOMCAT/conf下的server.xml文件

修改以下4处配置项:

1.

 2.

 3.


 4.

 

上图中<Context docBase="E:\nginx+memcached+tomcat\test0" path=""></Context>的docBase=“”是tomcat部署测试项目路径

以下是本人tomcat0的server.xml的配置后的文件

<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
 -->
<Server port="8006" shutdown="SHUTDOWN">

  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  <Listener className="org.apache.catalina.core.JasperListener" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->
  <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container", 
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">
  
    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" 
        maxThreads="150" minSpareThreads="4"/>
    -->
    
    
    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->
    <Connector port="8088" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" />
    -->           
    <!-- Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the JSSE configuration, when using APR, the 
         connector should be using the OpenSSL style configuration
         described in the APR documentation -->
    <!--
    <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
               maxThreads="150" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />


    <!-- An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">         
    --> 
    <Engine name="Catalina" jvmRoute="tomcat0" defaultHost="localhost">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->        

      <!-- The request dumper valve dumps useful debugging information about
           the request and response data received and sent by Tomcat.
           Documentation at: /docs/config/valve.html -->
      <!--
      <Valve className="org.apache.catalina.valves.RequestDumperValve"/>
      -->

      <!-- This Realm uses the UserDatabase configured in the global JNDI
           resources under the key "UserDatabase".  Any edits
           that are performed against this UserDatabase are immediately
           available for use by the Realm.  -->
      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
             resourceName="UserDatabase"/>

      <!-- Define the default virtual host
           Note: XML Schema validation will not work with Xerces 2.2.
       -->
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">
            <Context docBase="E:\nginx+memcached+tomcat\test0" path="">
			</Context>
        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"  
               prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>
        -->

      </Host>
     
    </Engine>
  </Service>
</Server>

 

 配置tomcat1  

修改$TOMCAT/conf下的server.xml文件和$TOMCAT/conf下的context.xml文件。

同第一个配置方法相同,只是将图中圈出的部分修改为不同的值即可,注意:tomcat1中的修改路径,即:<Context docBase="E:\nginx+memcached+tomcat\test1" path=""></Context>

 

三.安装&配置memcached

 

安装:

将memcached-win61.zip解压到到E:\nginx+memcached+tomcat\下面,

以管理员身份启动终端,进入到E:\nginx+memcached+tomcat\\memcached-win64

执行安装命令:memcached.exe –d install

启动命令:memcached -m 512 -d start,默认端口号为11211

 

配置:

 

1.添加jar包

将javolution-5.4.3.1;memcached-session-manager-1.6.1;memcached-session-manager-tc6-1.6.1;msm-javolution-serializer-1.6.1;spymemcached-2.7.3 这些jar包分别复制到tomcat0、tomcat1下的$TOMCAT/lib目录下。

 

2.配置文件

然后修改$TOMCAT/conf下的context.xml

在<Context>标签下添加如下配置::如果有多个memcached,memcachedNodes=""的值用空格将多个节点分开。如:memcachedNodes="n1:localhost:11211 n2:localhost:11212 n3 localhost:11213"

<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">
</Manager>

 

四.安装&配置Nginx

 

安装:

将nginx-1.4.0.zip解压到E:\nginx+memcached+tomcat\下,

以管理员身份启动终端,进入到E:\nginx+memcached+tomcat\\nginx-1.4.0执行命令;

常用命令:

启动:start nginx.exe

停止:nginx.exe -s stop

检验错误:nginx.exe -t

重启:nginx.exe -s reload

 

配置:

修改$NGINX_HOME/conf/nginx.conf文件:

以下是本人修改后的文件:

 

#Nginx用户和组,windows下不指定
#user  nobody

#工作的子进程(通常等于CPU数量或者1倍于CPU)  
worker_processes  4;

#错误日志存放路径
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#指定pid存放文件
#pid        logs/nginx.pid;

events {
    #允许最大连接数
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #定义日志格式
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                '$status $body_bytes_sent "$http_referer" '
    #                '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay on;

    #客户端上传文件大小控制  
    client_max_body_size 8m; 

    #开启gzip压缩
    #gzip  on;

    #负载均衡服务器列表
    upstream www.test.com {
	server 127.0.0.1:8080;#可以加权重-->weight=1
	server 127.0.0.1:8088;

	#同一机器在多网情况下,路由切换,ip可能不同
	#ip_hash;
    }

    server {
        listen       80;
	#主机名称
	server_name 127.0.0.1;

	#字符集设置
        #charset koi8-r;

	#访问日志存放路径
        #access_log  logs/host.access.log  main;

        location / {
	    #html文件存放路径
            root   html;
            index  index.jsp index.html index.htm;

	    #此处的 http://www.test.com与upstream www.test.com对应
	    proxy_pass http://www.test.com;

	    proxy_redirect              off;
            proxy_set_header            Host $host;
            proxy_set_header            X-Real-IP $remote_addr;
            proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;
            client_max_body_size        10m;
            client_body_buffer_size     128k;
            proxy_connect_timeout       90;
            proxy_send_timeout          90;
            proxy_read_timeout          90;
            proxy_buffer_size           4k;
            proxy_buffers               4 32k;
            proxy_busy_buffers_size     64k;
            proxy_temp_file_write_size  64k; 

        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
	location ~ .*.jsp$ {
	    index index.jsp
            proxy_pass   http://127.0.0.1:8080;
        }
	location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
            expires      30d; 
        }
        location ~ .*\.(js|css)?$ {
            expires      1h; 
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
 

 

 

 

五.测试
以上配置都完成
然后在E:\nginx+memcached+tomcat\test0目录下添加测试文件:index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/admin_form/";
	out.println("the server address: " + basePath);
	out.println("<br/><br/>");

	String tmp = (String)session.getAttribute("tmp");
	if(tmp == null || tmp == "") {
		session.setAttribute("tmp", "i am created in tomcat 0 . my id is : " + session.getId());
		tmp = (String)session.getAttribute("tmp");
		out.println(" null now to created value !");
		out.println("<br/>");
		out.println(tmp);
		System.out.println(tmp);
	} else {
		out.println(tmp);
		System.out.println(tmp);
	}
%>
 
在E:\nginx+memcached+tomcat\test1目录下也添加测试文件:index.jsp,文件内容和test0一样,只是为了区别,将session.setAttribute("tmp", "i am created in tomcat 0 . my id is : " + session.getId());改成tomcat 1
 
最后:
1.启动memcached
2.分别启动两个tomcat
3.启动nginx
在浏览器输入: http://127.0.0.1:80测试
浏览器显示信息:
the server address: http://127.0.0.1:80/admin_form/ 
i am created in tomcat 0 . my id is : 4AF931E5DDBE816C49B83F5A63E9FEB6-n1.tomcat0
说明达到session共享。
 
如果将memcached关闭:
会发现:浏览器信息中tomcat0和tomcat1交替出现。
  • 大小: 12.7 KB
  • 大小: 15.5 KB
  • 大小: 16.8 KB
  • 大小: 14.1 KB
  • 大小: 24.5 KB
分享到:
评论

相关推荐

    Nginx+Tomcat+Memcached实现tomcat集群和session共享 nginx配置

    Nginx+Tomcat+Memcached实现tomcat集群和session共享 nginx配置

    Nginx+Tomcat+Memcached实现tomcat集群和session共享 tomcat部分

    Nginx+Tomcat+Memcached实现tomcat集群和session共享 tomcat部分

    tomcat8+memcached session共享

    描述中提到的“nginx+tomcat8+memcached session共享所需jar包,直接放到tomcat/lib下即可”,暗示了实现这一功能需要一些特定的Java库(JAR包)。这些JAR包将集成到Tomcat的运行环境中,使Tomcat能够与Memcached...

    nginx+tomcat7+memcached session会话保持

    本文将详细介绍如何在Nginx、Tomcat7和Memcached的环境下实现会话保持,确保用户在多台服务器之间切换时仍能保持其会话状态。 首先,我们要理解会话保持的重要性。在分布式系统中,用户可能与集群中的任何一台...

    nginx+memcached+tomcat 负载均衡+session共享实现所有代码

    nginx+memcached+tomcat 负载均衡+session共享实现所有代码 教程请看:https://blog.csdn.net/dayuang2009/article/details/80312249

    Nginx+Tomcat+Memcached集群Session共享实例

    Nginx+Tomcat+Memcached集群Session共享实例,Nginx 1.81 + tomcat1 + tomcat2 + Memcached 完整可运行 访问根目录下 test.jsp 可看效果

    Nginx+Tomcat7+Memcached集群Session共享

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

    lvs+keepalived+nginx+tomcat+memcached实现服务负载均衡及session共享

    本文将详细探讨如何使用lvs(Linux Virtual Server)、keepalived、nginx、tomcat以及memcached来实现这一目标。 首先,lvs是Linux内核中的一个模块,它提供了四层负载均衡功能,能够将进来的网络请求分发到后端多...

    负载均衡和session 共享(Nginx + Memcached + Tomcat)

    本文将详细介绍如何利用Nginx作为反向代理服务器,结合Memcached内存缓存系统,以及Tomcat应用服务器来实现这一目标。 首先,我们要理解负载均衡的概念。负载均衡是一种分布式系统架构技术,它的目的是将工作负载...

    Nginx+Tomcat+Memcached实现tomcat集群和session共享

    在构建高性能、高可用性的Web应用系统时,通常会采用Nginx作为反向代理和负载均衡器,Tomcat作为应用服务器处理Java Servlet和JSP,而Memcached用于存储和共享Session数据。这个配置可以有效地分发用户请求,提高...

    Windows + Nginx + Memcached + Tomcat 集群 session共享

    本知识点聚焦于如何在Windows环境下,利用Nginx作为反向代理,Memcached进行session共享,以及Tomcat作为Java应用服务器来搭建这样一个集群。这个配置确保了用户在集群中的任何一台服务器上的操作都能被其他服务器...

    Nginx+Tomcat+Memcached共享session集群配置

    通过以上配置,我们可以实现一个基于 Nginx、Tomcat 和 Memcached 的高效且可扩展的集群架构,该架构不仅解决了 Session 共享的问题,还大大提高了系统的并发处理能力和稳定性。在实际应用中,还需要结合具体的业务...

    Nginx+Tomcat+memcached实现集群部署、负载均衡session共享.rar

    在IT行业中,构建高效、可扩展的Web服务是至关重要的,而"Nginx+Tomcat+memcached实现集群部署、负载均衡session共享"是一种常见的解决方案。这个方案结合了Nginx作为反向代理和负载均衡器,Tomcat作为Java应用...

    Windows7 + Nginx + Memcached + Tomcat 集群 session 共享

    本主题将详细探讨如何在Windows7环境下,利用Nginx作为负载均衡器,Memcached作为session存储,以及Tomcat作为应用服务器,实现session共享。 **1. Nginx介绍与配置** Nginx是一款高性能的HTTP和反向代理服务器,它...

    windows下Nginx+memcached+Tomcat集群配置实现session共享与负载均衡

    【Nginx、memcached和Tomcat集群配置实现session共享与负载均衡】 在Windows环境下,构建一个基于Nginx的高可用性Web集群,通常包括Nginx作为反向代理和负载均衡器,memcached用于session共享,以及多个Tomcat实例...

    Nginx + Memcached + Tomcat 集群负载均衡

    标题 "Nginx + Memcached + Tomcat 集群负载均衡" 涉及到的是在高并发环境下,如何通过一系列技术手段实现网站服务的扩展性和稳定性。这个集群架构通常用于大型Web应用,以提高服务性能、容错能力和可伸缩性。以下是...

    tomcat7+nginx+memcached 配置tomcat 集群以及负载均衡

    通过以上步骤,我们可以构建出一个高效、可靠的基于Tomcat 7、Nginx和Memcached的Web应用集群,有效地解决了session共享问题,实现了负载均衡,提高了系统的稳定性和性能。在实际操作中,可能还需要根据具体需求调整...

    nginx+tomcat+memcached服务架构实现session共享所需jar包

    "nginx+tomcat+memcached"服务架构是一种常见的解决方案,它利用Nginx作为反向代理和负载均衡器,Tomcat作为Java应用服务器,而Memcached作为分布式缓存系统来实现session共享。以下是对这个架构和相关知识点的详细...

    Nginx+Memcached+Tomcat所需要JAR包文件

    Nginx-1.6.2+Memcached-1.2.6+Tomcat7负载均衡实现Session共享 搭建所需要的JAR包文件, 请复制到Tomcat Lib目录下, 搭建方法, 请查看博客:http://blog.csdn.net/army16/article/details/40585637

    tomcat中Nginx+memcached实现session共享

    总结来说,通过Nginx和memcached的结合,我们可以在多台Tomcat服务器之间实现Session的无缝共享,提高系统的可扩展性和容错性。这不仅可以优化用户体验,还可以降低运维复杂度,为大型Web应用提供稳定的支持。

Global site tag (gtag.js) - Google Analytics