一、Tomcat Session Server (memcached)
tomcat会话存储服务使用memcached实现
支持的session server会话服务存储类型:memcached、couchbase、redis
二、配置
前提:
两个tomcat节点:192.168.1.15(tomcatA),192.168.1.16(tomcatB)
两个memcached节点:192.168.1.17, 192.168.1.18
一个负载均衡节点:192.168.1.14
- 所有节点关闭防火墙和selinux并同步时间
访问流程:Clients-->反向代理-->(tomcatA, tomcatB)-->会话保持器mecached
1、安装JDK和tomcat
- 在192.168.1.15(tomcatA)和192.168.1.16(tomcatB)上分别安装jdk和tomcat
[root@tomcat-15 ~]# yum install ntpdate -y
[root@tomcat-15 ~]# ntpdate time1.aliyun.com #同步时间
[root@tomcat-15 ~]# yum -y install java-1.8.0-openjdk
[root@tomcat-15 ~]# java -version #版本查询
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode) #混合模式
注意:安装完成后,要配置JAVA_HOME环境变量,指向java的安装路径;
设定java环境变量
[root@tomcat-15 ~]# echo $JAVA_HOME #查询是否有路径
[root@tomcat-15 ~]# vim /etc/profile.d/java.sh #编写脚本
export JAVA_HOME=/usr
[root@tomcat-15 ~]# . /etc/profile.d/java.sh
[root@tomcat-15 ~]# echo $JAVA_HOME
/usr
#安装tomcat
[root@tomcat-15 ~]# yum install -y tomcat tomcat-lib tomcat-admin-webapps tomcat-webapps tomcat-docs-webapp
2、下载相关jar
memcached-session-manager项目地址,
http://code.google.com/p/memcached-session-manager
jar下载地址
https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration
下载如下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
- 分别在192.168.1.15(tomcatA)和192.168.1.16(tomcatB)上的某host上定义一个用于测试的context容器,并在其中创建一个会话管理器,如下所示:
#下载到指定目录
[root@tomcat-15 ~]# cd /usr/share/java/tomcat/
[root@tomcat-15 tomcat]# wget http://central.maven.org/maven2/javolution/javolution/5.4.3/javolution-5.4.3.jar
[root@tomcat-15 tomcat]# wget http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager/2.3.0/memcached-session-manager-2.3.0.jar
[root@tomcat-15 tomcat]# wget http://repo1.maven.org/maven2/de/javakaffee/msm/memcached-session-manager-tc7/2.3.0/memcached-session-manager-tc7-2.3.0.jar
[root@tomcat-15 tomcat]# wget http://repo1.maven.org/maven2/de/javakaffee/msm/msm-javolution-serializer/2.1.1/msm-javolution-serializer-2.1.1.jar #下载序列化工具
[root@tomcat-15 tomcat]# wget http://repo1.maven.org/maven2/net/spy/spymemcached/2.12.3/spymemcached-2.12.3.jar
[root@tomcat-15 ~]# vim /etc/tomcat/server.xml
......
<Host name= localhost" appBase="webapps"
unpackWARS="true" autoDeploy="true"
#添加以下语句
<Context path="/test" docBase="/usr/local/tomcat/webapps/test" reloadable="true">
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:192.168.1.17:11211,n2:192.168.1.18:11211" #缓存服务器地址端口
failoverNodes="n1" #定义那个是主节点,这里是n1做主节点
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
/>
</Context>
- 分别在192.168.1.15(tomcatA)和192.168.1.16(tomcatB)上context提供测试页面:
tomcatA服务器:
[root@tomcat-15 ~]# mkdir -pv /usr/local/tomcat/webapps/test/WEB-INF/{classes,lib}
[root@tomcat-15 ~]# vim /usr/local/tomcat/webapps/test/index.jsp
添加如下内容:
<%@ page language="java" %>
<html>
<head><title>TomcatA</title></head>
<body>
<h1><font color="green">TomcatA.ilinux.io</font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("ilinux.io","ilinux.io"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
[root@tomcat-15 ~]#systemctl start tomcat.servicen #启动tomcat
tomcatB服务器:
[root@tomcat-16 ~]# mkdir -pv /usr/local/tomcat/webapps/test/WEB-INF/{classes,lib}
[root@tomcat-16 ~]# vim /usr/local/tomcat/webapps/test/index.jsp
添加如下内容:
<%@ page language="java" %>
<html>
<head><title>TomcatB</title></head>
<body>
<h1><font color="red">TomcatB.ilinux.io</font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("ilinux.io","ilinux.io"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
[root@tomcat-16 ~]#systemctl start tomcat.servicen #启动tomcat
3、memcached会话存储服务器设置
- 分别在192.168.1.17(memcached-17)和192.168.1.18(memcached-18)安装memcached。
- 实验环境使用默认配置即可,直接启动。
[root@memcached-17 ~]# yum install ntpdate -y
[root@memcached-17 ~]# ntpdate time1.aliyun.com #同步时间
[root@memcached-17 ~]# yum install -y memcached
[root@memcached-17 ~]# systemctl start memcached.service
[root@memcached-17 ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:11211 *:*
LISTEN 0 128 :::11211 :::*
.....
4、httpd反代服务器设置
- 在192.168.1.14上配置反向代理的负载均衡内容,类似如下所示:
[root@httpd-14 ~]# yum install -y httpd
[root@httpd-14 ~]# vim /etc/httpd/conf.d/ilinux.conf
#proxy_http_module反向代理配置:
<VirtualHost *:80>
ServerName tc1.ilinux.io
ProxyRequests Off
ProxyVia On
ProxyPreserveHost On
<Proxy balancer://tomcat>
BalancerMember http://192.168.1.15:8080 loadfactor=1
BalancerMember http://192.168.1.16:8080 loadfactor=1
ProxySet lbmethod=byrequests
</Proxy>
ProxyVia Off
ProxyRequests Off
ProxyPass / balancer://tomcat/
ProxyPassReverse / balancer://tomcat/
<Proxy *>
Order Allow,Deny
Allow From all
</Proxy>
<Location />
Order Allow,Deny
Allow From all
</Location>
</VirtualHost>
>
[root@httpd-14 ~]# httpd -t
[root@httpd-14 ~]# systemctl start httpd.service
三、测试
- 测试结果,在浏览器中访问http://192.168.1.14/test,结果如下所示,其session ID在负载均衡环境中保持不变。
- 关闭默认n1节点memcached服务测试
[root@memcached-17 ~]# systemctl stop memcached.service