Spring session Data Redis implements session sharing

Source: Internet
Author: User
Tags session id

1. Preface

Encountering a problem with the user experience in development, each time an operation is updated to restart the server, it causes the logged-in user of the membership platform to be dropped. This is because the session information and status of each user are saved by a session, and the session object is created by the server, and the session ID is sent to the client browser in the form of a cookie (each session has a separate sessionid). When the object is not used for a certain amount of time or the server restarts, the object is destroyed, causing the user to drop the line.

2. Workaround

In the process of solving the problem found that just remember the user's SessionID, restart the server and still use the original ID, will not be dropped, that is to ensure that the session is not changed to maintain the user's login status. The spring session Data Redis is used here to share the session (Redis: Cache database), In other words, using Redis to perform a persistent operation on the session (using MySQL and other databases to store the session separately is a bit wasteful, the speed is not redis fast), when the server restarts, can be deserialized from Redis to remove the session, Retrieve user session information again.

Brief configuration steps:

(1) Pom.xml join dependency : Spring-session-data-redis, Spring-session, of course, the premise of Spring (4.3.5), Redis dependencies (Redis uses 3.0 version)

  

(2) Add in applicationcontext.xml config file Redishttpsessionconfiguration (Below is a separate configuration file, then import in)

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://www.springframework.org/schema/beans"3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4 xmlns:p= "http://www.springframework.org/schema/p"5 xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" >6     <BeanID= "Jedispoolconfig"class= "Redis.clients.jedis.JedisPoolConfig" >7         < Propertyname= "Maxidle"value= "0" />8         < Propertyname= "Maxtotal"value= " the" />9         < Propertyname= "Maxwaitmillis"value= "+" />Ten         < Propertyname= "Testonborrow"value= "true" /> One     </Bean> A  -     <!--Redis Connection Configuration, in turn, host IP, port, whether to use pool, (usepool=true) Redis pool configuration - -     <BeanID= "ConnectionFactory"class= "Org.springframework.data.redis.connection.jedis.JedisConnectionFactory" the P:host-name= "0.0.0.0"P:port= "1111"P:database= "Ten"P:pool-config-ref= "Jedispoolconfig"> -     </Bean> -  -     <!--Configure Spring-session - +     <Beanclass= "Org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"> -         <!--expiry TIME 100 minutes - +         < Propertyname= "Maxinactiveintervalinseconds"value= "6000"></ Property> A     </Bean> at </Beans>

(3) Config filter, session timeout time in Web. xml

  

After the configuration is complete, the basic can realize the session sharing, restart the server test, the user who has logged on will not have the situation of the drop line.

Finally post a few reference links:

Session Principle and realization sharing

Pits encountered in Spring Session Data Redis Configuration

5 Kinds of Session processing strategies in cluster/distributed environment

Spring session Data Redis implements session sharing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.