您可以使用 Pulsar Go客户端 来创建 Pulsar , consumers , 和 .

这里也提供 api 文档.

有关标准 api 文档, 请参阅.

You can install the library locally using go get.

Once installed locally, you can import it into your project:

  1. import "github.com/apache/pulsar-client-go/pulsar"

连接URL

To connect to Pulsar using client libraries, you need to specify a URL.

Pulsar protocol URLs are assigned to specific clusters, use the pulsar scheme and have a default port of 6650. Here’s an example for localhost:

  1. pulsar://localhost:6650

A URL for a production Pulsar cluster may look something like this:

  1. pulsar://pulsar.us-west.example.com:6650

If you’re using TLS authentication, the URL will look like something like this:

  1. pulsar+ssl://pulsar.us-west.example.com:6651

创建客户端

  1. import (
  2. "log"
  3. "time"
  4. "github.com/apache/pulsar-client-go/pulsar"
  5. )
  6. func main() {
  7. client, err := pulsar.NewClient(pulsar.ClientOptions{
  8. URL: "pulsar://localhost:6650",
  9. OperationTimeout: 30 * time.Second,
  10. ConnectionTimeout: 30 * time.Second,
  11. })
  12. if err != nil {
  13. log.Fatalf("Could not instantiate Pulsar client: %v", err)
  14. }
  15. defer client.Close()
  16. }

The following configurable parameters are available for Pulsar clients:

Name | Description | Default | :———— | :————— |:————— | | URL | Configure the service URL for the Pulsar service. This parameter is required | | | ConnectionTimeout | Timeout for the establishment of a TCP connection | 30s | | OperationTimeout| Set the operation timeout. Producer-create, subscribe and unsubscribe operations will be retried until this interval, after which the operation will be marked as failed| 30s| | Authentication | Configure the authentication provider. Example: Authentication: NewAuthenticationTLS("my-cert.pem", "my-key.pem") | no authentication | | TLSTrustCertsFilePath | Set the path to the trusted TLS certificate file | | | TLSAllowInsecureConnection | Configure whether the Pulsar client accept untrusted TLS certificate from broker | false | | TLSValidateHostname | Configure whether the Pulsar client verify the validity of the host name from broker | false |

Pulsar producers publish messages to Pulsar topics. You can configure Go producers using a ProducerOptions object. Here’s an example:

  1. producer, err := client.CreateProducer(pulsar.ProducerOptions{
  2. Topic: "my-topic",
  3. })
  4. _, err = producer.Send(context.Background(), &pulsar.ProducerMessage{
  5. Payload: []byte("hello"),
  6. })
  7. defer producer.Close()
  8. if err != nil {
  9. fmt.Println("Failed to publish message", err)
  10. }
  11. fmt.Println("Published message")

Producer operations

Pulsar Go producers have the following methods available:

生产者示例

How to use message router in producer

How to use delay relative in producer

  1. client, err := NewClient(ClientOptions{
  2. URL: "pulsar://localhost:6650",
  3. })
  4. if err != nil {
  5. log.Fatal(err)
  6. }
  7. defer client.Close()
  8. topicName := newTopicName()
  9. producer, err := client.CreateProducer(ProducerOptions{
  10. Topic: topicName,
  11. })
  12. if err != nil {
  13. log.Fatal(err)
  14. }
  15. defer producer.Close()
  16. consumer, err := client.Subscribe(ConsumerOptions{
  17. Topic: topicName,
  18. SubscriptionName: "subName",
  19. Type: Shared,
  20. })
  21. if err != nil {
  22. log.Fatal(err)
  23. }
  24. defer consumer.Close()
  25. ID, err := producer.Send(context.Background(), &ProducerMessage{
  26. Payload: []byte(fmt.Sprintf("test")),
  27. DeliverAfter: 3 * time.Second,
  28. })
  29. if err != nil {
  30. log.Fatal(err)
  31. }
  32. fmt.Println(ID)
  33. ctx, canc := context.WithTimeout(context.Background(), 1*time.Second)
  34. msg, err := consumer.Receive(ctx)
  35. if err != nil {
  36. log.Fatal(err)
  37. }
  38. fmt.Println(msg.Payload())
  39. canc()
  40. ctx, canc = context.WithTimeout(context.Background(), 5*time.Second)
  41. msg, err = consumer.Receive(ctx)
  42. if err != nil {
  43. log.Fatal(err)
  44. }
  45. fmt.Println(msg.Payload())
  46. canc()

