ngnix+ tomcat +memcached 集群设置服务器负载均衡,session共享

1.准备工作:

下载nginx-1.8.0  网址:http://nginx.org/

tomcat7两个,apache-tomcat-7.0.62版本的,我命名为tomcat1,tomcat2

memcache服务: memcached.exe,

1.1 负载均衡配置

1.1.1 配置nginx

#user  nobody;
worker_processes  2;

#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 localhost {
      #根据ip计算将请求分配各那个后端tomcat,许多人误认为可以解决session问题,其实并不能。
      #同一机器在多网情况下,路由切换,ip可能不同
      #ip_hash;
      server localhost:8080 weight=5;
      server localhost:8088 weight=5;
    }
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
			proxy_connect_timeout   3;
			proxy_send_timeout      30;
			proxy_read_timeout      30;
			proxy_pass http://localhost;
        }

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

}

更改地方:

upstream localhost {  
      #根据ip计算将请求分配各那个后端tomcat,许多人误认为可以解决session问题,其实并不能。  
      #同一机器在多网情况下,路由切换,ip可能不同  
      #ip_hash;   
      server localhost:8080 weight=5;  
      server localhost:8088 weight=5;  
}

location / {
    proxy_connect_timeout   3;  
    proxy_send_timeout      30;  
    proxy_read_timeout      30;  
    proxy_pass http://localhost;
}

1.1.2tomcat 配置

要均衡两个tomcat,需要修改一些端口,一个默认,一个tomcat修改成其它端口(改为在默认端口值+1)。

有三处端口修改:

1)<Server port="8006" shutdown="SHUTDOWN">

2) <Connector port="8081" protocol="HTTP/1.1"   connectionTimeout="20000"              redirectPort="8443" />

3)<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />

还有一处是两个tomcat都需要改的地方:在Engine上添加个jvmRoute属性,要求两个tomcat的值都不一样

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">

本人完整的tomcat 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="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!--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" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <!-- 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="8080" 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 BIO implementation that requires the JSSE
         style configuration. When using the APR/native implementation, the
         OpenSSL style configuration is required as described in the APR/native
         documentation -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" 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" defaultHost="localhost" jvmRoute="tomcat">

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

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- 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"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <!-- 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
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

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

测试负载:更改两个tomcat的webapp目录下ROOT的index.jsp文件,只是做个标记,页面分别输出tomcat1,tomcat2,

启动ngnix ,tomcat1,tomcat2服务,在地址栏上输入localhost/index.jsp,刷新几次则会出现

说明通过ngnix负载成功了.

上面的SESSION ID相同,是因为我已做好了session复制功能了,

session复制功能配置能简单:

需要的jar包,

asm-3.2.jar,kryo-1.04.jar,kryo-serializers-0.10.jar,memcached-session-manager-1.6.3.jar,minlog-1.2.jar,msm-javolution-serializer-1.6.3.jar,msm-kryo-serializer-1.6.3.jar,msm-xstream-serializer-1.6.3.jar,reflectasm-0.9.jar,spymemcached-2.8.4.jar,memcached-session-manager-tc7-1.6.5.jar将这个jar包放到tomcat lib里.

tomcat6使用的memcached-session-manager-tc6-1.6.3.jar,我的是tomcat7使用的是memcached-session-manager-tc7-1.6.5.jar 文件下载地址http://pan.baidu.com/s/1hqxYXyG

自行更换对应版本的jar

两个tomcat里面的context.xml加入

<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
      memcachedNodes="m1:127.0.0.1:11211"
      sticky="false"
      lockingMode="auto"
      sessionBackupAsync="false"
      sessionBackupTimeout="1000"
      transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"  />

tomcat/webapp/ROOT/index.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);
   }
%>
<h2>tomcat1</h2>
</body>
</html>

效果图就是上面的负载图,

去掉tomcat下context.xml里面的

<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"  
      memcachedNodes="m1:127.0.0.1:11211"  
      sticky="false"  
      lockingMode="auto"  
      sessionBackupAsync="false"  
      sessionBackupTimeout="1000"  
      transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"  />

效果图:

由此可见session不是共享的.

时间: 2024-09-30 16:23:51

ngnix+ tomcat +memcached 集群设置服务器负载均衡,session共享的相关文章

Nginx+Tomcat+MemCached 集群配置手册

系统实施文档 Nginx+Tomcat+MemCached 集群配置手册 目    录 第1章   概述 1.1   目标 互联网的快速发展带来了互联网系统的高负载和高可用性, 这要求我们在设计系统架构时会应用很多高性能的产品, 本文主要描述互联网架构中门户应用的集群的配置工作,最终用以指导系统实施. 1.2   预期读者 本文档用于指导系统工程师进行系统实施工作,架构师和系统工程师应该通读本文档,选择适当版本用于自己的系统架构. 第2章   产品介绍 2.1   Nginx介绍 Nginx是一

企业网站架构之Nginx+tomcat+memcached集群

