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.8.1</version>
</dependency>

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

<?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="redisUtil" class="com.zkxl.fep.redis.RedisUtil">
        <property name="redisTemplate" ref="redisTemplate" />
    </bean> -->
    <bean id="redisHttpSessionConfiguration"
class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
    <property name="maxInactiveIntervalInSeconds" value="1800" />
</bean>
</beans>

5、配置web.xml

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

部署tomcat,启动nginx即可

猜你喜欢

转载自www.cnblogs.com/guo-eric/p/9118248.html