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

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?