Name | Description | Default | :———— | :————— |:————— | | Topic | Topic specify the topic this consumer will subscribe to. This argument is required when constructing the reader. | | | Name | Name specify a name for the producer. If not assigned, the system will generate a globally unique name which can be access with Producer.ProducerName(). | | | Properties | Properties attach a set of application defined properties to the producer This properties will be visible in the topic stats | | | MaxPendingMessages| MaxPendingMessages set the max size of the queue holding the messages pending to receive an acknowledgment from the broker. | | | HashingScheme | HashingScheme change the HashingScheme used to chose the partition on where to publish a particular message. | JavaStringHash | | CompressionType | CompressionType set the compression type for the producer. | not compressed | | MessageRouter | MessageRouter set a custom message routing policy by passing an implementation of MessageRouter | | | DisableBatching | DisableBatching control whether automatic batching of messages is enabled for the producer. | false | | BatchingMaxPublishDelay | BatchingMaxPublishDelay set the time period within which the messages sent will be batched | 10ms | | BatchingMaxMessages | BatchingMaxMessages set the maximum number of messages permitted in a batch. | 1000 |

Consumers

Pulsar consumers subscribe to one or more Pulsar topics and listen for incoming messages produced on that topic/those topics. You can configure Go consumers using a ConsumerOptions object. Here’s a basic example that uses channels:

  1. consumer, err := client.Subscribe(pulsar.ConsumerOptions{
  2. Topic: "topic-1",
  3. SubscriptionName: "my-sub",
  4. Type: pulsar.Shared,
  5. })
  6. if err != nil {
  7. log.Fatal(err)
  8. }
  9. defer consumer.Close()
  10. for i := 0; i < 10; i++ {
  11. msg, err := consumer.Receive(context.Background())
  12. if err != nil {
  13. log.Fatal(err)
  14. }
  15. fmt.Printf("Received message msgId: %#v -- content: '%s'\n",
  16. msg.ID(), string(msg.Payload()))
  17. consumer.Ack(msg)
  18. }
  19. if err := consumer.Unsubscribe(); err != nil {
  20. log.Fatal(err)
  21. }

Consumer operations

Pulsar Go consumers have the following methods available:

Receive example

How to use regx consumer

  1. client, err := pulsar.NewClient(pulsar.ClientOptions{
  2. URL: "pulsar://localhost:6650",
  3. })
  4. defer client.Close()
  5. p, err := client.CreateProducer(ProducerOptions{
  6. Topic: topicInRegex,
  7. DisableBatching: true,
  8. })
  9. if err != nil {
  10. log.Fatal(err)
  11. }
  12. defer p.Close()
  13. topicsPattern := fmt.Sprintf("persistent://%s/foo.*", namespace)
  14. opts := ConsumerOptions{
  15. TopicsPattern: topicsPattern,
  16. SubscriptionName: "regex-sub",
  17. }
  18. consumer, err := client.Subscribe(opts)
  19. if err != nil {
  20. log.Fatal(err)
  21. }
  22. defer consumer.Close()

How to use multi topics Consumer

  1. func newTopicName() string {
  2. return fmt.Sprintf("my-topic-%v", time.Now().Nanosecond())
  3. }
  4. topic1 := "topic-1"
  5. topic2 := "topic-2"
  6. client, err := NewClient(ClientOptions{
  7. URL: "pulsar://localhost:6650",
  8. })
  9. if err != nil {
  10. log.Fatal(err)
  11. }
  12. topics := []string{topic1, topic2}
  13. consumer, err := client.Subscribe(ConsumerOptions{
  14. Topics: topics,
  15. SubscriptionName: "multi-topic-sub",
  16. })
  17. if err != nil {
  18. log.Fatal(err)
  19. }
  20. defer consumer.Close()

How to use consumer listener

  1. import (
  2. "fmt"
  3. "log"
  4. "github.com/apache/pulsar-client-go/pulsar"
  5. )
  6. func main() {
  7. client, err := pulsar.NewClient(pulsar.ClientOptions{URL: "pulsar://localhost:6650"})
  8. if err != nil {
  9. log.Fatal(err)
  10. }
  11. defer client.Close()
  12. channel := make(chan pulsar.ConsumerMessage, 100)
  13. options := pulsar.ConsumerOptions{
  14. Topic: "topic-1",
  15. SubscriptionName: "my-subscription",
  16. Type: pulsar.Shared,
  17. }
  18. options.MessageChannel = channel
  19. consumer, err := client.Subscribe(options)
  20. if err != nil {
  21. log.Fatal(err)
  22. }
  23. defer consumer.Close()
  24. // Receive messages from channel. The channel returns a struct which contains message and the consumer from where
  25. // the message was received. It's not necessary here since we have 1 single consumer, but the channel could be
  26. // shared across multiple consumers as well
  27. for cm := range channel {
  28. msg := cm.Message
  29. fmt.Printf("Received message msgId: %v -- content: '%s'\n",
  30. msg.ID(), string(msg.Payload()))
  31. consumer.Ack(msg)
  32. }
  33. }

