Home > Database > Redis > body text

How nginx+redis realizes session sharing

WBOY
Release: 2023-05-26 12:49:06
forward
1558 people have browsed it

1. The first step is to install redis. My server is Windows. I downloaded the installation-free version. Just decompress it. The directory is as follows. At the beginning, redis does not require a password by default. If you want to set a password, you can go to the redis.windows.conf file and find requirepass, delete the # sign in front, and set the password after it.

How nginx+redis realizes session sharing

2. Enter the root directory of redis from cmd and type the following command: redis-server.exeredis.windows.conf. In this way, redis can be started. If the startup is successful, the following screen will appear. Of course, you can also modify the conf file and add a password. requirepass xxxxx

How nginx+redis realizes session sharing

#3. Next we can do some configuration work to achieve global caching of session data.

1) First, add the jar package. If you are a maven project, you need to add the following code to 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>
Copy after login

If it is not a maven project, you need to add the following jar packages.

How nginx+redis realizes session sharing

2) Write redis.properties, the code is as follows

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
Copy after login

is basically similar to the connection statement for our configuration database.

3) Write the spring-redis.xml configuration file. This file configures some basic information about 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>
Copy after login

4) In application.xml (spring’s main configuration file), you need to add scanning of the redis.properties configuration file, as follows.

<!-- 读取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>
Copy after login

5) Introduce spring-redis.xml into the main configuration file, as follows.

<import resource="spring-redis.xml" />
Copy after login

6) In web.xml, add a filter about the session. Only in this way will the session be manipulated by 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>
Copy after login

After this, we will realize the management of session by redis.

7) We can install a redis client to view the data inside, called redis desktop manager. As shown below, it is very useful and you can see the data in the redis database.

How nginx+redis realizes session sharing

#ps. When you exit again, you need to write like this to avoid errors. (ssh project)

public string yipinexit(){
 iterator<string>keys=session.keyset().iterator();
 while(keys.hasnext()){
  string key=keys.next();
  session.remove(key);
 }
 return "yipinexit";
 }
Copy after login

The above is the detailed content of How nginx+redis realizes session sharing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template