mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-16 13:04:34 +00:00
Moving cruftier services that are only used for tests under a test repo
This commit is contained in:
50
test/pubsub/main.go
Normal file
50
test/pubsub/main.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/micro/micro/v3/service"
|
||||
"github.com/micro/micro/v3/service/logger"
|
||||
pb "github.com/micro/services/pubsub/proto"
|
||||
)
|
||||
|
||||
// Pub will publish messages every second
|
||||
func Pub() {
|
||||
ev := service.NewEvent("messages")
|
||||
|
||||
for {
|
||||
err := ev.Publish(context.TODO(), &pb.Message{
|
||||
Id: "1",
|
||||
Body: []byte(`hello`),
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error("Error Publish:", err.Error())
|
||||
}
|
||||
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
// Sub processes messages
|
||||
func Sub(ctx context.Context, msg *pb.Message) error {
|
||||
fmt.Println("Received a message")
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
service := service.New(
|
||||
service.Name("pubsub"),
|
||||
)
|
||||
|
||||
// subscribe to the "messages" topic
|
||||
service.Subscribe("messages", Sub)
|
||||
|
||||
// publish messages
|
||||
go Pub()
|
||||
|
||||
if err := service.Run(); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user