有什么新内容?
1.1 以来 1.2 的新功能
本节涵盖了从版本 1.1 到版本 1.2 的更改。
自定义对象映射器
您可以提供自己的 Jackson ObjectMapper
,Pulsar 将在生产和消费 JSON 消息时使用。有关更多详细信息,请参见 Custom Object Mapper。
默认租户和命名空间
您可以指定一个默认租户和/或命名空间,以便在针对非完全限定的主题 URL 生成或消费消息时使用。有关更多详细信息,请参见 Default Tenant / Namespace。
消息容器启动策略
您现在可以将消息监听容器的启动失败策略配置为 stop
、continue
或 retry
。有关更多详细信息,请参见支持的容器之一的相应部分 @PulsarListener、@PulsarReader 或 @ReactivePulsarListener。
消息容器工厂自定义器 (Spring Boot)
Spring Boot 引入了一个通用消息容器工厂自定义器 org.springframework.boot.autoconfigure.pulsar.PulsarContainerFactoryCustomizer<T extends PulsarContainerFactory<?, ?>>
,可以用来进一步配置一个或多个自动配置的容器工厂,这些容器工厂支持以下监听器注解:
-
对于
@PulsarListener
注册一个或多个 PulsarContainerFactoryCustomizer<ConcurrentPulsarListenerContainerFactory<?>> beans。 -
对于
@PulsarReader
注册一个或多个 PulsarContainerFactoryCustomizer<DefaultPulsarReaderContainerFactory<?>> beans。 -
对于
@ReactivePulsarListener
注册一个或多个 PulsarContainerFactoryCustomizer<DefaultReactivePulsarListenerContainerFactory<?>> beans。
弃用
PulsarClient#getPartitionsForTopic(java.lang.String)
Pulsar 客户端的版本 3.3.1
不再推荐使用 getPartitionsForTopic(java.lang.String)
,而是使用 getPartitionsForTopic(java.lang.String, boolean metadataAutoCreationEnabled)
。
PulsarTopic#builder
在使用 Spring Boot 时,PulsarTopicBuilder
现在是一个注册的 bean,默认配置了域、租户和命名空间的值。因此,如果您使用 Spring Boot,您可以简单地在需要的地方注入构建器。否则,请直接使用 PulsarTopicBuilder
的构造函数之一。
Listener/ReaderContainerFactory
PulsarContainerFactory
通用接口的引入旨在弥合监听器和读取器容器工厂之间的差距。作为此的一部分,以下 API 被弃用、复制并重命名:
-
ListenerContainerFactory#createListenerContainer
替换为ListenerContainerFactory#createRegisteredContainer
-
ReaderContainerFactory#createReaderContainer(E endpoint)
替换为ReaderContainerFactory#createRegisteredContainer
-
ReaderContainerFactory#createReaderContainer(String… topics)
替换为ReaderContainerFactory#createContainer
ConcurrentPulsarListenerContainerFactoryCustomizer
ConcurrentPulsarListenerContainerFactoryCustomizer
的目的是自定义 Spring Boot 自动配置的消息容器工厂。然而,Spring Boot 引入了一个通用的消息容器工厂自定义器 org.springframework.boot.autoconfigure.pulsar.PulsarContainerFactoryCustomizer<T extends PulsarContainerFactory<?, ?>>
,这消除了对该自定义器的需求。
将所有 ConcurrentPulsarListenerContainerFactoryCustomizer
替换为 org.springframework.boot.autoconfigure.pulsar.PulsarContainerFactoryCustomizer<ConcurrentPulsarListenerContainerFactoryCustomizer<?>>
。
移除
以下之前被弃用的监听器端点适配器已被移除,以支持监听器端点接口中的默认方法:
-
org.springframework.pulsar.config.PulsarListenerEndpointAdapter
-
org.springframework.pulsar.reactive.config.ReactivePulsarListenerEndpointAdapter
破坏性变更
PulsarTopic#<init>
PulsarTopic
构造函数现在需要一个完全限定的主题名称(domain://tenant/namespace/name
)。如果您调用构造函数,您需要确保传入的主题是完全限定的。一个更好的选择是使用 PulsarTopicBuilder
,因为它不需要完全限定的名称,并会为指定名称中缺失的组件添加默认值。
PulsarReaderFactory#createReader
PulsarReaderFactory#createReader
API 现在抛出一个未检查的 PulsarException
,而不是一个检查的 PulsarClientException
。相应地替换此 API 上的任何 try/catch
块。
1.0 以来 1.1 的新内容
本节涵盖了从版本 1.0 到版本 1.1 的更改。
自动模式支持
如果无法提前知道 Pulsar 主题的模式,可以使用 AUTO Schemas 从代理生产/消费通用记录。有关更多详细信息,请参见 Producing with AUTO_SCHEMA 和 Consuming with AUTO_SCHEMA。
虽然上述链接专注于 PulsarTemplate
和 @PulsarListener
,但此功能也支持 ReactivePulsarTemplate
、@ReactivePulsarListener
和 @PulsarReader
。每个功能的详细信息可以在本参考指南的相应部分找到。
默认主题/模式通过消息注解
移除检查异常
框架提供的 API 不再抛出已检查的 PulsarClientException
,而是抛出未检查的 PulsarException
。
如果您之前只是为了让编译器满意而捕获或重新抛出 PulsarClientException
,并没有实际处理该异常,您可以简单地删除您的 catch
或 throws
子句。如果您确实在处理该异常,那么您需要在您的 catch 子句中将 PulsarClientException
替换为 PulsarException
。
测试支持
spring-pulsar-test
模块现在可用,以帮助测试您的 Spring for Apache Pulsar 应用程序。有关更多详细信息,请参见 Testing Applications。