tomcat会话持久性实验-Nginx+tomcat+memcached
实验要求
1.nginx+tomcat负载平衡
2.memcached会话保持期
实验拓扑
ip网络规划
Nginxnode2
tomcatanode3:172.16.76.30memcached
tomcatbnode4:172.16.76.40memcached
基本配置
节点2
[root@node2~]#yuminstallnginx–y
节点3
[root@node3~]# yum install tomcat –y [root@node3~]# cd /var/lib/tomcat/webapps/ [root@node3 webapps]# mkdir -pv test/{claess,lib,WEB-INF [root@node3 webapps]#vim test/index.jsp <%@ page language="java" %> <html> <head><title>TomcatA</title></head> <body> <h1><fontcolor="red">TomcatA.linuxinfo.top</font></h1> <tablealign="centre" border="1"> <tr> <td>SessionID</td> <%session.setAttribute("linuxinfo.top","linuxinfo.top");%> <td><%=session.getId() %></td> </tr> <tr> <td>Createdon</td> <td><%=session.getCreationTime() %></td> </tr> </table> </body> </html> [root@node3 ~]# systemctl restart tomcat节点4
[root@node4~]# yum install tomcat –y [root@node4~]# cd /var/lib/tomcat/webapps/ [root@node4 webapps]# mkdir -pv test/{claess,lib,WEB-INF [root@node4 webapps]#vim test/index.jsp <%@ page language="java" %> <html> <head><title>TomcatB</title></head> <body> <h1><font color="blue">TomcatA.linuxinfo.top</font></h1> <tablealign="centre" border="1"> <tr> <td>SessionID</td> <%session.setAttribute("linuxinfo.top","linuxinfo.top");%> <td><%=session.getId() %></td> </tr> <tr> <td>Createdon</td> <td><%=session.getCreationTime() %></td> </tr> </table> </body> </html> [root@node4 ~]# systemctl restart tomcatNginx配置
root@node2~]#cat/etc/nginx/nginx.conf
user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status$body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; upstream tomcatser { #服务器组配置负载均衡 server172.16.76.30:8080; server172.16.76.40:8080; # ip_hash } access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; include/etc/nginx/conf.d/*.conf; server { listen 80default_server; listen [::]:80 default_server; server_name _; root /usr/share/nginx/html; # Load configurationfiles for the default server block. include/etc/nginx/default.d/*.conf; #location 代理至后端服务器组;负载均衡 location / { proxy_pass http://tomcatser; location ~* (\.jsp|do)${ proxy_passhttp://tomcatser; } } error_page 404 /404.html; location =/40x.html { } error_page 500 502 503504 /50x.html; location =/50x.html { } } [root@node2 ~]# systemctlrestart nginxmemcached
将以下jar文件下载到每个tomcat节点的tomcat安装目录下的lib目录下,其中${version}要改成你需要的版本号,tc${6,7,8}要改成和tomcat版本相同的版本号。
memcached-session-manager-${version}。冲突
memcached-session-manager-TC${6,7,8}-${version}。冲突
spymemcached-${version}。冲突
MSM-javolution-序列化程序-${version}。冲突
Java解决方案-${version}。冲突
本实验中使用的jar包已经上传(仅针对实验版)。您可以通过以下链接下载它:
http://down.51cto.com/3363989
或者到官方下载(这个应该是。。。)
Memcached-session-manager项目地址,https://code.Google.com/archive/p/memcached-session-manager/downloads
实验环境版本如下:
[root@node3~]#tomcat版本
服务器版本:ApacheTomcat/7.0.69
服务器构建时间:世界协调时11月6日201601时55分51秒
服务器号:7.0.69.0
*** 作系统名称:Linux
*** 作系统版本:3.10.0-327.el7.x86_64
架构:amd64
JVM版本:1.8.0_121-b13
JVM供应商:OracleCorporation
安装memcached
[root@node3~]#yum安装memcached–y
[root@node4~]#yum安装memcached-y
tomcat
ndoe3
1、[root@node3~]#vim/etc/Tomcat/server.XML
定义一个上下文容器,用于在两台tomcat上的主机上进行测试,并在其中创建一个会话管理器,如下所示:
<contextpath="/test"docBase="/usr/local/Tomcat/webapps/test"reloadable="true">;
<managerclassname="de.Javakaffee.web.MSM.memcachedbackupsessionmanager"
memcachedNodes="n1:172.16.100.9:11211,n2:172.16.100.10:11211"
failoverNodes="n1"
requestUriIgnorePattern="。*\.(ico|png|gif|jpg|CSS|js)$"transcoderFactoryClass="de.javakaffee.web.MSM.serializer.javolution.javolutiontranscoderfactory"/>;
</Context>;
2.at
<enginename="Catalina"defaulthost="localhost"JVMroute="Tomcata">;
#tomcatB主机会将其修改为tomcatB。
节点4:
1.将node3tomcat配置文件server.xml复制到本地计算机;
[root@node4~]#cd/etc/tomcat/
[root@node4Tomcat]#scp172.16.76.30:/etc/Tomcat/server.XML。
2.注意:修改<引擎>JvmRoute=TomcatB在;
重新启动所有服务
[root@node2~]#systemctl重启nginx
[root@node3~]#systemctl重新启动tomcat
[root@node3~]#systemctl重新启动memcached
[root@node4~]#systemctl重新启动tomcat
[root@node4~]#systemctl重新启动memcached
测试
至此,实验完成;访问172.16.76.20/test/通过memcached实现nginx负载均衡和维护会话;
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)