前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Tomcat7基于Redis的Session共享

Tomcat7基于Redis的Session共享

作者头像
shaonbean
发布2018-01-02 11:47:37
1.3K0
发布2018-01-02 11:47:37
举报
文章被收录于专栏:运维前线运维前线

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

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

基于epel源安装、启动redis

代码语言:javascript
复制
yum install redis -y && /etc/init.d/redis start 

redis具体使用配置参考:http://blog.csdn.net/wh211212/article/details/52817923,贴出笔者的redis.conf

代码语言:javascript
复制
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

代码语言:javascript
复制
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页面内容如下:
代码语言:javascript
复制
<%@ 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下
代码语言:javascript
复制
[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中加入

代码语言:javascript
复制
    <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界面如下:

代码语言:javascript
复制
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
代码语言:javascript
复制
[root@yunwei src]# redis-cli -a Aniuredis123 -h 127.0.0.1 -p 6379
127.0.0.1:6379> keys 71D850EDC7C97D9F8*
1) "71D850EDC7C97D9F87035C6A5777E263"

笔者的redis做了持久化

参考链接

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Tomcat7/8基于Redis(Sentinel)的Session共享实战
    • 基于epel源安装、启动redis
      • 安装并配置Tomcat
        • 下载必须jar文件
          • 配置Tomcat
            • 测试结果
              • 参考链接
              相关产品与服务
              云数据库 Redis
              腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档