connect and publish
This commit is contained in:
@@ -1,59 +1,36 @@
|
||||
package main
|
||||
package pub_client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/seaweedfs/seaweedfs/weed/pb"
|
||||
cmap "github.com/orcaman/concurrent-map/v2"
|
||||
"github.com/rdleal/intervalst/interval"
|
||||
"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)
|
||||
}
|
||||
|
||||
type PublisherConfiguration struct {
|
||||
}
|
||||
type TopicPublisher struct {
|
||||
namespace string
|
||||
topic string
|
||||
partition2Broker *interval.SearchTree[string, int32]
|
||||
broker2PublishClient cmap.ConcurrentMap[string, mq_pb.SeaweedMessaging_PublishClient]
|
||||
}
|
||||
|
||||
func NewTopicPublisher(namespace, topic string) *TopicPublisher {
|
||||
return &TopicPublisher{
|
||||
namespace: namespace,
|
||||
topic: topic,
|
||||
partition2Broker: interval.NewSearchTree[string](func(a, b int32) int {
|
||||
return int(a - b)
|
||||
}),
|
||||
broker2PublishClient: cmap.New[mq_pb.SeaweedMessaging_PublishClient](),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *TopicPublisher) Connect(bootstrapBroker string) error {
|
||||
if err := p.doLookup(bootstrapBroker, grpc.WithTransportCredentials(insecure.NewCredentials())); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user