之前写过两篇 springboot 分布式中的 session 共享解决方案,参考
《springboot 中 redis 的使用和分布式 session 共享问题》 和 《再谈 session 共享》
经过同事共同研究,原来 springboot 提供另外一种解决方案:0 行代码解决方案
配置文件
spring.session.redis.namespace=自己定义的命名空间
spring.session.store-type=redis
是的,只需要这两行即可,不再需要其他配置或者代码实现,测试方法见上面两篇文章.
TIPS:1,使用 redis 解决 session 共享,那么就不能使用其他的 session 管理器,比如 shiro 自带的 sessionManger,否则会导致以上配置失效
2,上面配置中没有提供超时配置,redis 超时可以使用 server.session.timeout=1800 实现
附 springboot 提供另外的 session 管理机制
# SPRING SESSION ([SessionProperties](https://github.com/spring-projects/spring-boot/tree/v1.5.9.RELEASE/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionProperties.java))
spring.session.hazelcast.flush-mode=on-save # Sessions flush mode.
spring.session.hazelcast.map-name=spring:session:sessions # Name of the map used to store sessions.
spring.session.jdbc.initializer.enabled= # Create the required session tables on startup if necessary. Enabled automatically if the default table name is set or a custom schema is configured.
spring.session.jdbc.schema=classpath:org/springframework/session/jdbc/schema-@@platform@@.sql # Path to the SQL file to use to initialize the database schema.
spring.session.jdbc.table-name=SPRING_SESSION # Name of database table used to store sessions.
spring.session.mongo.collection-name=sessions # Collection name used to store sessions.
spring.session.redis.flush-mode=on-save # Sessions flush mode.
spring.session.redis.namespace= # Namespace for keys used to store sessions.
spring.session.store-type= # Session store type.
作者:梦想修补师 链接:https://www.jianshu.com/p/9197c5f99d7b 來源:简书 著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.
来源: http://www.jianshu.com/p/9197c5f99d7b