windows下nginx+tomcat+memcache负载均衡tomcat集群session共享搭建

使用nginx 做负载均衡 memcached处理session共享
  环境 windows 7  X64
  java : jdk-7windows-x64.rar
  nginx : http://nginx.org/en/download.html ,这里我们推荐下载稳定版(stable versions),本文采用nginx-1.8.0
  tomcat:apache-tomcat-7.0.63

在同一台电脑上配置多个tomcat(本次采用两个tomcat来示范),修改 conf/server.xml 中的三个配置

安装memcached解压开,打开cmd 进入memcached解压到的路径D:\memcached


memcached.exe –d install 回车安装windows服务
输入:memcached.exe –p 11211 –d start 回车启动memcached服务 -p 表示端口

解压nginx-1.8.0到D盘

修改conf/nginx.conf文件

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#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;

    #gzip  on;
	#设定负载均衡的服务器列表
	upstream 127.0.0.1 {   

         #weigth参数表示权值,权值越高被分配到的几率越大   

         server 127.0.0.1:8080 weight=1;  

         server 127.0.0.1:8081 weight=1;  

     }
    server {
        listen       80;
        server_name  localhost;  

        #charset koi8-r;  

        #access_log  logs/host.access.log  main;  

        location / {
            root   html;
            index  index.html index.htm;
			proxy_pass http://127.0.0.1;
        }  

        #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;
        #}

        # 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 ssl;
    #    server_name  localhost;

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

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

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

}

session的共享

tomcat7下添加的jar包:

安装方法:  将这几个包放到两个tomcat7的lib里面

PS:利用memcache来保存tomcat的session会话

修改两个tomcat下的conf/context.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.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context>

    <!-- Default set of monitored resources -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->
	<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
	memcachedNodes="n1:127.0.1.1:11211"   requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"
	sessionBackupAsync="false"         sessionBackupTimeout="100"
	transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
	copyCollectionsForSerialization="false" />
    <!-- Uncomment this to enable Comet connection tacking (provides events
         on session expiration as well as webapp lifecycle) -->
    <!--
    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
    -->

</Context>

因为支持memcached分布式  如果有多台memcached 在 memcachedNodes="nx:IP:port" 即可
注意:这里的端口号要和启动的设置的端口一致  

打开cmd 进入nginx 的解压目录  输入 nginx start  重新启动nginx依次启动两台tomcat

然后在E:\javanv\apache-tomcat-71\webapps\ROOT,E:\javanv\apache-tomcat-72\webapps\ROOT下各添加一个

session.jsp

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster Test</title></head>
<body>
<%
  //HttpSession session=request.getSession(true);
  System.out.println(session.getId());
  out.println("<br>SESSION ID:" + session.getId()+"<br>");
  // 如果有新的请求,则添加session属性
  String name=request.getParameter("name");
  if (name != null &&name.length() >0) {
     String value=request.getParameter("value");
     session.setAttribute(name, value);
 }
    out.print("<b>Session List:</b>");
    Enumeration<String>names=session.getAttributeNames();
    while (names.hasMoreElements()) {
        String sname=names.nextElement();
        String value=session.getAttribute(sname).toString();
        out.println( sname + "=" + value+"<br>");
        System.out.println( sname + "=" + value);
  }
%>
jvm1
</body>
</html>

JVM 分别是 1 2  分别放进 t1 t2  中 然后打开浏览器

http://127.0.0.1/session.jsp 
多次刷新看到不同的jvm 相同的session 表示成功
nginx + tomcat + memcached 环境就搭建好了

时间: 2024-08-27 02:58:12

windows下nginx+tomcat+memcache负载均衡tomcat集群session共享搭建的相关文章

Nginx+Tomcat7(tomcat6)+Memcached集群Session共享

