tomcat8+memcached session共享

一、环境准备

时间同步(同步后确认各服务器时间是否一致,不一致需要修改一下时区)

关闭防火墙

软件包和jar包链接:https://pan.baidu.com/s/1sl9Nob7

 

二、安装配置nginx和memcached

Nginx和memcached使用yum安装即可,下面是nginx配置文件内容

user  nginx;
worker_processes  1;


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"';
    sendfile        on;
    tcp_nopush     on;
    keepalive_timeout  65;
    gzip  on;
    upstream tomcat-web {
      server 192.168.0.14:8080;
      server 192.168.0.15:8080;
       }
    server {
        listen       80;
        server_name  www.tomcat.com;


        location / {
            root   html;
            index  index.html index.htm index.jsp;
        }
        location ~* \.(jsp|do)$ {
                proxy_pass http://tomcat-web;
                proxy_redirect off;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
                   }
       location /nginx_status {
                stub_status on;
                access_log off;
                allow 192.168.0.0/24;
                deny all;
                }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
                                                
nginx.conf

分别启动nginx和memcached

 

三、安装配置tomcat

安装步骤忽略

下载jar包至各tomcat节点的tomcat安装目录下的lib目录中

分别在两个tomcat上的某host上定义一个用于测试的context容器,并在其中创建一个会话管理器,如下所示:

[root@tomcat-server-1 ~]# vim /usr/local/tomcat/conf/server.xml
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <Context path="/test" docBase="/usr/local/tomcat/webapps/test" reloadable="true">
            <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
                     memcachedNodes="n1:192.168.0.12:11211,n2:192.168.0.13:11211" #n1代表memcached的名称,后面为具体的ip地址
                     failoverNodes="n1"
                     requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
                     transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
                        />
             </Context>
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>

提供测试页面

tomcat-server-1

[root@tomcat-server-1 lib]# mkdir -pv /usr/local/tomcat/webapps/test/WEB-INF/{classes,lib}
[root@tomcat-server-1 lib]# vim /usr/local/tomcat/webapps/test/index.jsp
<%@ page language="java" %>
<html>
  <head><title>TomcatA</title></head>
  <body>
    <h1><font color="red">TomcatA</font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("tomcat","tomcat"); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>

 tomcat-server-2

[root@tomcat-server-2 lib]# mkdir -pv /usr/local/tomcat/webapps/test/WEB-INF/{classes,lib}
[root@tomcat-server-2 lib]#  vim /usr/local/tomcat/webapps/test/index.jsp
<%@ page language="java" %>
<html>
  <head><title>TomcatB</title></head>
  <body>
    <h1><font color="blue">TomcatB</font></h1>
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
    <% session.setAttribute("tomcat","tomcat"); %>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>

重启两台tomcat,并用浏览器进行访问

测试结果,在浏览器中访问http://www.tomcat.com/test/index.jsp,结果如上所示,其session ID在负载均衡环境中保持不变。

 

posted @ 2017-12-31 19:30  百衲本  阅读(665)  评论(0编辑  收藏  举报
cnblogs_post_body { color: black; font: 0.875em/1.5em "微软雅黑" , "PTSans" , "Arial" ,sans-serif; font-size: 15px; } cnblogs_post_body h1 { text-align:center; background: #333366; border-radius: 6px 6px 6px 6px; box-shadow: 0 0 0 1px #5F5A4B, 1px 1px 6px 1px rgba(10, 10, 0, 0.5); color: #FFFFFF; font-family: "微软雅黑" , "宋体" , "黑体" ,Arial; font-size: 23px; font-weight: bold; height: 25px; line-height: 25px; margin: 18px 0 !important; padding: 8px 0 5px 5px; text-shadow: 2px 2px 3px #222222; } cnblogs_post_body h2 { text-align:center; background: #006699; border-radius: 6px 6px 6px 6px; box-shadow: 0 0 0 1px #5F5A4B, 1px 1px 6px 1px rgba(10, 10, 0, 0.5); color: #FFFFFF; font-family: "微软雅黑" , "宋体" , "黑体" ,Arial; font-size: 20px; font-weight: bold; height: 25px; line-height: 25px; margin: 18px 0 !important; padding: 8px 0 5px 5px; text-shadow: 2px 2px 3px #222222; } cnblogs_post_body h3 { background: #2B6695; border-radius: 6px 6px 6px 6px; box-shadow: 0 0 0 1px #5F5A4B, 1px 1px 6px 1px rgba(10, 10, 0, 0.5); color: #FFFFFF; font-family: "微软雅黑" , "宋体" , "黑体" ,Arial; font-size: 18px; font-weight: bold; height: 25px; line-height: 25px; margin: 18px 0 !important; padding: 8px 0 5px 5px; text-shadow: 2px 2px 3px #222222; } 回到顶部 博客侧边栏 回到顶部 页首代码 回到顶部 页脚代码