笔者线上环境的多个tomcat需要共享session
yum install redis -y && /etc/init.d/redis start
redis具体使用配置参考:http://blog.csdn.net/wh211212/article/details/52817923,贴出笔者的redis.conf
bind 0.0.0.0
protected-mode no
port 6379
tcp-backlog 511
timeout 60
tcp-keepalive 300
daemonize yes
supervised no
pidfile /var/run/redis/redis_6379.pid
loglevel notice
logfile /var/log/redis/redis_6379.log
databases 16
save 60 100000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump_6379.rdb
dir /var/lib/redis
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass Aniuredis123
rename-command CONFIG ""
rename-command FLUSHALL ""
maxclients 10000
maxmemory 32gb
maxmemory-policy volatile-lru
maxmemory-samples 5
appendonly yes
appendfilename "appendonly_6379.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
在服务器上安装并配置两个tomcat7,命令为tomcat7-7081、tomcat7-7082
wget http://mirrors.shuosc.org/apache/tomcat/tomcat-7/v7.0.82/bin/apache-tomcat-7.0.82.tar.gz -P /usr/local/src
mkdir /opt/tomcats && cd /usr/local/src && tar zxf apache-tomcat-7.0.82.tar.gz -C /opt/tomcats
cd /opt/tomcats && mv apache-tomcat-7.0.82 tomcat7-7082 && cp -r tomcat7-7082 tomcat7-7081
更改两个tomcat的server.xml,修改端口
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>获取session id</title>
</head>
<body>
Session Id : <%= request.getSession().getId() %>
</body>
</html>
Tomcat部分配置完成继续下面操作
jar下载: 链接: https://pan.baidu.com/s/1dEDXWf3 密码: gik6
[root@yunwei src]# cp commons-pool2-2.2.jar jedis-2.9.0.jar tomcat-redis-session-manage-tomcat7.jar /opt/tomcats/tomcat7-7081/lib/
[root@yunwei src]# cp commons-pool2-2.2.jar jedis-2.9.0.jar tomcat-redis-session-manage-tomcat7.jar /opt/tomcats/tomcat7-7082/lib/
编辑${TOMCAT_HOME}/conf/context.xml,在context中加入
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
host="localhost"
port="6379"
password='Aniuredis123'
database="0"
maxInactiveInterval="60" />
# 贴下使用redis sentinel的配置
<!-- Sentinel 配置 -->
<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
maxInactiveInterval="60"
sentinelMaster="mymaster"
sentinels="127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381,127.0.0.1:26382"
/>
两个tomcat都需要进行配置,配置完成启动tomcat界面如下:
Dec 06, 2017 11:10:25 AM org.apache.catalina.startup.TldConfig execute
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Dec 06, 2017 11:10:25 AM com.orangefunction.tomcat.redissessions.RedisSessionManager startInternal
INFO: Attached to RedisSessionHandlerValve
Dec 06, 2017 11:10:25 AM com.orangefunction.tomcat.redissessions.RedisSessionManager initializeSerializer
INFO: Attempting to use serializer :com.orangefunction.tomcat.redissessions.JavaSerializer
Dec 06, 2017 11:10:25 AM com.orangefunction.tomcat.redissessions.RedisSessionManager startInternal
INFO: Will expire sessions after 1800 seconds
Dec 06, 2017 11:10:25 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /opt/tomcats/tomcat7-7081/webapps/examples has finished in 921 ms
Dec 06, 2017 11:10:25 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /opt/tomcats/tomcat7-7081/webapps/host-manager
Dec 06, 2017 11:10:25 AM org.apache.catalina.session.ManagerBase setMaxInactiveInterval
WARNING: Manager.setMaxInactiveInterval() is deprecated and calls to this method are ignored. Session timeouts should be configured in web.xml or via Context.setSessionTimeout(int timeoutInMinutes)
Dec 06, 2017 11:10:25 AM org.apache.catalina.startup.TldConfig execute
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Dec 06, 2017 11:10:25 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /opt/tomcats/tomcat7-7081/webapps/host-manager has finished in 341 ms
Dec 06, 2017 11:10:25 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /opt/tomcats/tomcat7-7081/webapps/ROOT
Dec 06, 2017 11:10:25 AM org.apache.catalina.session.ManagerBase setMaxInactiveInterval
WARNING: Manager.setMaxInactiveInterval() is deprecated and calls to this method are ignored. Session timeouts should be configured in web.xml or via Context.setSessionTimeout(int timeoutInMinutes)
Dec 06, 2017 11:10:26 AM org.apache.catalina.startup.TldConfig execute
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Dec 06, 2017 11:10:26 AM com.orangefunction.tomcat.redissessions.RedisSessionManager startInternal
INFO: Attached to RedisSessionHandlerValve
Dec 06, 2017 11:10:26 AM com.orangefunction.tomcat.redissessions.RedisSessionManager initializeSerializer
INFO: Attempting to use serializer :com.orangefunction.tomcat.redissessions.JavaSerializer
Dec 06, 2017 11:10:26 AM com.orangefunction.tomcat.redissessions.RedisSessionManager startInternal
INFO: Will expire sessions after 1800 seconds
Dec 06, 2017 11:10:26 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /opt/tomcats/tomcat7-7081/webapps/ROOT has finished in 101 ms
Dec 06, 2017 11:10:26 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /opt/tomcats/tomcat7-7081/webapps/docs
Dec 06, 2017 11:10:26 AM org.apache.catalina.session.ManagerBase setMaxInactiveInterval
WARNING: Manager.setMaxInactiveInterval() is deprecated and calls to this method are ignored. Session timeouts should be configured in web.xml or via Context.setSessionTimeout(int timeoutInMinutes)
Dec 06, 2017 11:10:26 AM org.apache.catalina.startup.TldConfig execute
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Dec 06, 2017 11:10:26 AM com.orangefunction.tomcat.redissessions.RedisSessionManager startInternal
INFO: Attached to RedisSessionHandlerValve
Dec 06, 2017 11:10:26 AM com.orangefunction.tomcat.redissessions.RedisSessionManager initializeSerializer
INFO: Attempting to use serializer :com.orangefunction.tomcat.redissessions.JavaSerializer
Dec 06, 2017 11:10:26 AM com.orangefunction.tomcat.redissessions.RedisSessionManager startInternal
INFO: Will expire sessions after 1800 seconds
Dec 06, 2017 11:10:26 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /opt/tomcats/tomcat7-7081/webapps/docs has finished in 121 ms
Dec 06, 2017 11:10:26 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory /opt/tomcats/tomcat7-7081/webapps/manager
Dec 06, 2017 11:10:26 AM org.apache.catalina.session.ManagerBase setMaxInactiveInterval
WARNING: Manager.setMaxInactiveInterval() is deprecated and calls to this method are ignored. Session timeouts should be configured in web.xml or via Context.setSessionTimeout(int timeoutInMinutes)
Dec 06, 2017 11:10:26 AM org.apache.catalina.startup.TldConfig execute
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Dec 06, 2017 11:10:26 AM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory /opt/tomcats/tomcat7-7081/webapps/manager has finished in 95 ms
Dec 06, 2017 11:10:26 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-7081"]
Dec 06, 2017 11:10:26 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1685 ms
打开浏览器,输入http://192.168.1.137:7082/,http://192.168.1.137:7081/,如下:
获取的SESSIONID是同一个,说明成功了,其中71D850EDC7C97D9F87035C6A5777E263就是SESSIONID
[root@yunwei src]# redis-cli -a Aniuredis123 -h 127.0.0.1 -p 6379
127.0.0.1:6379> keys 71D850EDC7C97D9F8*
1) "71D850EDC7C97D9F87035C6A5777E263"
笔者的redis做了持久化