session共享服务器:tomcat和memcached集成在一台主机
Posted y_zilong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了session共享服务器:tomcat和memcached集成在一台主机相关的知识,希望对你有一定的参考价值。
1、配置nginx充当proxy
配置nginx充当proxy
[root@proxy ~]# cat /etc/nginx/nginx.conf
http {
upstream tomcat-server {
server t1.yzil.cn:8080;
server t2.yzil.cn:8080;
}
server {
location / {
}
location ~* \\.(jsp|do)$ {
proxy_pass http://tomcat-server;
}
[root@proxy ~]# cat /etc/hosts
10.0.0.10 proxy.yzil.cn
10.0.0.7 t1.yzil.cn
10.0.0.17 t2.yzil.cn
2、配置tomcat
在tomcat上配置memcached
[root@t1 ~]# yum install -y memcached
[root@t1 ~]# cat /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
[root@t1 ~]# systemctl enable --now memcached.service
配置tomcat
[root@t1 ~]# cd /usr/local/tomcat/
[root@t1 tomcat]# vim conf/server.xml
<Engine name="Catalina" defaultHost="t1.yzil.cn">
<Host name="t1.yzil.cn" appBase="/data/webapps"
unpackWARs="true" autoDeploy="true">
</Host>
[root@t1 tomcat]# vim conf/context.xml
#在倒数第一行前,即<Context> </Context>中间,加下面内容,注意从<Manager ---/>的内容
复制链接:
https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration
<Context>
...
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:t1.yzil.cn:11211,n2:t2.yzil.cn:11211"
failoverNodes="n1" #t2只修改此行,和t1不同,其他都一样
requestUriIgnorePattern=".*\\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
</Context>
t2:
<Context>
...
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:t1.yzil.cn:11211,n2:t2.yzil.cn:11211"
failoverNodes="n2"
requestUriIgnorePattern=".*\\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
</Context>
#将相关包传到lib/目录下
asm-5.2.jar
kryo-3.0.3.jar
kryo-serializers-0.45.jar
memcached-session-manager-2.3.2.jar
memcached-session-manager-tc8-2.3.2.jar
minlog-1.3.1.jar
msm-kryo-serializer-2.3.2.jar
objenesis-2.6.jar
reflectasm-1.11.9.jar
spymemcached-2.12.3.jar
[root@t1 tomcat]# ls lib/ -t |tail
asm-5.2.jar
kryo-serializers-0.45.jar
msm-kryo-serializer-2.3.2.jar
kryo-3.0.3.jar
minlog-1.3.1.jar
objenesis-2.6.jar
reflectasm-1.11.9.jar
memcached-session-manager-2.3.2.jar
memcached-session-manager-tc8-2.3.2.jar
spymemcached-2.12.3.jar
[root@t1 tomcat]# systemctl restart tomcat
[root@t1 ~]# mkdir /data/webapps/ROOT -pv
[root@t1 ~]# vim /data/webapps/ROOT/index.jsp
[root@t1 ~]# cat /data/webapps/ROOT/index.jsp
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>tomcat test</title>
</head>
<body>
<h1> Tomcat Website t1</h1>
<div>On <%=request.getServerName() %></div>
<div><%=request.getLocalAddr() + ":" + request.getLocalPort() %></div>
<div>SessionID = <span style="color:blue"><%=session.getId() %></span></div>
<%=new Date()%>
</body>
</html>
[root@t1 ~]#
访问:http://10.0.0.7:8080/index.jsp
访问:http://10.0.0.10/index.jsp
#查看日志
[root@t1 tomcat]# tail -n 20 logs/catalina.out
3、浏览器访问,python测试脚本查看结果
python测试脚本
在t1上安装部署python3环境,访问memcached
[root@t1 ~]# yum install -y python3 python3-memcached
[root@t1 ~]# pip3 install python-memcached
#准备python测试脚本
[root@t1 ~]# cat showmemcached.py
#!/usr/bin/python3
import memcache # pip install python-memcached
mc = memcache.Client(['10.0.0.101:11211','10.0.0.102:11211'], debug=True)
#print('-' * 30)
# 查看全部key
#for x in mc.get_stats('items'): # stats items 返回 items:5:number 1
# print(x)
print('-' * 30)
for x in mc.get_stats('cachedump 4 0'): # stats cachedump 4 0 # 4和上面的items返回的值有关;0表示全部
print(x)
[root@t1 ~]# chmod +x showmemcached.py
[root@t1 ~]# ./showmemcached.py
------------------------------
('10.0.0.7:11211 (1)', {})
('10.0.0.17:11211 (1)', {})
[root@t1 ~]#
浏览器访问,脚本查看结果:
[root@t1 ~]# ./showmemcached.py
------------------------------
('10.0.0.7:11211 (1)', {'9241BDF2F99412F4594FB07565E6B576-n1': '[89 b; 1624785083 s]', '48C780F07DB26EF9B6EEF8AD03D2A96F-n1': '[89 b; 1624784529 s]'})
('10.0.0.17:11211 (1)', {})
[root@t1 ~]#
memcached实现non-sticky模式
#在前面实验基础上修改,memcached配置不变,只需要修改tomcat配置
[root@t1 tomcat]# vim conf/context.xml
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:10.0.0.7:11211,n2:10.0.0.17:11211"
sticky="false" #这三行和sticky模式不同
sessionBackupAsync="false"
lockingMode="uriPattern:/path1|/path2"
requestUriIgnorePattern=".*\\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
/>
[root@t1 tomcat]# systemctl restart tomcat
查看日志:
[root@t1 ~]# tail /usr/local/tomcat/logs/catalina.out -f
- sticky: false
- operation timeout: 1000
- node ids: [n1, n2]
- failover node ids: []
- storage key prefix: null
- locking mode: uriPattern:/path1|/path2 (expiration: 5s)
--------
运行脚本查看
[root@t1 ~]# ./showmemcached.py
('10.0.0.7:11211 (1)', {'validity:A086628A467245DE87DDFEAACD11DE65-n1.tomcat1': '[20 b; 1624788896 s]'})
('10.0.0.17:11211 (1)', {'bak:validity:A086628A467245DE87DDFEAACD11DE65-n1.tomcat1': '[20 b; 1624788896 s]'})
[root@t1 ~]#
以上是关于session共享服务器:tomcat和memcached集成在一台主机的主要内容,如果未能解决你的问题,请参考以下文章
求助,jetty服务器和tomcat服务器session共享问题
Centos7部署Nginx负载均衡Tomcat服务器及session共享架构
session共享服务器:tomcat和memcached集成在一台主机