响应式久草编程基础教程:久草Spring Boot 与 Lettuce 在线整合「建议收藏」

响应式久草编程基础教程:久草Spring Boot 与 Lettuce 在线整合「建议收藏」本文主要介绍响应式编程访问Redis,以及SpringBoot与Lettuce的整合使用。Lettuce是可扩展性线程安全的Redis客户端,用于同步、异步和响应式使用。如果多个线程避免阻塞和事务性操作(例如BLPOP和MULTI/EXEC),则它们可以共享一个连接。Lettuce是基于Netty构建的。支持很多高级的Redis特性。根据SpringDataRedis2.0的更新的消息显示,SpringDataRedis不再支持JRedis的驱动,

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

本文主要介绍响应式编程访问 Redis,以及 Spring Boot 与 Lettuce 的整合使用。

Lettuce 是可扩展性线程安全的 Redis 客户端,用于同步、异步和响应式使用。如果多个线程避免阻塞和事务性操作(例如 BLPOP 和 MULTI/EXEC),则它们可以共享一个连接。Lettuce 是基于 Netty 构建的。支持很多高级的Redis 特性。

根据 Spring Data Redis 2.0 的更新的消息显示,Spring Data Redis 不再支持 JRedis 的驱动,使用 Lettuce 来支持响应式连接,所以了解 Lettuce 的使用还是很有必要。

响应式久草编程基础教程:久草Spring Boot 与 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://javaforall.net/180223.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 硬核!用Mac Mini翻新了一台12年前的电脑

    硬核!用Mac Mini翻新了一台12年前的电脑本文转载自差评君有这么一位差友,不知道从哪儿加到了托尼的微信,都还没打招呼呢,上来就向我展现了他最近的硬核杰作。他将M1款的MacMini塞进了09年款27寸iMac,并把详细过程一股脑发给了我。尽管不清楚这样做有什么意义,但确实是做的很硬核,手法也相当专业,简单来说,他愣是把一台新电脑,塞进了一台老电脑。。。整理了一下他发过来的内容,大致弄清了这事儿的前因后果。把M1款的Macmini,塞进了09年iMac的想法,是他在看到了油管上有一位叫L…

    2022年5月30日
    88
  • curl转让query string逃生参数

    curl转让query string逃生参数

    2022年1月6日
    50
  • 我的世界服务器指令大全电脑版_我的世界服务器专用指令

    我的世界服务器指令大全电脑版_我的世界服务器专用指令要成为一个合格的服主,熟悉我的世界服务器指令是必须的,服务器内指令的各种功能不仅是OP需要使用,还有部分是玩家也需要知道的,下面就看看小编为大家准备的我的世界服务器指令大全吧。【大全】我的世界服务器指令大全:首先/manuaddxxgm?如果要使用这个命令,需要自己先有权限在控制台输入manuaddxxadmin然后添加sethome权限manselect世界名字(默认world)输入m…

    2022年9月23日
    3
  • docker里面什么emule比较好_emule 服务器优先

    docker里面什么emule比较好_emule 服务器优先{“moduleinfo”:{“card_count”:[{“count_phone”:1,”count”:1}],”search_count”:[{“count_phone”:6,”count”:6}]},”card”:[{“des”:”云服务器ECS(ElasticComputeService)是一种弹性可伸缩的计算服务,助您降低IT成本,提升运维效率,使您更专注于核心业务创新。”,…

    2022年6月17日
    25
  • GoogleMaps_键盘网站

    GoogleMaps_键盘网站在Google地球中使用键盘/鼠标导航首先要明白导航过程中的三个中心,视野中心,相机视角,鼠标锁定位置。还要明白3D视图和俯视图、地平面视图的区别,因为在海拔为0时将进入地平面视图,上下的操作将变为拉近和推远。中间的位置为视野中心,可以通过Ctrl+Shif+左箭头/右箭头来触发显示,如果要展示的对象不在视野中心,可以通过Alt+左箭头/右箭头进行对象位置微调。-/+的中心为视野中心。相机视角可以通过Ctrl触发,为可以通过左箭头/右箭头控制水平方向旋转,上箭头/下箭头控制上下方向旋

    2022年9月2日
    5
  • js原生、jquery单选框radio总结(获取值、设置默认选中值、样式)

    js原生、jquery单选框radio总结(获取值、设置默认选中值、样式)单选框radio&lt;divclass="radio-inline"&gt;&lt;inputtype="radio"name="killOrder"value="1"/&gt;&lt;labelfor="killOrder1"&gt;是&lt;/label&gt;&lt;/div&gt;&

    2022年5月30日
    227

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号