Nginx+Tomcat+Memcached負載均衡和session共享

1. 演示搭建

說明:本文參考網絡日誌http://blog.csdn.net/remote_roamer/article/details/51133790,結合實際操做,僅作個演示記錄。css

1.1. 工具

一、Tomcat兩個html

二、Memcachedjava

三、Nginxnginx

如圖:web

1.2. 結果演示

1打開瀏覽器訪問http://localhost:8080/index.jsp瀏覽器

1.3. 配置

1.3.1. 安裝Nginx

一、準備nginx,解壓便可tomcat

 

二、配置打開nginx目錄下的conf/nginx.conf文件,替換複製以下內容到文件中。網絡

 1 worker_processes  1;
 2   
 3 events {  
 4 worker_connections  1024;
 5 }  
 6   
 7 http {  
 8     include       mime.types;
 9     default_type  application/octet-stream;
10     sendfile        on;
11     keepalive_timeout  65; 
12     gzip  on; 
13     upstream  clusterApp.com {
14         server    127.0.0.1:18080  weight=1;
15         server    127.0.0.1:28080  weight=2;
16     }     
17   
18     server {  
19         listen       8080;
20         server_name  localhost;
21         location / {  
22             proxy_pass http://clusterApp.com;
23             proxy_redirect default;
24         }  
25         error_page   500 502 503 504  /50x.html;
26         location = /50x.html {  
27             root   html;  
28         }  
29 } 
30  
31 } 

1.3.2. 安裝Memcached

一、準備Memcached,解壓便可session

 

1.3.3. 安裝Tomcat

一、準備兩個tomcatapp

 

二、修改conf/server.xml的端口,可隨意設置,只要保證兩個tomcat能夠正常跑起來。

三、下載memcached須要的jar包放到tomcat目錄下的conf/lib下。所需jar以下:

 

說明:
其餘版本下載連接(來自網絡日誌http://blog.csdn.net/remote_roamer/article/details/51133790)

#下載 memcached-session-manager jars to tomcat
http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager/1.9.5/memcached-session-manager-1.9.5.jar
http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager-tc8/1.9.5/memcached-session-manager-tc8-1.9.5.jar

# 使用memcached 的時候要用到的jar文件
http://repo1.maven.org/maven2/net/spy/spymemcached/2.11.1/spymemcached-2.11.1.jar

# 使用javolution-serializer的時候用到的jar文件下載
http://repo1.maven.org/maven2/de/javakaffee/msm/msm-javolution-serializer/1.9.5/msm-javolution-serializer-1.9.5.jar
http://www.java2s.com/Code/JarDownload/javolution/javolution-5.5.1.jar.zip

# 下面是使用kryo-serializer的時候用到的jar文件下載
http://repo1.maven.org/maven2/de/javakaffee/msm/msm-kryo-serializer/1.9.5/msm-kryo-serializer-1.9.5.jar
http://repo1.maven.org/maven2/de/javakaffee/kryo-serializers/0.9/kryo-serializers-0.9.jar
 
#(相配合的kryo只能用3.+的版本。)
http://repo1.maven.org/maven2/com/esotericsoftware/kryo/3.0.3/kryo-3.0.3.jar
http://repo1.maven.org/maven2/com/esotericsoftware/minlog/1.3.0/minlog-1.3.0.jar
http://repo1.maven.org/maven2/com/esotericsoftware/reflectasm/1.11.3/reflectasm-1.11.3.jar
http://repo1.maven.org/maven2/org/ow2/asm/asm/5.1/asm-5.1.jar

四、在tomcatconf/contex.xml文件中的context節點下追加以下內容。

<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
                sticky="false"
                memcachedNodes="n1:localhost:11211"
                failoverNodes=""
                requestUriIgnorePattern=".*\.(png|gif|jpg|css|js|ico)$"
                sessionBackupAsync="false"
                sessionBackupTimeout="500"
                transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
        />

 如圖:

1.4. 測試jsp文件編寫

新建index.jsp文件,放到兩個tomcatwebapps/ROOT目錄下(或自建項目存放)。內容以下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test</title>
</head>
<body>
        <h3>一、顯示信息</h3>
        <table border="1px">
            <tr>
                <td>Nginx</td>
                <td><% out.println(request.getServerName());%></td>
            </tr>
            <tr>
                <td>Tomcat</td>
                <td><% out.println(request.getLocalAddr()+ " : "+request.getLocalPort()); %></td>
            </tr>
            <tr>
                <td>sesion ID</td>
                <td><% out.println(session.getId());%></td>
            </tr>
            <tr>
                <td>參數列表</td>
                <td>
                    <%
                        String dataName = request.getParameter("dataName");
                        if (dataName != null && dataName.length() > 0) {
                                String dataValue = request.getParameter("dataValue");
                                session.setAttribute(dataName, dataValue);
                        }
                        System.out.println("Session列表");
                        Enumeration e = session.getAttributeNames();
                        while (e.hasMoreElements()) {
                                String name = (String) e.nextElement();
                                String value = session.getAttribute(name).toString();
                                out.println(name + " = " + value + "<br>");
                                System.out.println(name + " = " + value);
                        }
                %>
                </td>
            </tr>
        </table>
        <h3>二、參數設置</h3>
        <form action="index.jsp" method="POST">
                參數名<input type="text"  name="dataName"> <br/>
                參數值<input type="text"  name="dataValue"> <br/>
                <input type="submit" value="提交">
        </form>
</body>
</html>

1.5. 啓動

1.5.1. 啓動Memcached

cmd下,進入安裝目錄。第一次須要先執行memcached.exe -d install安裝,後面直接用memcached.exe -d start啓動。示例:

d:

cd  D:\java\tomcatJiQun\memcached

memcached.exe -d install

memcached.exe -d start

memcached.exe -d stop

1.5.2. 啓動Nginx

cmd下,進入安裝目錄。示例:

d:

cd D:\java\tomcatJiQun\nginx-1.13.9

start nginx

nginx -s stop

注意:

一、也能夠經過啓動安裝目錄下nginx.exe啓動時候,可能出現閃退現象。此時查看nginx安裝目錄下的error.log看看是否有錯誤日誌,或者檢查任務管理器是否有nginx的後臺進程存在。以下圖:

 

經常使用命令說明:

驗證配置是否正確: nginx -t

查看Nginx的版本號:nginx -V

啓動Nginxstart nginx

快速中止或關閉Nginxnginx -s stop

正常中止或關閉Nginxnginx -s quit

配置文件修改重裝載命令:nginx -s reload

1.5.3. 啓動Tomcat

直接點擊startup.bat啓動兩個tomcat

1.6. 訪問

打開瀏覽器訪問urlhttp://localhost:8080/index.jsp

須要下載演示包 " tomcatJiQun.rar " 的朋友看這裏 連接:https://pan.baidu.com/s/1i5XTaEL 密碼:w8el

下載後解壓到 D:\java下便可。

相關文章
相關標籤/搜索