最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

nginx+redis實(shí)現(xiàn)session共享

 更新時(shí)間:2018年03月29日 09:21:13   作者:xdxxdx  
這篇文章主要為大家詳細(xì)介紹了nginx+redis實(shí)現(xiàn)session的共享,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

上一篇我們介紹了nginx實(shí)現(xiàn)的負(fù)載均衡和動(dòng)靜分離,可看這邊。

我們?cè)谖恼碌哪┪舱f(shuō)到,負(fù)載均衡需要面臨的一個(gè)問(wèn)題是內(nèi)存數(shù)據(jù)的同步。例如:我有A,B兩臺(tái)服務(wù)器做了負(fù)載均衡,當(dāng)我在A服務(wù)器上執(zhí)行了登錄并且將登錄數(shù)據(jù)存入session的時(shí)候,這些session數(shù)據(jù)只存在于A服務(wù)器上,而沒(méi)有在B服務(wù)器上,假如在處理下一個(gè)請(qǐng)求的時(shí)候,我需要用到session的數(shù)據(jù),而不巧的是,這個(gè)請(qǐng)求剛好被交由B服務(wù)器來(lái)處理,這時(shí)候就會(huì)出現(xiàn)B服務(wù)器拿不到session數(shù)據(jù)的情況,從而造成錯(cuò)誤。

這是一個(gè)無(wú)法避免的問(wèn)題,有若干的解決方案,歸結(jié)起來(lái)都是要實(shí)現(xiàn)session等數(shù)據(jù)在各負(fù)載均衡分支中的同步,第一種想到的方案是把這些數(shù)據(jù)放在mysql等數(shù)據(jù)庫(kù),也就是說(shuō)存在磁盤(pán),但是我們都知道session之所以出現(xiàn)是因?yàn)樗窃趦?nèi)存中的,程序讀取內(nèi)存的數(shù)據(jù)要遠(yuǎn)遠(yuǎn)比讀取磁盤(pán)的數(shù)據(jù)快,所以我們把一些經(jīng)常用到的東西都放在session里面。

有沒(méi)有一種數(shù)據(jù)庫(kù),是存放在內(nèi)存中的呢?這就是redis。通俗的講,它就是一個(gè)數(shù)據(jù)庫(kù),但是這個(gè)數(shù)據(jù)庫(kù)是存在與內(nèi)存里面的,所以存取起來(lái)速度要比讀取磁盤(pán)的數(shù)據(jù)快得多。又因?yàn)樗且粋€(gè)數(shù)據(jù)庫(kù),所以可以實(shí)現(xiàn)數(shù)據(jù)的同步。

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

1.第一步是安裝redis,我的服務(wù)器是windows的,下載的是免安裝版本,解壓以后就可以了,其目錄如下。一開(kāi)始redis是默認(rèn)不需要密碼,如果想要設(shè)置密碼,可以進(jìn)入redis.windows.conf文件下找到requirepass,刪除前面的#號(hào),在其后面便可以設(shè)置密碼。

2.從cmd進(jìn)入redis的根目錄,鍵入如下指令:redis-server.exeredis.windows.conf。這樣就可以啟動(dòng)redis了,如果啟動(dòng)成功,則會(huì)出現(xiàn)下面畫(huà)面。當(dāng)然還可以修改conf文件,加上密碼。requirepass xxxxx

3.接下來(lái)我們就可以做一些配置工作,來(lái)實(shí)現(xiàn)session數(shù)據(jù)的全局緩存。

1)首先是添加jar包,如果你是maven項(xiàng)目,需要在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項(xiàng)目,你需要加入下面這些jar包。

2)編寫(xiě)redis.properties,代碼如下

redis_isopen:yes
#主機(jī)地址
redis_hostName=xxx.xxx.xxx.xxx
#端口
redis_port=6379
#密碼
redis_password=xxxxxxxx
#連接超時(shí)時(shí)間
redis_timeout=200000
redis_maxIdle:300
redis_maxActive:600
redis_maxWait:100000
redis_testOnBorrow:true

基本上與我們配置數(shù)據(jù)庫(kù)的連接語(yǔ)句類(lèi)似。

3)編寫(xiě)spring-redis.xml配置文件,這個(gè)文件配置關(guān)于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設(shè)置 maxInactiveIntervalInSeconds為session的失效時(shí)間,單位為秒-->
 <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參數(shù)配置 -->
 <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中,加入關(guān)于session的過(guò)濾器,只有這樣session才會(huì)被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>

這樣以后,我們就實(shí)現(xiàn)了redis對(duì)session的管理。

7)我們可以安裝一個(gè)redis的客戶(hù)端來(lái)查看里面的數(shù)據(jù),叫做Redis Desktop Manager。如下圖,很好用,可以看到redis數(shù)據(jù)庫(kù)中的數(shù)據(jù)。

PS.再退出的時(shí)候,需要這樣寫(xiě)才不會(huì)出錯(cuò)。(ssh項(xiàng)目)

public String yipinExit(){
 Iterator<String>keys=session.keySet().iterator();
 while(keys.hasNext()){
  String key=keys.next();
  session.remove(key);
 }
 return "yipinExit";
 }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

板桥市| 台安县| 巩义市| 广东省| 天台县| 任丘市| 五原县| 大厂| 门源| 鹤山市| 绥宁县| 景宁| 舞钢市| 平顶山市| 和田县| 承德县| 龙口市| 易门县| 南涧| 高邮市| 临沭县| 襄垣县| 桐城市| 兰州市| 永年县| 鞍山市| 吉首市| 巴彦淖尔市| 枣庄市| 甘孜县| 龙海市| 全南县| 合肥市| 松滋市| 兰西县| 喀喇沁旗| 平乐县| 达尔| 尚义县| 昭觉县| 长乐市|