首頁  >  文章  >  資料庫  >  nginx+redis怎麼實現session共享

nginx+redis怎麼實現session共享

WBOY
WBOY轉載
2023-05-26 12:49:061516瀏覽

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

nginx+redis怎麼實現session共享

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

nginx+redis怎麼實現session共享

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

nginx+redis怎麼實現session共享

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資料庫中的資料。

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";
 }

以上是nginx+redis怎麼實現session共享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除