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.
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
#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>
If it is not a maven project, you need to add the following jar packages.
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
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>
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>
5) Introduce spring-redis.xml into the main configuration file, as follows.
<import resource="spring-redis.xml" />
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>
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.
#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"; }
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!