[英]Using Redis as a Link Between Docker Containers
I have a docker-compose file with several containers in, two of which are supposed to communicate via a Redis DB. 我有一个包含几个容器的docker-compose文件,其中两个应该通过Redis DB进行通信。 Both containers have connections to Reids and I can read/write from both.
这两个容器都有到Reids的连接,我可以从两者读取/写入。 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.
我以为可以通过Redis Sub / Pub完成此操作,但是当我运行代码时,它永远不会触发任何操作,即使我看到我已经向Redis队列添加了新项目。
From this I have two questions: 1. Is what I'm looking to do even possible? 由此,我有两个问题:1.我想要做的事情是否可能? Can I do publish/subscribe in in two separate docker containers and expect it work as described above?
我可以在两个单独的Docker容器中进行发布/订阅,并期望它能够如上所述工作吗? 2. If it is possible, can someone please point me below where I"m going wrong with this tools?
2.如果有可能,有人可以指出我该工具出问题的地方吗?
This is my function that I add new data to the Redis queue and then publish the data in Docker container 1. 这是我将新数据添加到Redis队列,然后将数据发布到Docker容器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()
我在其他Docker容器中使用此行来订阅Redis队列并侦听更改:
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. 我希望如果将某些内容添加到redis队列中,它将与其他容器共享数据,并且我会看到收到的消息,但是现在Docker Container 2才运行然后关闭。
Thanks! 谢谢!
Just in case anyone else wonders about the answer: I ended up using a combination of both Aleksandrs and sui's answers. 以防万一其他人对答案感到疑惑:我最终使用了Aleksandrs和sui的答案。 In my first Docker container I published the results to a specific channel:
在我的第一个Docker容器中,我将结果发布到了特定的频道:
publishData := redisdb.Subscribe("CHANNELNAME")
And then in my second Docker container that was subscribing to the channel, thanks to sui for the assistance on this part, I subscribed to the channel and pulled the both the UID and the IP information like so: 然后在订阅该频道的第二个Docker容器中,感谢sui在这一部分的帮助,我订阅了该频道并像这样提取了UID和IP信息:
ch := pubsub.Channel()
for msg := range ch {
fmt.Println(msg.Payload)
s := strings.Split(msg.Payload, ":")
UID, IP := s[0], s[1]
fmt.Println(UID, IP)
}
This is working great for me so far - thanks so much to sui and Aleksandrs for the assistance! 到目前为止,这对我来说非常有效-非常感谢sui和Aleksandrs的帮助!
receiver.go 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 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)
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.