Pulsar Administration

1. Pulsar Admin Client

On the Pulsar administration side, Spring Boot auto-configuration provides a PulsarAdministration to manage Pulsar clusters. The administration implements an interface called PulsarAdminOperations and provides a createOrModify method to handle topic administration through its contract.spring-doc.cn

When you use the Pulsar Spring Boot starter, you get the PulsarAdministration auto-configured.spring-doc.cn

By default, the application tries to connect to a local Pulsar instance at http://localhost:8080. This can be adjusted by setting the spring.pulsar.admin.service-url property to a different value in the form (http|https)://<host>:<port>.spring-doc.cn

There are many application properties available to configure the client. See the spring.pulsar.admin.* application properties.spring-doc.cn

1.1. Authentication

When accessing a Pulsar cluster that requires authentication, the admin client requires the same security configuration as the regular Pulsar client. You can use the aforementioned security configuration by replacing spring.pulsar.client with spring.pulsar.admin.spring-doc.cn

2. Automatic Topic Creation

On initialization, the PulsarAdministration checks if there are any PulsarTopic beans in the application context. For all such beans, the PulsarAdministration either creates the corresponding topic or, if necessary, modifies the number of partitions.spring-doc.cn

The following example shows how to add PulsarTopic beans to let the PulsarAdministration auto-create topics for you:spring-doc.cn

@Bean
PulsarTopic simpleTopic(PulsarTopicBuilder topicBuilder) {
    // This will create a non-partitioned persistent topic in the 'public/default' tenant/namespace
    return topicBuilder.name("my-topic").build();
}

@Bean
PulsarTopic partitionedTopic(PulsarTopicBuilder topicBuilder) {
    // This will create a persistent topic with 3 partitions in the provided tenant and namespace
    return topicBuilder
        .name("persistent://my-tenant/my-namespace/partitioned-topic")
        .numberOfPartitions(3)
        .build();
}

When using Spring Boot the PulsarTopicBuilder is a registered bean that is configured with default values for domain, tenant, and namespace. You can simply inject the builder where needed. Otherwise, use one of the PulsarTopicBuilder constructors directly.spring-doc.cn