Pulsar 管理中心

1. Pulsar 管理客户端

在Pulsar管理界面中,Spring Boot自动配置提供了一个PulsarAdministration来管理Pulsar集群。 管理界面实现了一个名为PulsarAdminOperations的接口,并通过其契约提供了一个createOrModify方法来处理主题管理。spring-doc.cadn.net.cn

当你使用 Pulsar Spring Boot Starter 时,会自动配置 PulsarAdministrationspring-doc.cadn.net.cn

默认情况下,应用程序会尝试连接到位于 http://localhost:8080 的本地 Pulsar 实例。 可以通过将 spring.pulsar.admin.service-url 属性设置为格式为 (http|https)://<host>:<port> 的其他值来进行调整。spring-doc.cadn.net.cn

有许多可用于配置客户端的应用属性。 查看 spring.pulsar.admin.* 应用属性。spring-doc.cadn.net.cn

1.1. 认证

当访问需要身份验证的 Pulsar 集群时,admin 客户端需要与常规 Pulsar 客户端相同的安全配置。 你可以通过将 spring.pulsar.client 替换为 spring.pulsar.admin 使用上述 安全配置spring-doc.cadn.net.cn

2. 自动创建主题

初始化时,PulsarAdministration 会检查应用程序上下文中是否存在任何 PulsarTopic 类型的 bean。 对于所有此类 bean,PulsarAdministration 会创建相应的主题,或者在需要时修改分区数量。spring-doc.cadn.net.cn

以下示例展示了如何添加PulsarTopic beans,以让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 是一个已注册的 bean,其 domain、tenant 和 namespace 配置使用默认值。 您可以只需在需要的地方注入 builder。 否则,直接使用其中一个 PulsarTopicBuilder 构造函数。spring-doc.cadn.net.cn