spring boot 2.x 系列 —— spring boot 实现分布式 session
文章目录
源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all
一、项目结构
二、分布式session的配置
2.1 引入依赖
<!--分布式 session 相关依赖-->
<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>
2.2 Redis配置
spring:
redis:
host: 127.0.0.1
port: 6379
jedis:
pool:
# 连接池最大连接数,使用负值表示无限制。
max-active: 8
# 连接池最大阻塞等待时间,使用负值表示无限制。
max-wait: -1s
# 连接池最大空闲数,使用负值表示无限制。
max-idle: 8
# 连接池最小空闲连接,只有设置为正值时候才有效
min-idle: 1
timeout: 300ms
session:
# session 存储方式 支持redis、mongo、jdbc、hazelcast
store-type: redis
# 如果是集群节点 采用如下配置指定节点
#spring.redis.cluster.nodes
有两点需要特别说明:
- spring-session 不仅提供了redis作为公共session存储的方案,同时也支持jdbc、mongodb、Hazelcast等作为公共session的存储,可以用session.store-type 指定;
- 对于redis 存储方案而言,官方也提供了不止一种整合方式,这里我们选取的整合方案是jedis客户端作为连接,当然也可以使用Lettuce作为客户端连接。
2.3 启动类上添加@EnableRedisHttpSession 注解开启 spring-session-redis 整合方案的自动配置
@SpringBootApplication
@EnableRedisHttpSession(maxInactiveIntervalInSeconds= 1800) //开启redis session支持,并配置session过期时间
public class SpringBootSessionApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootSessionApplication.class, args);
}
}
三、验证分布式session
3.1 创建测试controller和测试页面
@Controller
public class LoginController {
@RequestMapping
public String index() {
return "index";
}
@RequestMapping("home")
public String home() {
return "home";
}
@PostMapping("login")
public String login(User user, HttpSession session) {
// 随机生成用户id
user.setUserId(Math.round(Math.floor(Math.random() * 10 * 1000)));
// 将用户信息保存到id中
session.setAttribute("USER", user);
return "home";
}
}
登录页面index.ftl:
<!doctype html>
<html lang="en">
<head>
<title>登录页面</title>
</head>
<body>
<form action="/login" method="post">
用户:<input type="text" name="username"><br/>
密码:<input type="password" name="password"><br/>
<button type="submit">登录</button>
</form>
</body>
</html>
session 信息展示页面home.ftl:
<!doctype html>
<html lang="en">
<head>
<title>主页面</title>
</head>
<body>
<h5>登录用户: ${Session["USER"].username} </h5>
<h5>用户编号: ${Session["USER"].userId} </h5>
</body>
</html>
3.2 启动项目
由于我们这里采用的是spring boot 的内置容器作为web容器,所以直接启动两个实例测试即可。
应用1启动配置:
应用2启动配置,需要用 --server.port指定不同的端口号:
测试结果:
附:源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all
spring boot 2.x 系列 —— spring boot 实现分布式 session的更多相关文章
- spring boot:使用redis cluster集群作为分布式session(redis 6.0.5/spring boot 2.3.1)
一,为什么要使用分布式session? HpptSession默认使用内存来管理Session,如果将应用横向扩展将会出现Session共享问题, 所以我们在创建web集群时,把session保存到r ...
- spring boot 2.x 系列 —— spring boot 整合 redis
文章目录 一.说明 1.1 项目结构 1.2 项目主要依赖 二.整合 Redis 2.1 在application.yml 中配置redis数据源 2.2 封装redis基本操作 2.3 redisT ...
- spring boot 2.x 系列 —— spring boot 整合 dubbo
文章目录 一. 项目结构说明 二.关键依赖 三.公共模块(boot-dubbo-common) 四. 服务提供者(boot-dubbo-provider) 4.1 提供方配置 4.2 使用注解@Ser ...
- spring boot 2.x 系列 —— spring boot 整合 druid+mybatis
源码Gitub地址:https://github.com/heibaiying/spring-samples-for-all 一.说明 1.1 项目结构 项目查询用的表对应的建表语句放置在resour ...
- spring boot 2.x 系列 —— spring boot 整合 servlet 3.0
文章目录 一.说明 1.1 项目结构说明 1.2 项目依赖 二.采用spring 注册方式整合 servlet 2.1 新建过滤器.监听器和servlet 2.2 注册过滤器.监听器和servlet ...
- spring boot 2.x 系列 —— spring boot 整合 RabbitMQ
文章目录 一. 项目结构说明 二.关键依赖 三.公共模块(rabbitmq-common) 四.服务消费者(rabbitmq-consumer) 4.1 消息消费者配置 4.2 使用注解@Rabbit ...
- spring boot 2.x 系列 —— spring boot 整合 kafka
文章目录 一.kafka的相关概念: 1.主题和分区 2.分区复制 3. 生产者 4. 消费者 5.broker和集群 二.项目说明 1.1 项目结构说明 1.2 主要依赖 二. 整合 kafka 2 ...
- Spring Boot 应用使用spring session+redis启用分布式session后,如何在配置文件里设置应用的cookiename、session超时时间、redis存储的namespace
现状 项目在使用Spring Cloud搭建微服务框架,其中分布式session采用spring session+redis 模式 需求 希望可以在配置文件(application.yml)里设置应用 ...
- Spring Boot 2.0系列文章(五):Spring Boot 2.0 项目源码结构预览
关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/15/springboot2_code/ 项目结构 结构分析: Spring-boot-pr ...
随机推荐
- [MVVM Light]Messenger 的使用
原文:[MVVM Light]Messenger 的使用 当我们使用MVVM开发模式进行开发时,ViewModel之间的通信常常是很头疼的事情,好在MVVM Light提供了Messenger类可以轻 ...
- WPF文字描边的解决方法(二)——支持文字竖排和字符间距调整
原文:WPF文字描边的解决方法(二)--支持文字竖排和字符间距调整 自前天格式化文本效果出来后,今天又添加文本竖排和调整字符间距的功能.另外,由于上次仓促,没来得及做有些功能的设计时支持,这次也调整好 ...
- 【树转数组】poj1195
/* 二维的树状数组: 更新某个元素时: NO.1:c[n1],c[n2],c[n3],....,c[nm]; 当中n1 = i,n(i+1) = ni+lowbit(ni); nm+lowbit(n ...
- 获取 UIElement 相对于屏幕原点所占用的矩形区域
原文:获取 UIElement 相对于屏幕原点所占用的矩形区域 <Grid Background="Transparent"> <StackPanel Margi ...
- WPF 自定义图片剪切器 - 头像剪切(扩展与完善、实时截图)
原文:WPF 自定义图片剪切器 - 头像剪切(扩展与完善.实时截图) 一.说明:上一次写的"WPF 自定义图片剪切器 - 头像剪切.你懂得"存在明显的缺陷,由于篇幅较长.重新写了一 ...
- SQL之Grant(分配权限)和Revoke(回收权限)
Grant Grant可以把指定的权限分配给特定的用户,如果这个用户不存在,则会创建一个用户 命令格式 grant 权限 on 数据库名.表名 to 用户名@登陆方式 identified by 'p ...
- php 将一个二维数组中两个相同的value 相同 指定值相加
array(3) { [0]=> array(7) { ["mlid"]=> int(1) ["num"]=> int(1) ["c ...
- wpf中防止界面卡死的写法
原文:wpf中防止界面卡死的写法 ); this.Dispatcher.BeginInvoke(new Action(() => { this.button1.Content = "计 ...
- liunx 桥接 上网 ip配置 外部网络访问
一.设置VMware 在vmware的[编辑]-->[虚拟网络编辑器]设置:将VMnet0设置为“桥接”,并桥接到宿主机器的网卡(可以是有线或者无线网络). 二.设置虚拟机系统(以cento ...
- Cordova 教程地址
原文:Cordova 教程地址 1.Cordova 官网 http://cordova.apache.org/ 2.Cordova插件库 for android http://cordova.apac ...