Java Web projects, which do not rely on web containers for load balancing, must address session sharing issues. There are many online solutions, but I think the use of Spring-session +redis is the most efficient, do not have to reinvent the wheel, and do not have to modify the code of the project, and the session used by the project with the Web container decoupling, Completely from the container's httpsession to the session provided by spring. Please visit the Spring's official website for details on how to use it. Write down the steps for using spring Session+redis in my project. The project uses a MAVEN-structured Web project 1.pom.xml
<!--spring-session begin--> <dependency> <groupid>org.springframework.dat A</groupid> <artifactId>spring-data-redis</artifactId> <version>1.7.6.release</ version> </dependency> <dependency> <groupid>org.springframework.session</groupid&
Gt <artifactId>spring-session</artifactId> <version>1.3.0.RELEASE</version> </dependen cy> <dependency> <groupId>redis.clients</groupId> <artifactid>jedis</art ifactid> <version>2.8.1</version> </dependency> <dependency> <groupid>org.ap Ache.commons</groupid> <artifactId>commons-pool2</artifactId> <version>2.4.2</version > <scope>compile</scope> </dependency> <!--spring-session End-->
Note: Just start my Spring framework package (that is, a lot of spring package) with 4.0, but start the Tomcat server error, said can not initialize redistemplate, run to Stackofflow look, there is said to be because of the jar problem, The need to upgrade the spring framework must be higher than 4.2.1 because Redistemplate changed the constructor. The spring frame is then raised to 4.3.7.RELEASE. It's OK. 2. Configure the filter in Web. XML, plus this configuration must be at the top of the filter chain
<!--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>
3. Register the required beans in applicationcontext.xml (this is the name of my spring container configuration file). (also line with annotations, but I am using XML configuration)
<!--Redis--<bean id= "Jedispoolconfig" class= "Redis.clients.jedis.JedisPoolConfig" > </bean> <bean id= "Jedisconnectionfactory" class= " Org.springframework.data.redis.connection.jedis.JedisConnectionFactory "> <property name=" hostName "value=" l
Ocalhost "/> <property name=" port "value=" 6379 "/> <property name=" password "value=" * * * "/>
<property name= "Usepool" value= "true"/> <property name= "poolconfig" ref= "Jedispoolconfig"/> </bean> <bean id= "redistemplate" class= "Org.springframework.data.redis.core.RedisTemplate" > <p Roperty name= "ConnectionFactory" ref= "Jedisconnectionfactory"/> </bean> <!--the session into Redis---& Lt;bean id= "Redishttpsessionconfiguration" class= " Org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration "> < Property Name= "Maxinactiveintervalinseconds" value= "1800"/> </bean>
OK, it's over here, and the code in the system doesn't have to change at all. This is really decoupling, spring is very powerful.