搜尋
首頁資料庫Redisnginx+redis怎麼實現session共享

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中文網其他相關文章!

陳述
本文轉載於:亿速云。如有侵權,請聯絡admin@php.cn刪除
es和redis区别es和redis区别Jul 06, 2019 pm 01:45 PM

Redis是现在最热门的key-value数据库,Redis的最大特点是key-value存储所带来的简单和高性能;相较于MongoDB和Redis,晚一年发布的ES可能知名度要低一些,ES的特点是搜索,ES是围绕搜索设计的。

一起来聊聊Redis有什么优势和特点一起来聊聊Redis有什么优势和特点May 16, 2022 pm 06:04 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于redis的一些优势和特点,Redis 是一个开源的使用ANSI C语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式存储数据库,下面一起来看一下,希望对大家有帮助。

Redis实现排行榜及相同积分按时间排序功能的实现Redis实现排行榜及相同积分按时间排序功能的实现Aug 22, 2022 pm 05:51 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,希望对大家有帮助。

实例详解Redis Cluster集群收缩主从节点实例详解Redis Cluster集群收缩主从节点Apr 21, 2022 pm 06:23 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis Cluster集群收缩主从节点的相关问题,包括了Cluster集群收缩概念、将6390主节点从集群中收缩、验证数据迁移过程是否导致数据异常等,希望对大家有帮助。

详细解析Redis中命令的原子性详细解析Redis中命令的原子性Jun 01, 2022 am 11:58 AM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于原子操作中命令原子性的相关问题,包括了处理并发的方案、编程模型、多IO线程以及单命令的相关内容,下面一起看一下,希望对大家有帮助。

一文搞懂redis的bitmap一文搞懂redis的bitmapApr 27, 2022 pm 07:48 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了bitmap问题,Redis 为我们提供了位图这一数据结构,位图数据结构其实并不是一个全新的玩意,我们可以简单的认为就是个数组,只是里面的内容只能为0或1而已,希望对大家有帮助。

实例详解Redis实现排行榜及相同积分按时间排序功能的实现实例详解Redis实现排行榜及相同积分按时间排序功能的实现Aug 26, 2022 pm 02:09 PM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,下面一起来看一下,希望对大家有帮助。

一起聊聊Redis实现秒杀的问题一起聊聊Redis实现秒杀的问题May 27, 2022 am 11:40 AM

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于实现秒杀的相关内容,包括了秒杀逻辑、存在的链接超时、超卖和库存遗留的问题,下面一起来看一下,希望对大家有帮助。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前By尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具