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文件中加入】

  1. <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
  2. memcachedNodes="n1:localhost:11211,n2:localhost:11212"
  3. requestUriIgnorePattern=".*\.(png|gif|jpg|css|js){1}quot;
  4. sessionBackupAsync="false"
  5. sessionBackupTimeout="1800000"
  6. copyCollectionsForSerialization="false"
  7. transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
  8. />

4.  测试发现,有人说:也可在server.xml配置文件的<Host>...<Host>中添加配置

  1. <Context docBase="F:/tomcats/tomcat-7.0.2_2/webapps"  path= "/msm"  reloadable= "true"  >
  2. <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
  3. memcachedNodes="n1:localhost:11211,n2:localhost:11212"
  4. requestUriIgnorePattern=".*\.(png|gif|jpg|css|js){1}quot;
  5. sessionBackupAsync="false"
  6. sessionBackupTimeout="1800000"
  7. copyCollectionsForSerialization="false"
  8. transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
  9. />
  10. </Context>

我发现测试没通过。

5. nginx的配置

[java] view
plain
copy

  1. #user  nobody;
  2. worker_processes  1;
  3. #error_log  logs/error.log;
  4. #error_log  logs/error.log  notice;
  5. #error_log  logs/error.log  info;
  6. #pid        logs/nginx.pid;
  7. events {
  8. worker_connections  10240;
  9. }
  10. http {
  11. include       mime.types;
  12. default_type  application/octet-stream;
  13. #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  14. #                  '$status $body_bytes_sent "$http_referer" '
  15. #                  '"$http_user_agent" "$http_x_forwarded_for"';
  16. #access_log  logs/access.log  main;
  17. sendfile        on;
  18. #tcp_nopush     on;
  19. #keepalive_timeout  0;
  20. tcp_nopush     on;
  21. keepalive_timeout 60;
  22. tcp_nodelay on;
  23. client_body_buffer_size  512k;
  24. proxy_connect_timeout    5;
  25. proxy_read_timeout       60;
  26. proxy_send_timeout       5;
  27. proxy_buffer_size        16k;
  28. proxy_buffers            4 64k;
  29. proxy_busy_buffers_size 128k;
  30. proxy_temp_file_write_size 128k;
  31. #gzip  on;
  32. gzip on;
  33. gzip_min_length  1k;
  34. gzip_buffers     4 16k;
  35. gzip_http_version 1.1;
  36. gzip_comp_level 2;
  37. gzip_types       text/plain application/x-javascript text/css application/xml;
  38. gzip_vary on;
  39. upstream   localhost{
  40. #weigth参数表示权值,权值越高被分配到的几率越大
  41. server 127.0.0.1:9001 weight=1  max_fails=2 fail_timeout=30s;
  42. server 127.0.0.1:9004 weight=1  max_fails=2 fail_timeout=30s;
  43. server 127.0.0.1:9007 weight=1  max_fails=2 fail_timeout=30s;
  44. #ip_hash;
  45. }
  46. server {
  47. listen       80;
  48. server_name  localhost;
  49. charset utf-8;
  50. #access_log  logs/host.access.log  main;
  51. location / {
  52. root   html;
  53. index  index.html index.htm;
  54. proxy_pass    http://localhost;
  55. }
  56. location ~ ^/(WEB-INF)/ {
  57. deny all;
  58. }
  59. #error_page  404              /404.html;
  60. # redirect server error pages to the static page /50x.html
  61. #
  62. error_page   500 502 503 504  /50x.html;
  63. location = /50x.html {
  64. root   html;
  65. }
  66. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  67. #
  68. #location ~ \.php$ {
  69. #    proxy_pass   http://127.0.0.1;
  70. #}
  71. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  72. #
  73. #location ~ \.php$ {
  74. #    root           html;
  75. #    fastcgi_pass   127.0.0.1:9000;
  76. #    fastcgi_index  index.php;
  77. #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  78. #    include        fastcgi_params;
  79. #}
  80. # deny access to .htaccess files, if Apache's document root
  81. # concurs with nginx's one
  82. #
  83. #location ~ /\.ht {
  84. #    deny  all;
  85. #}
  86. }
  87. # another virtual host using mix of IP-, name-, and port-based configuration
  88. #
  89. #server {
  90. #    listen       8000;
  91. #    listen       somename:8080;
  92. #    server_name  somename  alias  another.alias;
  93. #    location / {
  94. #        root   html;
  95. #        index  index.html index.htm;
  96. #    }
  97. #}
  98. # HTTPS server
  99. #
  100. #server {
  101. #    listen       443;
  102. #    server_name  localhost;
  103. #    ssl                  on;
  104. #    ssl_certificate      cert.pem;
  105. #    ssl_certificate_key  cert.key;
  106. #    ssl_session_timeout  5m;
  107. #    ssl_protocols  SSLv2 SSLv3 TLSv1;
  108. #    ssl_ciphers  HIGH:!aNULL:!MD5;
  109. #    ssl_prefer_server_ciphers   on;
  110. #    location / {
  111. #        root   html;
  112. #        index  index.html index.htm;
  113. #    }
  114. #}
  115. }

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共享。

