(十九)SpringBoot之使用Spring Session集群-redis
一、引入maven依赖
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<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.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependencies>
二、配置application.properties
server.port=8080
spring.redis.host=localhost
spring.redis.port=6379 # spring session使用存储类型
spring.session.store-type=redis
- spirngboot默认就是使用redis方式,如果不想用可以填none。
三、在启动类中加入@EnableRedisHttpSession 注解
package com.shyroke; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; @EnableCaching
@EnableRedisHttpSession
@SpringBootApplication
public class SpringbootSessionApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootSessionApplication.class, args);
}
}
四、编写控制器
package com.shyroke.controller; import java.util.HashMap;
import java.util.Map; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
@RequestMapping(value = "/")
public class IndexController { @ResponseBody
@RequestMapping(value = "/session")
public Map<String, Object> getSession(HttpServletRequest request) {
request.getSession().setAttribute("username", "admin");
Map<String, Object> map = new HashMap<String, Object>();
map.put("sessionId", request.getSession().getId());
return map;
} @ResponseBody
@RequestMapping(value = "/get")
public String get(HttpServletRequest request) {
String userName = (String) request.getSession().getAttribute("username"); return userName;
} }
五、测试
- 先输入http://localhost:8080/session,在session中设置一个值
- http://localhost:8080/get,获取session中的值
- 复制这个工程,application.properties中的server.port=8081,然后访问“http://localhost:8081/get”,如下获取到了另一个工程中设置的session的值。
(十九)SpringBoot之使用Spring Session集群-redis的更多相关文章
- SpringBoot2 使用Spring Session集群
有几种办法: 1.扩展指定server利用Servlet容器提供的插件功能,自定义HttpSession的创建和管理策略,并通过配置的方式替换掉默认的策略.缺点:耦合Tomcat/Jetty等Serv ...
- Springboot Session集群处理
在集群环境下,常见的基于Session的身份认证就会有一个问题,因为Session是跟着服务器走的,当用户在服务器1登陆成功后,当用户在访问服务器2的时候会因为服务器2没有用户的身份信息而再次跳转到认 ...
- Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群
Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群 >>>>>>>>>>>>>> ...
- Tomcat session集群
author:JevonWei 版权声明:原创作品 环境 tomcatA 172.16.253.108 tomcatB 172.16.253.105 代理服务器 172.16.253.191 Tomc ...
- spring-session之四:Spring Session下的Redis存储结构
spring-session项目启动后 127.0.0.1:6379> keys * 1) "spring:session:index:org.springframework.sess ...
- (十)RabbitMQ消息队列-高可用集群部署实战
原文:(十)RabbitMQ消息队列-高可用集群部署实战 前几章讲到RabbitMQ单主机模式的搭建和使用,我们在实际生产环境中出于对性能还有可用性的考虑会采用集群的模式来部署RabbitMQ. Ra ...
- 【Spring Session】和 Redis 结合实现 Session 共享
[Spring Session]和 Redis 结合实现 Session 共享 参考官方文档 HttpSession with Redis Guide https://docs.spring.io/s ...
- Spring+quartz集群解决多服务器部署定时器重复执行的问题
一.问题描述 Spring自带的Task虽然能很好使用定时任务,只需要做些简单的配置就可以了.不过如果部署在多台服务器上的时候,这样定时任务会在每台服务器都会执行,造成重复执行. 二.解决方案 Spr ...
- Spring + Jedis集成Redis(集群redis数据库)
前段时间说过单例redis数据库的方法,但是生成环境一般不会使用,基本上都是集群redis数据库,所以这里说说集群redis的代码. 1.pom.xml引入jar <!--Redis--> ...
随机推荐
- Fiddler自动响应AutoResponder正则匹配
AutoResponder-Add-Rule Editor 两个文本框,先说第一个: Mathes: 前缀为“EXACT:”表示完全匹配(大小写敏感) 无前缀表示基本搜索,表示搜索到字符串就匹配 前缀 ...
- app 爬虫
https://mp.weixin.qq.com/s/ClYYfpvylQGlYYHDFBuKpA 唯品会舆情监控系统 姚彬炎 唯技术 2月26日
- MySql 里的IFNULL用法
IFNULL(expr1,expr2)的用法: 假如expr1 不为 NULL,则 IFNULL() 的返回值为 expr1; 否则其返回值为expr2.IFNULL()的返回值是数字或是字符串,具体 ...
- jmeter -- beanshell 执行本地py文件
Process proc = Runtime.getRuntime().exec("python /Users/lucax/Desktop/工作/Ai双师项目/性能优化迭代_脚本准备/获取学 ...
- java springmvc 前端 跨域问题
有个朋友在写扇贝插件的时候遇到了跨域问题.于是我对解决跨域问题的方式进行了一番探讨. 问题 API:查询单词URL: https://api.shanbay.com/bdc/search/?word= ...
- css调用字体 没装微软雅黑,用css写@font-face让其能显示微软雅黑字体
在设计布局网页时 经常想要用一些比较好看的字体,比如微软雅黑,这个字体在近年来在网页设计中运用越来越平常, 然而所使用的字体也只有自己能看到 到别的机子上 又恢复了原来的宋体神马的. 经过一位高手的提 ...
- iOS-UITabbar自定义
[self createCustomTabBar]; -(void)createCustomTabBar{ //创建一个UIImageView,作为底图 UIImageView *bgVi ...
- charles 文件菜单总结
本文参考:charles 文件菜单总结 一.file(文件菜单) 需要注意的是 "导入"和"导出"这个功能在和别人沟通的时候用, 比如你向第三方工具/类库开发人 ...
- python介绍,计算机组成。内存分布,进制,操作系统介绍
学习小方法 三个W一个Hwwwh:what(是什么) why(为什么) where(怎么用) how(如何用) 来思考知识点多练,多写,多敲代码增加熟练度与代码量 Python 是一门面向后台的编 ...
- 谈谈NPM和Webpack的关系
为什么有NPM: 当包引入数量很多时管理就成为了一个问题,这个就是npm为开发者行了方便之处,npm已经为你做好了依赖和版本的控制,也就是说使用npm可以让你从繁杂的依赖安装和版本冲突中解脱出来,进而 ...