micro / go-micro

A Go microservices framework
https://go-micro.dev
Apache License 2.0
21.86k stars 2.35k forks source link

Using rabbitmq as broker in service , fail to publish mq message with " service not found" #2695

Closed szhengli closed 2 months ago

szhengli commented 6 months ago

fail to publish mq, with the "service not found". can anyone help ? thank you.


package main

import ( "context" "fmt" mq "github.com/go-micro/plugins/v4/broker/rabbitmq" "github.com/go-micro/plugins/v4/registry/etcd" "log" "time"

"go-micro.dev/v4"
"go-micro.dev/v4/broker"
registry "go-micro.dev/v4/registry"

)

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

type Request struct { Name string json:"name" }

type Response struct { Message string json:"message" }

type Helloworld struct{}

func (h Helloworld) Greeting(ctx context.Context, req Request, rsp *Response) error { rsp.Message = "Hello " + req.Name fmt.Println("!!!!!!!!!!!!!")

msg := &broker.Message{
    Header: map[string]string{
        "id": fmt.Sprintf("%d", 1),
    },
    Body: []byte(fmt.Sprintf("%d: %s", 1, 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))
}
fmt.Println("!!!!!!!!!!!!!")
return nil

}

func main() {

edcdRegistry := etcd.NewRegistry(registry.Addrs("192.168.2.89:2379"))
mqbroker := mq.NewBroker(broker.Addrs("amqp://guest:guest@192.168.2.207:5672/"))

service := micro.NewService(
    micro.Name("helloworld"),
    micro.Handle(new(Helloworld)),
    micro.Address(":8080"),
    micro.Registry(edcdRegistry),
)
service.Init(micro.Broker(mqbroker))

service.Run()

}

asim commented 2 months ago

Please fix the formatting off your code. It's unreadable. You are using an etcd registry and rabbitmq. Both publisher and consumer will need to be using the same configuration.