初衷:
nginx ip_hash 可以实现基本的,但是如果时间久了还有偶尔会出问题。
tomcat session redis 方式扩展容器,将session放在redis中,有点对程序没有侵入,但是有bug 因为没有官方的人员来维护,而且没有实现redis cluster 的方式。
所以采用spring-session 在运用级别来实现,
maven:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>${spring.data.redis.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>${spring.session.version}</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${redis.clients.version}</version>
</dependency>
< spring.session.version>1.2.1.RELEASE</spring.session.version>
<!-- redis -->
<redis.clients.version>2.9.0</redis.clients.version>
<spring.data.redis.version>1.7.2.RELEASE</spring.data.redis.version>
需要spring 4.2.5 +
同时需要用到spring-data-redis.
spring-context-session.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- 如果不用spring-session 则去掉该文件 ,或者让其不被扫描到,同时去掉web.xml的相关filter -->
<description>Spring Session Configuration</description>
<context:annotation-config />
<context:property-placeholder ignore-unresolvable="true" location="classpath:web.properties" />
<!-- 将session放入redis 有很多坑 web中放入spring session filter的顺序一定要靠前,且这个配置文件要在spring初始化的时候 在spring mvc 之前初始化,
spring version update to 4.2.5 +
-->
<bean id="redisHttpSessionConfiguration"
class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
<property name="maxInactiveIntervalInSeconds" value="${redis.session.timeout}" />
<property name="redisNamespace" value="${redis.session.prefix}"/>
</bean>
</beans>
#spring-redis session
redis.session.timeout=3600
redis.session.prefix=zerab-web
web.xml配置 要放在所有filter之前,因为是代理servlet的的session
<!--spring session redis delegatingFilterProxy 如果不需要就去掉 -->
<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>
</filter-mapping>