Tomcat实现session会话保持
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tomcat实现session会话保持相关的知识,希望对你有一定的参考价值。
在后端用session server做session存储服务器使用memcache做后端session server
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。其守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信。这里主要是做为tomcat的session存储设备
实验架构图:
实验设备
操作系统:CentOS7.6
nginx IP:192.168.8.134
tomcat1 IP:192.168.8.160
tomcat2 IP:192.168.8.161
session server IP:192.168.8.162
环境准备
#清空防火墙规则
iptables -F
iptables -X
#临时设置关闭selinux
setenforce 0
#安装jdk,centos7的源默认最高支持jdk1.8
yum -y install java-1.8.0-openjdk-devel
#更改主机名解析
vim /etc/hosts
192.168.8.161 tomcat2
192.168.8.160 tomcat1
实验部署步骤:
配置Nginx
安装并配置Nginx反向代理
[[email protected] ~]#yum -y instll nginx
修改nginx配置文件
[[email protected] ~]#vim /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"‘;
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;
upstream web
server 192.168.8.160:8080;
server 192.168.8.161:8080;
server
listen 80 default_server;
index index.jsp;
location /
proxy_pass http://web/;
检查无误后启动nginx
[[email protected] ~]#nginx -t
[[email protected] ~]#systemctl start nginx
配置tomcat1
安装tomcat以及对应的其他管理工具
[[email protected] ~]#yum -y install tomcat tomcat-lib tomcat-admin-webapps tomcat-webapps tomcat-docs-webapp
创建必要的文件夹
[[email protected] ~]#mkdir -pv /data/app/ROOT/WEB-INF,META-INF,classes,lib
编辑网页文件
[[email protected] ~]#vim /data/app/ROOT/index.jsp
<%@ page language="java" %>
<html>
<head><title>TomcatA</title></head>
<body>
<h1><font color="red">TomcatA</font></h1>
table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("www.zd.com","www.zd.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
编辑tomcat配置文件
[[email protected] ~]#vim /etc/tomcat/server.xml
<Context path="/" docBase="ROOT" reloadable="">
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
#memcache的地址,可以指定多个IP
memcachedNodes="n1:192.168.8.162:11211"
failoverNodes="n1"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"/>
修改tomcat-users.xml配置文件
[[email protected] ~]#vim /etc/tomcat/tomcat-users.xml
<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<user username="tomcat" password="www.test.com" roles="manager-gui,manager-script,admin-gui,admin-script"/>
把需要的软件包拷贝到/usr/share/java/tomcat下,可从GitHub上下载
启动tomcat服务
[[email protected] ~]#systemctl restart tomcat
[[email protected] ~]#systemctl enable tomcat
配置tomcat2
创建必要的目录
[[email protected] ~]#mkdir /data
从tomcat1上拷贝相关文件
[[email protected] ~]#scp -r /data/app/ 192.168.8.161:/data/
[[email protected] ~]#scp /etc/tomcat/server.xml 192.168.8.161:/etc/tomcat/server.xml
[[email protected] ~]#scp /etc/tomcat/tomcat-users.xml 192.168.8.161:/etc/tomcat/tomcat-users.xml
[[email protected] ~]#scp -r /usr/share/java/tomcat 192.168.8.161:/usr/share/java/tomcat
编辑网页文件
[[email protected] ~]#vim /data/app/ROOT/index.jsp
<%@ page language="java" %>
<html>
<head><title>TomcatB</title></head>
<body>
<h1><font color="blue">TomcatB</font></h1>
table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("www.test.com","www.test.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
启动tomcat服务
[[email protected] ~]#systemctl restart tomcat
[[email protected] ~]#systemctl enable tomcat
配置session server
安装memcache
[[email protected] ~]#yum -y install memcached
更改配置文件
[[email protected] ~]#vim /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
#最大使用内存1024M
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
启动服务
[[email protected] ~]#systemctl enable memcached
[[email protected] ~]#systemctl start memcached
测试实验效果,可观察session始终一样
以上是关于Tomcat实现session会话保持的主要内容,如果未能解决你的问题,请参考以下文章