• 响应式编程基础教程: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

    无论使用什么库连接,必须要使用到 ReactiveRedisConnectionReactiveRedisConnectionFactory 来操作 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

  • 相关阅读:
    Windows 8.1 应用再出发
    Windows 8.1 应用再出发
    python 列表,字典,元组,字符串,常用函数
    python 排序 sort和sorted
    python中的zip、map、reduce 、lambda、filter函数的使用
    SecureCRT的安装与破解(过程很详细!!!)
    L1和L2正则
    神经网络,机器学习公开课
    待整理
    tensorflow中文教程
  • 原文地址:https://www.cnblogs.com/lzeffort/p/15150291.html
Copyright © 2020-2023  润新知