How to use consumer receive timeout

  1. client, err := NewClient(ClientOptions{
  2. URL: "pulsar://localhost:6650",
  3. })
  4. if err != nil {
  5. log.Fatal(err)
  6. }
  7. defer client.Close()
  8. topic := "test-topic-with-no-messages"
  9. ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
  10. defer cancel()
  11. // create consumer
  12. consumer, err := client.Subscribe(ConsumerOptions{
  13. Topic: topic,
  14. SubscriptionName: "my-sub1",
  15. Type: Shared,
  16. })
  17. if err != nil {
  18. log.Fatal(err)
  19. }
  20. defer consumer.Close()
  21. msg, err := consumer.Receive(ctx)
  22. fmt.Println(msg.Payload())
  23. if err != nil {
  24. log.Fatal(err)
  25. }

Name | Description | Default | :———— | :————— |:————— | | Topic | Topic specify the topic this consumer will subscribe to. This argument is required when constructing the reader. | | | Topics | Specify a list of topics this consumer will subscribe on. Either a topic, a list of topics or a topics pattern are required when subscribing| | | TopicsPattern | Specify a regular expression to subscribe to multiple topics under the same namespace. Either a topic, a list of topics or a topics pattern are required when subscribing | | | AutoDiscoveryPeriod | Specify the interval in which to poll for new partitions or new topics if using a TopicsPattern. | | | SubscriptionName | Specify the subscription name for this consumer. This argument is required when subscribing | | | Name | Set the consumer name | | | Properties | Properties attach a set of application defined properties to the producer This properties will be visible in the topic stats | | | Type | Select the subscription type to be used when subscribing to the topic. | Exclusive | | SubscriptionInitialPosition | InitialPosition at which the cursor will be set when subscribe | Latest | | DLQ | Configuration for Dead Letter Queue consumer policy. | no DLQ | | MessageChannel | Sets a MessageChannel for the consumer. When a message is received, it will be pushed to the channel for consumption | | | ReceiverQueueSize | Sets the size of the consumer receive queue. | 1000| | NackRedeliveryDelay | The delay after which to redeliver the messages that failed to be processed | 1min | | ReadCompacted | If enabled, the consumer will read messages from the compacted topic rather than reading the full message backlog of the topic | false | | ReplicateSubscriptionState | Mark the subscription as replicated to keep it in sync across clusters | false |

Readers

Reader operations

Pulsar Go readers have the following methods available:

Reader 示例

How to use reader to read ‘next’ message

Here’s an example usage of a Go reader that uses the Next() method to process incoming messages:

  1. import (
  2. "context"
  3. "log"
  4. "github.com/apache/pulsar-client-go/pulsar"
  5. )
  6. client, err := pulsar.NewClient(pulsar.ClientOptions{URL: "pulsar://localhost:6650"})
  7. if err != nil {
  8. log.Fatal(err)
  9. }
  10. defer client.Close()
  11. reader, err := client.CreateReader(pulsar.ReaderOptions{
  12. Topic: "topic-1",
  13. StartMessageID: pulsar.EarliestMessageID(),
  14. })
  15. if err != nil {
  16. log.Fatal(err)
  17. }
  18. defer reader.Close()
  19. for reader.HasNext() {
  20. msg, err := reader.Next(context.Background())
  21. if err != nil {
  22. log.Fatal(err)
  23. }
  24. fmt.Printf("Received message msgId: %#v -- content: '%s'\n",
  25. msg.ID(), string(msg.Payload()))
  26. }
  27. }

In the example above, the reader begins reading from the earliest available message (specified by pulsar.EarliestMessage). The reader can also begin reading from the latest message (pulsar.LatestMessage) or some other message ID specified by bytes using the DeserializeMessageID function, which takes a byte array and returns a MessageID object. Here’s an example:

  1. lastSavedId := // Read last saved message id from external store as byte[]
  2. reader, err := client.CreateReader(pulsar.ReaderOptions{
  3. Topic: "my-golang-topic",
  4. StartMessageID: pulsar.DeserializeMessageID(lastSavedId),
  5. })

