一、Nginx服务器搭建以及反向代理设置,参考博文:http://www.cnblogs.com/sz-jack/p/5200989.htmlcss
二、添加spring-session和Redis的依赖:html
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>1.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency> nginx
若是是非maven工程的话,就本身下载如下四个包,导入项目便可web
三、配置redis,配置缓存相关信息redis
# Redis 配置
# server IP
redis.host=192.168.0.115
# server port
redis.port=6379
# use dbIndex
redis.database=0
#password
redis.password=
# 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例
redis.maxIdle=300
# 表示当borrow(引入)一个jedis实例时,最大的等待时间,若是超过等待时间(毫秒),则直接抛出JedisConnectio
redis.maxWait=3000
# 在borrow一个jedis实例时,是否提早进行validate操做;若是为true,则获得的jedis实例均是可用的
redis.testOnBorrow=truespring
redis.keyPrefix=wz
redis.timeout=2000
redis.db.index=0
redis.isopen:yes
#主机地址
redis.maxActive:600spring-mvc
四、配置spring-redis.xml文件,在配置文件中引入redis.properties文件,引入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" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="org.springframework.web.filter.DelegatingFilterProxy"/>
<!-- 读取redis参数配置 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/classes/redis.properties</value>
</list>
</property>
</bean>
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
<property name="maxTotal" value="1000" />
</bean>
<!-- redis服务器中心 -->
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="poolConfig" ref="poolConfig" />
<property name="port" value="${redis.port}" />
<property name="hostName" value="${redis.host}" />
<property name="timeout" value="${redis.timeout}"></property>
</bean>
</bean> -->
<bean id="redisHttpSessionConfiguration"
class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
<property name="maxInactiveIntervalInSeconds" value="1800" />
</bean>
</beans>tomcat
五、在spring-mvc.xml的配置文件中引入spring-redis.xml文件服务器
<context:component-scan base-package="org.springframework.web.filter.DelegatingFilterProxy"/>
<import resource="spring-redis.xml"/>
六、配置web.xml,主要就是将session托管给spring处理
<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>
过程当中,若是启动一直报错,报springSessionRepositoryFilter没法建立成功的话,就直接在web.xml中引入spring-redis.xml文件
<!-- Spring 上下文参数 加载Spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-mybatis.xml
classpath:spring-cxf.xml
classpath:spring-redis.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
部署tomcat,启动nginx便可
注意,启动nginx以后,访问项目的过程当中,若是session实现了共享,能够正常登陆,可是静态文件(css、js、image)加载不出来的话,首先须要检查加载文件的时候,url是否正确,注意修改ip和端口号
默认状况下listen是80端口,可是若是监听端口发生变化的话,对应的proxy_set_header Host $host;这个配置项,也要将端口加上:proxy_set_header Host $host:$server_port;