1. StreamNative Cloud
  2. Connect

Connect to cluster through pulsar-client

This document describes how to connect to a cluster through the pulsar-client CLI tool, and use the producer and consumer to produce and consume messages to and from a topic.

This document assumes that you have created a Pulsar cluster and a service account, and have granted the service account produce and consume permissions to the namespace for the target topic.

Prerequisites

  • Pulsar client 2.6.1 or higher version

Connect to cluster through OAuth2 authentication plugin

To connect a cluster through the OAuth2 authentication plugin, follow these steps.

  1. Get the service URL of your Pulsar cluster. For details, see get a service URL.

  2. Get the OAuth2 credential file of your service account. For details, see get an OAuth2 credential file.

  3. Connect to a Pulsar cluster through the OAuth2 authentication plugin.

    This example shows how to use the OAuth2 credential file to produce a test-message message to the test-topic topic.

    ./bin/pulsar-client \
      --url SERVICE_URL \
      --auth-plugin org.apache.pulsar.client.impl.auth.oauth2.AuthenticationOAuth2 \
      --auth-params '{"privateKey":"/absolute path/to/key/file.json",
        "issuerUrl":"https://auth.streamnative.cloud",
        "audience":"urn:sn:pulsar:test-pulsar-instance-namespace:test-pulsar-instance"}' \
      produce test-topic -m "test-message" -n 10
    
    • url: the broker URL of your Pulsar cluster.
    • privateKey: the path to your OAuth2 credential file. The privateKey parameter supports the following three pattern formats:
      • file:///path/to/file
      • file:/path/to/file
      • data:application/json;base64,<base64-encoded value>
    • issuerUrl: the URL of your OAuth2 authentication provider. You can get the value from your downloaded OAuth2 credential file.
    • audience: the audience parameter is the Uniform Resource Name (URN), which is a combination of the urn:sn:pulsar, the organization name, and the Pulsar instance name, in this format urn:sn:pulsar:<org_name>:<instance_name>.

    This example shows how to use the encoded OAuth2 private key to produce a test-message message to the test-topic topic.

    ./bin/pulsar-client \
      --url "pulsar-service-url" \
      --auth-plugin org.apache.pulsar.client.impl.auth.oauth2.AuthenticationOAuth2 \
      --auth-params '{"privateKey":"data:application/json;base64,eyJjbGllbnRfaWQiOiJiMDk3ZDg2NC00YjRkLTQwMjA...", "issuerUrl":"your-issuer-url", "audience":"your-audience"}' \
      produce test-topic -m "test-message" -n 10
    

Connect to cluster through Token authentication plugin

To connect a cluster through the Token authentication plugin, follow these steps.

  1. Get the service URL of your Pulsar cluster. For details, see get a service URL.

  2. Get the token of your service account. For details, see get a token.

  3. Connect to a Pulsar cluster through the Token authentication plugin.

    ./bin/pulsar-client \
      --url SERVICE_URL \
      --auth-plugin org.apache.pulsar.client.impl.auth.AuthenticationToken \
      --auth-params token:AUTH_PARAMS \
      produce test-topic -m "test-message" -n 10
    
    • url: the HTTP service URL of your Pulsar cluster.
    • auth-params: the token of your service account.

For details about how to use pulsar-admin to manage Pulsar components, see pulsar-client examples.

Previous
CLI - pulsar-admin