This article brings you an introduction to the method of integrating spring session with Spring boot to achieve session sharing. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
I recently used spring boot to develop a system. nginx does load balancing and distributes requests to multiple tomcats. At this time, the accessed page will distribute the requests to different servers. The session exists on the server side. If the first visit is distributed to A server, then the session will be stored in A server, and load balancing will be distributed to B server when accessed again. Then the session information accessed for the first time will not be able to obtain the previous session information, so session sharing needs to be implemented. Fortunately, there is Spring session can achieve session sharing using simple configuration. Here is an introduction:
<!-- Spring Boot Redis 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-core</artifactId> </dependency>
In the project directory, create a java file (with any name). My name here is RedisSessionConfig.java
@EnableRedisHttpSession. This annotation is very important. After adding it, an interception of spring will be used. The bean is configured to allow Spring to connect to Redis according to the configuration in the configuration file.
SpringSession It should be noted that redis requires version 2.8 or above, and then enable event notification. Add
notify-keyspace-events Ex // 打开此配置,其中Ex表示键事件通知里面的key过期事件,每当有过期键被删除时,会发送通知
in the redis configuration file or use the following command to enable event notification:
redis-cli config set notify-keyspace-events Egx
If your Redis is not maintained by yourself, for example, if you use Alibaba Cloud's Redis database (this is my case), and you cannot change its configuration, you can use the following java configuration file.
package org.spring.springboot.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.session.data.redis.config.ConfigureRedisAction; import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; @Configuration // maxInactiveIntervalInSeconds 默认是1800秒过期,这里测试修改为60秒 @EnableRedisHttpSession(maxInactiveIntervalInSeconds = 1800) public class RedisSessionConfig { @Bean public static ConfigureRedisAction configureRedisAction() { return ConfigureRedisAction.NO_OP; } }
Spring Boot will automatically create a RedisConnectionFactory to connect the Spring Session to the Redis server on localhost on port 6379 (default port). In a production environment you need to make sure you update the configuration to point to the Redis server
src/main/resources/application.properties
# Redis 配置 # Redis数据库索引(默认为0) spring.redis.database=0 # Redis服务器地址 spring.redis.host=192.168.0.1 # Redis服务器连接端口 spring.redis.port=6379 # Redis服务器连接密码(默认为空) spring.redis.password=1234
The above is the detailed content of Introduction to the method of integrating spring session with Spring boot to realize session sharing. For more information, please follow other related articles on the PHP Chinese website!