Nginx+Tomcat+Memcached负载均衡和session共享
1. 演示搭建
说明:本文参考网络日志http://blog.csdn.net/remote_roamer/article/details/51133790,结合实际操作,仅做个演示记录。
1.1. 工具
1、Tomcat两个
2、Memcached
3、Nginx
如图:
1.2. 结果演示
1、打开浏览器访问http://localhost:8080/index.jsp
1.3. 配置
1.3.1. 安装Nginx
1、准备nginx,解压即可
2、配置打开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
1、准备Memcached,解压即可
1.3.3. 安装Tomcat
1、准备两个tomcat
2、修改conf/server.xml的端口,可随意设置,只要保证两个tomcat可以正常跑起来。
3、下载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
4、在tomcat的conf/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文件,放到两个tomcat的webapps/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>1、显示信息</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>2、参数设置</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
注意:
1、也可以通过启动安装目录下nginx.exe启动时候,可能出现闪退现象。此时查看nginx安装目录下的error.log看看是否有错误日志,或者检查任务管理器是否有nginx的后台进程存在。如下图:
常用命令说明:
验证配置是否正确: nginx -t
查看Nginx的版本号:nginx -V
启动Nginx:start nginx
快速停止或关闭Nginx:nginx -s stop
正常停止或关闭Nginx:nginx -s quit
配置文件修改重装载命令:nginx -s reload
1.5.3. 启动Tomcat
直接点击startup.bat启动两个tomcat。
1.6. 访问
打开浏览器访问url:http://localhost:8080/index.jsp
需要下载演示包 " tomcatJiQun.rar " 的朋友看这里 链接:https://pan.baidu.com/s/1i5XTaEL 密码:w8el
下载后解压到 D:\java下即可。