nginx+redis实现session的共享

1.第一步是安装redis,我的服务器是windows的,下载的是免安装版本,解压以后就可以了,其目录如下。一开始redis是默认不需要密码,如果想要设置密码,可以进入redis.windows.conf文件下找到requirepass,删除前面的#号,在其后面便可以设置密码。

2.从cmd进入redis的根目录,键入如下指令:redis-server.exe redis.windows.conf。这样就可以启动redis了,如果启动成功,则会出现下面画面。当然还可以修改conf文件,加上密码。requirepass xxxxx。如果要允许远程也能访问,则需要将bind指令注释掉,该指令默认绑定了127.0.0.1这个ip也就是本地。

3.接下来我们就可以做一些配置工作,来实现session数据的全局缓存。

1)首先是添加jar包,如果你是maven项目,需要在pom.xml加入下面代码

    <!-- redis -->
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
            <version>1.3.1.RELEASE</version>
            <type>pom</type>
    </dependency>

如果不是maven项目,你需要加入下面这些jar包。

2)编写redis.properties,代码如下

redis_isopen=yes
#主机地址
redis_hostName=xxx.xxx.xxx.xxx
#端口
redis_port=6379
#密码
redis_password=xxxxxxxx
#连接超时时间
redis_timeout=200000
redis_maxIdle=300
redis_maxActive=600
redis_maxWait=100000
redis_testOnBorrow=true

基本上与我们配置数据库的连接语句类似。

3)编写spring-redis.xml配置文件,这个文件配置关于redis的一些基本信息。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" 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  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd  ">
    <!-- session设置 maxInactiveIntervalInSeconds为session的失效时间,单位为秒-->
    <bean
        class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
        <property name="maxInactiveIntervalInSeconds" value="3600"></property>
    </bean>
    <!-- redis连接池 -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis_maxIdle}" />
        <property name="testOnBorrow" value="${redis_testOnBorrow}" />
    </bean>
    <!-- redis连接工厂 -->
    <bean id="connectionFactory"
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis_hostName}" />
        <property name="port" value="${redis_port}" />
        <property name="password" value="${redis_password}" />
        <property name="timeout" value="${redis_timeout}" />
        <property name="poolConfig" ref="poolConfig"></property>
    </bean>
</beans>

4)在application.xml(spring的主配置文件)需要加入redis.properties配置文件的扫描,如下。

    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>/WEB-INF/classes/redis.properties</value>
            </list>
        </property>
    </bean>

5)在主配置文件中引入spring-redis.xml,如下。

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

6)在web.xml中,加入关于session的过滤器,只有这样session才会被redis所操纵。

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

这样以后,我们就实现了redis对session的管理。

7)我们可以安装一个redis的客户端来查看里面的数据,叫做Redis Desktop Manager。如下图,很好用,可以看到redis数据库中的数据。

 PS.再退出的时候,需要这样写才不会出错。(ssh项目)

public String yipinExit(){
        Iterator<String>keys=session.keySet().iterator();
        while(keys.hasNext()){
            String key=keys.next();
            session.remove(key);
        }
        return "yipinExit";
    }
posted @ 2019-03-21 14:58  努力化猿的鼠  阅读(2807)  评论(0编辑  收藏  举报