mirror of
				https://gitee.com/coder-xiaomo/flashsale
				synced 2025-11-04 06:03:13 +08:00 
			
		
		
		
	Redis 分布式会话(基于session)
This commit is contained in:
		
							
								
								
									
										12
									
								
								pom.xml
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								pom.xml
									
									
									
									
									
								
							@@ -80,6 +80,18 @@
 | 
				
			|||||||
            <version>2.9.1</version>
 | 
					            <version>2.9.1</version>
 | 
				
			||||||
        </dependency>
 | 
					        </dependency>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        <!--引入SpringBoot对Redis的依赖-->
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>org.springframework.boot</groupId>
 | 
				
			||||||
 | 
					            <artifactId>spring-boot-starter-data-redis</artifactId>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        <!--SpringBoot将Session存储在Redis中-->
 | 
				
			||||||
 | 
					        <dependency>
 | 
				
			||||||
 | 
					            <groupId>org.springframework.session</groupId>
 | 
				
			||||||
 | 
					            <artifactId>spring-session-data-redis</artifactId>
 | 
				
			||||||
 | 
					            <version>2.0.5.RELEASE</version>
 | 
				
			||||||
 | 
					        </dependency>
 | 
				
			||||||
    </dependencies>
 | 
					    </dependencies>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    <build>
 | 
					    <build>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -0,0 +1,10 @@
 | 
				
			|||||||
 | 
					package com.cxyxiaomo.flashsale.config;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
 | 
				
			||||||
 | 
					import org.springframework.stereotype.Component;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@Component
 | 
				
			||||||
 | 
					@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 3600) // 会话1hour过期
 | 
				
			||||||
 | 
					public class RedisConfig  {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -5,8 +5,9 @@ import javax.validation.constraints.Max;
 | 
				
			|||||||
import javax.validation.constraints.Min;
 | 
					import javax.validation.constraints.Min;
 | 
				
			||||||
import javax.validation.constraints.NotBlank;
 | 
					import javax.validation.constraints.NotBlank;
 | 
				
			||||||
import javax.validation.constraints.NotNull;
 | 
					import javax.validation.constraints.NotNull;
 | 
				
			||||||
 | 
					import java.io.Serializable;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class UserModel {
 | 
					public class UserModel implements Serializable {
 | 
				
			||||||
    private Integer id;
 | 
					    private Integer id;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @NotBlank(message = "用户名不能为空")
 | 
					    @NotBlank(message = "用户名不能为空")
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -11,3 +11,13 @@ spring.datasource.password=111111
 | 
				
			|||||||
# 使用druid数据源
 | 
					# 使用druid数据源
 | 
				
			||||||
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
 | 
					spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
 | 
				
			||||||
spring.datasource.driverClassName=com.mysql.jdbc.Driver
 | 
					spring.datasource.driverClassName=com.mysql.jdbc.Driver
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# 配置SpringBoot对Redis的依赖
 | 
				
			||||||
 | 
					spring.redis.host=127.0.0.1
 | 
				
			||||||
 | 
					spring.redis.port=6379
 | 
				
			||||||
 | 
					spring.redis.database=10
 | 
				
			||||||
 | 
					#spring.redis.password=
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# 设置jedis连接池
 | 
				
			||||||
 | 
					spring.redis.jedis.pool.max-active=50
 | 
				
			||||||
 | 
					spring.redis.jedis.pool.max-idle=20
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user