Home > Java > javaTutorial > body text

Introduction to the method of integrating spring session with Spring boot to realize session sharing

不言
Release: 2019-02-19 15:56:42
forward
2189 people have browsed it

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:

1. Introduce the jar package into pom.xml

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

2. Add the RedisSessionConfig configuration class

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过期事件,每当有过期键被删除时,会发送通知
Copy after login

in the redis configuration file or use the following command to enable event notification:

redis-cli config set notify-keyspace-events Egx
Copy after login

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

3. Configure redis connection

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

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!

Related labels:
source:cnblogs.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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!