stack-labs / questions

任何关于Micro的问题都可以在Issue中创建
27 stars 6 forks source link

go-micro 和nats-streaming #20

Closed scfobao closed 4 years ago

scfobao commented 4 years ago

和nats没有问题,但跟nats-streaming跑有问题

package main

import (
    "fmt"
    "log"

    "github.com/micro/go-micro/broker"
    "github.com/micro/go-plugins/broker/stan"
    stancf "github.com/nats-io/stan.go"

)

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

// 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.Printf("test=%v", 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() {

    opts := stancf.GetDefaultOptions()
    opts.NatsURL = "xxx:XXX@XXXXXX:4222"
    na := stan.NewBroker(stan.ClusterID("test-cluster"), stan.Options(opts))

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

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

错误提示: missing service port 不知道什么原因,请大佬帮忙看看

printfcoder commented 4 years ago

broker在使用前需要初始化:

    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)
    }
scfobao commented 4 years ago

@printfcoder 可以了。谢谢