How to use reader to read specific message

  1. client, err := NewClient(ClientOptions{
  2. URL: lookupURL,
  3. })
  4. if err != nil {
  5. log.Fatal(err)
  6. }
  7. defer client.Close()
  8. topic := "topic-1"
  9. ctx := context.Background()
  10. // create producer
  11. producer, err := client.CreateProducer(ProducerOptions{
  12. Topic: topic,
  13. DisableBatching: true,
  14. })
  15. if err != nil {
  16. log.Fatal(err)
  17. }
  18. defer producer.Close()
  19. // send 10 messages
  20. msgIDs := [10]MessageID{}
  21. for i := 0; i < 10; i++ {
  22. msgID, err := producer.Send(ctx, &ProducerMessage{
  23. Payload: []byte(fmt.Sprintf("hello-%d", i)),
  24. })
  25. assert.NoError(t, err)
  26. assert.NotNil(t, msgID)
  27. msgIDs[i] = msgID
  28. }
  29. // create reader on 5th message (not included)
  30. reader, err := client.CreateReader(ReaderOptions{
  31. Topic: topic,
  32. StartMessageID: msgIDs[4],
  33. })
  34. if err != nil {
  35. log.Fatal(err)
  36. }
  37. defer reader.Close()
  38. // receive the remaining 5 messages
  39. for i := 5; i < 10; i++ {
  40. msg, err := reader.Next(context.Background())
  41. if err != nil {
  42. log.Fatal(err)
  43. }
  44. // create reader on 5th message (included)
  45. readerInclusive, err := client.CreateReader(ReaderOptions{
  46. Topic: topic,
  47. StartMessageID: msgIDs[4],
  48. StartMessageIDInclusive: true,
  49. })
  50. if err != nil {
  51. log.Fatal(err)
  52. }
  53. defer readerInclusive.Close()

Name | Description | Default | :———— | :————— |:————— | | Topic | Topic specify the topic this consumer will subscribe to. This argument is required when constructing the reader. | | | Name | Name set the reader name. | | | Properties | Attach a set of application defined properties to the reader. This properties will be visible in the topic stats | | | StartMessageID | StartMessageID initial reader positioning is done by specifying a message id. | | | StartMessageIDInclusive | If true, the reader will start at the StartMessageID, included. Default is false and the reader will start from the “next” message | false | | MessageChannel | MessageChannel sets a MessageChannel for the consumer When a message is received, it will be pushed to the channel for consumption| | | ReceiverQueueSize | ReceiverQueueSize sets the size of the consumer receive queue. | 1000 | | SubscriptionRolePrefix| SubscriptionRolePrefix set the subscription role prefix. | “reader” | | ReadCompacted | If enabled, the reader will read messages from the compacted topic rather than reading the full message backlog of the topic. ReadCompacted can only be enabled when reading from a persistent topic. | false|

The Pulsar Go client provides a ProducerMessage interface that you can use to construct messages to producer on Pulsar topics. Here’s an example message:

  1. msg := pulsar.ProducerMessage{
  2. Payload: []byte("Here is some message data"),
  3. Key: "message-key",
  4. Properties: map[string]string{
  5. "foo": "bar",
  6. },
  7. EventTime: time.Now(),
  8. ReplicationClusters: []string{"cluster1", "cluster3"},
  9. }
  10. if _, err := producer.send(msg); err != nil {
  11. log.Fatalf("Could not publish message due to: %v", err)
  12. }

The following methods parameters are available for ProducerMessage objects:

TLS encryption and authentication

In order to use , you’ll need to configure your client to do so:

  • Use pulsar+ssl URL type
  • Set TLSTrustCertsFilePath to the path to the TLS certs used by your client and the Pulsar broker
  • Configure Authentication option

Here’s an example:

  1. opts := pulsar.ClientOptions{
  2. URL: "pulsar+ssl://my-cluster.com:6651",
  3. TLSTrustCertsFilePath: "/path/to/certs/my-cert.csr",
  4. Authentication: NewAuthenticationTLS("my-cert.pem", "my-key.pem"),
  5. }

OAuth2 authentication

  1. oauth := pulsar.NewAuthenticationOAuth2(map[string]string{
  2. "type": "client_credentials",
  3. "issuerUrl": "https://dev-kt-aa9ne.us.auth0.com/oauth/token",
  4. "audience": "https://dev-kt-aa9ne.us.auth0.com/api/v2/",
  5. "privateKey": "/path/to/privateKey",
  6. "clientId": "0Xx...Yyxeny",
  7. })
  8. client, err := pulsar.NewClient(pulsar.ClientOptions{
  9. URL: "puslar://my-cluster:6650",
  10. })