标签:主机 三台 虚拟机 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.
# 写了个脚本来做这个事情
[root@80e54d87 ~]# wget https://raw.githubusercontent.com/wqiang0917/LearnInMagedu/master/shell/sshtrust.sh
# 将集群中所有节点IP地址(包括本机)都加入这个主机列表数组
[root@80e54d87 ~]# vim sshtrust.sh
HOSTS=("172.18.71.101" "172.18.71.102" "172.18.71.103")
# 分发密钥, 建立主机信任.
[root@80e54d87 ~]# bash sshtrust.sh --key
# 同步时间
[root@80e54d87 ~]# bash sshtrust.sh "ntpdate 172.18.0.1"
[root@80e54d87 ~]# bash sshtrust.sh "hwclock --systohc"
[root@80e54d87 ~]# 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# 安装`OpenJDK`和`Tomcat`
[root@80e54d87 ~]# yum install -y java-1.7.0-openjdk java-1.7.0-openjdk-devel tomcat tomcat-lib tomcat-admin-webapps tomcat-webapps
[root@80e54d87 ~]# echo "export JAVA_HOME=/usr" > /etc/profile.d/java.sh
[root@80e54d87 ~]# exec bash
# 创建sessapp应用, 并提供session测试主页.
[root@80e54d87 ~]# mkdir -p /var/lib/tomcat/webapps/sessapp/{classes,lib,WEB-INF,META-INF}
[root@80e54d87 ~]# 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
# 启动服务, 并检查监听端口.
[root@80e54d87 ~]# systemctl start tomcat.service
[root@80e54d87 ~]# ss -tnl | grep "\(4000\|8009\|8080\)"
LISTEN 0 100 :::8009 :::*
LISTEN 0 100 :::8080 :::*
LISTEN 0 50 ::ffff:172.18.71.101:4000 :::*# 安装`OpenJDK`和`Tomcat`
[root@b9cf468b ~]# yum install -y java-1.7.0-openjdk java-1.7.0-openjdk-devel tomcat tomcat-lib tomcat-admin-webapps tomcat-webapps
[root@b9cf468b ~]# echo "export JAVA_HOME=/usr" > /etc/profile.d/java.sh
[root@b9cf468b ~]# exec bash
[root@b9cf468b ~]# mkdir -p /var/lib/tomcat/webapps/sessapp/{classes,lib,WEB-INF,META-INF}
# 创建sessapp应用, 并提供session测试主页.
[root@b9cf468b ~]# 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
# 启动服务, 并检查监听端口.
[root@b9cf468b ~]# systemctl start tomcat.service
[root@b9cf468b ~]# ss -tnl | grep "\(4000\|8009\|8080\)"
LISTEN 0 100 :::8009 :::*
LISTEN 0 100 :::8080 :::*
LISTEN 0 50 ::ffff:172.18.71.101:4000 :::*# 首先测试使用curl直接访问HostA与HostB的sessapp.
[root@1f5fafa6 ~]# 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>
[root@1f5fafa6 ~]# 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作为前端调度器反代.
[root@1f5fafa6 ~]# yum install -y nginx
[root@1f5fafa6 ~]# 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;
}
}
...
# 测试语法
[root@1f5fafa6 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# 启动服务
[root@1f5fafa6 ~]# systemctl start nginx.service
[root@1f5fafa6 ~]# 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进程, 执行完就退出了, 没有办法保存会话.
在HostA和HostB两个后端tomcat主机上均要执行以下操作, 以HostA为例.
# 安装memcached [root@80e54d87 ~]# yum install -y memcached [root@80e54d87 ~]# systemctl start memcached.service [root@80e54d87 ~]# ss -tnl | grep 11211 LISTEN 0 128 *:11211 *:* LISTEN 0 128 :::11211 :::* # 下载MSM的类库放置在tomcat的公共类库目录中 [root@80e54d87 ~]# 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 [root@80e54d87 ~]# cp msm/*.jar /usr/share/java/tomcat/ # 增加配置 [root@80e54d87 ~]# 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> ... # 重启服务 [root@80e54d87 ~]# systemctl restart tomcat.service
可以观察到调度器一直在按照权重轮调后端服务器, 但是SessionID一直没有变化. 此时使用的是标识为n2的memcached作为主session server. 说明session可以全局共享.
注意: 不要使用curl来测试, curl不是daemon进程, 执行完就退出了, 没有办法保存会话.
关闭当前作为主session server的HostB上的memcached, 再来刷新页面.
[root@b9cf468b ~]# systemctl stop memcached.service

可以观察到SessionID没有变化, 而session server已经切换到了是标识为n1的memcached. 说明单个session server发生故障, 会话也不会丢失.
本文出自 “王强的博客” 博客,请务必保留此出处http://wqiang.blog.51cto.com/6074114/1795869
标签:主机 三台 虚拟机 tomcat msm
原文地址:http://wqiang.blog.51cto.com/6074114/1795869