全栈程序猿的成长程序员

又双叒叕来session共享

2018-01-19  本文已影响55人  小尘哥

之前写过两篇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管理机制

<pre class="programlisting" style="line-height: 1.4; color: black; font-size: 15px; padding: 6px 10px; background-color: rgb(248, 248, 248); border: 1px solid rgb(204, 204, 204); border-radius: 3px; clear: both; overflow: auto; font-family: Consolas, &quot;Liberation Mono&quot;, Courier, monospace; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"># 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.</pre>

上一篇下一篇

猜你喜欢

热点阅读