windows下nginx+tomcat+memcache負載平衡tomcat叢集session共用搭建

來源:互聯網
上載者:User

標籤:

使用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 環境就搭建好了 

windows下nginx+tomcat+memcache負載平衡tomcat叢集session共用搭建

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.