https://docs.spring.io/spring-boot/docs/3.2.0/reference/htmlsingle/#messaging.pulsar
apache pulsar 通过提供 spring for apache pulsar 项目的自动配置而受到支持。
当类路径中存在 org.springframework.pulsar:spring-pulsar
时,spring boot 将自动配置并注册经典的(命令式)spring for apache pulsar 组件。当类路径中存在 org.springframework.pulsar:spring-pulsar-reactive
时,spring boot 也会对反应式组件执行相同的操作。
分别有适用于命令式和反应式使用的 spring-boot-starter-pulsar
和 spring-boot-starter-pulsar-reactive
“starters”,可方便地收集依赖项。
连接到pulsar
当使用 pulsar 启动器时,spring boot 将自动配置并注册一个 pulsarclient
bean。
默认情况下,应用程序尝试连接到位于 pulsar://localhost:6650
的本地 pulsar 实例。这可以通过将 spring.pulsar.client.service-url
属性设置为不同的值来进行调整。
注意:该值必须是有效的 pulsar 协议 url。
可以通过指定任何以 spring.pulsar.client.*
开头的应用程序属性来配置客户端。
如果需要更多控制权来配置 pulsarclient,请考虑注册一个或多个 pulsarclientbuildercustomizer
bean。
认证(authentication)
要连接到需要认证的 pulsar 集群,需要指定要使用哪个认证插件,通过设置 pluginclassname
和插件所需的任何参数。可以将参数设置为参数名称到参数值的映射。以下示例显示了如何配置 authenticationoauth2
插件。
spring.pulsar.client.authentication.plugin-class-name=org.apache.pulsar.client.impl.auth.oauth2.authenticationoauth2
spring.pulsar.client.authentication.param[issuerurl]=https://auth.server.cloud/
spring.pulsar.client.authentication.param[privatekey]=file:///users/some-key.json
spring.pulsar.client.authentication.param.audience=urn:sn:acme:dev:my-instance
注意:
需要确保在 spring.pulsar.client.authentication.param.*
下定义的名称与认证插件所期望的名称完全匹配(通常是驼峰命名法)。spring boot 不会尝试对这些条目进行任何形式的宽松绑定。
例如,如果想为 authenticationoauth2
认证插件配置issuer url,则必须使用 spring.pulsar.client.authentication.param.issuerurl
。如果使用其他形式,如 issuerurl
或 issuer-url
,则设置将不会应用于插件。
ssl
默认情况下,pulsar客户端以明文形式与pulsar服务进行通信。以下部分描述了如何配置pulsar客户端以使用tls加密(ssl)。一个先决条件是broker也已经配置为使用tls加密。
spring boot自动配置目前不支持任何tls/ssl配置属性。相反,你可以提供一个pulsarclientbuildercustomizer
,该定制器会在pulsar客户端构建器上设置必要的属性。pulsar支持privacy enhanced mail(pem)和java keystore(jks)两种证书格式。
按照以下步骤配置tls:
- 调整pulsar客户端服务url以使用
pulsar+ssl://
scheme 和tls端口(通常为6651
)。 - 调整管理客户端服务url以使用
https://
scheme 和tls web端口(通常为8443
)。 - 提供客户端构建器定制器,该定制器会在构建器上设置相关属性。
以响应式方式连接到pulsar
当reactive自动配置被激活时,spring boot将自动配置并注册一个reactivepulsarclient
bean。
连接到pulsar管理界面
spring for apache pulsar的pulsaradministration
客户端也实现了自动配置。
默认情况下,应用程序尝试连接到位于http://localhost:8080
的本地pulsar实例。可以通过将spring.pulsar.admin.service-url
属性设置为(http|https)://<host>:<port>
的不同值来调整此设置。
如果需要更多控制权来配置pulsaradmin
,请考虑注册一个或多个pulsaradminbuildercustomizer
bean。
认证
当访问需要身份验证的pulsar集群时,管理客户端需要与普通pulsar客户端相同的安全配置。可以通过将spring.pulsar.client.authentication
替换为spring.pulsar.admin.authentication
来使用上述身份验证配置。
提示:在启动时创建主题,请添加一个类型为pulsartopic
的bean。如果主题已经存在,则该bean将被忽略。
发送消息
spring的pulsartemplate
实现了自动配置,可以使用它来发送消息,如下所示:
import org.apache.pulsar.client.api.pulsarclientexception; import org.springframework.pulsar.core.pulsartemplate; import org.springframework.stereotype.component; @component public class mybean { private final pulsartemplate<string> pulsartemplate; public mybean(pulsartemplate<string> pulsartemplate) { this.pulsartemplate = pulsartemplate; } public void somemethod() throws pulsarclientexception { this.pulsartemplate.send("sometopic", "hello"); } }
pulsartemplate
依赖于pulsarproducerfactory
来创建底层的pulsar生产者。spring boot的自动配置也提供了这个生产者工厂,默认情况下,它会缓存所创建的生产者。你可以通过指定任何以spring.pulsar.producer.*
和 spring.pulsar.producer.cache.*
为前缀的应用属性来配置生产者工厂和缓存设置。
如果你需要对生产者工厂的配置进行更多的控制,考虑注册一个或多个producerbuildercustomizer
bean。这些定制器会应用于所有创建的生产者。你也可以在发送消息时传入一个producerbuildercustomizer
,只影响当前的生产者。
如果你需要对正在发送的消息进行更多的控制,你可以在发送消息时传入一个typedmessagebuildercustomizer
。
以响应式方式发送消息
当reactive自动配置被激活时,spring的reactivepulsartemplate
也会实现自动配置,可以使用它来发送消息,如下所示:
import org.springframework.pulsar.reactive.core.reactivepulsartemplate; import org.springframework.stereotype.component; @component public class mybean { private final reactivepulsartemplate<string> pulsartemplate; public mybean(reactivepulsartemplate<string> pulsartemplate) { this.pulsartemplate = pulsartemplate; } public void somemethod() { this.pulsartemplate.send("sometopic", "hello").subscribe(); } }
reactivepulsartemplate
依赖于reactivepulsarsenderfactory
来实际创建底层的发送器。spring boot的自动配置也提供了这个发送器工厂,默认情况下,它会缓存所创建的发送器。你可以通过指定任何以spring.pulsar.producer.*
和 spring.pulsar.producer.cache.*
为前缀的应用属性来配置发送器工厂和缓存设置。
如果你需要对发送器工厂的配置进行更多的控制,考虑注册一个或多个reactivemessagesenderbuildercustomizer
bean。这些定制器会应用于所有创建的发送器。你也可以在发送消息时传入一个reactivemessagesenderbuildercustomizer
,只影响当前的发送器。
如果你需要对正在发送的消息进行更多的控制,你可以在发送消息时传入一个messagespecbuildercustomizer
。
接收消息
当存在apache pulsar基础设施时,任何bean都可以通过添加@pulsarlistener
注解来创建监听器端点。以下组件在sometopic
主题上创建了一个监听器端点:
import org.springframework.pulsar.annotation.pulsarlistener; import org.springframework.stereotype.component; @component public class mybean { @pulsarlistener(topics = "sometopic") public void processmessage(string content) { // ... } }
spring boot的自动配置为pulsarlistener
提供了所有必要的组件,如pulsarlistenercontainerfactory
和用于构建底层pulsar消费者的消费者工厂。你可以通过指定任何以spring.pulsar.listener.*
和spring.pulsar.consumer.*
为前缀的应用属性来配置这些组件。
如果你需要对消费者工厂的配置进行更多的控制,考虑注册一个或多个consumerbuildercustomizer
bean。这些定制器会应用于工厂创建的所有消费者,因此适用于所有@pulsarlistener
实例。你还可以通过设置@pulsarlistener
注解的consumercustomizer
属性来定制单个监听器。
以响应式方式接收消息
当存在apache pulsar基础设施且reactive自动配置被激活时,任何bean都可以通过添加@reactivepulsarlistener
注解来创建响应式监听器端点。以下组件在sometopic
主题上创建了一个响应式监听器端点:
import reactor.core.publisher.mono; import org.springframework.pulsar.reactive.config.annotation.reactivepulsarlistener; import org.springframework.stereotype.component; @component public class mybean { @reactivepulsarlistener(topics = "sometopic") public mono<void> processmessage(string content) { // ... return mono.empty(); } }
spring boot的自动配置为reactivepulsarlistener
提供了所有必要的组件,如reactivepulsarlistenercontainerfactory
和用于构建底层响应式pulsar消费者的消费者工厂。你可以通过指定任何以spring.pulsar.listener.
和spring.pulsar.consumer.
为前缀的应用属性来配置这些组件。
如果你需要对消费者工厂的配置进行更多的控制,考虑注册一个或多个reactivemessageconsumerbuildercustomizer
bean。这些定制器会应用于工厂创建的所有消费者,因此适用于所有@reactivepulsarlistener
实例。你还可以通过设置@reactivepulsarlistener
注解的consumercustomizer
属性来定制单个监听器。
读取消息
pulsar的读取器接口使应用程序能够手动管理游标。当你使用读取器连接到主题时,你需要指定当读取器连接到主题时从哪个消息开始读取。
当存在apache pulsar基础设施时,任何bean都可以通过添加@pulsarreader
注解来使用读取器消费消息。以下组件创建了一个读取器端点,该端点从sometopic
主题的开头开始读取消息:
import org.springframework.pulsar.annotation.pulsarreader; import org.springframework.stereotype.component; @component public class mybean { @pulsarreader(topics = "sometopic", startmessageid = "earliest") public void processmessage(string content) { // ... } }
@pulsarreader
依赖于pulsarreaderfactory
来创建底层的pulsar读取器。spring boot的自动配置提供了这个读取器工厂,可以通过设置任何以spring.pulsar.reader.*
为前缀的应用属性来定制它。
如果你需要对读取器工厂的配置进行更多的控制,考虑注册一个或多个readerbuildercustomizer
bean。这些定制器会应用于工厂创建的所有读取器,因此适用于所有@pulsarreader
实例。你还可以通过设置@pulsarreader
注解的readercustomizer
属性来定制单个监听器。
以响应式方式读取消息
当存在apache pulsar基础设施且reactive自动配置被激活时,spring会提供reactivepulsarreaderfactory
,你可以使用它来创建读取器,以响应式的方式读取消息。以下组件使用提供的工厂创建一个读取器,并从sometopic
主题中读取5秒钟前的一条消息:
import java.time.instant; import java.util.list; import org.apache.pulsar.client.api.message; import org.apache.pulsar.client.api.schema; import org.apache.pulsar.reactive.client.api.startatspec; import reactor.core.publisher.mono; import org.springframework.pulsar.reactive.core.reactivemessagereaderbuildercustomizer; import org.springframework.pulsar.reactive.core.reactivepulsarreaderfactory; import org.springframework.stereotype.component; @component public class mybean { private final reactivepulsarreaderfactory<string> pulsarreaderfactory; public mybean(reactivepulsarreaderfactory<string> pulsarreaderfactory) { this.pulsarreaderfactory = pulsarreaderfactory; } public void somemethod() { reactivemessagereaderbuildercustomizer<string> readerbuildercustomizer = (readerbuilder) -> readerbuilder .topic("sometopic") .startatspec(startatspec.ofinstant(instant.now().minusseconds(5))); mono<message<string>> message = this.pulsarreaderfactory .createreader(schema.string, list.of(readerbuildercustomizer)) .readone(); // ... } }
spring boot的自动配置提供了这个读取器工厂,可以通过设置任何以spring.pulsar.reader.*
为前缀的应用属性来定制它。
如果你需要对读取器工厂的配置进行更多的控制,当使用工厂创建读取器时,考虑传入一个或多个reactivemessagereaderbuildercustomizer
实例。
如果你需要对读取器工厂的配置进行更多的控制,考虑注册一个或多个reactivemessagereaderbuildercustomizer
bean。这些定制器会应用于所有创建的读取器。你也可以在创建读取器时传入一个或多个reactivemessagereaderbuildercustomizer
,只将定制应用于创建的读取器。
额外的pulsar属性
只有pulsar支持的属性子集才能直接通过pulsarproperties
类使用。如果你希望使用额外的属性来调整自动配置的组件,而这些属性不被直接支持,你可以使用前面提到的每个组件支持的定制器。
到此这篇关于详解spring boot对 apache pulsar的支持的文章就介绍到这了,更多相关spring boot apache pulsar内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论