Nginx+Tomcat7+Memcached集群Session共享 原理: 主要是利用memcached-session-manager(下面简称msm)开源tomcat插件改变tomcat原始的session存储机制,将session的存储放到分布式缓存memcache中,从而实现对session的共享. 下载 1.      下载nginx 2.      下载tomcat7 (本文介绍的是tomcat7的session共享,如果是tomcat6,配置是不成功的,需要更换其中的jar包,m

nginx+keepalived+tomcat+memcache负载均衡搭建小集群

最近一段时间一直在研究高可用高并发负载均衡分布式集群等技术,先前发布了lvs基于网络第四次协议搭建的小集群,现在用空刚好搭建了一个基于nginx搭建的小集群. 我准备了四台机器,情况如下 机器名称 机器IP 安装软件 角色 虚拟ip 描述 vmm01 192.168.252.11 Nginx.keepalived Nginx主机 192.168.252.200 反向代理到tomcat1和tomcat2 vmm04 192.168.252.14 Nginx.keepalived Nginx备机 主

windows使用nginx+memcached实现负载均衡和session或者缓存共享

windows使用nginx+memcached实现负载均衡和session或者缓存共享 两台服务器 服务器1:115.29.186.215 windows2008 64位操作系统 服务器2:114.215.193.64 windows2008 32位操作系统 其中服务器1同时做nginx负载均衡服务器 使用概要:由于两台服务器:所以数据库连接可以使用一台服务器: 由于本人使用的ibatis框架:所以在数据库层使用ibatis Cache 这样就可以使用一台sql服务器:两台服务器访问都是缓存数

Apache + Tomcat + JK 实现负载均衡和集群(状态复制)

原文请见http://www.cnblogs.com/dennisit/p/3370220.html 本文介绍了集群和负载均衡的基本开源实现,实现了用Apache分发请求到多个Tomcat里面对应的应用. 模块介绍 - Apache作为Web服务器,用于处理静态Http请求: - Tomcat作为应用服务器(Servlet容器),处理动态请求: - JK 作为Apache与Tomcat之间的桥梁,实现了Apache与Tomcat一对多的对应,使后端Tomcat负载均衡. 开发环境 - Windo

Apache + Tomcat + JK 实现负载均衡和集群

本文介绍了集群和负载均衡的基本开源实现,实现了用Apache分发请求到多个Tomcat里面对应的应用. 模块介绍 - Apache作为Web服务器,用于处理静态Http请求: - Tomcat作为应用服务器(Servlet容器),处理动态请求: - JK 作为Apache与Tomcat之间的桥梁,实现了Apache与Tomcat一对多的对应,使后端Tomcat负载均衡. 开发环境 - Windows 7操作系统 - Java SE8 - Apache 2.2.14    (本地安装路径:D:\A

Apache、Tomcat负载均衡与集群

一. 环境准备 1.软件下载 a) apache_2.0.55-win32-x86-no_ssl.msi: b) apache-tomcat-5.5.17.rar c) mod_jk-apache-2.0.55.so 下载地址: http://download.csdn.net/detail/crazyiter_com/4178657 2.软件安装 a)  Apache安装路径为:E:\ide\apache\Apache2 (具体可以根据自己的盘符而定) b) Tomcat放置位置和名称注意:

Centos基于Apache的Tomcat负载均衡和集群

Centos基于Apache的Tomcat负载均衡和集群 一.背景原理1.tomcat 做个WEB服务器有它的局限性,处理能力低,效率低.承受并发小(1000左右).但目前有不少网站或者页面是JSP的.并采用了tomcat做为WEB,因此只能在此基础上调优.2.目前采取的办法是Apache + Mod_JK + tomcat 来解决一部分请求,用户访问的是apache,但有jsp页面的时候才会去请求tomcat.如果量一大,那么tomcat无法承受,那么只能做tomat集群,Apache + M

基于mod_proxy+Apache 2.2.16+Tomcat 7的负载均衡与集群配置

第一章. 背景简介 对于大多数企业应用,都希望能做到7*24小时不间断运行.要保持如此高的可用性并非易事,比较常见的做法是将系统部署到多台机器上,每台机器都对外提供同样的功能,这就是集群.系统变为集群时,除了要求系统能够支持水平伸缩外,还要解决两个问题: 1, 如何均衡地访问到提供业务功能的机器. 2, 如何保证当机器出现问题时,用户能自动跳转到另外的机器,不影响使用. 常用的负载均衡技术有硬件和软件两种,本示例常用软件的技术实现.软件也有很多实现技术,如基于apache的mod_jk以及mod

nginx+memcached+tomcat集群 session共享完整版

nginx+memcached+tomcat集群 session共享完整版 集群环境 1.nginx版本 nginx-1.6.2.tar.gz 2.jdk 版本 jdk-7u21-linux-x64.tar.gz 3.tomcat 版本  7.0.29 4.memcached 版本 memcached-1.4.22.tar.gz 5. CentOS 6.5 系统采用一台服务做测试 一.nginx安装 安装依赖包 yum -y install gcc gcc-c++ 1.安装pcre库 tar z