NATS
NATS-Server 基于docker compose部署 1 2 3 4 5 6 7 8 services: nats-main: image: nats:latest ports: - "4222:4222" - "6222:6222" - "8222:8222" restart: unless-stopped Go-NATS 引入依赖 1 go get github.com/nats-io/nats.go 发布订阅模式 在此模式下,发送者发送消息后,所有在线订阅者都能接收到消息 sub.go 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 package main import "github.com/nats-io/nats.go" func main() { conn, err := nats.Connect("nats://ip:4222") if err != nil { return } for i := 1; i <= 2; i++ { dummy := i conn.Subscribe("hello", func(msg *nats.Msg) { fmt.Printf("消费者[%d]收到:%s\n", dummy, string(msg.Data)) }) } select {} } pub.go ...