tencent cloud

Feedback

Use of Go SDK

Last updated: 2024-01-17 16:55:52

    Overview

    This document describes how to use an open-source SDK to send and receive messages with the Golang SDK serving as example, for you to better understand the complete process of message sending and receiving.
    Note:
    The Golang client is used as an example. For clients of other languages, see the SDK Documentation.

    Prerequisites

    You have created and prepared the required resources.

    Directions:

    Step 1: Installing the Golang Dependency Library

    Incorporate the relevant dependencies in the Golang project. go get is used as example. Run the following command:
    go get github.com/apache/rocketmq-clients/golang/v5

    Step 2: Producing Messages

    package main
    
    import (
    "context"
    "fmt"
    "log"
    "os"
    "strconv"
    "time"
    
    rmq_client "github.com/apache/rocketmq-clients/golang/v5"
    "github.com/apache/rocketmq-clients/golang/v5/credentials"
    )
    
    const (
    Topic = "xxxxxx"
    // Set Endpoint to the access address provided by Tencent Cloud
    Endpoint = "xxxxxx"
    // Add the configured ak to AccessKey
    AccessKey = "xxxxxx"
    // Add the configured sk to SecretKey
    SecretKey = "xxxxxx"
    )
    
    func main() {
    os.Setenv("mq.consoleAppender.enabled", "true")
    rmq_client.ResetLogger()
    // In most case, you don't need to create many producers, singleton pattern is more recommended.
    producer, err := rmq_client.NewProducer(&rmq_client.Config{
    Endpoint: Endpoint,
    Credentials: &credentials.SessionCredentials{
    AccessKey: AccessKey,
    AccessSecret: SecretKey,
    },
    },
    rmq_client.WithTopics(Topic),
    )
    if err != nil {
    log.Fatal(err)
    }
    // start producer
    err = producer.Start()
    if err != nil {
    log.Fatal(err)
    }
    // graceful stop producer
    defer producer.GracefulStop()
    
    for i := 0; i < 10; i++ {
    // new a message
    msg := &rmq_client.Message{
    Topic,
    Body: []byte("this is a message : " + strconv.Itoa(i)),
    }
    // set keys and tag
    msg.SetKeys("a", "b")
    msg.SetTag("ab")
    // send message in sync
    resp, err := producer.Send(context.TODO(), msg)
    if err != nil {
    log.Fatal(err)
    }
    for i := 0; i < len(resp); i++ {
    fmt.Printf("%#v\\n", resp[i])
    }
    // wait a moment
    time.Sleep(time.Second * 1)
    }
    }

    Step 3: Consuming Messages

    TDMQ for RocketMQ 5.x series by Tencent Cloud supports two types of clients: Push Consumer and Simple Consumer.
    Note:
    At this time, the community version of the Golang SDK only supports Simple Consumer.
    The following sample code uses Simple Consumer as an example:
    package main
    
    import (
    "context"
    "fmt"
    "log"
    "os"
    "time"
    
    rmq_client "github.com/apache/rocketmq-clients/golang/v5"
    "github.com/apache/rocketmq-clients/golang/v5/credentials"
    )
    
    const (
    Topic = "xxxxxx"
    ConsumerGroup = "xxxxxx"
    // Set Endpoint to the access address provided by Tencent Cloud
    Endpoint = "xxxxxx"
    // Add the configured ak to AccessKey
    AccessKey = "xxxxxx"
    // Add the configured sk to SecretKey
    SecretKey = "xxxxxx"
    )
    
    var (
    // maximum waiting time for receive func
    awaitDuration = time.Second * 5
    // maximum number of messages received at one time
    maxMessageNum int32 = 16
    // invisibleDuration should > 20s
    invisibleDuration = time.Second * 20
    // receive messages in a loop
    )
    
    func main() {
    // log to console
    os.Setenv("mq.consoleAppender.enabled", "true")
    rmq_client.ResetLogger()
    // In most case, you don't need to create many consumers, singleton pattern is more recommended.
    simpleConsumer, err := rmq_client.NewSimpleConsumer(&rmq_client.Config{
    Endpoint: Endpoint,
    ConsumerGroup: ConsumerGroup,
    Credentials: &credentials.SessionCredentials{
    AccessKey: AccessKey,
    AccessSecret: SecretKey,
    },
    },
    rmq_client.WithAwaitDuration(awaitDuration),
    rmq_client.WithSubscriptionExpressions(map[string]*rmq_client.FilterExpression{
    Topic: rmq_client.SUB_ALL,
    }),
    )
    if err != nil {
    log.Fatal(err)
    }
    // start simpleConsumer
    err = simpleConsumer.Start()
    if err != nil {
    log.Fatal(err)
    }
    // graceful stop simpleConsumer
    defer simpleConsumer.GracefulStop()
    for {
    fmt.Println("start receive message")
    mvs, err := simpleConsumer.Receive(context.TODO(), maxMessageNum, invisibleDuration)
    if err != nil {
    fmt.Println(err)
    }
    // ack message
    for _, mv := range mvs {
    simpleConsumer.Ack(context.TODO(), mv)
    fmt.Println(mv)
    }
    fmt.Println("wait a moment")
    fmt.Println()
    time.Sleep(time.Second * 1)
    }
    }

    Step 4: Viewing Message Details

    After the message is sent, you will receive a message ID (messageID). Developers can query the recently sent messages on the Message Query page, as shown in the following figure. Information such as details and traces for specific messages is also available. For details, see Message Query.
    
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support