Nginx+Tomcat+memcached负载均衡实现session共享
http://blog.csdn.net/love_ubuntu/article/details/8464983
1. 安装各个软件不用说了。
2. 到tomcat的安装目录lib中,加入:
memcached-2.6.jar(http://spymemcached.googlecode.com/files/memcached-2.6.jar)
javolution-5.4.3.1.jar
memcached-session-manager-1.5.1.jar
memcached-session-manager-tc7-1.5.1.jar
msm-javolution-serializer-1.5.1.jar
msm-kryo-serializer-1.5.1.jar
msm-xstream-serializer-1.5.1.jar
3. 配置tomcat. 在%TOMCAT_HOME%\config\context.xml文件中加入】
- <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
- memcachedNodes="n1:localhost:11211,n2:localhost:11212"
- requestUriIgnorePattern=".*\.(png|gif|jpg|css|js){1}quot;
- sessionBackupAsync="false"
- sessionBackupTimeout="1800000"
- copyCollectionsForSerialization="false"
- transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
- />
4. 测试发现,有人说:也可在server.xml配置文件的<Host>...<Host>中添加配置
- <Context docBase="F:/tomcats/tomcat-7.0.2_2/webapps" path= "/msm" reloadable= "true" >
- <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
- memcachedNodes="n1:localhost:11211,n2:localhost:11212"
- requestUriIgnorePattern=".*\.(png|gif|jpg|css|js){1}quot;
- sessionBackupAsync="false"
- sessionBackupTimeout="1800000"
- copyCollectionsForSerialization="false"
- transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
- />
- </Context>
我发现测试没通过。
5. nginx的配置
- #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 10240;
- }
- 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;
- tcp_nopush on;
- keepalive_timeout 60;
- tcp_nodelay on;
- client_body_buffer_size 512k;
- proxy_connect_timeout 5;
- proxy_read_timeout 60;
- proxy_send_timeout 5;
- proxy_buffer_size 16k;
- proxy_buffers 4 64k;
- proxy_busy_buffers_size 128k;
- proxy_temp_file_write_size 128k;
- #gzip on;
- gzip on;
- gzip_min_length 1k;
- gzip_buffers 4 16k;
- gzip_http_version 1.1;
- gzip_comp_level 2;
- gzip_types text/plain application/x-javascript text/css application/xml;
- gzip_vary on;
- upstream localhost{
- #weigth参数表示权值,权值越高被分配到的几率越大
- server 127.0.0.1:9001 weight=1 max_fails=2 fail_timeout=30s;
- server 127.0.0.1:9004 weight=1 max_fails=2 fail_timeout=30s;
- server 127.0.0.1:9007 weight=1 max_fails=2 fail_timeout=30s;
- #ip_hash;
- }
- server {
- listen 80;
- server_name localhost;
- charset utf-8;
- #access_log logs/host.access.log main;
- location / {
- root html;
- index index.html index.htm;
- proxy_pass http://localhost;
- }
- location ~ ^/(WEB-INF)/ {
- deny all;
- }
- #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;
- # server_name localhost;
- # ssl on;
- # ssl_certificate cert.pem;
- # ssl_certificate_key cert.key;
- # ssl_session_timeout 5m;
- # ssl_protocols SSLv2 SSLv3 TLSv1;
- # ssl_ciphers HIGH:!aNULL:!MD5;
- # ssl_prefer_server_ciphers on;
- # location / {
- # root html;
- # index index.html index.htm;
- # }
- #}
- }
6.
1、 session存储到memchached实现方案时。他主要功能是修改tomcat的session存储机制,使之能够把session序列化存放到memcached中。
2、Manager标签属性说明:
className
此属性是必须的。
memcachedNodes
此属性是必须的。这个属性必须包含你所有运行的memcached节点。每个节点的定义格式为<id>:<host>:<port>。
多个之间用空格或半角逗号隔开(如:memcachedNodes="n1:localhost:11211,n2:localhost:11212")。
如果你设置单个memcache节点<id>是可选的,所以它允许设置为<host>:<port>(memcachedNodes="localhost:11211")。
failoverNodes
可选项,属性只能用在非粘连Session机制中。
此属性必须包含memcached节点的Id,此节点是Tomcat作为备份使用。多个之间用空格或逗号隔开
memcachedProtocol
可选项,默认为text。出属性指明memcached使用的存储协议。只支持text或者binary。
sticky 可选项,默认为true。
指定使用粘性的还是非粘性的Session机制。
lockingMode 可选项, 此属性只对非粘性Session有用,默认为none。
指定非粘性Session的锁定策略。他的只有
(1)、none:从来不加锁
(2)、all: 当请求时对Session锁定,直到请求结束
(3)、auto:对只读的request不加锁,对非只读的request加锁
(4)、uriPattern:<regexp>: 使用正则表达式来比较requestRUI + "?" + queryString来决定是否加锁,
requestUriIgnorePattern 可选项
此属性是那些不能改备份Session的请求的正则表达式。如果像css,javascript,图片等静态文件被同一个Tomcat和同一个应用上下文来提供,这些
请求也会通过memcached-session-manager。但是这些请求在一个http会话中几乎没什么改变,所以他们没必要触发Session备份。所以那些静态文件
没必要触发Session备份,你就可以使用此属性定义。此属性必须符合java regex正则规范。
sessionBackupAsync 可选项,默认true
指定Session是否应该被异步保存到Memcached中。 如果被设置为true,backupThreadCount设置起作用,如果设置false,通过sessionBackupTimeout
设置的过期时间起作用。
backupThreadCount 可选项,默认为CPU内核数。
用来异步保存Session的线程数(如果sessionBackupAsync="true")。
sessionBackupTimeout 可选项,默认100,单位毫秒
设置备份一个Session所用的时间,如果操作超过时间那么保存失败。此属性只在sessionBackupAsync="false"是起作用。默认100毫秒
sessionAttributeFilter 可选项 从1.5.0版本有
此属性是用来控制Session中的那个属性值保存到Memcached中的正则表达式。郑则表达式被用来匹配Session中属性名称。如
sessionAttributeFilter="^(userName|sessionHistory)$" 指定了只有"userName"和"sessionHistory"属性保存到Memcached中。
依赖于选择的序列化策略。
transcoderFactoryClass 可选,默认为 de.javakaffee.web.msm.JavaSerializationTranscoderFactory
此属性值是创建序列化和反序列化保存到Memcached中的Session的编码转换器的工厂类名。这个指定的类必须实现了de.javakaffee.web.msm.TranscoderFactory
和提供一个无参的构造方法。例如其他的有效的实现在其他packages/jars中提供如:msm-kryo-serializer,msm-xstrea-serializer和msm-javolution-serializer.
copyCollectionsForSerialization 可选项,默认false。
customConverter 可选项
enableStatistics 可选项,默认true
用来指定是否进行统计。
enabled 可选项,默认true
指定Session保存到Memcached中是否可用和是否可以通过JMX进行改变。只用于粘性Session。
7. 编辑session.jsp 进行测试是否session共享。
- <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'session.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <body>
- <%
- System.out.println(session.getId());
- out.println("<br> This is (TOMCAT1|TOMCAT2), SESSION ID:" + session.getId()+"<br>");
- %>
- </body>
- </html>
要打印出n1,tomcat等字样才表示session共享成功。
8. 注意的问题。memcached最好安装成windows服务。tomcat的端口不要冲突。nginx启动最好用命令。
Nginx+Tomcat+memcached负载均衡实现session共享的更多相关文章
- Nginx+Tomcat+Memcached负载均衡和session共享
1. 演示搭建 说明:本文参考网络日志http://blog.csdn.net/remote_roamer/article/details/51133790,结合实际操作,仅做个演示记录. 1.1. ...
- nginx+tomcat+redis负载均衡及session共享
概述 本文档是用来详细描述 nginx+tomcat+redis负载均衡实现session共享 所需软件及下载地址 软件名称 下载地址 功能说明 Nginx-v1.6.0 http://nginx.o ...
- nginx+tomcat实现负载均衡以及session共享(linux centos7环境)
一.nginx的安装 1.准备三份tomcat tomcat1 设置端口 8080 tomcat2 设置端口 8081 tomcat3 设置端口 8082 2. 下载nginx 3. 解压到/home ...
- haproxy+tomcat实现负载均衡以及session共享(linux centos7环境)
一.安装HAProxy 1.进入home目录,下载最新haproxy安装包. cd /home wget http://haproxy.1wt.eu/download/1.4/src/haproxy- ...
- Nginx+Tomcat+Memcached负载均衡集群服务搭建
操作系统:CentOS6.5 本文档主要讲解,如何在CentOS6.5下搭建Nginx+Tomcat+Memcached负载均衡集群服务器,Nginx负责负载均衡,Tomcat负责实际服务,Memc ...
- nginx之 nginx + tomcat + redis 负载均衡且session一致性
说明: 本文描述的是 nginx + tomcat + redis 实现应用负载均衡且满足session一致性,从安装到配置的全部过程,供大家学习!nginx 代理服务器ip: 10.219.24.2 ...
- Nginx+Tomcat+Memcache实现负载均衡及Session共享
第一部分 环境介绍 部署环境: Host1:Nginx.Memcached.Tomcat1 Host2:Tomcat2 Tomcat_version:8.0.38 第二部分 Nginx+Tomcat实 ...
- Nginx+Tomcat8+Memcached实现负载均衡及session共享
1> 基础环境 简易拓扑图: 2> 部署Tomcat [root@node01 ~]# ll -h ~ |egrep 'jdk|tomcat'-rw-r--r-- 1 root root ...
- keepalived+nginx+tomcat+redis实现负载均衡和session共享(原创)
keepalived+nginx+tomcat+redis实现负载均衡和session共享 直接上链接,码了一天,就不再重写了,希望能帮到大家,有问题欢迎留言交流.
随机推荐
- Javascript 多线程?
最近在遇到一个问题:HTML页面中的页面无法刷新,只能在底层全部处理完成后才能进行页面刷新.在里面已经采用SetTimeout进行了处理,但是明显没有达到预期的效果. 主要的原因是对SetTimeou ...
- C# asp.net 操作Word的前提配置和简单的方法
操作的前提: 1.要保证机器本身要安装OFFICE. 有时安装了Office,但是不能找到Microsoft Word 11.0(或者更高的版本) Object Library.那可能是因为在安装of ...
- 利用OpenXml生成Word2007文档
一.OpenXml简介 利用C#生成Word文档并非一定要利用OpenXml技术,至少可以使用微软提供的Office相关组件来编程,不过对于Office2007(确切的说是Word.Excel和Pow ...
- curl post请求
libcurl发送post请求,包括httpheader参数 static size_t getCharCode(void *ptr, size_t size, size_t nmemb, void ...
- opengl (1) 基本API的熟悉
代码从此处下载 1 运行如下代码,可以看到如下效果,我们利用opengl画出一个三角形. void renderScene(void) { /* glClear清除缓冲区 */ glClear(GL_ ...
- Exception in thread "main" java.lang.IllegalArgumentException:解决方法
使用fileSystem的delete方法无法删除文件或目录 Exception in thread "main" java.lang.IllegalArgumentExcepti ...
- HDOJ-ACM1006(JAVA)
题意:输入一个角度degree,求出一天中时针分针秒针之间的角度大于这个角度degree的时间占一天总时间的比例 因此输入是0-120度, 输出比例,保留三位小数,-1为终止 暂时没想出来如何做这道题 ...
- A Tour of Go Variables
The var statement declares a list of variables; as in function argument lists, the type is last. pac ...
- 操作系统课堂笔记(2)操作系统的硬件环境之I/O技术和时钟
I/O技术 1.程序控制I/O技术 有处理器提供相关的IO指令来实现的.主要缺陷是,处理器必须关注IO处理单元的状态,因而它会耗费大量的时间轮询以获得这个信息,这严重降低了系统性能. 2.中断驱动I/ ...
- 点击modal确定键后删除tr
做第一个笔记,关于 “书单”.2016-09-03关于一个表格调用modal后,在点击表格中的删除按钮弹出modal,点击确定删除后,将一整行tr 删除的功能. 以下内容为table,表示为某班学生. ...