tencent cloud

Feedback

SDK for Go

Last updated: 2022-05-20 17:13:57

    Overview

    This document describes how to use open-source SDK to send and receive messages by using the SDK for Go as an example and helps you better understand the message sending and receiving processes.

    Prerequisites

    Directions

    1. Import the pulsar-client-go library in the client environment.
      1. Run the following command in the client environment to download the dependency package of the Pulsar client.
        go get -u "github.com/apache/pulsar-client-go/pulsar"
      2. After the installation is completed, use the following code to import the client into your Go project file.
        import "github.com/apache/pulsar-client-go/pulsar"
    2. Create a Pulsar client.
      // Create a Pulsar client
      client, err := pulsar.NewClient(pulsar.ClientOptions{
      // Service access address
      URL: serviceUrl,
      // Authorize the role token
      Authentication: pulsar.NewAuthenticationToken(authentication),
      OperationTimeout: 30 * time.Second,
      ConnectionTimeout: 30 * time.Second,
      })

      if err != nil {
      log.Fatalf("Could not instantiate Pulsar client: %v", err)
      }

      defer client.Close()
      Parameter Description
      serviceUrl Cluster access address, which can be viewed and copied on the Cluster Management page in the console.
      img
      Authentication Role token, which can be copied in the Token column on the Role Management page. img
    3. Create a producer.
      // Create a producer with the client
      producer, err := client.CreateProducer(pulsar.ProducerOptions{
      // Complete path of the topic in the format of `persistent://cluster (tenant) ID/namespace/topic name`
      Topic: "persistent://pulsar-mmqwr5xx9n7g/sdk_go/topic1",
      })

      if err != nil {
      log.Fatal(err)
      }
      defer producer.Close()
      Note

      You need to enter the complete path of the topic name, i.e., persistent://clusterid/namespace/Topic, where the clusterid/namespace/topic part can be copied directly from the Topic Management page in the console.

    4. Send a message.
      // Send a message
      _, err = producer.Send(context.Background(), &pulsar.ProducerMessage{
      // Message content
      Payload: []byte("hello go client, this is a message."),
      // Business key
      Key: "yourKey",
      // Business parameter
      Properties: map[string]string{"key": "value"},
      })
    5. Create a consumer.
      // Create a consumer with the client
      consumer, err := client.Subscribe(pulsar.ConsumerOptions{
      // Complete path of the topic in the format of `persistent://cluster (tenant) ID/namespace/topic name`
      Topic: "persistent://pulsar-mmqwr5xx9n7g/sdk_go/topic1",
      // Subscription name
      SubscriptionName: "topic1_sub",
      // Subscription mode
      Type: pulsar.Shared,
      })
      if err != nil {
      log.Fatal(err)
      }
      defer consumer.Close()
    Note:

    • You need to enter the complete path of the topic name, i.e., persistent://clusterid/namespace/Topic, where the clusterid/namespace/topic part can be copied directly from the Topic Management page in the console.
      img
    • You need to enter the subscription name in the subscriptionName parameter, which can be viewed on the Consumption Management page.
    1. Consume the message.
      // Obtain the message
      msg, err := consumer.Receive(context.Background())
      if err != nil {
      log.Fatal(err)
      }
      // Simulate business processing
      fmt.Printf("Received message msgId: %#v -- content: '%s'\n",
      msg.ID(), string(msg.Payload()))

      // If the consumption is successful, return `ack`; otherwise, return `nack` or `ReconsumeLater` according to your business needs
      consumer.Ack(msg)
    2. Log in to the TDMQ for Pulsar console, click Topic Management > Topic Name to enter the Consumption Management page, and click the triangle below a subscription name to view the production and consumption records.
      img
    Note:

    The above is a brief introduction to the way of publishing and subscribing to messages. For more operations, see Demo or Pulsar Go client.

    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