私信  •  关注

Aleksandrs Antonovs

Aleksandrs Antonovs 最近创建的主题
Aleksandrs Antonovs 最近回复了
5 年前
回复了 Aleksandrs Antonovs 创建的主题 » 使用Redis作为Docker容器之间的链接

docs

接收器。开始

package main

import (
    "fmt"

    "github.com/go-redis/redis"
)

func main() {
    c := redis.NewClient(&redis.Options{
        Addr: ":6379",
    })

    pubsub := c.Subscribe("mychannel1")

    // Wait for confirmation that subscription is created before publishing anything.
    _, err := pubsub.Receive()
    if err != nil {
        panic(err)
    }

    // Go channel which receives messages.
    ch := pubsub.Channel()

    // Consume messages.
    for msg := range ch {
        fmt.Println(msg.Channel, msg.Payload)
    }
}

发件人.go

package main

import (
    "time"

    "github.com/go-redis/redis"
)

func main() {
    c := redis.NewClient(&redis.Options{
        Addr: ":6379",
    })

    // Publish a message.
    for range time.Tick(time.Second) {
        err := c.Publish("mychannel1", "hello").Err()
        if err != nil {
            panic(err)
        }
    }
}