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
org.springframework.session spring-session-data-redis 1.3.1.release pom
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.
4) In application.xml (spring’s main configuration file), you need to add scanning of the redis.properties configuration file, as follows.
/web-inf/classes/redis.properties
5) Introduce spring-redis.xml into the main configuration file, as follows.
6) In web.xml, add a filter about the session. Only in this way will the session be manipulated by redis.
springsessionrepositoryfilter org.springframework.web.filter.delegatingfilterproxy springsessionrepositoryfilter /*
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(){ iteratorkeys=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!