nginx+redis實(shí)現(xiàn)session共享
上一篇我們介紹了nginx實(shí)現(xiàn)的負(fù)載均衡和動(dòng)靜分離,可看這邊。
我們?cè)谖恼碌哪┪舱f到,負(fù)載均衡需要面臨的一個(gè)問題是內(nèi)存數(shù)據(jù)的同步。例如:我有A,B兩臺(tái)服務(wù)器做了負(fù)載均衡,當(dāng)我在A服務(wù)器上執(zhí)行了登錄并且將登錄數(shù)據(jù)存入session的時(shí)候,這些session數(shù)據(jù)只存在于A服務(wù)器上,而沒有在B服務(wù)器上,假如在處理下一個(gè)請(qǐng)求的時(shí)候,我需要用到session的數(shù)據(jù),而不巧的是,這個(gè)請(qǐng)求剛好被交由B服務(wù)器來處理,這時(shí)候就會(huì)出現(xiàn)B服務(wù)器拿不到session數(shù)據(jù)的情況,從而造成錯(cuò)誤。
這是一個(gè)無法避免的問題,有若干的解決方案,歸結(jié)起來都是要實(shí)現(xiàn)session等數(shù)據(jù)在各負(fù)載均衡分支中的同步,第一種想到的方案是把這些數(shù)據(jù)放在mysql等數(shù)據(jù)庫,也就是說存在磁盤,但是我們都知道session之所以出現(xiàn)是因?yàn)樗窃趦?nèi)存中的,程序讀取內(nèi)存的數(shù)據(jù)要遠(yuǎn)遠(yuǎn)比讀取磁盤的數(shù)據(jù)快,所以我們把一些經(jīng)常用到的東西都放在session里面。
有沒有一種數(shù)據(jù)庫,是存放在內(nèi)存中的呢?這就是redis。通俗的講,它就是一個(gè)數(shù)據(jù)庫,但是這個(gè)數(shù)據(jù)庫是存在與內(nèi)存里面的,所以存取起來速度要比讀取磁盤的數(shù)據(jù)快得多。又因?yàn)樗且粋€(gè)數(shù)據(jù)庫,所以可以實(shí)現(xiàn)數(shù)據(jù)的同步。
我們把session數(shù)據(jù)存放在redis中,然后所有的集群分支都可以去訪問這個(gè)數(shù)據(jù)庫里面的東西,這就是全局緩存的原理。
1.第一步是安裝redis,我的服務(wù)器是windows的,下載的是免安裝版本,解壓以后就可以了,其目錄如下。一開始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)下面畫面。當(dāng)然還可以修改conf文件,加上密碼。requirepass xxxxx
3.接下來我們就可以做一些配置工作,來實(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)編寫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ù)庫的連接語句類似。
3)編寫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的過濾器,只有這樣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的客戶端來查看里面的數(shù)據(jù),叫做Redis Desktop Manager。如下圖,很好用,可以看到redis數(shù)據(jù)庫中的數(shù)據(jù)。
PS.再退出的時(shí)候,需要這樣寫才不會(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)文章
負(fù)載均衡的基本知識(shí)以及使用nginx進(jìn)行負(fù)載均衡的簡(jiǎn)單例子
今天小編就為大家分享一篇關(guān)于負(fù)載均衡的基本知識(shí)以及使用nginx進(jìn)行負(fù)載均衡的簡(jiǎn)單例子,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12nginx訪問報(bào)403錯(cuò)誤的幾種情況詳解
最近訪問新安裝的nginx,發(fā)現(xiàn)報(bào)403錯(cuò)誤,所以這篇文章主要給大家介紹了關(guān)于nginx訪問報(bào)403錯(cuò)誤的幾種情況,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07Nginx中日志模塊的應(yīng)用和配置應(yīng)用示例
Nginx是一款高性能的HTTP和反向代理服務(wù)器,廣泛應(yīng)用于互聯(lián)網(wǎng)領(lǐng)域,這篇文章主要介紹了Nginx中日志模塊的應(yīng)用和配置,下面通過一個(gè)簡(jiǎn)單的實(shí)例來演示Nginx日志模塊的應(yīng)用和配置,需要的朋友可以參考下2024-02-02