tencent cloud

Feedback

SDK for Go

Last updated: 2022-05-24 16:36:45

    Overview

    This document uses Go as an example to describe how to connect to TDMQ for Pulsar to send and receive messages over the HTTP protocol.

    Prerequisites

    Directions

    1. Prepare the environment.
      1. Use a Tencent Cloud mirror for faster download.
        1. Linux or macOS.
          export GOPROXY=https://mirrors.tencent.com/go/
        2. Windows。
          set GOPROXY=https://mirrors.tencent.com/go/
      2. Install the basic and product packages.
        1. Install the common basic package.
          go get -v -u github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common
        2. Install the corresponding TDMQ product package.
          go get -v -u github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tdmq
          Note

          For more information, see SDK for Go Installation.

    2. Create the TDMQ client.
      // Authentication information
      credential := common.NewCredential(SECRET_ID, SECRET_KEY)
      cpf := profile.NewClientProfile()
      cpf.HttpProfile.Endpoint = ENDPOINT
      // Create the TDMQ client
      client, _ := tdmq.NewClient(credential, REGION, cpf)
      Parameter Description
      SECRET_ID、SECRET_KEY TencentCloud API key, which can be copied on the Access Key > API Key Management page in the CAM console. img
      ENDPOINT API request domain: tdmq.tencentcloudapi.com.
      REGION Cluster region.
    3. Send messages.
      request := tdmq.NewSendMessagesRequest()
      // Authorized role key
      request.StringToken = common.StringPtr(token)
      // Authorized role name
      request.ProducerName = common.StringPtr(userName)
      // Set the topic name in the following format: cluster (tenant) ID/namespace/topic name
      request.Topic = common.StringPtr(topicName)
      // Message content
      request.Payload = common.StringPtr("this is a new message.")
      // Message sending timeout period
      request.SendTimeout = common.Int64Ptr(3000)
      // Send the message
      response, err := client.SendMessages(request)
      if _, ok := err.(*errors.TencentCloudSDKError); ok {
      fmt.Printf("An API error has returned: %s", err)
      return
      }
      if err != nil {
      panic(err)
      }
      Parameter Description
      token Role key, which can be copied in the Key column on the Role Management page. img
      userName Role name, which can be copied in the Name column on the Role Management page.
      topicName Topic name in the following format: cluster (tenant) ID/namespace/topic name, such as `pulsar-xxx/sdk_http/topic1`. You can directly copy it on the Topic Management page in the console.
    4. Consume messages.
      request := tdmq.NewReceiveMessageRequest()
      // Set the topic name in the following format: cluster (tenant) ID/namespace/topic name
      request.Topic = common.StringPtr(topicName)
      // Set the subscription name
      request.SubscriptionName = common.StringPtr(subName)
      // Messages received by the consumer will first be stored in the `receiverQueueSize` queue to tune the message receiving rate
      request.ReceiverQueueSize = common.Int64Ptr(10)
      // It is used to determine the position where the consumer initially receives messages. Valid values: Earliest, Latest.
      request.SubInitialPosition = common.StringPtr("Latest")
      // Consume messages
      response, err := client.ReceiveMessage(request)
      if _, ok := err.(*errors.TencentCloudSDKError); ok {
      fmt.Printf("An API error has returned: %s", err)
      return
      }
      if err != nil {
      panic(err)
      }
      Parameter Description
      topicName Topic name in the following format: cluster (tenant) ID/namespace/topic name, such as `pulsar-xxx/sdk_http/topic1`. You can directly copy it on the Topic Management page in the console.
      subName Subscription name, which can be copied on the Cluster > Consumer tab in the console.
    5. Acknowledge messages.
      request := tdmq.NewAcknowledgeMessageRequest()
      // Set the message ID
      request.MessageId = common.StringPtr(messageId)
      // Set the topic name in the following format: cluster (tenant) ID/namespace/topic name
      request.AckTopic = common.StringPtr(topicName)
      // Set the message subscription
      request.SubName = common.StringPtr(subName)
      // Acknowledge messages
      response, err := client.AcknowledgeMessage(request)
      if _, ok := err.(*errors.TencentCloudSDKError); ok {
      fmt.Printf("An API error has returned: %s", err)
      return
      }
      if err != nil {
      panic(err)
      }
      Parameter Description
      messageId Message ID obtained after message consumption.
      topicName Topic name in the following format: cluster (tenant) ID/namespace/topic name, such as `pulsar-xxx/sdk_http/topic1`. You can directly copy it on the Topic Management page in the console.
      subName Subscription name, which can be copied on the Cluster > Consumer tab in the console.
    Note:

    Above is a brief introduction to message sending and receiving operations. For more information, see Demo or API Explorer.

    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