1. Build Applications
  2. Pulsar Clients

Connect to your cluster in Spring applications

This document describes how to connect to a cluster in Spring applications, and use the producer and consumer to produce and consume messages to and from a topic. You can use either OAuth2 or API Keys authentication in Spring applications.

Note

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

Prerequisites

See the minimum supported versions required for the underlying libraries for more details.

Connect to your cluster using API keys

To connect a StreamNative cluster using API keys, follow these steps.

Step 1: Get the broker service URL of your cluster

To get the service URL of a Pulsar cluster through the StreamNative Console, follow these steps.

  1. On the left navigation pane, in the Admin area, click Pulsar Clusters.

  2. Select the Details tab, and in the Access Points area, click Copy at the end of the row of the service URL.

Step 2: Create an API key of your service account

Note

Before using an API key, verify that the service account is authorized to access the resources, such as tenants, namespaces, and topics.

You can follow the instructions to create an API key for the service account you choose to use.

Step 3: Connect to your cluster

Configure a YAML file

Set the following configurations in the code of your Spring applications.

spring:
  pulsar:
    client:
      service-url: ${brokerServiceURL}
      auth-plugin-class-name: org.apache.pulsar.client.impl.auth.AuthenticationToken
      authentication:
        token: ${apikey}
  • ${brokerServiceURL}l: the broker service URL of your StreamNative cluster.
  • ${apikey}: an API key of your service account.

Consume messages

You can consume messages using Token authentication in your Spring application as follows.

@SpringBootApplication
public class PulsarBootHelloWorld {

    public static void main(String[] args) {
        SpringApplication.run(PulsarBootHelloWorld.class, args);
    }

    @PulsarListener(subscriptionName = "${subscription}", topics = "persistent://${tenant}/${namespace}/${topic}")
    void listen(String message) {
        System.out.println("Message Received: " + message);
    }

}

Produce messages

You can produce messages using Token authentication in your Spring application as follows.

@SpringBootApplication
public class PulsarBootHelloWorld {

    public static void main(String[] args) {
        SpringApplication.run(PulsarBootHelloWorld.class, args);
    }

    @Bean
    ApplicationRunner runner(PulsarTemplate<String> pulsarTemplate) {
        return (args) -> pulsarTemplate.send("persistent://${tenant}/${namespace}/${topic}", "Hello Pulsar World!");
    }

}

For a complete example of how to connect to a cluster in a Spring application, see Spring client examples.

Connect to your cluster using OAuth2 authentication

To connect a StreamNative cluster using OAuth2 authentication, follow these steps.

Step 1: Get the broker service URL of your cluster

To get the service URL of a Pulsar cluster through the StreamNative Console, follow these steps.

  1. On the left navigation pane, in the Admin area, click Pulsar Clusters.

  2. Select the Details tab, and in the Access Points area, click Copy at the end of the row of the service URL.

Step 2: Get the OAuth2 credential file of your service account

To get an OAuth2 credential file of a service account through the StreamNative Console, follow these steps.

  1. On the left navigation pane, click Service Accounts.

  2. In the row of the service account you want to use, in the Key File column, click the Download icon to download the OAuth2 credential file to your local directory.

    The OAuth2 credential file should be something like this:

    {
      "type": "SN_SERVICE_ACCOUNT",
      "client_id": "CLIENT_ID",
      "client_secret": "CLIENT_SECRET",
      "client_email": "[email protected]",
      "issuer_url": "https://auth.streamnative.cloud"
    }
    

Step 3: Connect to your cluster

Configure a YAML file

Set the following configurations in the code of your Spring applications.

spring:
  pulsar:
    client:
      service-url: ${brokerServiceURL}
      auth-plugin-class-name: org.apache.pulsar.client.impl.auth.oauth2.AuthenticationOAuth2
      authentication:
        issuer-url: 'https://auth.streamnative.cloud/'
        private-key: '/YOUR-KEY-FILE-PATH' # TODO Absolute file path of your downloaded key file
        audience: 'urn:sn:pulsar:${orgName}:${instanceName}'
  • service-url: the broker service URL of your StreamNative cluster.
  • private-key: your downloaded OAuth2 credential. This parameter supports the following two pattern formats:
    • file:///path/to/file: the path to your downloaded OAuth2 credential file.
    • data:application/json;base64,<base64-encoded value>: the credential file content encoded into Base64 format.
  • audience: the Uniform Resource Name (URN), which is a combination of the urn:sn:pulsar, your organization name, and your Pulsar instance name.

Consume messages

You can consume messages using OAuth2 authentication in your Spring application as follows.

import org.springframework.pulsar.annotation.PulsarListener;

@SpringBootApplication
public class PulsarBootHelloWorld {

    public static void main(String[] args) {
        SpringApplication.run(PulsarBootHelloWorld.class, args);
    }

    @PulsarListener(subscriptionName = "${subscription}", topics = "persistent://${tenant}/${namespace}/${topic}")
    void listen(String message) {
        System.out.println("Message Received: " + message);
    }

}

Produce messages

You can produce messages using OAuth2 authentication in your Spring application as follows.

import org.springframework.pulsar.core.PulsarTemplate;

@SpringBootApplication
public class PulsarBootHelloWorld {

    public static void main(String[] args) {
        SpringApplication.run(PulsarBootHelloWorld.class, args);
    }

    @Bean
    ApplicationRunner runner(PulsarTemplate<String> pulsarTemplate) {
        return (args) -> pulsarTemplate.send("persistent://${tenant}/${namespace}/${topic}", "Hello Pulsar World!");
    }

}

For a complete example of how to connect to a cluster in your Spring application, see Spring client examples.

Previous
Rust