1.引入spring-boot-starter-data-redisjar
包,注意spring boot 2.1 没有对应的spring-boot-starter-redis
版本,改名为spring-boot-starter-data-redis
。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.在application中添加redis配置
spring:
redis:
host: 192.168.187.11
port: 6379
3.测试
redis客户端查看,没有任何key
192.168.187.11:6379> keys *
(empty list or set)
@RestController
@RequestMapping(value = "/string")
public class RedisStringController {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@PutMapping(value = "/put")
public void put(String key, @RequestParam(required = false, defaultValue = "default") String value) {
stringRedisTemplate.opsForValue().set(key, value);
}
@GetMapping(value = "/get")
public Object get(String key) {
return stringRedisTemplate.opsForValue().get(key);
}
}
使用postman送请求: localhost:8080/string/put?key=hello&value=world
localhost:8080/string/get?key=hello
在上述使用中,是无法存储对象的,存储对象的话需要使用RedisTemplate并且要使用响应的序列化机制,下面我们就来测试下:
1.引入序列化的jar包,这里我们是使用jackson
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
2.添加redis配置类
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
//使用StringRedisSerializer来序列化和反序列化redis的ke
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
//开启事务
redisTemplate.setEnableTransactionSupport(true);
redisTemplate.setConnectionFactory(redisConnectionFactory);
return redisTemplate;
}
}
3.添加测试controller
@RestController
@RequestMapping(value = "/object")
public class RedisObjectController {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@GetMapping("/get/{username}")
public Object get(@PathVariable String username) {
return redisTemplate.opsForValue().get(username);
}
@PutMapping("/put")
public void put(String username, String nickname) {
User user = new User(username, nickname);
redisTemplate.opsForValue().set(username, user);
}
}
4.使用postman测试
使用redis客户端查看
192.168.187.11:6379> get qianjiangtao
"{\"@class\":\"com.lvmama.tony.model.User\",\"username\":\"qianjiangtao\",\"nickname\":\"Tony-J\"}"
我们都知道spring boot自动化配置中的配置都是通过spring-configuration-metadata.json来约束的,同理redis也是这样的,我们配置了spring.redis.host,不妨来找下这个配置项
{
"name": "spring.redis.host",
"type": "java.lang.String",
"description": "Redis server host.",
"sourceType": "org.springframework.boot.autoconfigure.data.redis.RedisProperties",
"defaultValue": "localhost"
}
从这能看出来redis的配置都是通过RedisProperties这个类来配置的,在这里面我们能看到众多的redis配置及默认配置,我们可以从一个入口切入,就拿port切入,来看下port在哪设置的
org.springframework.boot.autoconfigure.data.redis.RedisConnectionConfiguration#getStandaloneConfig
看出在RedisConnectionConfiguration中的getStandaloneConfig中赋值的,那这个方法又是谁调用的呢?继续找?
从图中能看出来有两个地方可能会调用,从类的名字能看出来,spring boot是支持Jedis和Lettuce两种客户端来操作redis,那到底是用哪个呢? 都看看呗
从图中截取的源码中能看出来,我是使用了LettuceConnectionConfiguration
,看注解是我引入了RedisClient
,我什么时候引入的?于是我就看看maven的依赖
从maven依赖中能看出一些重要的信息:
如何验证呢?不能瞎说
要想知道很简单的,在我们自己写的RedisConfig
中打下断点,看看用的RedisConnectionFactory
到底是不是LettuceConnectionFactory
就能证明了
果然如此!
简单的流程就是:
1.spring boot通过application配置加载redis配置
2.解析封装成RedisProperties
3.根据@ConditionalOnClass
判断使用哪个Redis客户端,封装成LettuceClientConfiguration
并创建LettuceConnectionFactory
4.通过@Bean创建我们自己的配置类在LettuceConnectionFactory
基础上添加我们自己自定义的配置
Original url: Access
Created at: 2019-12-12 17:40:36
Category: default
Tags: none
未标明原创文章均为采集,版权归作者所有,转载无需和我联系,请注明原出处,南摩阿彌陀佛,知识,不只知道,要得到
java windows火焰图_mob64ca12ec8020的技术博客_51CTO博客 - 在windows下不可行,不知道作者是怎样搞的 监听SpringBoot 服务启动成功事件并打印信息_监听springboot启动完毕-CSDN博客 SpringBoot中就绪探针和存活探针_management.endpoint.health.probes.enabled-CSDN博客 u2u转换板 - 嘉立创EDA开源硬件平台 Spring Boot 项目的轻量级 HTTP 客户端 retrofit 框架,快来试试它!_Java精选-CSDN博客 手把手教你打造一套最牛的知识笔记管理系统! - 知乎 - 想法有重合-理论可参考 安宇雨 闲鱼 机械键盘 客制化 开贴记录 文本 linux 使用find命令查找包含某字符串的文件_beijihukk的博客-CSDN博客_find 查找字符串 ---- mac 也适用 安宇雨 打字音 记录集合 B站 bilibili 自行搭建 开坑 真正的客制化 安宇雨 黑苹果开坑 查找工具包maven pom 引用地 工具网站 Dantelis 介绍的玩轴入坑攻略 --- 关于轴的一些说法 --- 非官方 ---- 心得而已 --- 长期开坑更新 [本人问题][新开坑位]关于自动化测试的工具与平台应用 机械键盘 开团 网站记录 -- 能做一个收集的程序就好了 不过现在没时间 -- 信息大多是在群里发的 - 你要让垃圾佬 都去一个地方看难度也是很大的 精神支柱 [超级前台]sprinbboot maven superdesk-app 记录 [信息有用] [环境准备] [基本完成] [sebp/elk] 给已创建的Docker容器增加新的端口映射 - qq_30599553的博客 - CSDN博客 [正在研究] Elasticsearch, Logstash, Kibana (ELK) Docker image documentation elasticsearch centos 安装记录 及 启动手记 正式服务器 39 elasticsearch 问题合集 不断更新 6.1.1 | 6.5.1 两个版本 博客程序 - 测试 - bug记录 等等问题 laravel的启动过程解析 - lpfuture - 博客园 OAuth2 Server PHP 用 Laravel 搭建带 OAuth2 验证的 RESTful 服务 | Laravel China 社区 - 高品质的 Laravel 和 PHP 开发者社区 利用Laravel 搭建oauth2 API接口 附 Unauthenticated 解决办法 - 煮茶的博客 - SegmentFault 思否 使用 OAuth2-Server-php 搭建 OAuth2 Server - 午时的海 - 博客园 基于PHP构建OAuth 2.0 服务端 认证平台 - Endv - 博客园 Laravel 的 Artisan 命令行工具 Laravel 的文件系统和云存储功能集成 浅谈Chromium中的设计模式--终--Observer模式 浅谈Chromium中的设计模式--二--pre/post和Delegate模式 浅谈Chromium中的设计模式--一--Chromium中模块分层和进程模型 DeepMind 4 Hacking Yourself README.md update 20211011
Laravel China 简书 知乎 博客园 CSDN博客 开源中国 Go Further Ryan是菜鸟 | LNMP技术栈笔记 云栖社区-阿里云 Netflix技术博客 Techie Delight Linkedin技术博客 Dropbox技术博客 Facebook技术博客 淘宝中间件团队 美团技术博客 360技术博客 古巷博客 - 一个专注于分享的不正常博客 软件测试知识传播 - 测试窝 有赞技术团队 阮一峰 语雀 静觅丨崔庆才的个人博客 软件测试从业者综合能力提升 - isTester IBM Java 开发 使用开放 Java 生态系统开发现代应用程序 pengdai 一个强大的博主 HTML5资源教程 | 分享HTML5开发资源和开发教程 蘑菇博客 - 专注于技术分享的博客平台 个人博客-leapMie 流星007 CSDN博客 - 舍其小伙伴 稀土掘金 Go 技术论坛 | Golang / Go 语言中国知识社区
最新评论