1. redis依赖:
<dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> <version>${redis.version}</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.1</version> </dependency>
org.springframework.session 会自动引入 spring-data-redis
2. web.xml配置:
<!--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> </filter-mapping>
3. spring-redis配置:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- session设置 --> <bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"> <property name="maxInactiveIntervalInSeconds" value="600"/> </bean> <!-- redis连接池 --> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="100" /> <property name="maxIdle" value="10" /> </bean> <!-- redis连接工厂 --> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy"> <property name="hostName" value="${redis_hostName}"/> <property name="port" value="${redis_port}"/> <property name="password" value="${redis_password}" /> <property name="timeout" value="3000"/> <property name="usePool" value="true"/> <property name="poolConfig" ref="jedisPoolConfig"/> </bean> </beans>
maxInactiveIntervalInSeconds 这里配置session的过期时间,单位为秒,默认是1800半个小时。
4. applicationContext.xml引入spring-redis.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!-- 加载属性配置文件 --> <!-- 方式一 <context:property-placeholder location="classpath:jdbc.properties"/>--> <!-- 方式二 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <!--要是有多个配置文件,只需在这里继续添加即可 --> <value>classpath:jdbc.properties</value> <value>classpath:redis.properties</value> </list> </property> </bean> <!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入数据源 --> <property name="dataSource" ref="dataSource"/> </bean> <!-- 自动扫描了所有的XxxxMapper.xml对应的mapper接口文件,这样就不用一个一个手动配置Mpper的映射了,只要Mapper接口类和Mapper映射文件对应起来就可以了。 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.ssm.dao" /> </bean> <!-- 配置mybatis --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- mapperLocations指定mapper.xml文件位置 --> <property name="mapperLocations" value="classpath:mapper/*Mapper.xml"/> <!-- configLocation指定mybatis配置文件位置 --> <property name="configLocation" value="classpath:mybatis-config.xml" /> <!-- 实体别名 --> <property name="typeAliasesPackage" value="com.ssm.domain"/> </bean> <!-- 自动扫描注解的bean --> <context:component-scan base-package="com.ssm.service" /> <!-- 启用注解配置 --> <context:annotation-config/> <!-- 使用事务注解 --> <tx:annotation-driven/> <!-- Spring中引入其他配置文件 --> <import resource="classpath*:/spring-redis.xml" /> </beans>
5. redis.properties
redis_hostName = xxx
redis_port = 6379
redis_password = xxxx
redis_timeout = 20000
6. 这样配置之后,对于Nginx+多台Tomcat的集群而言,多台tomcat使用的session就是基于redis缓存实现的session,当某台Tomcat挂了时,原来访问这台Tomcat的用户会被Nginx转发到其他Tomcat,因为JSESSIONID没有变,所以还是可以从Reids中找到原来的session,也就是说这样配置之后,session只和redis相关了,和tomcat没有关系,tomcat是否重启不影响用户的session.