响应式编程基础教程:Spring Boot 与 Lettuce 整合
本文主要介绍响应式编程访问 Redis,以及 Spring Boot 与 Lettuce 的整合使用。
Lettuce 是可扩展性线程安全的 Redis 客户端,用于同步、异步和响应式使用。如果多个线程避免阻塞和事务性操作(例如 BLPOP 和 MULTI/EXEC),则它们可以共享一个连接。Lettuce 是基于 Netty 构建的。支持很多高级的Redis 特性。
根据 Spring Data Redis 2.0 的更新的消息显示,Spring Data Redis 不再支持 JRedis 的驱动,使用 Lettuce 来支持响应式连接,所以了解 Lettuce 的使用还是很有必要。
使用Reactive 驱动连接到Redis
无论使用什么库连接,必须要使用到 ReactiveRedisConnection
和 ReactiveRedisConnectionFactory
来操作 Redis 或者查询存活的连接。
Lettuce 支持 单机,Redis Sentinel、Redis Cluster 集群模式
ReactiveRedisConnection
是与 Redis 通信的核心组件, ReactiveRedisConnectionFactory
用于创建 ReativeRedisConnection
实例。
Spring Boot 整合Lettuce 使用
增加依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-reactive-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-reactive-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置 ReactiveRedisTemplate
@Configuration
public class LettuceConfig {
@Bean
ReactiveRedisTemplate<String, String> reactiveRedisTemplate(ReactiveRedisConnectionFactory reactiveRedisConnectionFactory) {
return new ReactiveRedisTemplate<>(reactiveRedisConnectionFactory, RedisSerializationContext.string());
}
}
ReactiveRedisTempalte 操作
@RestController
public class LettuceController {
@Autowired
private ReactiveRedisTemplate reactiveRedisTemplate;
@GetMapping("/put")
public Mono put(@RequestParam("key") String key, @RequestParam("val") String val) {
return reactiveRedisTemplate.opsForValue().set(key, val);
}
@GetMapping("/get")
public Mono<String> get(@RequestParam("key") String key) {
return reactiveRedisTemplate.opsForValue().get(key);
}
@GetMapping("/addList")
public Mono<Long> addList(@RequestParam("key") String key, @RequestParam("val") String val) {
return reactiveRedisTemplate.opsForList().rightPush(key, val);
}
@GetMapping("/getList")
public Flux<String> getList(@RequestParam("key") String key) {
return reactiveRedisTemplate.opsForList().range(key, 0L, Long.MAX_VALUE);
}
@GetMapping("/setHash")
public Mono<Boolean> setHash(@RequestParam("key") String key, @RequestParam("hashKey") String hashKey, @RequestParam("val") String val) {
return reactiveRedisTemplate.opsForHash().put(key, hashKey, val);
}
@GetMapping("/getHash")
public Flux<Map.Entry<String, String>> getHash(@RequestParam("key") String key) {
return reactiveRedisTemplate.opsForHash().entries(key);
}
}
通过 ReactiveRedisTemplate
操作 Redis 的 string, list, hash类型的使用, 大致的了解 Lettuce 的使用,还有很多其他操作的类型,可以通过官方文章自行查阅。
参考:
https://docs.spring.io/spring-data/redis/docs/2.5.4/reference/html/#redis:reactive
响应式编程基础教程:Spring Boot 与 Lettuce 整合的更多相关文章
- SpringBoot使用WebFlux响应式编程操作数据库
这一篇文章介绍SpringBoot使用WebFlux响应式编程操作MongoDb数据库. 前言 在之前一篇简单介绍了WebFlux响应式编程的操作,我们在来看一下下图,可以看到,在目前的Spring ...
- (转)Spring Boot 2 (十):Spring Boot 中的响应式编程和 WebFlux 入门
http://www.ityouknow.com/springboot/2019/02/12/spring-boot-webflux.html Spring 5.0 中发布了重量级组件 Webflux ...
- Spring Boot 2 (十):Spring Boot 中的响应式编程和 WebFlux 入门
Spring 5.0 中发布了重量级组件 Webflux,拉起了响应式编程的规模使用序幕. WebFlux 使用的场景是异步非阻塞的,使用 Webflux 作为系统解决方案,在大多数场景下可以提高系统 ...
- Spring Boot (十四): 响应式编程以及 Spring Boot Webflux 快速入门
1. 什么是响应式编程 在计算机中,响应式编程或反应式编程(英语:Reactive programming)是一种面向数据流和变化传播的编程范式.这意味着可以在编程语言中很方便地表达静态或动态的数据流 ...
- Spring 5 响应式编程
要点 Reactor 是一个运行在 Java8 之上的响应式流框架,它提供了一组响应式风格的 API 除了个别 API 上的区别,它的原理跟 RxJava 很相似 它是第四代响应式框架,支持操作融合, ...
- 浅谈Spring 5的响应式编程
这篇使用Spring 5进行响应式编程的入门文章展示了你现在可以使用的一些新的non-blocking, asynchronous.感谢优锐课老师给予的指导! 近年来,由于响应式编程能够以声明性的方式 ...
- WebFlux基础之响应式编程
上篇文章,我们简单的了解了WebFlux的一些基础与背景,并通过示例来写了一个demo.我们知道WebFlux是响应式的web框架,其特点之一就是可以通过函数式编程方式配置route.另外究竟什么是响 ...
- 什么是响应式编程——响应式Spring的道法术器
响应式编程之道 1.1 什么是响应式编程? 在开始讨论响应式编程(Reactive Programming)之前,先来看一个我们经常使用的一款堪称“响应式典范”的强大的生产力工具——电子表格. 举个简 ...
- 函数响应式编程(FRP)—基础概念篇
原文出处:http://ios.jobbole.com/86815/. 一函数响应式编程 说到函数响应式编程,就不得不提到函数式编程,他们俩有什么关系呢?今天我们就详细的解析一下他们的关系. 现在下面 ...
随机推荐
- 22、部署drdb
22.1.heartbeat部署规划: 本文的实验环境是虚拟机设备: 名称 接口 ip 用途 master-db(主) eth0 10.0.0.16/24 用于服务器之间的数据同步(直连) eth1 ...
- iOS 针对txt文档进行解码
如我上一篇文章记录,我加了打开其他APPtxt文件的小功能,紧接着碰到新问题了,我在测试过程中发现用户上传的TXT编码格式很多不单单是utf-8和gb2312,针对TXT文档进行解码,我一共经历过两个 ...
- 基于Yarp的http内网穿透库HttpMouse
简介 前几天发表了<基于Yarp实现内网http穿透>,当时刚刚从原理图变成了粗糙的代码实现,项目连名字都还没有,也没有开放源代码.在之后几天的时间,我不断地重构,朝着"可集成. ...
- mysql中函数cast使用
CAST函数语法规则是:Cast(字段名 as 转换的类型 ),其中类型可以为: CHAR[(N)] 字符型DATE 日期型DATETIME 日期和时间型DECIMAL float型SIGNED in ...
- Django基础-04篇 Django开发前后端联动
1. 写views views.py代码块 1.在前端以/article/{{ article.id }}这种方式请求后台, 参数配置在urls.py中path('category/<int:i ...
- python 遍历字典中的键和值
#遍历字典中的所有键和值 zd1={"姓名":"张三","年龄":20,"性别":"女"} zd2= ...
- SQL2008 合并多个结构相同的表的所有数据到新的表
select * into tikua from (select * from tiku20210303 union all select * from tiku) a
- [源码解析] 深度学习分布式训练框架 horovod (17) --- 弹性训练之容错
[源码解析] 深度学习分布式训练框架 horovod (17) --- 弹性训练之容错 目录 [源码解析] 深度学习分布式训练框架 horovod (17) --- 弹性训练之容错 0x00 摘要 0 ...
- SLAM的数学基础(3):几种常见的概率分布的实现及验证。
分布,在计算机学科里一般是指概率分布,是概率论的基本概念之一.分布反映的是随机或某个系统中的某个变量,它的取值的范围和规律. 常见的分布有:二项分布.泊松分布.正态分布.指数分布等,下面对它们进行一一 ...
- 网络损伤仪WANsim--不同的部署方式
网络损伤仪WANsim的业务口在逻辑上是不存在IP地址与MAC地址的,所以,WANsim可以串接在测试拓扑中的任意位置,只需要确保有流量通过WANsim即可. 不同的拓扑结构会对测试的结果造成影响.在 ...