响应式编程基础教程: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/. 一函数响应式编程 说到函数响应式编程,就不得不提到函数式编程,他们俩有什么关系呢?今天我们就详细的解析一下他们的关系. 现在下面 ...
随机推荐
- 13.7Cloneable接口
要点提示:Cloneable接口给出了一个可克隆对象.
- 如何消除inline-block元素之间的间隙?
一.问题现象 在CSS布局中,如果我们想要将多个行内块元素并排,会发现同行显示的inline-block元素之间会出现一定的空隙,这就是换行符/空格导致的,叫做换行符/空格间隙. 1 <!DOC ...
- hadoop学习(一)环境的搭建
1.安装几台Linux虚拟机.安装的过程就不赘述了,网上教程很多.win7系统上装了一个VMWare,因为一些原因,VMWare版本不是最新的,是VMWare7.1版本,由于VMWare版本不高,所以 ...
- linux 退出状态码
状态码 描述 0 命令成功结束 1 一般性未知错误 2 不适合的shell 命令 123 命令不可执行 127 没找到命令 128 无效退出参数 128+x 与linux信号x相关的严重错误 130 ...
- spring集成flyway
最近给公司项目集成flyway,由于我们项目移动端使用的是spring框架,网上看了很多博客,感觉这方面的东西还是很少的,毕竟现在是springboot的天下,大多数都是springboot集成fly ...
- TestComplete 64位和32位之间的区别
在64位系统上,有两种版本的TestComplete:32位和64位.本主题描述了TestComplete x64及其32位版本之间的区别.关于TestComplete x64启动TestComple ...
- gitlab配置邮箱服务
目录 1. SMTP服务 2. 服务端配置 3. 更新配置 4. 邮件测试 当需要进行 账号注册,创建项目,或合并分支等操作时,可通过邮件通知.邮件验证的方式实现. 1. SMTP服务 用于配置在服务 ...
- Linux中ftp服务器搭建
一.FTP工作原理 (1)FTP使用端口 [root@localhost ~]# cat /etc/services | grep ftp ftp-data 20/tcp #数据链路:端口20 ftp ...
- PYTHON 当前.PY文件名不能与引入的模块同名
当前文件名:sqlite3.py 文件引入import sqlite3 运行会出错,因为调用sqlite3的方法首先从当前文件找方法,当然找不到,所以会报错了
- ES6新增语法(七)——async...await
什么是async async的意思是"异步",顾名思义就是有关异步操作的关键字,async 是 ES7 才有的,与我们之前说的Promise.Generator有很大的关联. 使用 ...