常见的session集群方案:session复制和session共享
Session复制:指session信息会在集群节点之间复制,每个节点服务上都会有相同的session信息;主要是实现后端多个节点的冗余功能。但是node之间通信频繁,响应速度有影响,多并发,高频发操作的情况下,性能存在严重问题;
Session共享:通俗来说就是一个浏览器对应多个web服务时,服务端的session数据需要共享
【集群环境】
因为是在个人虚机模拟,资源有限,故tomcat和redis环境复用(Ps:软件环境需要的话,可以找到博客左边的联系方式)
软件环境:jdk1.8.0、redis-5.0.3.tar.gz、apache-tomcat-8.0.50.tar.gz、redisson-tomcat-8-3.12.1.jar、redisson-all-3.12.1.jar
Nginx代理端:192.168.109.100
Tomcat_node1/redis服务: 192.168.109.101
Tomcat_node2 :192.168.109.102
【集群部署】
部署redis环境
#tar xvf redis-5.0.3.tar.gz
#cd redis-5.0.3
#make
#cp redis.conf /etc/
#cd src
#cp redis-cli redis-server redis-sentinel /usr/sbin/
打开redis配置文件/etc/redis.conf,修改两个配置项的值为如下内容:
#vim /etc/redis.conf
bind 0.0.0.0
daemonize yes
最后,启动redis服务即可
#/usr/sbin/redis-server /etc/redis.conf
【部署tomcat环境】
首先部署jdk环境
#mkdir /usr/java
#tar zxvf jdk1.8.0_131.tar.gz -C /usr/java/
#vim /erc/profile
export JAVA_HOME=/usr/java/jdk1.8.0_131 export PATH=$PATH:$JAVA_HOME/bin export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$CLASSPATH
#source /etc/profile
#tar zxvf apache-tomcat-8.0.50.tar.gz -C /usr/local/
#mv /usr/local/apache-tomcat-8.0.50/ /usr/local/tomcat8
目前为止tomcat部署完毕,接下来整合tomcat与redis实现session共享
此时需要部署jar包环境,可以从https://github.com/redisson/redisson/tree/master/redisson-tomcat此链接下载对应的版本。
下载完毕之后将两个jar包解压到/usr/local/tomcat8/lib/目录下面
#mv redis-sson-al-3.12.1.jar redisson-tomcat-8-3.12.1.jar /usr/local/tomcat8/lib
#jar redis-sson-al-3.12.1.jar
#jar redisson-tomcat-8-3.12.1.jar
增加RedissonSessionManager配置
# vim /usr/local/tomcat8/conf/context.xml
<Manager className="org.redisson.tomcat.RedissonSessionManager" configPath="${catalina.base}/conf/redisson.json" readMode="REDIS" updateMode="DEFAULT"/>
#vim /usr/local/tomcat8/conf/redisson.json
![复制代码](http://common.cnblogs.com/images/copycode.gif)
{ "singleServerConfig":{ "idleConnectionTimeout":10000, "connectTimeout":10000, "timeout":3000, "retryAttempts":3, "retryInterval":1500, "password":null, "subscriptionsPerConnection":5, "clientName":null, "address": "redis://127.0.0.1:6379", #这里的IP地址如果是将redis和tomcat放在一起就写127.0.0.1,如果是分离部署的话,就写redis所在的主机IP "subscriptionConnectionMinimumIdleSize":1, "subscriptionConnectionPoolSize":50, "connectionMinimumIdleSize":32, "connectionPoolSize":64, "database":0, "dnsMonitoringInterval":5000 }, "threads":0, "nettyThreads":0, "codec":{ "class":"org.redisson.codec.FstCodec" }, "transportMode":"NIO" }
![复制代码](http://common.cnblogs.com/images/copycode.gif)
最后一步就是写一个用于访问tomcat的测试页面
#vim /usr/local/tomcat8/webapps/ROOT/testsession.jsp
![复制代码](http://common.cnblogs.com/images/copycode.gif)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Welcome to visit tomcat1</title> </head> <body> <center><h1>Welcome to visit tomcat1</h1></center> <center> <h3>sessionId:</h3><%=session.getId()%> <h3>session创建时间:</h3><%=session.getCreationTime()%> <center> </body> </html>
![复制代码](http://common.cnblogs.com/images/copycode.gif)
重启tomcat服务让配置生效即可
# /usr/local/tomcat8/bin/startup.sh
【测试】
在这里我们可以看到tomcat测试页面可以正常访问,登录redis客户端,我们发现,redis已经生成了session信息;
http://192.168.109.101:8080/testsession.jsp
配置第二个tomcat实例的话,按照上述操作步骤即可,需要注意的是tomcat测试页面要保证与109.101不冲突即可
另外这里的redisson.json配置指定109.101上即可,因为之前将redis部署在了109.101上,此时的109.102不需要在安装redis,直接指定redis的IP即可
![复制代码](http://common.cnblogs.com/images/copycode.gif)
[root@hostname-109102 lib]# cat /usr/local/tomcat8/conf/redisson.json { "singleServerConfig":{ "idleConnectionTimeout":10000, "connectTimeout":10000, "timeout":3000, "retryAttempts":3, "retryInterval":1500, "password":null, "subscriptionsPerConnection":5, "clientName":null, "address": "redis://192.168.109.101:6379", "subscriptionConnectionMinimumIdleSize":1, "subscriptionConnectionPoolSize":50, "connectionMinimumIdleSize":32, "connectionPoolSize":64, "database":0, "dnsMonitoringInterval":5000 }, "threads":0, "nettyThreads":0, "codec":{ "class":"org.redisson.codec.FstCodec" }, "transportMode":"NIO" }
![复制代码](http://common.cnblogs.com/images/copycode.gif)
【Nginx】
#tar zxvf nginx-1.14.2.tar.gz -C /usr/src/
# cd /usr/src/nginx-1.14.2/
#./configure \
--user=www \
--group=www \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/usr/local/nginx/logs/error.log \
--http-log-path=/usr/local/nginx/logs/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/lock/subsys/nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-pcre
# make
# make install
# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
# vim /usr/local/nginx/conf/nginx.conf
![复制代码](http://common.cnblogs.com/images/copycode.gif)
user www; worker_processes 8; events { worker_connections 65536; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream tomcat { server 192.168.109.101:8080 weight=1; server 192.168.109.102:8080 weight=1; } server { listen 80; server_name localhost; location / { proxy_pass http://tomcat; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; include fastcgi_params; } } }
![复制代码](http://common.cnblogs.com/images/copycode.gif)
nginx -s reload #重启nginx
Nginx配置完毕之后,通过访问192.168.109.100nginx代理端即可均衡后端两台tomcat,不停的刷新会轮训切换两个tomcat页面,但是session值并没有发生改变,而且都相同。这说明两个tomcat实例都共享了redis里面存储的session信息
也就实现了Nginx+Tomcat+redis的session共享功能;
END!
标签:Tomcat,tomcat,redis,nginx,session,usr,local From: https://www.cnblogs.com/Lqdream/p/16993556.html