Tomcat7基于Redis的Session共享

Tomcat7/8基于Redis(Sentinel)的Session共享实战

笔者线上环境的多个tomcat需要共享session

基于epel源安装、启动redis

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

安装并配置Tomcat

在服务器上安装并配置两个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,修改端口

  • 编制两个index.jsp页面,分别放入tomcat7-7081\webapps\ROOT、tomcat7-7082\webapps\ROOT目录下,index.jsp页面内容如下:
<%@ 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文件

jar下载: 链接: https://pan.baidu.com/s/1dEDXWf3 密码: gik6

  • 将下载的jar拷贝到${TOMCAT_HOME}/lib下
[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

编辑${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

  • 连接Redis查看已经存在的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做了持久化

参考链接

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值