Go에서 Redis 7의 새로운 샤딩된 Pub/Sub 사용

Redis 7.0 RC1은 2022/1/31에 출시됩니다. 그리고 새로운Sharded Pub/Sub 기능이 제공됩니다.

Sharded pubsub helps to scale the usage of pubsub in cluster mode. It restricts the propagation of message to be within the shard of a cluster. Hence, the amount of data passing through the cluster bus is limited in comparison to global pubsub where each message propagates to each node in the cluster. This allows users to horizontally scale the pubsub usage by adding more shards.



이제 사용자는 새로운SPUBLISH 명령을 사용하여 전체 Redis 클러스터를 압도할 걱정 없이 메시지를 게시할 수 있습니다.

그리고 SSUBSCRIBE 명령을 사용하여 해당 메시지를 수신하십시오.

다음은 고성능 RESP3 go 클라이언트 라이브러리인 rueidis 과 함께 이 새로운 기능을 사용하는 방법을 보여주는 예입니다.

package main

import (
    "context"
    "fmt"
    "time"

    "github.com/rueian/rueidis"
)

func main() {
    client, _ := rueidis.NewClient(rueidis.ClientOption{
        InitAddress: []string{"127.0.0.1:7001", "127.0.0.1:7002", "127.0.0.1:7003"},
    })

    ctx := context.Background()

    go func() {
        for {
            client.Do(ctx, client.B().Spublish().Channel("ch").Message("hi").Build())
            time.Sleep(time.Second)
        }
    }()

    client.Receive(ctx, client.B().Ssubscribe().Channel("ch").Build(), func(msg rueidis.PubSubMessage) {
        fmt.Println(msg.Channel, msg.Message)
    })
}



위의 스니펫은 다음을 수행합니다.
  • Redis 클러스터 주소로 클라이언트를 초기화합니다.
  • 매초 수행할 고루틴을 시작합니다SPUBLISH.
  • client.Receive 명령 및 콜백 기능과 함께 SSUBSCRIBE를 사용하여 수신된 메시지를 계속 처리합니다.
  • client.Receivectx가 완료되거나 클라이언트가 채널에서 구독 취소될 때까지 영원히 차단됩니다.

    그게 다야! rueidis 에 질문이나 문제가 있으면 Github에서 저에게 연락하십시오.

    좋은 웹페이지 즐겨찾기