Nginx反代至Tomcat基于memcached的session保持

时间:2021-11-14 19:59:08

实现功能:基于前面tomcat基础简介与示例文章

(1) tomcat cluster将会话保存至memcached中;
实现模型:
这里写图片描述

配置B,C主机
安装openjdk与tomcat[本次均使用yum安装]
配置tomcat的server.xml文件在host配置段中加入
<Context path="/test" docBase="test" reloadable="true"> #指定实例的目录
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:172.16.254.4:11211,n2:172.16.254.51:11211" #此处设置后端memcache服务节点
failoverNodes="n1" #故障节点,n2故障后启用n1
requestUriIgnorePattern=".*\.(ico|png|gif|jpg| www.wmsjwz.cn css|js)$" #不对匹配到的资源进行会话缓存
transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
/> # 属性值是创建序列化和反序列[memcached类]
</Context>
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
获取以下文件,将文件复制到/usr/share/tomcat/lib [以下文件是tomcat与memcached用到的jar文件],注意版本问题
javolution-5.4.3.1.jar
memcached-session-manager-1.8.3.jar
memcached-session-manager-tc8-1.8.3.jar
msm-javolution-serializer-1.8.3.jar
spymemcached-2.11.1.jar
1
2
3
4
5
1
2
3
4
5
创建目录,同时添加所需要index.jsp文件方便测试
]# mkdir -pv /usr/share/tomcat/webapps/test/{classes,lib,META-INF,WEB-INF}
]# vim /usr/share/tomcat/webapps/test/index.jsp
<%@ page language="java" %>
<html>
<head><title>TomcatB</title></head>
<body>
<h1><font color="red">TomcatB.msm-test.com</font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("msm-test.com","msm-test.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</ www.jfcyule.com td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
配置D,E主机
安装memcached并启动[无需过多配置]
/etc/sysconfig www.kuuucai.cn /memcaced
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
1
2
3
4
5
1
2
3
4
5
配置A主机代理
upstream tomserver {#负载均衡
server 172.16.252.234:8080;
server 172.16.252.92:8080;
}
server {
listen 80;
server_name www.msm-test. www.mingcaiiii.cn com;
location / {
proxy_pass http://tomserver;
#Proxy Settings
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 www.xinhuanchaoyule.com invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

测试ok!