Tomcat+Memcached+Nginx实现session会话共享

环境:

ip hostname software version OS port
192.168.1.120 nginx-proxy-master.gz01 nginx-1.8.1 CentOS Linux release 7.4.1708 (Core)  80
192.168.1.136 tomcat-node01.gz01 apache-tomcat-8.5.53 CentOS Linux release 7.4.1708 (Core)  8080
192.168.1.137 tomcat-node02.gz01 apache-tomcat-8.5.53 CentOS Linux release 7.4.1708 (Core)  8080
192.168.1.124 mem-node01.gz01 memcached-1.4.34    CentOS Linux release 7.4.1708 (Core)  11211
192.168.1.135 mem-node02.gz01 memcached-1.4.34    CentOS Linux release 7.4.1708 (Core)  11211

关闭防火墙和selinux,便于后面测试

1 [[email protected] ~]#  systemctl stop firewalld
2 [[email protected] ~]#  systemctl stop firewalld
3 [[email protected] ~]#  systemctl stop firewalld
4 [[email protected] ~]#  systemctl stop firewalld
5 [[email protected] ~]# systemctl stop firewalld
1 [[email protected] ~]# setenforce 0
2 [[email protected] ~]# setenforce 0
3 [[email protected] ~]# setenforce 0
4 [[email protected] ~]# setenforce 0
5 [[email protected] ~]# setenforce 0

主机192.168.1.136 和192.168.1.137安装jdk和tomcat:

 1 [[email protected]node01 tools]# ll
 2 总用量 195372
 3 -rw-r--r--. 1 root root  10300600 3月  25 21:42 apache-tomcat-8.5.53.tar.gz
 4 -rw-r--r--. 1 root root 189756259 3月  25 22:08 jdk-8u161-linux-x64.tar.gz
 5 [[email protected] tools]# tar -zxvf jdk-8u161-linux-x64.tar.gz
 6 [[email protected] tools]# cp -r jdk1.8.0_161 /usr/local/jdk1.8.0_161
 7 [[email protected] tools]# ln -s /usr/local/jdk1.8.0_161/ /usr/local/jdk
 8 [[email protected] tools]# vim /etc/profile
 9 JAVA_HOME=/usr/local/jdk/
10 CLASSPATH=$CLASSPATH:$JAVA_HOME/lib
11 PATH=$JAVA_HOME/bin:$PATH
12 [[email protected] tools]# source /etc/profile
13 [[email protected] tools]# java -version
14 java version "1.8.0_161"
15 Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
16 Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)
17
18 [[email protected] tools]# tar -zxvf apache-tomcat-8.5.53.tar.gz
19 [[email protected] tools]# mv apache-tomcat-8.5.53 /usr/local/
20 [[email protected] tools]# ln -s /usr/local/apache-tomcat-8.5.53/ /usr/local/tomcat
21 [[email protected] tools]# vim /etc/profile
22 CATALINA_HOME=/usr/local/tomcat/
23 PATH=$JAVA_HOME/bin:/usr/local/tomcat/bin/:$PATH
24 [[email protected] tools]# source /etc/profile
25 [[email protected] tools]# /usr/local/tomcat/bin/startup.sh
26 Using CATALINA_BASE:   /usr/local/tomcat
27 Using CATALINA_HOME:   /usr/local/tomcat
28 Using CATALINA_TMPDIR: /usr/local/tomcat/temp
29 Using JRE_HOME:        /usr/local/jdk
30 Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
31 Tomcat started.
32 [[email protected] tools]# netstat -lnt
33 Active Internet connections (only servers)
34 Proto Recv-Q Send-Q Local Address           Foreign Address         State
35 tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
36 tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN
37 tcp6       0      0 :::8080                 :::*                    LISTEN
38 tcp6       0      0 :::22                   :::*                    LISTEN
39 tcp6       0      0 ::1:25                  :::*                    LISTEN
40 tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN
41 [[email protected]node01 tools]#
42
43 tomcat-node02 也是一样操作

设置一下tomcat测试主页

 1 [[email protected] ROOT]# pwd
 2 /usr/local/tomcat/webapps/ROOT
 3 [[email protected] ROOT]# cat index.jsp
 4 <%@ page language="java" %>
 5   <%@ page import="java.util.*" %>
 6   <html>
 7   <head>
 8   <title>>Tomcat 192.168.1.136:8080 </title>
 9           </head>
