spring boot session redis的使用

spring boot session redis的使用

前面几章我们介绍了spring boot redis的使用,相信大家对于redis已经不陌生了,而在分布式系统中呢session共享也是一个典型的问题了,网上已经有了很多session共享的解决方案了,这章我们将介绍使用spring-session+redis实现session共享的方案。

1.引入相关的依赖

2.进行session的配置,直接上代码(注明:maxInactiveIntervalInSeconds: 设置Session失效时间,使用Redis Session之后,原Boot的server.session.timeout属性不再生效):

package com.zxl.examples.session;

import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

/**
 * Created by Administrator on 2017/8/1.
 */
@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 86400*30)
public class SessionConfig {
}

  这时将端口修改一下打包,同时启动多个应用,用一个登录后另一个直接访问需要登录认证成功后才能访问的资源,这时发现session共享成功了,并且redis中也有了对应的值。


小提示:如启动的时候报错如:Caused by: redis.clients.jedis.exceptions.JedisDataException: ERR Unsupported CONFIG parameter: notify-keyspace-events,说明redis的版本与之不匹配,需要下载与之对应的redis即可

猜你喜欢

转载自blog.csdn.net/long290046464/article/details/76692454