Nginx + Spring-session + Redis 实现 session共享

1、Nginx服务器搭建以及反向代理设置,参考博文:http://www.cnblogs.com/sz-jack/p/5200989.html


2、添加spring-session和Redis的依赖:

   <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> 

如果是非maven工程的话,就自己下载以下四个包,导入项目即可

 

3、配置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=true

redis.keyPrefix=wz
redis.timeout=2000
redis.db.index=0
redis.isopen:yes
#主机地址
redis.maxActive:600

4、配置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>

5、在spring-mvc.xml的配置文件中引入spring-redis.xml文件

<context:component-scan base-package="org.springframework.web.filter.DelegatingFilterProxy"/>

<import resource="spring-redis.xml"/>

6、配置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;

参考:https://blog.csdn.net/jackpk/article/details/49335189

 

posted on 2018-05-31 18:18  海风1213  阅读(268)  评论(0)    收藏  举报

导航