Apache RocketMQ client wrapper with simplified producer and consumer interfaces for Go.
π Simplified API: Clean producer and consumer API for RocketMQ message queue operations π Auto Resolution: Automatic hostname resolution with IPv4 address support π‘οΈ Seamless Errors: Elegant error handling with mate ecosystem integration π Go Practices: Interface design following Go best practices π Auto Pulling: Automatic message pulling with callback-based message handling βοΈ Configurable: Timeout and retry settings for message sending operations
go get github.com/go-xlan/go-rocketmq
- Go 1.23.0 or higher
- Apache RocketMQ server instance (NameServer accessible)
This example shows how to create both a producer and consumer in the same program.
package main
import (
"context"
"time"
"github.com/apache/rocketmq-client-go/v2/consumer"
"github.com/apache/rocketmq-client-go/v2/primitive"
"github.com/go-xlan/go-rocketmq/rocketmq"
"github.com/yyle88/must"
"github.com/yyle88/neatjson/neatjsons"
"github.com/yyle88/rese"
"github.com/yyle88/zaplog"
)
func main() {
config := &rocketmq.Config{
NameServerAddress: "127.0.0.1:9876",
GroupName: "TestGroup",
ProducerOptions: &rocketmq.ProducerOptions{
SendMessageTimeout: 3 * time.Second,
RetryAttempts: 3,
},
}
const topic = "TestTopic"
consumerCli := rese.P1(rocketmq.NewConsumer(config))
defer rese.F0(consumerCli.Close)
must.Done(consumerCli.StartSubscribe(topic, func(message *primitive.MessageExt) (consumer.ConsumeResult, error) {
zaplog.SUG.Debugln("consume message body:", string(message.Body))
return consumer.ConsumeSuccess, nil
}))
producerCli := rese.P1(rocketmq.NewProducer(config))
defer rese.F0(producerCli.Close)
for idx := 0; idx < 10000; idx++ {
type MessagePayload struct {
Name string `json:"name"`
SequenceNo int64 `json:"sequenceNo"`
}
payload := neatjsons.S(&MessagePayload{
Name: "demo",
SequenceNo: int64(idx),
})
must.Done(producerCli.SendMessage(context.Background(), topic, []byte(payload)))
time.Sleep(time.Second)
}
select {}
}
β¬οΈ Source: Source
Config
- Client configuration for both producer and consumerProducerOptions
- Producer specific configuration options
NewProducer(config *Config) (*Producer, error)
- Creates and starts a new producer instanceSendMessage(ctx context.Context, topic string, payload []byte) error
- Sends message to specified topicClose() error
- Shuts down producer and releases resources
NewConsumer(config *Config) (*Consumer, error)
- Creates a new consumer instanceStartSubscribe(topic string, handler func(*primitive.MessageExt) (consumer.ConsumeResult, error)) error
- Starts consumer and subscribes to topicClose() error
- Shuts down consumer and releases resources
ResolveNameServer(nameServer string) ([]string, error)
- Resolves NameServer hostname to IPv4 addresses
Basic producer setup:
config := &rocketmq.Config{
NameServerAddress: "127.0.0.1:9876",
GroupName: "ProducerGroup",
ProducerOptions: &rocketmq.ProducerOptions{
SendMessageTimeout: 3 * time.Second,
RetryAttempts: 3,
},
}
producerCli := rese.P1(rocketmq.NewProducer(config))
defer rese.F0(producerCli.Close)
Sending messages:
payload := []byte(`{"name":"demo","value":123}`)
must.Done(producerCli.SendMessage(context.Background(), "MyTopic", payload))
Basic consumer setup:
config := &rocketmq.Config{
NameServerAddress: "127.0.0.1:9876",
GroupName: "ConsumerGroup",
}
consumerCli := rese.P1(rocketmq.NewConsumer(config))
defer rese.F0(consumerCli.Close)
Subscribing to topics:
must.Done(consumerCli.StartSubscribe("MyTopic", func(message *primitive.MessageExt) (consumer.ConsumeResult, error) {
fmt.Printf("Message: %s\n", string(message.Body))
return consumer.ConsumeSuccess, nil
}))
Resolve NameServer hostname to IPv4 addresses:
addresses := rese.V1(rocketmq.ResolveNameServer("mq.example.com:9876"))
fmt.Printf("Resolved addresses: %v\n", addresses)
This project uses the mate ecosystem for elegant error handling and resource management:
github.com/apache/rocketmq-client-go/v2
- Official Apache RocketMQ Go clientgithub.com/yyle88/erero
- Error handlinggithub.com/yyle88/must
- Panic-based assertion for errorsgithub.com/yyle88/rese
- Resource management with panic handlinggithub.com/yyle88/zaplog
- Structured logginggo.uber.org/zap
- Logging foundation
MIT License. See LICENSE.
Contributions are welcome! Report bugs, suggest features, and contribute code:
- π Found a mistake? Open an issue on GitHub with reproduction steps
- π‘ Have a feature idea? Create an issue to discuss the suggestion
- π Documentation confusing? Report it so we can improve
- π Need new features? Share the use cases to help us understand requirements
- β‘ Performance issue? Help us optimize through reporting slow operations
- π§ Configuration problem? Ask questions about complex setups
- π’ Follow project progress? Watch the repo to get new releases and features
- π Success stories? Share how this package improved the workflow
- π¬ Feedback? We welcome suggestions and comments
New code contributions, follow this process:
- Fork: Fork the repo on GitHub (using the webpage UI).
- Clone: Clone the forked project (
git clone https://github.com/yourname/repo-name.git
). - Navigate: Navigate to the cloned project (
cd repo-name
) - Branch: Create a feature branch (
git checkout -b feature/xxx
). - Code: Implement the changes with comprehensive tests
- Testing: (Golang project) Ensure tests pass (
go test ./...
) and follow Go code style conventions - Documentation: Update documentation to support client-facing changes and use significant commit messages
- Stage: Stage changes (
git add .
) - Commit: Commit changes (
git commit -m "Add feature xxx"
) ensuring backward compatible code - Push: Push to the branch (
git push origin feature/xxx
). - PR: Open a merge request on GitHub (on the GitHub webpage) with detailed description.
Please ensure tests pass and include relevant documentation updates.
Welcome to contribute to this project via submitting merge requests and reporting issues.
Project Support:
- β Give GitHub stars if this project helps you
- π€ Share with teammates and (golang) programming friends
- π Write tech blogs about development tools and workflows - we provide content writing support
- π Join the ecosystem - committed to supporting open source and the (golang) development scene
Have Fun Coding with this package! πππ