dssqq82402 2019-01-15 15:54
浏览 949

使用Redis作为Docker容器之间的链接

I have a docker-compose file with several containers in, two of which are supposed to communicate via a Redis DB. Both containers have connections to Reids and I can read/write from both. However I'd like a container to be triggered every time the something is added from the other container. I thought I could accomplish this via Redis Sub/Pub but it when I run the code it never triggers anything, even when I can see I've added new items to the Redis queue.

From this I have two questions: 1. Is what I'm looking to do even possible? Can I do publish/subscribe in in two separate docker containers and expect it work as described above? 2. If it is possible, can someone please point me below where I"m going wrong with this tools?

This is my function that I add new data to the Redis queue and then publish the data in Docker container 1.

func redisShare(key string, value string) {
jobsQueue.Set(key, value, 0) //setting in the queue
jobsQueue.Publish(key, value) //publishing for the other docker container to notice
fmt.Println("added ", key, "with a value of ", value, "to the redis queue")
}

I'm using this line in my other docker container to subscribe to the Redis queue and listen for changes: redisdb.Subscribe()

I would expect if something was added to the redis queue it would share the data to the other container and I'd see the message received, but right now Docker Container 2 just runs and then closes.

Thanks!

  • 写回答

2条回答 默认 最新

  • dtrj74376 2019-01-15 16:47
    关注

    As from docs

    receiver.go

    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)
        }
    }
    

    sender.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)
            }
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app