10          <body>
11
12  <h1><font color="blue">Tomcat 192.168.1.136:8080 </h1>
13  <table align="centre" border="1">
14  <tr>
15  <td>Session ID</td>
16  <% session.setAttribute("abc","abc"); %>
17  <td><%= session.getId() %></td>
18  </tr>
19  <tr>
20  <td>Created on</td>
21  <td><%= session.getCreationTime() %></td>
22  </tr>
23  </table>
24          </body>
25  </html>
26
27
28 [[email protected] ROOT]# pwd
29 /usr/local/tomcat/webapps/ROOT
30 [[email protected] ROOT]# cat index.jsp
31 <%@ page language="java" %>
32   <%@ page import="java.util.*" %>
33   <html>
34   <head>
35   <title>>Tomcat 192.168.1.137:8080 </title>
36           </head>
37          <body>
38
39  <h1><font color="blue">Tomcat 192.168.1.137:8080 </h1>
40  <table align="centre" border="1">
41  <tr>
42  <td>Session ID</td>
43  <% session.setAttribute("abc","abc"); %>
44  <td><%= session.getId() %></td>
45  </tr>
46  <tr>
47  <td>Created on</td>
48  <td><%= session.getCreationTime() %></td>
49  </tr>
50  </table>
51          </body>
52  </html>

nginx-proxy-master.gz01 192.168.1.120 安装nginx

1 安装依赖
2 [[email protected] ~]# rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
3 Retrieving http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
4 warning: /var/tmp/rpm-tmp.K0JmFW: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
5 Preparing...                          ################################# [100%]
6 Updating / installing...
7    1:epel-release-7-12                ################################# [100%]
8 [[email protected] ~]# yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel
 1 [[email protected] tools]# pwd
 2 /usr/local/tools
 3 [[email protected] tools]# tar -zxvf nginx-1.8.1.tar.gz
 4 [[email protected] tools]# cd nginx-1.8.1
 5 [[email protected] nginx-1.8.1]# ./configure --prefix=/usr/local/nginx-1.8.1  6  --with-http_ssl_module  7  --with-http_stub_status_module --with-file-aio
 8 [[email protected] nginx-1.8.1]# make
 9 [[email protected] nginx-1.8.1]# make install
10 [[email protected] nginx-1.8.1]#   ln -s /usr/local/nginx-1.8.1/ /usr/local/nginx
11 [[email protected] nginx-1.8.1]# /usr/local/nginx/sbin/nginx
12 [[email protected] nginx-1.8.1]# netstat -nltp|grep 80
13 tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      12495/nginx: master 

