require (
github.com/dreaminglwj/rocketmq-client-go v2.0.0-alpha1
)
Go Client define the Logger
interface for log output, user can specify implementation of private.
in default, client use logrus
.
rlog.SetLogger(Logger)
Producer interface {
Start() error
Shutdown() error
SendSync(context.Context, *primitive.Message) (*internal.SendResult, error)
SendOneWay(context.Context, *primitive.Message) error
}
- create a new
Producer
instance
opt := producer.ProducerOptions{
NameServerAddr: "127.0.0.1:9876",
RetryTimesWhenSendFailed: 2,
}
p := producer.NewProducer(opt)
- start the producer
err := p.Start()
- send message with sync
result, err := p.SendSync(context.Background(), &primitive.Message{
Topic: "test",
Body: []byte("Hello RocketMQ Go Client!"),
})
// do something with result
- or send message with oneway
err := p.SendOneWay(context.Background(), &primitive.Message{
Topic: "test",
Body: []byte("Hello RocketMQ Go Client!"),
})
Full examples: producer
alpha1 only support PushConsumer
PushConsumer interface {
Start() error
Shutdown()
Subscribe(topic string, selector MessageSelector,
f func(*ConsumeMessageContext, []*primitive.MessageExt) (ConsumeResult, error)) error
}
- Create a
PushConsumer
instance
c := consumer.NewPushConsumer("testGroup", consumer.ConsumerOption{
NameServerAddr: "127.0.0.1:9876",
ConsumerModel: consumer.Clustering,
FromWhere: consumer.ConsumeFromFirstOffset,
})
- Subscribe a topic(only support one topic now), and define your consuming function
err := c.Subscribe("test", consumer.MessageSelector{}, func(ctx *consumer.ConsumeMessageContext,
msgs []*primitive.MessageExt) (consumer.ConsumeResult, error) {
fmt.Println(msgs)
return consumer.ConsumeSuccess, nil
})
- start the consumer(NOTE: MUST after subscribe)
Full examples: consumer