web开发中session一直都是做分布式集群应用时需要解决的一个难题,过去解决这个难题的一般方式是从serlvet容器上解决,而现在使用spring session能够很容易的把session存储到第三方存储容器,框架提供了redis、jvm的map、mongo、gemfire、hazelcast、jdbc等多种存储session的容器的方式。 关于SpringSession更多的介绍以及功能,可以到SpringSession的官网上查看,SpringSession官方网站中也给出了简单快速的入门案例,Spring Session - HttpSession (Quick Start),本文是在此基础上实践后给予总结,以及分享一下我在学习springsession过程中遇到的问题以及解决方法。
Spring Session+Redis案例
1.Redis
Redis版本需要在2.8+以上,本案例使用的是redis-4.0.1.tar.gz(下载地址:redis4.0.1),在CentOS6.5下搭建了redis环境,运行并且监听默认的6379端口。
为了能让redis正常运行起来并且顺利使用客户端连接之前需要注意几点:
启动之前需要修改redis目录下的redis.conf配置文件:
(1)远程主机连接redis服务器
# bind 127.0.0.1 ::1 首先要注释掉这个绑定的ip地址,这是让其他机子能通过网络连接redis的前提。
(2)连接redis服务器的登录问题
# Protected mode is a layer of security protection, in order to avoid that
# Redis instances left open on the internet are accessed and exploited.
#
# When protected mode is on and if:
#
# 1) The server is not binding explicitly to a set of addresses using the
# "bind" directive.
# 2) No password is configured.
#
# The server only accepts connections from clients connecting from the
# IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain
# sockets.
#
# By default protected mode is enabled. You should disable it only if
# you are sure you want clients from other hosts to connect to Redis
# even if no authentication is configured, nor a specific set of interfaces
# are explicitly listed using the "bind" directive.
#protected-mode yes
#daemonize no
#requirepass 677714
protected-mode no
这是连接redis服务器时是不是需要登录密码的设置,默认protected-mode的值为true,需要自己加上requirepass作为登录密码。以后客户端连接该服务器时需要使用-a passpw来指定密码验证登录。假设说redis服务器不需要使用密码进行登录,直接将p