springboot集成springsession 利用redis实现session共享问题

redis实现session共享,百度上的大神解答看着简单,但在过程中各种各样的坑,爬不出来啊,今天我来说说一下我进的坑

1.pom.xml

<parent>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-parent</artifactId>

    <version>1.3.8.RELEASE</version>

</parent>

<dependencies>

    ...

    <!-- redis session部分 -->

    <dependency>

    <groupId>org.springframework.data</groupId>

    <artifactId>spring-data-redis</artifactId>

    <version>1.6.2.RELEASE</version> 

</dependency>

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-data-redis</artifactId>

    <version>1.5.3.RELEASE</version> 

</dependency>

<dependency>

    <groupId>spring-session</groupId>

    <artifactId>spring-session</artifactId>

    <version>1.3.0.RELEASE</version>

</dependency>

<dependency>

    <groupId>spring-session-data-redis</groupId>

    <artifactId>spring-session-data-redis</artifactId>

    <version>1.3.0.RELEASE</version>

    </dependency>

</dependencies>

我的原项目里是写过springboot版本号的,导致我后面添加高版本号的spring-session-data-redis的时候会报版本错误

原项目里的依赖

1)项目里的是1.3.8的, spring-session-data-redis用2.x版本上的就会报错,不能用高版本的导致我出现了2)的错

2)spring-session-data-redis版本号低了@EnableAutoConfiguration注解用不了,后来又引入了spring-session

很多文章都说只要引入下图两个文件就可以了,我这边应该是版本号过低才导致我各种掉坑的

2.redis配置文件 

spring.redis.host=x.x.x.x

spring.redis.port=6379

 这是xml方式写的,其他的配置可以看详细文档

3.@EnableRedisHttpSession注解

springboot启动类上直接使用注解@EnableRedisHttpSession即可或是自己写一个类加上注解。打开@EnableRedisHttpSession源码,发现maxInactiveIntervalInSeconds  session的过期时间默认是1800秒即30分钟,如果需要修改,注解时进行修改即可

@EnableRedisHttpSession(maxInactiveIntervalInSeconds=60)

到此,spring boot中的session redis配置就结束了,可以运行了

4.测试

开启两个端口看到返回值sessionId相同

redis客户端检查

图是我盗的,公司没有网,没法截图

谢谢阅读,觉得我说的有误的地方给我留言就好

猜你喜欢

转载自blog.csdn.net/weixin_42458930/article/details/82660298