环境准备:
- 时间同步,确保NTP或Chrony服务正常运行。
- 防火墙规则
- 禁用SELinux
- 三台主机
IP | 主机名 | 服务 | 软件 |
10.0.1.8 | proxy | 调度器 | CentOS8、Nginx |
10.0.1.18 | t1 | tocmat1 | CentOS8、JDK8、Tomcat8、memcached |
10.0.1.28 | t1 | tomcat2 | CentOS8、JDK8、Tomcat8、memcached |
1.配置nginx充当proxy
[root@proxy ~]#cat /etc/nginx/nginx.conf
http { ...... upstream tomcat-server { #ip_hash; server t1.magedu.org:8080; server t2.magedu.org:8080; } server { ...... location / { } location ~* .(jsp|do)$ { proxy_pass http://tomcat-server; }
}
}
[root@proxy ~]#cat /etc/hosts
10.0.1.8 proxy.magedu.org proxy 10.0.1.18 t1.magedu.org t1 10.0.1.28 t2.magedu.org t2
2.配置memcached
2.1在tomcat1上配置memcached
[root@t1 ~]#dnf -y install memcached [root@t1 ~]#vim /etc/sysconfig/memcached [root@t1 ~]#cat /etc/sysconfig/memcached PORT="11211" USER="memcached" MAXCONN="1024" CACHESIZE="64" #注释下面行 #OPTIONS="-l 127.0.0.1,::1" [root@t1 ~]#systemctl enable --now memcached.service
2.2 在tomcat2 上配置memcached
配置和t1相同
[root@t2 ~]#dnf -y install memcached [root@t2 ~]#vim /etc/sysconfig/memcached [root@t2 ~]#cat /etc/sysconfig/memcached PORT="11211" USER="memcached" MAXCONN="1024" CACHESIZE="64" #注释下面行 #OPTIONS="-l 127.0.0.1,::1" [root@t2 ~]#systemctl enable --now memcached.service
3.配置 tomcat
配置tomcat1
[root@t1 tomcat]#vim conf/server.xml <Engine name="Catalina" defaultHost="t1.magedu.org" jvmRoute="Tomcat1"> ...... <Host name="t1.magedu.org" appBase="/data/webapps" autoDeploy="true" > </Host> </Engine> </Service> </Server> [root@t1 tomcat]#vim conf/context.xml <Context> ...... <Manager pathname="" /> --> ###倒数第一行前,即</Context>行的前面,加下面内容 <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:10.0.1.18:11211,n2:10.0.1.28:11211" failoverNodes="n1" 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 kryo-3.0.3.jar asm-5.2.jar objenesis-2.6.jar reflectasm-1.11.9.jar minlog-1.3.1.jar kryo-serializers-0.45.jar msm-kryo-serializer-2.3.2.jar memcached-session-manager-tc8-2.3.2.jar spymemcached-2.12.3.jar memcached-session-manager-2.3.2.jar
[root@t1 tomcat]#chown -R tomcat.tomcat /data/webapps/ROOT/
[root@t1 tomcat]#systemctl restart tomcat
[root@t1 tomcat]#mkdir /data/webapps/ROOT -p
[root@t1 tomcat]#vi /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 website1 </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>
同理配置tomcat2
#t2参考上面t1做配置 [root@t2 tomcat]#vim conf/server.xml <Engine name="Catalina" defaultHost="t2.magedu.org" jvmRoute="Tomcat2"> ...... <Host name="t2.magedu.org" appBase="/data/webapps" autoDeploy="true" > </Host> </Engine> </Service> </Server> [root@t2 tomcat]#vim conf/context.xml <Context> ...... <Manager pathname="" /> --> ####################加下面内容################ <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:10.0.1.18:11211,n2:10.0.1.28:11211" failoverNodes="n2" #只修改此行,和t1不同,其它都一样 requestUriIgnorePattern=".*.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFact ory" /> ####################加上面内容################ </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@t2 tomcat]#vi /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 website2 </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 tomcat]#chown -R tomcat.tomcat /data/webapps/ROOT/
[root@t2 tomcat]#systemctl restart tomcat
4.查看日志
[root@t1 tomcat]#tail -n 20 logs/catalina.out 2020-12-11 15:30:49.092 INFO net.spy.memcached.MemcachedConnection: Setting retryQueueSize to -1 2020-12-11 15:30:49.098 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=/10.0.1.18:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue 2020-12-11 15:30:49.110 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=/10.0.1.28:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue 11-Dec-2020 15:30:49.125 INFO [t1.magedu.org-startStop-1] de.javakaffee.web.msm.RequestTrackingHostValve.<init> Setting ignorePattern to .*.(ico|png|gif|jpg|css|js)$ 11-Dec-2020 15:30:49.136 INFO [t1.magedu.org-startStop-1] de.javakaffee.web.msm.MemcachedSessionService.setLockingMode Setting lockingMode to null 11-Dec-2020 15:30:49.138 INFO [t1.magedu.org-startStop-1] de.javakaffee.web.msm.MemcachedSessionService.createTranscoderFactory Creating transcoder factory de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory 11-Dec-2020 15:30:49.156 INFO [t1.magedu.org-startStop-1] de.javakaffee.web.msm.serializer.kryo.KryoTranscoder.<init> Starting with initialBufferSize 102400, maxBufferSize 2048000 and defaultSerializerFactory de.javakaffee.web.msm.serializer.kryo.DefaultFieldSerializerFactory 11-Dec-2020 15:30:49.162 INFO [t1.magedu.org-startStop-1] de.javakaffee.web.msm.MemcachedSessionService.startInternal -------- - finished initialization: - sticky: true - operation timeout: 1000 - node ids: [n2] - failover node ids: [n1] - storage key prefix: null - locking mode: null (expiration: 5s) -------- 11-Dec-2020 15:30:49.189 INFO [t1.magedu.org-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/data/webapps/ROOT] has finished in [990] ms 11-Dec-2020 15:30:49.206 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"] 11-Dec-2020 15:30:49.234 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"] 11-Dec-2020 15:30:49.236 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 1118 ms
5.访问测试 http://proxy.magedu.org/index.jsp 反复刷新页面显示如下:
说明:配置成功!!!
6.通过python脚本测试
6.1在t1 上安装部署python3环境,访问memcached
[root@t1 tomcat]#vi showmemcached.py #!/usr/bin/python3 import memcache # pip install python-memcached mc = memcache.Client(['10.0.1.18:11211','10.0.1.28: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 5 0'):print(x)
[root@t1 tomcat]#chmod +x showmemcached.py
[root@t1 tomcat]#./showmemcached.py
------------------------------
('10.0.1.18:11211 (1)', {'6C53482119B3B15FB21A9CC9FD70CBA7-n1.Tomcat2': '[97 b; 1607675590 s]'})
('10.0.1.28:11211 (1)', {'6C53482119B3B15FB21A9CC9FD70CBA7-n2.Tomcat1': '[97 b; 1607636279 s]'})
之后多次刷新页面,执行脚本后,session信息不在变化,那么恭喜你配置成功!!!
备注相关共享包下载地址:
链接:https://pan.baidu.com/s/15wFtkx_VHLBp49MJJ_Ub4Q
提取码:vbf4
官方文档地址:https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration#example-for-non-sticky-sessions--kryo--redis