配置nginx

 1 http {
 2     include       mime.types;
 3     default_type  application/octet-stream;
 4     sendfile        on;
 5     upstream tomcat{
 6         server 192.168.1.137:8080 weight=1;
 7         server 192.168.1.136:8080 weight=2;
 8         }
 9
10     server {
11         listen       80;
12         server_name  localhost;
13
14         location / {
15             proxy_pass http://tomcat;
16         }
17
18         location ~* \.(jsp|do)$ {
19               proxy_pass http://tomcat;
20             proxy_set_header Host $host;
21        }
1 [[email protected] conf]# /usr/local/nginx/sbin/nginx -t
2 nginx: the configuration file /usr/local/nginx-1.8.1/conf/nginx.conf syntax is ok
3 nginx: configuration file /usr/local/nginx-1.8.1/conf/nginx.conf test is successful
4 [[email protected] conf]# /usr/local/nginx/sbin/nginx -s reload

访问192.168.1.120,发现访问请求结果会负载到192.168.1.136和192.168.1.137的tomcat上了。

在配置memcached-session-manager会话共享之前,访问nginx主机的请求会轮询负载到tomcat-node1和tomcat-node2两个节点上,并且session id会随着页面的刷新而改变,即此时还没有实现session会话共享

安装Memcached:(两个节点部署一样)

 1 [[email protected] tools]# yum -y install libevent libevent-devel
 2 [[email protected] tools]# tar -zxvf memcached-1.4.34.tar.gz
 3 [[email protected] tools]#  cd memcached-1.4.34
 4 [[email protected] memcached-1.4.34]#  ./configure --prefix=/usr/local/memcached
 5 [[email protected] memcached-1.4.34]# make
 6 [[email protected] memcached-1.4.34]#  make install
 7 [[email protected] memcached-1.4.34]# /usr/local/memcached/bin/memcached -d -m 512 -u root -p 11211 -c 1024 -P /var/lib/memcached.11211pid
 8 [[email protected] memcached-1.4.34]#
 9 [[email protected] memcached-1.4.34]#  ps -ef|grep memcached
10 root       3105      1  0 17:41 ?        00:00:00 /usr/local/memcached/bin/memcached -d -m 512 -u root -p 11211 -c 1024 -P /var/lib/memcached.11211pid
11 root       3138   1234  0 17:51 pts/0    00:00:00 grep --color=auto memcached
12 [[email protected] memcached-1.4.34]# lsof -i:11211
13 COMMAND    PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
14 memcached 3105 root   26u  IPv4  34290      0t0  TCP *:memcache (LISTEN)
15 memcached 3105 root   27u  IPv6  34291      0t0  TCP *:memcache (LISTEN)
16 memcached 3105 root   28u  IPv4  34294      0t0  UDP *:memcache
17 memcached 3105 root   29u  IPv4  34294      0t0  UDP *:memcache
18 memcached 3105 root   30u  IPv4  34294      0t0  UDP *:memcache
19 memcached 3105 root   31u  IPv4  34294      0t0  UDP *:memcache
20 memcached 3105 root   32u  IPv6  34295      0t0  UDP *:memcache
21 memcached 3105 root   33u  IPv6  34295      0t0  UDP *:memcache
22 memcached 3105 root   34u  IPv6  34295      0t0  UDP *:memcache
23 memcached 3105 root   35u  IPv6  34295      0t0  UDP *:memcache 

配置Tomcat,通过MSM实现共享session(192.168.1.136和192.168.1.137机器上操作)

1、把memcached的jar包上传到/usr/local/tomcat/lib/目录


[[email protected] ROOT]# cd /usr/local/tomcat/lib/
  [[email protected] lib]# ll

 4 -rw-rw-r-- 1 sys-noc sysnoc-work  53259 8月  27 2018 asm-5.2.jar
 5 -rw-rw-r-- 1 sys-noc sysnoc-work 323740 8月  27 2018 kryo-4.0.0.jar
 6 -rw-rw-r-- 1 sys-noc sysnoc-work  85217 8月  27 2018 kryo-serializers-0.38.jar
 7 -rw-rw-r-- 1 sys-noc sysnoc-work 152401 8月  27 2018 memcached-session-manager-1.9.7.jar
 8 -rw-rw-r-- 1 sys-noc sysnoc-work  10788 8月  27 2018 memcached-session-manager-tc8-1.9.7.jar
 9 -rw-rw-r-- 1 sys-noc sysnoc-work   5711 8月  27 2018 minlog-1.3.0.jar
10 -rw-rw-r-- 1 sys-noc sysnoc-work  37160 8月  27 2018 msm-kryo-serializer-1.9.7.jar
11 -rw-rw-r-- 1 sys-noc sysnoc-work  51287 8月  27 2018 objenesis-2.4.jar
12 -rw-rw-r-- 1 sys-noc sysnoc-work  20883 8月  27 2018 reflectasm-1.11.3.jar
13 -rw-rw-r-- 1 sys-noc sysnoc-work 472838 8月  27 2018 spymemcached-2.12.2.jar

2、修改conf/context.xml文件:(No-Stick模式)多个tomcat实例时 需要选择Non-Sticky模式,即sticky="false"

 1 [[email protected] conf]# cp context.xml context.xml.bak
 2 [[email protected]node01 conf]# vim context.xml
 3
 4  <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
 5          memcachedNodes="n1:192.168.1.124:11211,n2:192.168.1.135:11211"
 6          lockingMode="auto"
 7          sticky="false"
 8          sessionBackupAsync="false"
 9          sessionBackupTimeout= "1000"
10          copyCollectionsForSerialization="true"
11          requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
12          transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
13     />
 1 [[email protected] conf]# /usr/local/tomcat/bin/shutdown.sh
 2 Using CATALINA_BASE:   /usr/local/tomcat
 3 Using CATALINA_HOME:   /usr/local/tomcat
 4 Using CATALINA_TMPDIR: /usr/local/tomcat/temp
 5 Using JRE_HOME:        /usr/local/jdk
 6 Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
 7 [[email protected] conf]# /usr/local/tomcat/bin/startup.sh
 8 Using CATALINA_BASE:   /usr/local/tomcat
 9 Using CATALINA_HOME:   /usr/local/tomcat
10 Using CATALINA_TMPDIR: /usr/local/tomcat/temp
11 Using JRE_HOME:        /usr/local/jdk
12 Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
13 Tomcat started.
14 [[email protected] conf]# 

 MSM会话共享测试

1、访问nginx主机,按ctrl+F5强刷页面,发现session信息会变,但是sessionid不会改变!说明session实现了共享! 如下,表示当前sessionid保存到了n2这个memcached节点上了.

2、关闭tomcat-node1和tomcat-node2中的任意一个节点的tomcat服务,继续访问页面,发现前端从nginx负载过来的请求达到未关闭的tomcat节点上,sessionid都不会改变,任然在共享中!即可以实现tomcat故障转移

[[email protected] conf]# /usr/local/tomcat/bin/shutdown.sh

[[email protected] conf]# /usr/local/tomcat/bin/shutdown.sh

3、关闭mem-node2节点的memcached服务,继续访问页面,发现sessionid保存到了n1这个memcached节点上了,但是sessionid任然没有改变,说明session已共享. 也就是说,关闭memcached集群中的任意一个节点.访问页面,sessionid都不会改变.即可以实现memcached故障转移!

原文地址:https://www.cnblogs.com/xiaozhebao/p/12638560.html

时间: 2024-10-06 23:02:13

Tomcat+Memcached+Nginx实现session会话共享的相关文章

redis缓存服务器(Nginx+Tomcat+redis+MySQL实现session会话共享)

一.redis介绍 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set --有序集合)和hash(哈希类型).与memcached一样,为了保证效率,数据都是缓存在内存中.区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现master-slave(主从)同步. Redis是一个高性能的key-valu

图文并茂超详细搭建redis缓存服务器(nginx+tomcat+redis+mysql实现session会话共享)

博主QQ:819594300 博客地址:http://zpf666.blog.51cto.com/ 有什么疑问的朋友可以联系博主,博主会帮你们解答,谢谢支持! 一.redis介绍 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set --有序集合)和hash(哈希类型).与memcached一样,为了保证效率,数据都是缓存在内存中.区别的是redis会

Nginx+Memcached+Tomcat session会话共享

服务器说明: 192.168.1.8 tomcat1 memcached(虚拟机1) 192.168.1.9 tomcat2(虚拟机2) 192.168.1.200 nginx (本机) 对于tomcat+nginx的配置请参考博客:[Nginx-反向代理服务器]实践篇(三)之Tomcat+Nginx搭建集群 接下来我们来安装memcached,注意linux系统的用户权限问题 1.分别把memcached和libevent下载回来,放到/usr/src目录下: # cd /usr/src# w

开机自动启动tomcat+memcached+nginx

编辑 /etc/rc.d/rc.local 如下: export JAVA_HOME=/usr/java/jdk1.7.0_71 export CLASSPATH=.:/usr/java/jdk1.7.0_71/lib/dt.jar:/usr/java/jdk1.7.0_71/lib/tools.jar export PATH=/usr/java/jdk1.7.0_71/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bi

Nginx负载均衡会话共享

在使用负载均衡的时候会遇到会话保持的问题,可通过如下方式进行解决 1.使用nginx的ip_hash,根据客户端的来源IP,将请求分配到相同服务器上 2.基于服务端的Session会话共享(mysql/memcache/redis/file) 在解决负载均衡会话问题我们需要了解session和cookie. 1.用户第一次请求服务端网站时,服务端会生成对应的session_id,然后存储至客户端浏览器的cookie中. 2.客户端尝试登陆服务端网站时,浏览器的请求头自动携带cookie信息,在c

Nginx+Tomcat+Memcached集群Session共享

提供给大家整合实例: http://download.csdn.net/detail/zld1987/9577962 cookie是怎样工作的? 例如,我们创建了一个名字为login的Cookie来包含访问者的信息,创建Cookie时,服务器端的Header如下面所示,这里假设访问者的注册名是"Michael Jordan",同时还对所创建的Cookie的属性如path.domain.expires等进行了指定. Set-Cookie:login=Michael Jordan;path

【转】Nginx+Tomcat+Memcached集群Session共享

cookie是怎样工作的? 例 如,我们创建了一个名字为login的Cookie来包含访问者的信息,创建Cookie时,服务器端的Header如下面所示,这里假设访问者的注册名 是“Michael Jordan”,同时还对所创建的Cookie的属性如path.domain.expires等进行了指定. Set-Cookie:login=Michael Jordan;path=/;domain=msn.com; expires=Monday,01-Mar-99 00:00:01 GMT 上面这个H

session服务器Nginx+Tomcat+Memcached集群Session共享

cookie是怎样工作的? 例如,我们创立了一个名字为login的Cookie来包含访问者的信息,创立Cookie时,服务器端的Header如下面所示,这里假设访问者的注册名是“Michael Jordan”,同时还对所创立的Cookie的属性如path.domain.expires等进行了指定. Set-Cookie:login=Michael Jordan;path=/;domain=msn.com; expires=Monday,01-Mar-99 00:00:01 GMT 下面这个Hea

Terracotta+Tomcat+nginx实现session的共享

环境准备:(Nginx,tomcat的安装部署本例不赘述.) 192.168.1.13     nginx 192.168.1.225    tomcat7,terracotta 192.168.1.226    tomcat7 部署terracotta+tomcat集群: 一:安装java程序至系统,配置java环境. # vi /etc/profile export JAVA_HOME=/usr/local/java export PATH=/usr/local/java/bin:$PATH