Stream

Redis Stream 是 Redis 5.0 版本新增加的數據結構。 Redis Stream 主要用於消息隊列(MQ,Message Queue),Redis 本身是有一個 Redis 發布訂閱 (pub/sub) 來實現消息隊列的功能,但它有個缺點就是消息無法持久化,如果出現網絡斷開、Redis 宕機等,消息就會被丟棄。

以下都是我根據國人的教學再重新寫一遍,做一些調整轉換成自己的東西。 完整內容可以去GitHub上面找。

build.gradle > dependencies

implementation 'org.springframework.boot:spring-boot-starter-data-redis-reactive'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'

// https://mvnrepository.com/artifact/org.apache.commons/commons-pool2
implementation 'org.apache.commons:commons-pool2:2.11.1'

// https://mvnrepository.com/artifact/com.github.javafaker/javafaker
implementation 'com.github.javafaker:javafaker:1.0.2'

yml

spring:
  application:
    name: WebStorageSystemRestApi

  redis:
    host: 127.0.0.1
    port: 6379
    password: cPKGpSGvky
    database: 0
    lettuce:
      pool:
        max-wait: 1s
        min-idle: 3
        max-active: 8
    timeout: 2s

config

stream listener

stream configuration

consumer

producer

entity

spring boot application runner

spring boot schedule

initializer redis stream

console log

參考資料

Last updated