GO 微服务GO-Micro(17)-消息的订阅和发布的示例实践

实践流程

查阅官方提供的示例,按官方的示例手动练习一次:

首先我们写一个pubsub的服务,用于我们的服务的订阅:

1).编写一个pubsub.proto

syntax = "proto3";

// Example message
message Event {
    // unique id
    string id = 1;
    // unix timestamp
    int64 timestamp = 2;
    // message
    string message = 3;
}

2)然后生成对应的文件信息:

protoc --proto_path=. --micro_out=. --go_out=. srv/proto/pubsub.proto

3)然后编写我们的微服务的启动的入口main.go(实现消息的消费):

package main

import (
    proto "gotodolist2/pubsub/srv/proto"
    "github.com/micro/go-micro/v2"
    "github.com/micro/go-micro/v2/metadata"
    "github.com/micro/go-micro/v2/server"
    "github.com/micro/go-micro/v2/util/log"


    "context"
)

// All methods of Sub will be executed when
// a message is received
type Sub struct{}

// Method can be of any name
func (s *Sub) Process(ctx context.Context, event *proto.Event) error {
    md, _ := metadata.FromContext(ctx)
    log.Logf("[pubsub.1] Received event %+v with metadata %+v\n", event, md)
    // do something with event
    return nil
}

// Alternatively a function can be used
func subEv(ctx context.Context, event *proto.Event) error {
    md, _ := metadata.FromContext(ctx)
    log.Logf("[pubsub.2] Received event %+v with metadata %+v\n", event, md)
    // do something with event
    return nil
}

func main() {
    // create a service
    service := micro.NewService(
        micro.Name("go.micro.srv.pubsub"),
    )
    // parse command line
    service.Init()

    // register subscriber
    micro.RegisterSubscriber("example.topic.pubsub.1", service.Server(), new(Sub))

    // register subscriber with queue, each message is delivered to a unique subscriber
    micro.RegisterSubscriber("example.topic.pubsub.2", service.Server(), subEv, server.SubscriberQueue("queue.pubsub"))

    if err := service.Run(); err != nil {
        log.Fatal(err)
    }
}

主要的中音的是实现主题的订阅:

micro.RegisterSubscriber("example.topic.pubsub.1", service.Server(), new(Sub))

    // register subscriber with queue, each message is delivered to a unique subscriber
    micro.RegisterSubscriber("example.topic.pubsub.2", service.Server(), subEv, server.SubscriberQueue("queue.pubsub"))

注意事项点:1. 引入的proto文件不能导入其他包的

4)编写一个测试cli,用于事件的发布(消息生产者)main.go:

package main

import (
    "fmt"
    //"gotodolist2/task-srv2/handler"
    "time"

    "context"
    proto "gotodolist2/pubsub/srv/proto"
    "github.com/micro/go-micro/v2"
    "github.com/micro/go-micro/v2/util/log"
    "github.com/pborman/uuid"
)

// send events using the publisher
func sendEv(topic string, p micro.Publisher) {
    t := time.NewTicker(time.Second)

    for _ = range t.C {
        // create new event
        ev := &proto.Event{
            Id:        uuid.NewUUID().String(),
            Timestamp: time.Now().Unix(),
            Message:   fmt.Sprintf("Messaging you all day on %s", topic),
        }

        log.Logf("publishing %+v\n", ev)

        // publish an event
        if err := p.Publish(context.Background(), ev); err != nil {
            log.Logf("error publishing: %v", err)
        }
    }
}

func main() {
    // create a service
    service := micro.NewService(
        micro.Name("go.micro.cli.pubsub"),
    )
    // parse command line
    service.Init()

    // create publisher
    pub1 := micro.NewPublisher("example.topic.pubsub.1", service.Client())
    //pub2 := micro.NewPublisher("example.topic.pubsub.2", service.Client())

    pub2 := micro.NewEvent("example.topic.pubsub.2", service.Client())

    // pub to topic 1
    go sendEv("example.topic.pubsub.1", pub1)
    // pub to topic 2
    go sendEv("example.topic.pubsub.2", pub2)

    // block forever
    select {}
}

注意点:


image.png

对应的事件对象创建不一样了!

5)启动这两个main.go:

  • cli-main.go生产者端的日志:
