pub sub initial tests
This commit is contained in:
@@ -1,5 +1,59 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
err := pb.WithBrokerGrpcClient(true,
|
||||
"localhost:17777",
|
||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
func(client mq_pb.SeaweedMessagingClient) error {
|
||||
pubClient, err := client.Publish(context.Background())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if initErr := pubClient.Send(&mq_pb.PublishRequest{
|
||||
Message: &mq_pb.PublishRequest_Init{
|
||||
Init: &mq_pb.PublishRequest_InitMessage{
|
||||
Topic: &mq_pb.Topic{
|
||||
Namespace: "test",
|
||||
Name: "test",
|
||||
},
|
||||
Partition: &mq_pb.Partition{
|
||||
RangeStart: 0,
|
||||
RangeStop: 1,
|
||||
RingSize: 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
}); initErr != nil {
|
||||
return initErr
|
||||
}
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
if dataErr := pubClient.Send(&mq_pb.PublishRequest{
|
||||
Message: &mq_pb.PublishRequest_Data{
|
||||
Data: &mq_pb.DataMessage{
|
||||
Key: []byte(fmt.Sprintf("key-%d", i)),
|
||||
Value: []byte(fmt.Sprintf("value-%d", i)),
|
||||
},
|
||||
},
|
||||
}); dataErr != nil {
|
||||
return dataErr
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
50
weed/mq/client/sub_client/subscriber.go
Normal file
50
weed/mq/client/sub_client/subscriber.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
)
|
||||
|
||||
func main() {
|
||||
err := pb.WithBrokerGrpcClient(true,
|
||||
"localhost:17777",
|
||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
func(client mq_pb.SeaweedMessagingClient) error {
|
||||
subClient, err := client.Subscribe(context.Background(), &mq_pb.SubscribeRequest{
|
||||
Init: &mq_pb.SubscribeRequest_InitMessage{
|
||||
Topic: &mq_pb.Topic{
|
||||
Namespace: "test",
|
||||
Name: "test",
|
||||
},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for {
|
||||
resp, err := subClient.Recv()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resp.GetCtrl() != nil {
|
||||
if resp.GetCtrl().Error != "" {
|
||||
return fmt.Errorf("ctrl error: %v", resp.GetCtrl().Error)
|
||||
}
|
||||
}
|
||||
if resp.GetData() != nil {
|
||||
println(string(resp.GetData().Key), "=>", string(resp.GetData().Value))
|
||||
}
|
||||
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user