printfcoder / stack-rpc-tutorials

Stack-RPC 中文示例、教程、资料,源码解读
https://stacklabs.cn
Apache License 2.0
1.69k stars 302 forks source link

有关broker的问题 #169

Closed bluedragonflyliu closed 4 years ago

bluedragonflyliu commented 5 years ago

我看到transport 有用到 nats的例子,但是broker没有nats的例子, examples 里的broker好像即使没安装RabbitMQ,和NATS 也能工作,但是如果指定使用nats呢?我尝试用go-micro/broker/nats 没能成功,不知道正确用法是什么样的。 1 .在192.168.1.3 上启动nats
nats-server -m 8222 2.在192.168.1.4和192.168.1.5 上运行修改后的 broker/consumer 和 broker/producer

   _ "github.com/micro/go-micro/broker/nats

    br := nats.NewBroker(broker.Addrs("nats://192.168.1.3:4222"))
if err := br.Connect(); err != nil {
    log.Fatalf("Broker Connect error: %v", err)
}
bluedragonflyliu commented 5 years ago

即便是同一个机器 consumer.go

package main

import ( "fmt" "log"

"github.com/micro/go-micro/broker"
"github.com/micro/go-micro/config/cmd"
// To enable rabbitmq plugin uncomment
_ "github.com/micro/go-micro/broker/nats"

)

var ( topic = "go.micro.topic.aaa" )

// Example of a shared subscription which receives a subset of messages func sharedSub() { _, err := broker.Subscribe(topic, func(p broker.Event) error { fmt.Println("[sub] received message:", string(p.Message().Body), "header", p.Message().Header) return nil }, broker.Queue("consumer")) if err != nil { fmt.Println(err) } }

// Example of a subscription which receives all the messages func sub() { _, err := broker.Subscribe(topic, func(p broker.Event) error { fmt.Println("[sub] received message:", string(p.Message().Body), "header", p.Message().Header) return nil }) if err != nil { fmt.Println(err) } }

func main() { cmd.Init() if err := broker.Init(); err != nil { log.Fatalf("Broker Init error: %v", err) } if err :=broker.Connect(); err != nil { log.Fatalf("Broker Connect error: %v", err) }

sub()
select {}

}

启动 go run consumer.go --broker=nats --broker_address=127.0.0.1:4222 producer.go package main

import ( "fmt" "log" "time"

"github.com/micro/go-micro/broker"
"github.com/micro/go-micro/config/cmd"
// To enable rabbitmq plugin uncomment
_ "github.com/micro/go-micro/broker/nats"

)

var ( topic = "go.micro.topic.aaa" )

func pub() { tick := time.NewTicker(time.Second) i := 0 for _ = range tick.C { msg := &broker.Message{ Header: map[string]string{ "id": fmt.Sprintf("%d", i), }, Body: []byte(fmt.Sprintf("%d: %s", i, time.Now().String())), } if err := broker.Publish(topic, msg); err != nil { log.Printf("[pub] failed: %v", err) } else { fmt.Println("[pub] pubbed message:", string(msg.Body)) } i++ } }

func main() { cmd.Init() if err := broker.Init(); err != nil { log.Fatalf("Broker Init error: %v", err) }

if err := broker.Connect(); err != nil {
    log.Fatalf("Broker Connect error: %v", err)
}

pub()

}

启动go run producer.go --broker=nats --broker_address=127.0.0.1:4222 能看到 producer.go 在发消息但是consumer却没接收 image

bluedragonflyliu commented 5 years ago

得到解决 需要引入 github.com/micro/go-plugins/broker/nats 而非 "github.com/micro/go-micro/broker/nats" 这两个有点混乱

printfcoder commented 5 years ago

混乱是指我们示例没有写好还是指?如果没有写好还请指示一下,我们好改进或者您帮忙修正一下

bluedragonflyliu commented 5 years ago

这个问题要从"github.com/micro/go-micro/config"说起,以前这个在go-plugins 搞得我问很久,各种实验,才知道换地方了,我还以为go-plugins 已经放弃不在使用了,于是在练习broker的时候也用了go-micro 里的broker 关键里面确实有而且还有nats,最关键的是还能生成消息,nats里也能看到,但是却不消费消息。昨天我看go-plugins仍在更新所以知道之前的理解有误并没放弃go-plugins我就尝试修改用go-plugins里的broker和nats 一实验成了,说起来都是泪啊。