Tomcat 基于MSM做Session共享


前言

  在Tomcat集群中,当一个节点出现故障,虽然我们有高可用集群来负责故障转移,但用户的session信息如何保持呢?下面介绍一下如何利用MSM+Memcached做Session共享、

相关介绍

MSM

  MSM–Memcached Session Manager是一个高可用的Tomcat Session共享解决方案,除了可以从本机内存快速读取Session信息(仅针对黏性Session)外,同时可使用Memcached存取Session,以实现高可用。

安装配置

一、Nginxi配置

1、安装nginx服务

[root@centos7 ~]# yum install nginx -y

2、编辑配置文件

1
2
3
4
5
6
7
8
9
10
http {
	upstream tomsrv {
        server  172.16.100.5:8080;
        server  172.16.100.6:8080;
    }
    server {
        location / {
            proxy_pass  http://tomsrv;
    }
}

3、启动服务

[root@centos7 ~]# systemctl start nginx.service

二、所有节点统一配置

1、安装jdk

[root@node5 ~]# rpm -ivh jdk-8u73-linux-x64.rpm

2、安装Tomcat

[root@node5 ~]# tar -xf apache-tomcat-8.0.33.tar.gz -C /usr/local/
[root@node5 ~]# cd /usr/local/
[root@node5 ~]# ln -sv apache-tomcat-8.0.33 tomcat

3、设置环境变量

JAVA

1
2
3
4
5
6
7
[root@node5 ~]# echo "export JAVA_HOME=/usr/java/latest" > /etc/profile.d/java.sh 
[root@node5 ~]# echo 'port PATH=$JAVA_HOME/bin:$PATH'> /etc/profile.d/java.sh
[root@node5 ~]# . /etc/profile.d/java.sh 
[root@node5 ~]# java -version
java version "1.8.0_73"
Java(TM) SE Runtime Environment (build 1.8.0_73-b02)
Java HotSpot(TM) 64-Bit Server VM (build 25.73-b02, mixed mode)

Tomcat

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@node5 ~]# echo "CATALINA_HOME=/usr/local/tomcat" > /etc/profile.d/tomcat.sh
[root@node5 ~]# echo "export CATALINA_HOME=/usr/local/tomcat" > /etc/profile.d/tomcat.sh
[root@node5 ~]# echo 'export PATH=$CATALINA_HOME/bin:$PATH' >> /etc/profile.d/tomcat.sh
[root@node5 ~]# . /etc/profile.d/tomcat.sh 
[root@node5 ~]# catalina.sh vsrsion

Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/java/latest
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Usage: catalina.sh ( commands ... )
commands:......

4、复制库文件

[root@node5 ~]# cp memcached-session-manager-1.9.2.jar memcached-session-manager-tc8-1.9.2.jar javolution-5.4.3.1.jar msm-javolution-serializer-1.9.2.jar spymemcached-2.11.1.jar /usr/local/tomcat/lib/

5、编辑配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<Engine name="Catalina" defaultHost="node5.redhat.com" jvmRoute="node5">
		
<Host name="node5.redhat.com" appBase="/data/webapps" unpackWARS="true" autoDeploy="true">
		
	<Context path="" docBase="/data/webapps" reloadable="true" >
		<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
 memcachedNodes="n1:172.16.100.5:11211,n2:172.16.100.6:11211"
 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="/data/logs"
 prefix="web1_access_log." suffix=".txt"
 pattern="%h %l %u %t "%r" %s %b" />
Host>

6、创建项目目录

[root@node5 ~]# mkdir -pv /data/webapps/{WEB-INF,META-INF,classes,lib}

7、编辑主页面文件

[root@node5 ~]# vim /data/webapps/index.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<%@ page language="java" %>
<html>
 <head><title>node5title>head>
 <body>
 <h1><font color="blue">node5.redhat.comfont>h1>
 <table align="centre" border="1">
 <tr>
 <td>Session IDtd>
 <% session.setAttribute("magedu.com","magedu.com"); %>
 <td><%= session.getId() %>td>
 tr>
 <tr>
 <td>Created ontd>
 <td><%= session.getCreationTime() %>td>
 tr>
 table>
 body>
html>

结论

  当配置好上述节点后,直接访问nginx服务器,就能访问tomcat提供的页面了,就算是轮询调度,Session ID也不会改变。


转自:https://magedu.org/2016/05/04/tomcat-session-msm/


你可能感兴趣的:(服务器)