tomcat msm
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了tomcat msm相关的知识,希望对你有一定的参考价值。
准备工作
准备三台虚拟机, 均为CentOS-7-x86_64最小化安装, iptables与SELinux均处于关闭状态, 配置好yum源(base和epel). 做好快照, 以便每次实验后快速恢复.
HostA OS: CentOS-7-x86_64 hostname: 80e54d87.twoyang.com eno16777736: 172.18.71.101/16 gateway: 172.18.0.1 HostB OS: CentOS-7-x86_64 hostname: b9cf468b.twoyang.com eno16777736: 172.18.71.102/16 gateway: 172.18.0.1 HostC OS: CentOS-7-x86_64 hostname: 1f5fafa6.twoyang.com eno16777736: 172.18.71.103/16 gateway: 172.18.0.1
先不管Memcached和MSM, 按照nginx负载均衡tomcat部署好集群.
集群初始化
建立主机信任, 时间同步, 统一增加yum源. 这一步其实在哪个节点上做都可以, 我这里选择HostA.
# 写了个脚本来做这个事情 [[email protected] ~]# wget https://raw.githubusercontent.com/wqiang0917/LearnInMagedu/master/shell/sshtrust.sh # 将集群中所有节点IP地址(包括本机)都加入这个主机列表数组 [[email protected] ~]# vim sshtrust.sh HOSTS=("172.18.71.101" "172.18.71.102" "172.18.71.103") # 分发密钥, 建立主机信任. [[email protected] ~]# bash sshtrust.sh --key # 同步时间 [[email protected] ~]# bash sshtrust.sh "ntpdate 172.18.0.1" [[email protected] ~]# bash sshtrust.sh "hwclock --systohc" [[email protected] ~]# bash sshtrust.sh date Tue Jun 7 21:54:22 CST 2016 Tue Jun 7 21:54:22 CST 2016 Tue Jun 7 21:54:22 CST 2016
HostA
# 安装`OpenJDK`和`Tomcat` [[email protected] ~]# yum install -y java-1.7.0-openjdk java-1.7.0-openjdk-devel tomcat tomcat-lib tomcat-admin-webapps tomcat-webapps [[email protected] ~]# echo "export JAVA_HOME=/usr" > /etc/profile.d/java.sh [[email protected] ~]# exec bash # 创建sessapp应用, 并提供session测试主页. [[email protected] ~]# mkdir -p /var/lib/tomcat/webapps/sessapp/{classes,lib,WEB-INF,META-INF} [[email protected] ~]# cat << EOF > /var/lib/tomcat/webapps/sessapp/index.jsp <%@ page language="java" %> <html> <head><title>TomcatA</title></head> <body> <h1><font color="red">TomcatA.magedu.com</font></h1> <table align="centre" border="1"> <tr> <td>Session ID</td> <% session.setAttribute("magedu.com","magedu.com"); %> <td><%= session.getId() %></td> </tr> <tr> <td>Created on</td> <td><%= session.getCreationTime() %></td> </tr> </table> </body> </html> EOF # 启动服务, 并检查监听端口. [[email protected] ~]# systemctl start tomcat.service [[email protected] ~]# ss -tnl | grep "\(4000\|8009\|8080\)" LISTEN 0 100 :::8009 :::* LISTEN 0 100 :::8080 :::* LISTEN 0 50 ::ffff:172.18.71.101:4000 :::*
HostB
# 安装`OpenJDK`和`Tomcat` [[email protected] ~]# yum install -y java-1.7.0-openjdk java-1.7.0-openjdk-devel tomcat tomcat-lib tomcat-admin-webapps tomcat-webapps [[email protected] ~]# echo "export JAVA_HOME=/usr" > /etc/profile.d/java.sh [[email protected] ~]# exec bash [[email protected] ~]# mkdir -p /var/lib/tomcat/webapps/sessapp/{classes,lib,WEB-INF,META-INF} # 创建sessapp应用, 并提供session测试主页. [[email protected] ~]# cat << EOF > /var/lib/tomcat/webapps/sessapp/index.jsp <%@ page language="java" %> <html> <head><title>TomcatB</title></head> <body> <h1><font color="blue">TomcatB.magedu.com</font></h1> <table align="centre" border="1"> <tr> <td>Session ID</td> <% session.setAttribute("magedu.com","magedu.com"); %> <td><%= session.getId() %></td> </tr> <tr> <td>Created on</td> <td><%= session.getCreationTime() %></td> </tr> </table> </body> </html> EOF # 启动服务, 并检查监听端口. [[email protected] ~]# systemctl start tomcat.service [[email protected] ~]# ss -tnl | grep "\(4000\|8009\|8080\)" LISTEN 0 100 :::8009 :::* LISTEN 0 100 :::8080 :::* LISTEN 0 50 ::ffff:172.18.71.101:4000 :::*
HostC
# 首先测试使用curl直接访问HostA与HostB的sessapp. [[email protected] ~]# curl http://172.18.71.101:8080/sessapp/ <html> <head><title>TomcatA</title></head> <body> <h1><font color="red">TomcatA.magedu.com</font></h1> <table align="centre" border="1"> <tr> <td>Session ID</td> <td>4D5C16AFE6FDA767E506729B0781A0B7</td> </tr> <tr> <td>Created on</td> <td>1465284908776</td> </tr> </table> </body> </html> [[email protected] ~]# curl http://172.18.71.102:8080/sessapp/ <html> <head><title>TomcatB</title></head> <body> <h1><font color="blue">TomcatB.magedu.com</font></h1> <table align="centre" border="1"> <tr> <td>Session ID</td> <td>185E2BDA227CB166CA75B7FDC1A07BE4</td> </tr> <tr> <td>Created on</td> <td>1465284924384</td> </tr> </table> </body> </html> # 安装nginx作为前端调度器反代. [[email protected] ~]# yum install -y nginx [[email protected] ~]# vim /etc/nginx/nginx.conf ... upstream tcsrvs { server 172.18.71.101:8080 weight=1; server 172.18.71.102:8080 weight=2; } server { ... location / { proxy_pass http://tcsrvs; } } ... # 测试语法 [[email protected] ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful # 启动服务 [[email protected] ~]# systemctl start nginx.service [[email protected] ~]# ss -tnl | grep 80 LISTEN 0 128 *:80 *:* LISTEN 0 128 :::80 :::*
使用浏览器访问http://172.18.71.103/sessapp/, 持续刷新页面. 可以看到通过前端调度器nginx在按照权重轮调后端服务器, 并且SessionID一直在变化. 此时使用Memcached和MSM存储会话.
注意: 不要使用curl来测试, curl不是daemon进程, 执行完就退出了, 没有办法保存会话.
Memcached和MSM
在HostA和HostB两个后端tomcat主机上均要执行以下操作, 以HostA为例.
# 安装memcached [[email protected] ~]# yum install -y memcached [[email protected] ~]# systemctl start memcached.service [[email protected] ~]# ss -tnl | grep 11211 LISTEN 0 128 *:11211 *:* LISTEN 0 128 :::11211 :::* # 下载MSM的类库放置在tomcat的公共类库目录中 [[email protected] ~]# ls msm/ javolution-5.4.3.1.jar memcached-session-manager-tc7-1.8.3.jar spymemcached-2.11.1.jar memcached-session-manager-1.8.3.jar msm-javolution-serializer-1.8.3.jar [[email protected] ~]# cp msm/*.jar /usr/share/java/tomcat/ # 增加配置 [[email protected] ~]# vim /etc/tomcat/server.xml ... <Host ...> <Context path="/sessapp" docBase="/var/lib/tomcat/webapps/sessapp" reloadable="true"> <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:172.18.71.101:11211,n2:172.18.71.102:11211" # 使用non-sticky sessions模式, 请参照参考资料中关于非粘性session的解释. sticky="false" # session备份使用同步模式 sessionBackupAsync="false" requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory" /> </Context> </Host> ... # 重启服务 [[email protected] ~]# systemctl restart tomcat.service
测试验证
可以观察到调度器一直在按照权重轮调后端服务器, 但是SessionID一直没有变化. 此时使用的是标识为n2的memcached作为主session server. 说明session可以全局共享.
注意: 不要使用curl来测试, curl不是daemon进程, 执行完就退出了, 没有办法保存会话.
关闭当前作为主session server的HostB上的memcached, 再来刷新页面.
[[email protected] ~]# systemctl stop memcached.service
可以观察到SessionID没有变化, 而session server已经切换到了是标识为n1的memcached. 说明单个session server发生故障, 会话也不会丢失.
本文出自 “王强的博客” 博客,请务必保留此出处http://wqiang.blog.51cto.com/6074114/1795869
以上是关于tomcat msm的主要内容,如果未能解决你的问题,请参考以下文章
Tomcat集群环境下session共享方案梳理-通过memcached(MSM)方法实现