nginx+tomcat+memcached应用 系统环境:RHEL6.4  x64   iptables -F   and selinux is disabled 主机角色:node1 :192.168.0.24 :lnmp环境 tomcat memcached node2 :192.168.0.99 : tomcat memcached 软件下载:在lnmp环境上测试tomcat,使用nginx简单发布jsp jdk-6u32-linux-x64.bin apache-tomcat-7.0.

实践出真知——一文教你搭建Nginx+Tomcat集群,实现负载均衡及动静分离

实践出真知--一文教你搭建Nginx+Tomcat集群,实现负载均衡及动静分离 前言 ? 想必大家对于Nginx和Tomcat都非常熟悉了,Nginx的应用非常广泛,不仅是对web静态资源非常友好,而且也是非常实用的反向代理和负载均衡软件.结合后端Tomcat的服务,从而搭建Nginx+Tomcat集群. ? 对于直接想要实践的朋友而言可以获取本文的链接中的软件包后直接看第三备份的内容. 一.集群搭建结构及拓扑 1.1集群架构图示 Nginx+Tomcat集群的结构示意图如下: 1.2系统环境与

Apache反向代理结合Tomcat集群来实现负载均衡(一)

好的博文一般都能做到"望题知文",看下标题就知道下边要讲的内容,写这个标题时犹豫了一下,本来要将标题定位apache+tomcat实现负载均衡,可是又觉得这样显得比较模糊,后来想了一下,觉得利用Apache做负载均衡底层结合的是tomcat的集群功能,所以便用了这个标题. 首先先做下集群与负载均衡的概念区分,很多人一直都不理解这两个概念,不知道该如何区分这两个概念,又觉得这两个概念仿佛是一个技术的两个术语,其实不然,下边介绍下这两个概念: 什么是集群(Cluster) 所谓集群是指一组

Apache反向代理结合Tomcat集群来实现负载均衡(四)、Apache反向代理

上文中主要介绍了tomcat的集群方式,下边再来说下apache的反向代理.反向代理可以这样来理解,我们通常认为的代理(正向代理)是局域网内部机器本身不能上网,网关可以上网,大家在局域网内是通过网关上网的,这个网关就是个代理机器.反向代理意思就反了过来,现在网关并不是给内部局域网做代理的,而是给外部请求内部局域网做代理的,例如,当前一个应用系统在局域网内的A机器上,外部网络不能直接访问A机器,而是通过网关访问到了A,这里网关就是个反向代理了. 总结:正向代理---为在防火墙内的局域网客户端提供I

Apache反向代理结合Tomcat集群来实现负载均衡(一)、概念理解

好的博文一般都能做到"望题知文",看下标题就知道下边要讲的内容,写这个标题时犹豫了一下,本来要将标题定位apache+tomcat实现负载均衡,但是又认为这样显得比較模糊.后来想了一下,认为利用Apache做负载均衡底层结合的是tomcat的集群功能.所以便用了这个标题. 首先先做下集群与负载均衡的概念区分,非常多人一直都不理解这两个概念,不知道该怎样区分这两个概念,又认为这两个概念仿佛是一个技术的两个术语,事实上不然,下边介绍下这两个概念: 什么是集群(Cluster) 所谓集群是指

Dubbo之旅--集群容错和负载均衡

当我们的系统中用到Dubbo的集群环境,因为各种原因在集群调用失败时,Dubbo提供了多种容错方案,缺省为failover重试. Dubbo的集群容错在这里想说说他是因为我们实际的项目中出现了此类的问题,因为依赖的第三方项目出现异常,导致dubbo调用超时,此时使用的是默认的集群容错方式,而配置的reties='3',这样前段系统连续掉用了三次服务,结果可想而知. 先说一下各节点关系: 这里的Invoker是Provider的一个可调用Service的抽象,Invoker封装了Provider地

Openfire 集群部署和负载均衡方案

一.   概述 Openfire是在即时通讯中广泛使用的XMPP协议通讯服务器,本方案采用Openfire的Hazelcast插件进行集群部署,采用Haproxy进行负载均衡,在示例环境中测试通过. 二.   示例环境 服务器配置如下: 服务器名 用途 操作系统 IP PC647 Openfire节点1 Windows7 10.200.10.11 seasky Openfire节点2 Windows xp 10.200.10.18 Lub1 Mysql数据库 LUbuntu 12 10.200.

ASP.NET Core使用Docker-Swarm集群部署实现负载均衡实战演练

一.需求背景 人生苦短,我用.NET Core!阿笨对Docker是这样评价的:Docker在手,环境我有!Docker出手,集群我有!前面的Doc基础课程我们学习了如何使用Docker来部署搭建单机容器应用.当一台服务器的处理能力.存储空间不足时,不要企图去换更强大的服务器,对大型网站而言,不管多么强大的服务器,都满足不了网站持续增长的业务需求.实践中会发现,生产环境中使用单个 Docker 节点是远远不够的,搭建 Docker 集群势在必行.然而,面对 Kubernetes, Mesos等众