Tomcat7基于Redis的Session共享

项目需求:

单点登录,接口调用,分布式系统,退出时需要调用接口,通过token找到sessionId,但是没有session,需要寻找session,使session失效。

解决办法:

登录时,将token 和 sessionId放入redis中,key=token  value=sessionId

退出时,通过token 获取sessionId,通过sessionId删除redis里的session,就可以使session失效

一:安装Redis,哨兵模式

192.168.1.105 6379 主 master
192.168.1.108 6379 备 slave
192.168.1.108 6380 备 slave
192.168.1.105 26379 哨兵
192.168.1.108 26379 哨兵
192.168.1.108 26380 哨兵

1. 主redis.conf
daemonize yes
bind 192.168.1.105
requirepass "123456"
logfile "6379.log"
dbfilename "dump-6379.rdb"
dir "/var/redis/data/"

2. 备redis.conf
daemonize yes
bind 192.168.1.108
requirepass "123456"
slaveof 192.168.1.105 6379
masterauth 123456
logfile "6379.log"
dbfilename "dump-6379.rdb"
dir "/var/redis/data/"
slave-read-only yes

3.sentinel.conf

daemonize yes
port 26379

protected-mode no 禁止保护模式

# 配置监听的主服务器,这里sentinel monitor代表监控,mymaster代表服务器的名称,可以自定义,192.168.11.128代表监控的主服务器,6379代表端口,2代表只有两个或两个以上的哨兵认为主服务器不可用的时候,才会进行failover操作。
sentinel monitor mymaster 192.168.1.105 6379 2

# sentinel author-pass定义服务的密码,mymaster是服务名称,123456是Redis服务器密码
# sentinel auth-pass <master-name> <password>
sentinel auth-pass mymaster 123456

//每个Sentinel节点都要定期PING命令来判断Redis数据节点和其余Sentinel节点是否可达,
//如果超过30000毫秒且没有回复,则判定不可达

sentinel down-after-milliseconds mymaster 30000

//当Sentinel节点集合对主节点故障判定达成一致时,Sentinel领导者节点会做故障转移操作,选出新的主节点,原来的从节点会向新的主节点发起复制操作,限制每次向新的主节点发起复制操作的从节点个数为1

sentinel parallel-syncs mymaster 1

//故障转移超时时间为180000毫秒

sentinel failover-timeout mymaster 180000


4. 启动
# 启动Redis服务器进程
./redis-server ../redis.conf
# 启动哨兵进程 两种方法
redis-sentinel ../sentinel-26379.conf
redis-server ../sentinel-26379.conf --sentinel

 

二:需要的jar包

tomcat-redis-session-manager-VERSION.jar 
jedis-2.5.2.jar 
commons-pool2-2.2.jar

下载地址 :
https://github.com/jcoleman/tomcat-redis-session-manager
https://github.com/izerui/tomcat-redis-session-manager

网上查到的其他下载地址,
https://github.com/jcoleman/tomcat-redis-session-manager/downloads
这个jar包与上面的不一样,所以修改context.xml时
com.orangefunction.tomcat.redissessions.RedisSessionManager需要修改
修改成为:com.radiadesign.catalina.session.RedisSessionHandlerValve

三:配置Tomcat以及需要的环境

jdk7 + tomcat7

1.修改apache-tomcat-7.0.55\conf\context.xml

<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
sentinelMaster="mymaster"
sentinels="192.168.1.105:26379,192.168.1.108:26379,192.168.1.108:26380"
database="0"
password="123456"
maxInactiveInterval="60" />

四:遇到的问题:

tomcat启动报错too low setting for -Xss

具体报错如下

Caused by: java.lang.IllegalStateException: Unable to complete the scan for annotations for web application [] due to a StackOverflowError. Possible root causes include a too low setting for -Xss and illegal cyclic inheritance dependencies. The class hierarchy being processed was [org.bouncycastle.asn1.ASN1EncodableVector->org.bouncycastle.asn1.DEREncodableVector->org.bouncycastle.asn1.ASN1EncodableVe
ctor]


解决方案:

因为tomcat启动会去扫描jar包,看错误信息org.bouncycastle.asn1.ASN1EncodableVector

这个类似出现在bcprov*.jar这个包

所以在tomcat的conf目录里面catalina.properties的文件,

在tomcat.util.scan.DefaultJarScanner.jarsToSkip=里面加上bcprov*.jar过滤

启动不会报错了

 

五:问题补充

redis主机宕机后,从机没有自动切换成主机,导致集群失败。

原因分析:

通过查看日志发现,3台哨兵的Sentinel ID相同,因为Sentinel.conf都是拷贝的,由于配置中如果有2个或者以上同意切换,才能切,因为id一样,因此只认为有一个哨兵,所以切换永远失败

解决办法:

修改每个哨兵配置文件(例如sentinel-26379.conf等),将myid 这一行去掉即可。

重启sentinel时,会重新生成myid

 

 

参考链接:

https://blog.csdn.net/linkw_92/article/details/52297981

https://blog.csdn.net/a70235/article/details/55210704

https://www.cnblogs.com/linjiqin/p/5761281.html

https://blog.csdn.net/lb89012784/article/details/50820118

 

posted on 2019-06-11 11:10  美丽的小鹿  阅读(616)  评论(0编辑  收藏  举报