nginx+redis가 세션 공유를 실현하는 방법

WBOY
풀어 주다: 2023-05-26 12:49:06
앞으로
1488명이 탐색했습니다.

1. 첫 번째 단계는 redis를 설치하는 것입니다. 내 서버는 Windows입니다. 압축을 풀면 다음과 같습니다. 처음에 redis는 기본적으로 비밀번호를 요구하지 않습니다. 비밀번호를 설정하려면 redis.windows.conf 파일에 가서 requirepass를 찾아 앞에 # 기호를 삭제하고 그 뒤에 비밀번호를 설정하면 됩니다.

nginx+redis가 세션 공유를 실현하는 방법

2. cmd에서 redis의 루트 디렉터리를 입력하고 redis-server.exeredis.windows.conf 명령을 입력합니다. 이렇게 하면 redis가 시작될 수 있습니다. 성공적으로 시작되면 다음 화면이 나타납니다. 물론 conf 파일을 수정하고 비밀번호를 추가할 수도 있습니다. requirepass xxxxxx

nginx+redis가 세션 공유를 실현하는 방법

3. 다음으로 세션 데이터의 글로벌 캐싱을 달성하기 위한 몇 가지 구성 작업을 수행할 수 있습니다.

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가 세션 공유를 실현하는 방법

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에서 세션에 대한 필터를 추가합니다. 이 방법으로만 세션이 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의 세션 관리를 구현하겠습니다.

7) Redis 데스크톱 관리자라는 Redis 클라이언트를 설치하여 내부 데이터를 볼 수 있습니다. 아래와 같이 매우 유용하며 redis 데이터베이스에서 데이터를 볼 수 있습니다.

nginx+redis가 세션 공유를 실현하는 방법

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가 세션 공유를 실현하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:yisu.com
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!