[html] view
plain
copy

  1. <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'session.jsp' starting page</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="styles.css">
  18. -->
  19. </head>
  20. <body>
  21. <%
  22. System.out.println(session.getId());
  23. out.println("<br> This is (TOMCAT1|TOMCAT2), SESSION ID:" + session.getId()+"<br>");
  24. %>
  25. </body>
  26. </html>

要打印出n1,tomcat等字样才表示session共享成功。

8. 注意的问题。memcached最好安装成windows服务。tomcat的端口不要冲突。nginx启动最好用命令。

Nginx+Tomcat+memcached负载均衡实现session共享的更多相关文章

  1. Nginx+Tomcat+Memcached负载均衡和session共享

    1. 演示搭建 说明:本文参考网络日志http://blog.csdn.net/remote_roamer/article/details/51133790,结合实际操作,仅做个演示记录. 1.1.  ...

  2. nginx+tomcat+redis负载均衡及session共享

    概述 本文档是用来详细描述 nginx+tomcat+redis负载均衡实现session共享 所需软件及下载地址 软件名称 下载地址 功能说明 Nginx-v1.6.0 http://nginx.o ...

  3. nginx+tomcat实现负载均衡以及session共享(linux centos7环境)

    一.nginx的安装 1.准备三份tomcat tomcat1 设置端口 8080 tomcat2 设置端口 8081 tomcat3 设置端口 8082 2. 下载nginx 3. 解压到/home ...

  4. haproxy+tomcat实现负载均衡以及session共享(linux centos7环境)

    一.安装HAProxy 1.进入home目录,下载最新haproxy安装包. cd /home wget http://haproxy.1wt.eu/download/1.4/src/haproxy- ...

  5. Nginx+Tomcat+Memcached负载均衡集群服务搭建

    操作系统:CentOS6.5  本文档主要讲解,如何在CentOS6.5下搭建Nginx+Tomcat+Memcached负载均衡集群服务器,Nginx负责负载均衡,Tomcat负责实际服务,Memc ...

  6. nginx之 nginx + tomcat + redis 负载均衡且session一致性

    说明: 本文描述的是 nginx + tomcat + redis 实现应用负载均衡且满足session一致性,从安装到配置的全部过程,供大家学习!nginx 代理服务器ip: 10.219.24.2 ...

  7. Nginx+Tomcat+Memcache实现负载均衡及Session共享

    第一部分 环境介绍 部署环境: Host1:Nginx.Memcached.Tomcat1 Host2:Tomcat2 Tomcat_version:8.0.38 第二部分 Nginx+Tomcat实 ...

  8. Nginx+Tomcat8+Memcached实现负载均衡及session共享

    1> 基础环境 简易拓扑图: 2> 部署Tomcat [root@node01 ~]# ll -h ~ |egrep 'jdk|tomcat'-rw-r--r-- 1 root root ...

  9. keepalived+nginx+tomcat+redis实现负载均衡和session共享(原创)

    keepalived+nginx+tomcat+redis实现负载均衡和session共享 直接上链接,码了一天,就不再重写了,希望能帮到大家,有问题欢迎留言交流.

随机推荐

  1. jQuery append xmlNode 修改 xml 内容

    jQuery append xmlNode 修改 xml 内容 http://blog.darkthread.net/blogs/darkthreadtw/archive/2009/04/29/jqu ...

  2. LVS与其他负载均衡软件的区别

    有人在邮件列表问haproxy的作者为何haproxy无论是tcp模式还是http模式,能支撑的并发量都不是太大. Willy回答了这个问题.   Exactly. The difference is ...

  3. PHP 201307 月最新手册chm 免费下载

    本次更新两个版本  php_manual_zh_notreview.chm (15MB 不带评论)     php_manual_zh_review.chm    (32MB 带评论)     制作方 ...

  4. 改进的SMO算法

    S. S. Keerthi等人在Improvements to Platt's SMO Algorithm for SVM Classifier Design一文中提出了对SMO算法的改进,纵观SMO ...

  5. iLearning D3.js 2.0 released

    There are some great changes in 2.0 version. Console in tutorial: In previous version, there will be ...

  6. java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0 *&* 解决方法

    java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0*&*^    at java. ...

  7. HDU4349--Xiao Ming's Hope(数论)

    输入一个n(1<=n<=108),求C(n,0),C(n,1),C(n,2)...C(n,n)有多少个奇数. Lacus定理 http://blog.csdn.net/acm_cxlove ...

  8. IE-首页跳转到 q160的问题解决

    IE首页跳转到 q160的问题解决­ 服了又中找了,IE快捷方式被 www.q160.com劫持­ 该死的这个网站什么也没有做,就是做了一个google搜索的连接.­ ­ 进行了一次搜索­ http: ...

  9. 【三支火把】---C文件学习

    ---恢复内容开始--- 又看了一遍文件的知识点了,断断续续已经看了2-3遍,也就这次花了点时间做了一下总结,以后我想都不会再去翻书了,哈哈. 1. 基于缓冲区的文件操作2. 打开关闭文件3. 单个字 ...

  10. URAL 1992 CVS 链表

    不更改链表结构,只是添加节点,没有删除节点.通过记录和更改标记来模拟题意的插入和删除,复制 指针模拟链表: 预开指针,存在M[]中,可以提高效率 #include<functional> ...