[id:"31b100e7-6214-11eb-8b08-40b07681cf65" timestamp:1611912443 message:"Messaging you all day on example.topic.pubsub.2" ]
2021-01-29 17:27:24  file=cli/main.go:27 level=info publishing %+v
[id:"3249a31d-6214-11eb-8b08-40b07681cf65" timestamp:1611912444 message:"Messaging you all day on example.topic.pubsub.2" ]
2021-01-29 17:27:24  file=cli/main.go:27 level=info publishing %+v
[id:"3249a31d-6214-11eb-8b09-40b07681cf65" timestamp:1611912444 message:"Messaging you all day on example.topic.pubsub.1" ]
2021-01-29 17:27:25  file=cli/main.go:27 level=info publishing %+v
[id:"32e1f826-6214-11eb-8b09-40b07681cf65" timestamp:1611912445 message:"Messaging you all day on example.topic.pubsub.2" ]
2021-01-29 17:27:25  file=cli/main.go:27 level=info publishing %+v
[id:"32e21f1b-6214-11eb-8b09-40b07681cf65" timestamp:1611912445 message:"Messaging you all day on example.topic.pubsub.1" ]
2021-01-29 17:27:26  file=cli/main.go:27 level=info publishing %+v
[id:"337ac234-6214-11eb-8b0a-40b07681cf65" timestamp:1611912446 message:"Messaging you all day on example.topic.pubsub.1" ]
2021-01-29 17:27:26  file=cli/main.go:27 level=info publishing %+v
[id:"337ac234-6214-11eb-8b09-40b07681cf65" timestamp:1611912446 message:"Messaging you all day on example.topic.pubsub.2" ]

  • pubsub-main.go消息消费端的日志:
2021-01-29 17:27:33  file=srv/main.go:21 level=info [pubsub.1] Received event %+v with metadata %+v
[id:"37a6ea9a-6214-11eb-8b0e-40b07681cf65" timestamp:1611912453 message:"Messaging you all day on example.topic.pubsub.1"  map[Micro-From-Service:go.micro.cli.pubsub Micro-Topic:example.topic.
pubsub.1]]
2021-01-29 17:27:33  file=srv/main.go:29 level=info [pubsub.2] Received event %+v with metadata %+v
[id:"37a6ea9a-6214-11eb-8b0d-40b07681cf65" timestamp:1611912453 message:"Messaging you all day on example.topic.pubsub.2"  map[Micro-From-Service:go.micro.cli.pubsub Micro-Topic:example.topic.
pubsub.2]]
2021-01-29 17:27:34  file=srv/main.go:29 level=info [pubsub.2] Received event %+v with metadata %+v
[id:"383f3fd0-6214-11eb-8b0e-40b07681cf65" timestamp:1611912454 message:"Messaging you all day on example.topic.pubsub.2"  map[Micro-From-Service:go.micro.cli.pubsub Micro-Topic:example.topic.
pubsub.2]]
2021-01-29 17:27:34  file=srv/main.go:21 level=info [pubsub.1] Received event %+v with metadata %+v
[id:"383f66bf-6214-11eb-8b0e-40b07681cf65" timestamp:1611912454 message:"Messaging you all day on example.topic.pubsub.1"  map[Micro-From-Service:go.micro.cli.pubsub Micro-Topic:example.topic.
pubsub.1]]
2021-01-29 17:27:35  file=srv/main.go:29 level=info [pubsub.2] Received event %+v with metadata %+v
[id:"38d7e427-6214-11eb-8b0e-40b07681cf65" timestamp:1611912455 message:"Messaging you all day on example.topic.pubsub.2"  map[Micro-From-Service:go.micro.cli.pubsub Micro-Topic:example.topic.
pubsub.2]]
2021-01-29 17:27:35  file=srv/main.go:21 level=info [pubsub.1] Received event %+v with metadata %+v
[id:"38d809df-6214-11eb-8b0e-40b07681cf65" timestamp:1611912455 message:"Messaging you all day on example.topic.pubsub.1"  map[Micro-From-Service:go.micro.cli.pubsub Micro-Topic:example.topic.
pubsub.1]]

你可能感兴趣的:(GO 微服务GO-Micro(17)-消息的订阅和发布的示例实践)