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做了持久化

参考链接

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏三杯水

Ambari安装部署Hadoop

Apache Ambari是一种基于Web的工具,支持Apache Hadoop集群的供应、管理和监控。Ambari已支持大多数Hadoop组件,包括HDFS、...

713
来自专栏Golang语言社区

推荐很好用的Goroutine连接池

ants是一个高性能的协程池,实现了对大规模goroutine的调度管理、goroutine复用,允许使用者在开发并发程序的时候限制协程数量,复用资源,达到更高...

1173
来自专栏程序员与猫

Log system architecture

Keywords: Collector, Processor, Aggregator

871
来自专栏生信技能树

WashU EpiGenome Browser使用教程

WashU EpiGenome Browser 是我用过最赞的浏览器,没有之一。希望大伙跟着教程好好学习下! 还有更多教程见:http://epigenomeg...

38210
来自专栏有困难要上,没有困难创造困难也要上!

Apache-Ignite入门实战之一

35411
来自专栏杨建荣的学习笔记

缓慢的update语句性能分析(r6笔记第61天)

最近处理一个问题的时候,先是收到DB time升高的报警,然后查看DB time的情况发现,已经有近1000%的负载了。 ? 带着好奇心想看看到底是什么样的...

2595
来自专栏项勇

笔记57 | Android保持设备唤醒

1757

beanstalkd:获取队列的状态

在过去的几天中,Jason和我一直在将我们的一些应用程序移植到一个新的puppet(一种集中配置管理系统)中,我们需要做的一件事是检查消息是否正确通过了bean...

2236
来自专栏炉边夜话

很幽默的讲解六种Socket I/O模型

信息来源:幻影论坛     作  者: flyinwuhan (制怒·三思而后行)

801
来自专栏恰同学骚年

设计模式的征途—19.命令(Command)模式

在生活中,我们装修新房的最后几道工序之一是安装插座和开关,通过开关可以控制一些电器的打开和关闭,例如电灯或换气扇。在购买开关时,用户并不知道它将来到底用于控制什...

432

扫码关注云+社区