響應式編程基礎教程: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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章