The spring session is used in spring MVC. MD

Source: Internet
Author: User
Tags server memory

  1. The Web project will be session-held through sessions, which are stored in server memory;
  2. Now in order to improve the performance and stability of the site, the Web project published to multiple servers, through proxy such as Nginx or F5 do load balancing;
  3. Due to the normal configuration of load balancing, the client requests are randomly forwarded to a server, which causes the session to be lost;
  4. Solution: One can be a proxy such as Nginx or F5 configured to be highly available, that is, when the user accesses, during the same session, only one server forward, the other is to introduce the spring session, the session is persistent, unified management;
  5. The spring session transparently encapsulates the session by persisting the memory data from the session to a cache database such as Redis, enabling session persistence and unified management, and developers simply introduce the spring session Use the same as usual with the session operation;

Official Document: Https://spring.io/projects/spring-session

The following records implement the spring Session+redis in spring MVC.

First step: Pom Configuration introduces dependency packages
        <!-- spring session依赖包 -->        <!-- Spring Data Redis -->        <dependency>            <groupId>org.springframework.data</groupId>            <artifactId>spring-data-redis</artifactId>            <version>1.7.3.RELEASE</version>        </dependency>        <!-- Spring Session -->        <dependency>            <groupId>org.springframework.session</groupId>            <artifactId>spring-session</artifactId>            <version>1.2.2.RELEASE</version>        </dependency>        <!-- Apache Commons Pool -->        <dependency>            <groupId>org.apache.commons</groupId>            <artifactId>commons-pool2</artifactId>            <version>2.4.2</version>        </dependency>
Step Two: Configure Spring-mvc.xml:
    <!--get detailed configuration information with profile scanning-<bean class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfi Gurer "> <property name=" Location "value=" Classpath:app.properties "/> </bean> <!--new Spring-s Ession configuration-<bean class= " Org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration "/> <!-- <bean class= "Org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >--> <!--< Property Name= "HostName" value= "localhost"/>--> <!--<property name= "password" value= "Your-password"/& gt;--> <!--<property name= "Port" value= "6379"/>--> <!--<property name= "Database" value = "Ten"/>--> <!--</bean>--> <!--jedisconnectionfactory--<bean class= "Org.springframe Work.data.redis.connection.jedis.JedisConnectionFactory "> <constructor-arg index=" 0 "> <bean Class= "Org.springframework.data.redis.connection.RedisClusterConfiguration "> <constructor-arg index=" 0 "> <set> <value>${redis. clusters}</value> </set> </constructor-arg> <propert Y name= "maxredirects" value= "5"/> </bean> </constructor-arg> <constructor-arg                index= "1" > <!--Redis cache configuration--<bean class= "Redis.clients.jedis.JedisPoolConfig" > <property name= "Maxidle" value= "6000"/><!--maximum idle time-<property name= "Maxwaitmi Llis "value="/><!--maximum wait milliseconds to get a link, less than 0: blocking indeterminate time, Default-1--and <property name= "Testonborrow" value            = "true"/><!--check for validity when the link is obtained, by default, <property name= "Testonreturn" value= "true"/>   </bean> </constructor-arg> </bean> <!--let spring session no longer execute config command---<util:constant static-field= " Org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP "/>
Step three: Configure Web. xml:
  <!-- 配置spring session -->  <filter>    <filter-name>springSessionRepositoryFilter</filter-name>    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  </filter>  <filter-mapping>    <filter-name>springSessionRepositoryFilter</filter-name>    <url-pattern>/*</url-pattern>    <dispatcher>REQUEST</dispatcher>    <dispatcher>ERROR</dispatcher>  </filter-mapping>
Encounter problems

Error:

org.springframework.data.redis.serializer.SerializationException: Cannot serialize;

Solution:
The JavaBean to be cached needs to implement the serializable interface, because spring will serialize the object first into Redis, and for this exception, modify the corresponding JavaBean object to implement serializable:

public class SessionUser implements Serializable {}

PS: So the JavaBean object called by the session should be implemented serializable, otherwise the deserialization will be wrong;

The spring session is used in spring MVC. MD

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.