SpringBoot初始教程之Redis集中式Session管理(四)
2017-07-12 17:37
344 查看
SpringBoot初始教程之Redis集中式Session管理(四)
1.介绍
有关Session的管理方式这里就不再进行讨论,目前无非就是三种单机Session(基于单机内存,无法部署多台机器)、基于Cookie(安全性差)、基于全局的统一Session管理(Redis、MySQL)等多种方式 针对于像淘宝这种超大型网站来说Session如何管理的就无从得知了、但是可以通过yy的方式想象一下,这种大型架构都需要部署多台认证Server,但是一般来说集中式Session无法存储那么多的Session
那么就可以通过UID分片的形式来存储,不同UID分布在不同的Server上认证即可(纯属猜测丷)
2.快速开始
这里采用的是redis进行集中式Session管理,核心如下依赖<dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session</artifactId> <version>1.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency>1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
完整pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springboot-4</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session</artifactId> <version>1.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.4.1.RELEASE</version> </plugin> </plugins> </build> </project>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
application.yaml配置
这块主要是通过application.yaml去配置redis的链接信息server: port: 8080 spring: redis: database: 1 host: localhost pool: max-active: 201
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10
开启@EnableRedisHttpSession
通过加上@EnableRedisHttpSession注解,开启redis集中式session管理,所有的session都存放到了redis中
@SpringBootApplication @EnableRedisHttpSession public class AppApplication { public static void main(String[] args) throws Exception { SpringApplication.run(AppApplication.class, args); } }1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
通过源码可知、可以通过设置maxInactiveIntervalInSeconds来设定session的统一过期时间,
@Retention(java.lang.annotation.RetentionPolicy.RUNTIME) @Target({ java.lang.annotation.ElementType.TYPE }) @Documented @Import(RedisHttpSessionConfiguration.class) @Configuration public @interface EnableRedisHttpSession { int maxInactiveIntervalInSeconds() default 1800; String redisNamespace() default ""; RedisFlushMode redisFlushMode() default RedisFlushMode.ON_SAVE; }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
通过redis集中式管理session这种方式在使用上面对客户端是透明的,无需自己操作redis,在使用HttpSession对象的时候直接使用即可
@RestController public class IndexController { @GetMapping("/index") public ResponseEntity index(HttpSession httpSession) { httpSession.setAttribute("user", "helloword"); return ResponseEntity.ok("ok"); } @GetMapping("/helloword") public ResponseEntity hello(HttpSession httpSession) { return ResponseEntity.ok(httpSession.getAttribute("user")); } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
3. 其他扩展
SpringBoot Session集中式管理不仅仅限于redis这种方式,目前内部支持HttpSession with Pivotal GemFire、HttpSession with JDBC、HttpSession with Mongo 等多种方式具体可以参考http://docs.spring.io/spring-session/docs/1.2.2.RELEASE/reference/html5/#httpsession-jdbc
本文代码
https://git.oschina.net/wangkang_daydayup/SpringBoot-Learn/tree/master
相关文章推荐
- SpringBoot初始教程之Redis集中式Session管理(四)
- SpringBoot初始教程之Redis集中式Session管理
- shiro安全框架扩展教程--如何扩展实现集中式session管理(redis,memcached等)
- 通过redmon监控管理Redis服务器的安装配置教程
- WebService之Axis2系列教程(六)跨服务会话(Session)管理
- Redis3.2+Tomcat实现集群的Session管理 -- tomcat-redis-session-manager的编译和开发部署环境搭建
- Redis简单案例(四) Session的管理
- sna集中式session管理方案实现
- 使用shiro和redis结合,管理SessionDAO的对Session的CRUD,并源码分析
- Redis3.2+Tomcat实现集群的Session管理 -- tomcat-redis-session-manager的编译和开发部署环境搭建
- 使用Spring Session和Redis管理session
- tomcat服务器redis管理session
- tomcat 使用redis 管理session
- Nginx+Tomcat负载平衡,Redis管理session存储
- tomcat+redis集群管理session
- WebService之Axis2系列教程(五)会话(Session)管理
- 【Redis】Redis学习笔记02_使用Redis来管理Tomcat集群的Session
- Redis教程(十二):服务器管理命令总结
- tomcat-redis-session管理 使用说明
- SpringBoot 入门教程:集成mybatis,redis