nginx+redis實現session的共享


上一篇我們介紹了nginx實現的負載均衡和動靜分離,可看這邊。

我們在文章的末尾說到,負載均衡需要面臨的一個問題是內存數據的同步。例如:我有A,B兩台服務器做了負載均衡,當我在A服務器上執行了登錄並且將登錄數據存入session的時候,這些session數據只存在於A服務器上,而沒有在B服務器上,假如在處理下一個請求的時候,我需要用到session的數據,而不巧的是,這個請求剛好被交由B服務器來處理,這時候就會出現B服務器拿不到session數據的情況,從而造成錯誤。

這是一個無法避免的問題,有若干的解決方案,歸結起來都是要實現session等數據在各負載均衡分支中的同步,第一種想到的方案是把這些數據放在mysql等數據庫,也就是說存在磁盤,但是我們都知道session之所以出現是因為它是在內存中的,程序讀取內存的數據要遠遠比讀取磁盤的數據快,所以我們把一些經常用到的東西都放在session里面。

有沒有一種數據庫,是存放在內存中的呢?這就是redis。通俗的講,它就是一個數據庫,但是這個數據庫是存在與內存里面的,所以存取起來速度要比讀取磁盤的數據快得多。又因為它是一個數據庫,所以可以實現數據的同步。

我們把session數據存放在redis中,然后所有的集群分支都可以去訪問這個數據庫里面的東西,這就是全局緩存的原理。

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配置文件的掃描,如下。

    <!-- 讀取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>

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

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

6)在web.xml中,加入關於session的過濾器,只有這樣session才會被redis所操縱。

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

這樣以后,我們就實現了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";
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM