跳到主要内容
版本:2.0.7

有什么新特性?

Doubao 中英对照 What’s new?

自 1.2 以来 2.0 的新特性​

本节介绍从版本 1.2 到版本 2.0 所做的变更。

Spring Retry 已被 Core Retry 取代​

Spring Retry 的所有用法都已被 Spring Framework 近期引入的更简单的重试机制取代。

这主要是重启消息容器时使用的内部实现细节。只有当你通过 PulsarContainerFactoryCustomizer bean 提供自定义 RetryTemplate 时,才会受到影响。

RetryTemplate 类在 Core Retry 中仍然存在,但包名已从 org.springframework.retry.support 改为 org.springframework.core.retry,API 也略有变化。

更多细节请参阅该 commit。

移除项​

先前已弃用的 API​

以下先前已弃用、并在版本 2.0.x 中标记为移除的 API,现已被移除:

  • org.springframework.pulsar.config.ConcurrentPulsarListenerContainerFactory#setConcurrency
  • org.springframework.pulsar.config.ConcurrentPulsarListenerContainerFactoryCustomizer
  • org.springframework.pulsar.config.ListenerContainerFactory#createListenerContainer
  • org.springframework.pulsar.config.ReaderContainerFactory#createReaderContainer
  • org.springframework.pulsar.config.ProducerBuilderConfigurationUtil
  • org.springframework.pulsar.config.PulsarClientProxy#getPartitionsForTopic
  • org.springframework.pulsar.config.PulsarTopic#builder
  • org.springframework.pulsar.config.PulsarTopic#getFullyQualifiedTopicName
  • org.springframework.pulsar.config.Resolved#get
  • org.springframework.pulsar.test.support.model.UserPojo
  • org.springframework.pulsar.test.support.model.UserRecord

响应式支持​

响应式支持已从 Spring Pulsar 2.0.0 中移除——不再发布 spring-pulsar-reactive 模块。

自 1.1 以来 1.2 的新特性​

本节介绍从版本 1.1 到版本 1.2 所做的变更。

自定义 Object Mapper​

你可以提供自己的 Jackson ObjectMapper,Pulsar 在生产和消费 JSON 消息时会使用它。更多细节请参阅自定义 Object Mapper。

默认租户与命名空间​

当针对非完全限定的 topic URL 生产或消费消息时,你可以指定要使用的默认租户和/或命名空间。更多细节请参阅默认租户/命名空间。

消息容器启动策略​

现在你可以将消息监听容器启动失败策略配置为 stop、continue 或 retry。更多细节请参阅所支持容器之一的相应章节:@PulsarListener 或 @PulsarReader。

消息容器工厂定制器(Spring Boot)​

Spring Boot 引入了通用的消息容器工厂定制器 org.springframework.boot.pulsar.autoconfigure.PulsarContainerFactoryCustomizer<T extends PulsarContainerFactory<?, ?>>,可用于进一步配置一个或多个自动配置的容器工厂,这些工厂支撑以下监听注解:

  • 对于 @PulsarListener,注册一个或多个 PulsarContainerFactoryCustomizer<ConcurrentPulsarListenerContainerFactory<?>> bean。
  • 对于 @PulsarReader,注册一个或多个 PulsarContainerFactoryCustomizer<DefaultPulsarReaderContainerFactory<?>> bean。

弃用项​

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,并以 domain、tenant 和 namespace 的默认值进行配置。因此,如果你使用 Spring Boot,只需在需要处注入该 builder 即可。否则,直接使用 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.pulsar.autoconfigure.PulsarContainerFactoryCustomizer<T extends PulsarContainerFactory<?, ?>>,从而不再需要该定制器。

将所有 ConcurrentPulsarListenerContainerFactoryCustomizer 替换为 org.springframework.boot.pulsar.autoconfigure.PulsarContainerFactoryCustomizer<ConcurrentPulsarListenerContainerFactoryCustomizer<?>>。

移除项​

以下先前已弃用的监听端点适配器已被移除,转而使用监听端点接口中的默认方法:

  • org.springframework.pulsar.config.PulsarListenerEndpointAdapter
  • org.springframework.pulsar.reactive.config.ReactivePulsarListenerEndpointAdapter

破坏性变更​

PulsarTopic#<init>​

PulsarTopic 构造函数现在要求传入完全限定的 topic 名称(domain://tenant/namespace/name)。如果你在调用该构造函数,请确保传入的 topic 是完全限定的。更好的替代方案是使用 PulsarTopicBuilder,因为它不要求完全限定名称,并会为指定名称中缺失的组成部分补充默认值。

PulsarReaderFactory#createReader​

PulsarReaderFactory#createReader API 现在抛出非受检的 PulsarException,而不是受检的 PulsarClientException。请相应地替换该 API 上的所有 try/catch 块。

自 1.0 以来 1.1 的新特性​

本节介绍从版本 1.0 到版本 1.1 所做的变更。

Auto Schema 支持​

如果无法提前知道 Pulsar topic 的 schema,你可以使用 AUTO Schema 向 broker 生产/从 broker 消费通用记录。更多细节请参阅使用 AUTO_SCHEMA 生产和使用 AUTO_SCHEMA 消费。

备注

虽然上述链接聚焦于 PulsarTemplate 和 @PulsarListener,但该特性同样受支持于 ReactivePulsarTemplate、@ReactivePulsarListener 和 @PulsarReader。各自的细节可在本参考指南的相应章节中找到。

通过消息注解指定默认 topic/schema​

现在你可以用 @PulsarMessage 标记一个消息类,以指定生产/消费该类型消息时要使用的默认 topic和/或默认 schema。

移除受检异常​

框架提供的 API 不再抛出受检的 PulsarClientException,而是抛出非受检的 PulsarException。

注意

如果你此前只是为了让编译器通过而捕获或重新抛出 PulsarClientException,而并未实际处理该异常,那么可以直接移除你的 catch 或 throws 子句。如果你确实在处理该异常,则需要在 catch 子句中将 PulsarClientException 替换为 PulsarException。

测试支持​

spring-pulsar-test 模块现已可用,用于帮助你测试 Spring for Apache Pulsar 应用程序。更多细节请参阅测试应用程序。