Pulsar 管理

1. Pulsar Admin 客户端

在 Pulsar 管理端,Spring Boot 自动配置提供了一个PulsarAdministration来管理 Pulsar 集群。 管理实现一个名为PulsarAdminOperations并提供一个createOrModify方法通过其 Contract 处理 topic 管理。spring-doc.cadn.net.cn

当您使用 Pulsar Spring Boot Starters时,您将获得PulsarAdministrationauto-configured的。spring-doc.cadn.net.cn

默认情况下,应用程序会尝试连接到位于http://localhost:8080. 这可以通过设置spring.pulsar.admin.service-urlproperty 设置为表单中的其他值(http|https)://<host>:<port>.spring-doc.cadn.net.cn

有许多应用程序属性可用于配置客户端。 请参阅spring.pulsar.admin.*应用程序属性。spring-doc.cadn.net.cn

1.1. 身份验证

访问需要身份验证的 Pulsar 集群时,admin 客户端需要与常规 Pulsar 客户端相同的安全配置。 您可以通过替换spring.pulsar.clientspring.pulsar.admin.spring-doc.cadn.net.cn

2. 自动创建主题

初始化时,PulsarAdministration检查是否有任何PulsarTopicbean 中的对象。 对于所有这些 bean,PulsarAdministration创建相应的主题,或者在必要时修改分区的数量。spring-doc.cadn.net.cn

以下示例显示了如何添加PulsarTopicbean 让PulsarAdministration自动创建主题:spring-doc.cadn.net.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();
}

使用 Spring Boot 时,PulsarTopicBuilder是配置了 domain、tenant 和 namespace 的默认值的已注册 Bean。 您只需在需要的地方注入构建器即可。 否则,请使用PulsarTopicBuilder构造函数。spring-doc.cadn.net.cn