国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > Nginx > 正文

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

2024-08-30 12:29:30
字體:
供稿:網(wǎng)友

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

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

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

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

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

1.第一步是安裝redis,我的服務器是windows的,下載的是免安裝版本,解壓以后就可以了,其目錄如下。一開始redis是默認不需要密碼,如果想要設置密碼,可以進入redis.windows.conf文件下找到requirepass,刪除前面的#號,在其后面便可以設置密碼。

nginx,redis,session,共享

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

nginx,redis,session,共享

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

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

nginx,redis,session,共享

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

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

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

3)編寫spring-redis.xml配置文件,這個文件配置關(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設置 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參數(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的過濾器,只有這樣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>

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

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

nginx,redis,session,共享

PS.再退出的時候,需要這樣寫才不會出錯。(ssh項目)

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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到服務器教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 隆林| 万盛区| 兴义市| 华容县| 玉树县| 灵武市| 浦县| 辛集市| 南阳市| 嘉鱼县| 县级市| 成安县| 积石山| 沙田区| 平阴县| 嘉兴市| 岳普湖县| 临西县| 从化市| 务川| 东安县| 密云县| 宁波市| 玉树县| 巍山| 商洛市| 平顺县| 涿州市| 梁山县| 遵义县| 炉霍县| 温宿县| 阿勒泰市| 交城县| 洪洞县| 浦县| 宜宾市| 新营市| 甘泉县| 韶关市| 胶州市|