一、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;