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 条评论
登录 后参与评论

相关文章

来自专栏FreeBuf

DNS迭代穷举脚本

在普通的DNS穷举中,如果使用字典进行穷举,会发现没有哪个字典能穷举完所有的域名,国外安全研究者在常年累月的DNS记录收集中发现,很多域名有大量的短主机名,并且...

2089
来自专栏运维一切

marathon错误记录 原

这个问题是这个样子,如果你的zookeeper集群不稳定,而且此前有部署过marathon集群,这下就经常会出现这种问题。marathon如果开启集群模式(--...

783
来自专栏乐沙弥的世界

ORA-01092 ORA-12432: LBAC error: zllegnp:OCIStmtExecute 故障一例

    最近由于数据库hang住,无奈之下直接干掉了pmon进程,再次启动的时候收到了ORA-01092: ORACLE instance terminated...

491
来自专栏北京马哥教育

两大Linux发行版迎来大幅更新 Debian 9及Fedora 26 Beta终于发布

Debian 9终于发布 Debian 发行版宣布正式释出代号为 Stretch 的 Debian 9,该版本将提供五年的支持。Stretch 将专门献给于 2...

2854
来自专栏开发与安全

python network programming tutorial

关于网络编程以及socket 等一些概念和函数介绍就不再重复了,这里示例性用python 编写客户端和服务器端。 一、最简单的客户端流程: 1. Create ...

1880
来自专栏Netkiller

Glusterfs 文件系统

Glusterfs 我的系列文档 Netkiller Architect 手札 Netkiller Developer 手札 Netkiller PHP...

33810
来自专栏虚拟化云计算

时钟也能虚拟化吗

我们知道kvm有一个半虚拟化的时钟kvm-clock,但是现在只对Linux Guest支持,半虚拟化的时钟具有准确高效的有点,而使用TSC和RTC等时...

4439
来自专栏Java编程技术

Dubbo剖析-监控平台的搭建与使用

dubbo-monitor主要用来统计服务的调用次调和调用时间的监控中心,服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中...

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

alert日志中出现ash size的警告

今天查看数据库的alert日志总出现了如下的警告。 Archived Log entry 202 added for thread 1 sequence 2...

34912
来自专栏大魏分享(微信公众号:david-share)

怎样一个金箍圈(Pipeline),让至尊宝(Openshift)完成了到孙悟空(DevOps)的蜕变

但说出这句话,和实现Devops全工具链落地之间的差距,与造出原子弹和E=MC2公式的差距,实不逞多让。

1282

扫码关注云+社区