响应式编程基础教程: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/. 一函数响应式编程 说到函数响应式编程,就不得不提到函数式编程,他们俩有什么关系呢?今天我们就详细的解析一下他们的关系. 现在下面 ...
随机推荐
- MySQL 删除devices表中id最大的一行
原想法:delete from devices where id in (select max(id) from devices); 报错:ERROR 1093 (HY000): You can't ...
- python 字符串 增、删、改、查基本操作
private static String TAG = "MainActivity"; private String str = " a,bB,1cCcc,2dDd d2 ...
- ELK处理Spring Boot 日志,妙!
在排查线上异常的过程中,查询日志总是必不可缺的一部分.现今大多采用的微服务架构,日志被分散在不同的机器上,使得日志的查询变得异常困难. 工欲善其事,必先利其器.如果此时有一个统一的实时日志分析平台,那 ...
- iOS基于AVFoundation实现朗读文字
iOS基于AVFoundation实现朗读文字 1.心理建设 众所周知AVFoundation的朗读是个智障语气,所以想不花钱就只能忍着. 2.speechManager @import AVFoun ...
- 洛谷 P4402 BZOJ1552 / 3506 [Cerc2007]robotic sort 机械排序
FHQ_Treap 太神辣 蒟蒻初学FHQ_Treap,于是来到了这道略显板子的题目 因为Treap既满足BST的性质,又满足Heap的性质,所以,对于这道题目,我们可以将以往随机出的额外权值转化为每 ...
- Django基础-02篇 Models的属性与字段
1.models字段类型 AutoField():一个IntegerField,根据可用ID自动递增.如果没指定主键,就创建它自动设置为主键. IntegerField():一个整数: FloatFi ...
- SHELL 变量一
SHELL变量分为三类:本地变量.环境变量和位置参数 变量存在三种基本结构:变量名.操作符(个人定义).变量值 比如:var=blue 变量名:var 操作符:= 变量值:blue 变量的设置规则: ...
- Spring 学习笔记(2) Spring Bean
一.IoC 容器 IoC 容器是 Spring 的核心,Spring 通过 IoC 容器来管理对象的实例化和初始化(这些对象就是 Spring Bean),以及对象从创建到销毁的整个生命周期.也就是管 ...
- C#.NET RSA 私钥签名 公钥验证签名
C#.NET RSA 私钥签名 公钥验证签名 公钥验签 1.待签名字符串转为byte数组时,一般使用UTF8. 2.将私钥字符串(PKCS8或PKCS1格式)转为C#.NET的RSACryptoSer ...
- python里的StringIO
Python2中StringIO调用方法如下: import StringIO iost = StringIO.StringIO() Python3中已将StringIO归入io,调用方法如下: im ...