tomcat session會話保持實驗-之Nginx+tomcat+memcachedcss
實驗要求html
一、nginx+tomcat 負載均衡java
二、memcached 會話保持node
實驗拓撲linux
ip網絡規劃nginx
Nginxnode2:172.16.76.20web
tomcatAnode3:172.16.76.30 memcachedvim
tomcatBnode4:172.16.76.40 memcached後端
基礎配置tomcat
node2
[root@node2~]# yum install nginx –y
node3
[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
node4
[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 tomcat
Nginx配置
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 nginx
memcached
下載以下jar文件至各tomcat節點的tomcat安裝目錄下的lib目錄中,其中的${version}要換成你所須要的版本號,tc${6,7,8}要換成與tomcat版本相同的版本號。
memcached-session-manager-${version}.jar
memcached-session-manager-tc${6,7,8}-${version}.jar
spymemcached-${version}.jar
msm-javolution-serializer-${version}.jar
javolution-${version}.jar
本實驗所用jar包已上傳(僅供實驗版本),各位能夠去下面連接下載:
http://down.51cto.com/3363989
或者到官方下載(這個應該被。。。)
memcached-session-manager項目地址,https://code.google.com/archive/p/memcached-session-manager/downloads
本實驗環境版本以下:
[root@node3~]# tomcat version
Server version: Apache Tomcat/7.0.69
Server built: Nov 6 201601:55:51 UTC
Server number: 7.0.69.0
OS Name: Linux
OS Version: 3.10.0-327.el7.x86_64
Architecture: amd64
JVM Version: 1.8.0_121-b13
JVM Vendor: OracleCorporation
安裝memcached
[root@node3~]# yum install memcached –y
[root@node4~]# yum install memcached -y
tomcat
ndoe3
一、[root@node3 ~]# vim /etc/tomcat/server.xml
分別在兩個tomcat上的某host上定義一個用於測試的context容器,並在其中建立一個會話管理器,以下所示:
<Context path="/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>
二、 在<Engine name="Catalina"defaultHost="localhost"> 中添加jvmRoute="TomcatA"即
<Enginename="Catalina" defaultHost="localhost"jvmRoute="TomcatA">
#tomcatB主機 則將此修改成tomcatB
node4:
一、 將node3 tomcat配置文件server.xml拷貝一份至本機;
[root@node4~]# cd /etc/tomcat/
[root@node4 tomcat]# scp172.16.76.30:/etc/tomcat/server.xml .
二、注:修改 <Engine> 中jvmRoute=TomcatB;
重啓全部服務
[root@node2~]# systemctl restart nginx
[root@node3~]# systemctl restart tomcat
[root@node3~]# systemctl restart memcached
[root@node4~]# systemctl restart tomcat
[root@node4~]# systemctl restart memcached
測試
至此實驗完成;訪問172.16.76.20/test/實現nginx負載均衡同時利用memcached實現保持會話;