SoFunction
Updated on 2025-03-05

Example code for golang operation rocketmq

What is RocketMQ

Introduction to RocketMQ on Github:
RcoketMQ is a low latency, high reliability, scalable, and easy to use messaging middleware. It has the following characteristics:

  • Supports publish/subscribe (Pub/Sub) and peer-to-peer (P2P) message models
  • Reliable first-in-first-out (FIFO) and strict sequential pass in a queue
  • Supports two message modes: pull and push
  • Accumulation capability of a single queue of million messages
  • Supports multiple message protocols, such as JMS, MQTT, etc.
  • Distributed and highly available deployment architecture that satisfies at least one messaging semantics
  • Provide docker images for isolating tests and cloud cluster deployments
  • Provides feature-rich Dashboards such as configuration, metrics and monitoring

You can just take a quick look at these characteristics descriptions, and you will naturally understand after in-depth study.

Let’s take a look at the sample code of golang operating rocketmq, the content is as follows:

download

go get /apache/rocketmq-client-go/v2

Code

func main() {
	// 1. Create a topic	//CreateTopic("test-04", 10909)
	// 2. Producer sends messages to the topic	//SendSyncMessage("hello world0002")
	// 3. Consumers subscribe to topics and consume	SubscribeMessage()
}
func CreateTopic(topicName string, port int) {
	// Create a theme	testAdmin, err := ((([]string{"ip:server_port"})))
	if err != nil {
		(err)
	}
	err = (
		(),
		(topicName),
		(("ip:%d", port)),
	)
	(err)
func SendSyncMessage(message string) {
	endPoint := []string{"ip:server_port"}
	p, err := (
		(endPoint),
		//((endPoint)),
		(2),
	err = ()
	result, err := ((), &{
		Topic: "test",
		Body:  []byte(message),
	})
	(, result)
func SubscribeMessage() {
	// Subscribe to the topic and consume	c, err := (
		(endPoint),
		(),
		("GID_TEST01"),
		//(err)
	err = ("test", {}, func(ctx ,
		msgs ...*) (, error) {
		for i := range msgs {
			("subscribe callback: %v \n", msgs[i])
		}
		return , nil
		//(())
	// Note: start after subscribe
	err = ()
		(-1)
	()

This is the article about the sample code of golang operation rocketmq. This is all about this. For more related golang operation rocketmq content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!