SpringSession使用redis存储HttpSession(共享session)

本文介绍了如何使用SpringSession结合Redis实现HttpSession的分布式存储。SpringSession提供多种存储方式,如Redis、JVM Map等,以解决分布式集群中的session共享问题。文中详细讲解了在Redis环境下设置和配置SpringSession的步骤,并通过测试验证了HttpSession数据成功存储到Redis中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值