redis实现session共享

redis单机简单安装

1
2
3
4
wget http://download.redis.io/releases/redis-4.0.1.tar.gz
tar xzf redis-4.0.1.tar.gz
cd redis-4.0.1
make

二进制文件是编译完成后在src目录下
启动脚本https://gist.github.com/paulrosania/257849
redis-server文件要稍作修改
配置文件放到/etc/redis/redis.conf
系统配置/etc/sysconfig/redis

1
2
3
4
5
# pwd
/work/bigdata/redis-4.0.1
cp redis.conf /etc/redis/

vi /etc/redis/redis.conf
1
2
3
4
5
6
7
daemonize yes
loglevel warning
logfile "/work/log/redis.log"
protected-mode no
port 6379
bind 192.168.99.100
maxmemory 512M

redis-server

1
2
cd /etc/init.d/
vi redis-server

chkconfig: - 85 15 注意此行必加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/sh
# chkconfig: - 85 15
# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

redis="/work/bigdata/redis-4.0.1/src/redis-server"
prog=$(basename $redis)

REDIS_CONF_FILE="/etc/redis/redis.conf"

[ -f /etc/sysconfig/redis ] && . /etc/sysconfig/redis

lockfile=/work/lock/subsys/redis

start() {
[ -x $redis ] || exit 5
[ -f $REDIS_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $redis $REDIS_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}

stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}

restart() {
stop
start
}

reload() {
echo -n $"Reloading $prog: "
killproc $redis -HUP
RETVAL=$?
echo
}

force_reload() {
restart
}

rh_status() {
status $prog
}

rh_status_q() {
rh_status >/dev/null 2>&1
}

case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
exit 2
esac

修改权限

1
chmod a+x nginx

配置chkconfig

1
chkconfig --add redis-server

修改服务的默认启动等级

1
chkconfig --level 35 redis-server on

启动 停止 重启

1
2
3
4
5
6
7
8
9
10
11
12
13
# service redis-server start
正在启动 redis-server: [确定]

# ps -ef|grep redis
root 4475 1 0 10:06 ? 00:00:00 /work/bigdata/redis-4.0.1/src/redis-server 127.0.0.1:6379
root 4492 32690 0 10:08 pts/0 00:00:00 grep redis

# service redis-server restart
停止 redis-server: [确定]
正在启动 redis-server: [确定]

# service redis-server stop
停止 redis-server: [确定]

redis-cli简单测试
新版命令行有提示
如 127.0.0.1:6379> set key value [EX seconds] [PX milliseconds] [NX|XX]

1
2
3
4
5
6
7
# /work/bigdata/redis-4.0.1/src/redis-cli -h 192.168.99.100
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set test helloworld
OK
127.0.0.1:6379> get test
"helloworld"

依赖jar包

clone https://github.com/chexagon/redis-session-manager
mvn clean install
将jar包上传至tomcat的lib下

1
2
3
4
ls /bigdata/apache-tomcat-8.5.16/lib

redis-session-manager-2.1.2-SNAPSHOT.jar
redis-session-manager-with-dependencies-2.1.2-SNAPSHOT.jar

修改tomcat配置文件

context.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<Context>

<!-- Default set of monitored resources. If one of these changes, the -->
<!-- web application will be reloaded. -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

<!-- Uncomment this to disable session persistence across Tomcat restarts -->

<Manager className="com.crimsonhexagon.rsm.redisson.SingleServerSessionManager"
endpoint="bigdata1:6379"
sessionKeyPrefix="_rsm_"
saveOnChange="false"
forceSaveAfterRequest="false"
dirtyOnMutation="false"
ignorePattern=".*\\.(ico|png|gif|jpg|jpeg|swf|css|js)$"
connectionPoolSize="100"
timeout="60000"
pingTimeout="1000"
retryAttempts="20"
retryInterval="1000"
/>
</Context>

测试session是否一致

重启两台tomcat
浏览器访问

image
image

查看redis key

1
2
3
4
# /work/bigdata/redis-4.0.1/src/redis-cli -h 192.168.99.100
192.168.99.100:6379> keys *
1) "test"
2) "_rsm_DFC666B06F91C0182340C526104661DC"

总结

tomcat及其相关配置、拦截和redis相关操作

邵志鹏 wechat
扫一扫上面的二维码关注我的公众号
0%