forked from real-jacket/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
39 lines (32 loc) · 821 Bytes
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main
import (
"context"
"time"
proto "github.com/micro-in-cn/tutorials/examples/basic-practices/micro-broker/nsq/proto"
"github.com/micro/go-micro/v2"
"github.com/micro/go-micro/v2/broker"
log "github.com/micro/go-micro/v2/logger"
"github.com/micro/go-plugins/broker/nsq/v2"
)
func main() {
srv := micro.NewService(
micro.Name("go.micro.broker.nsq.client"),
micro.Broker(nsq.NewBroker(
broker.Addrs([]string{"127.0.0.1:4150"}...),
)),
)
srv.Init()
pub := micro.NewEvent("go.micro.broker.topic.nsq", srv.Client())
go func() {
for i := 0; i < 10; i++ {
time.Sleep(1 * time.Second)
_ = pub.Publish(context.TODO(), &proto.DemoEvent{
Id: int32(i),
Current: time.Now().Unix(),
})
}
}()
if err := srv.Run(); err != nil {
log.Fatalf("error occurs: %v", err)
}
}