生成或使用消息时需要目标主题。 该框架在以下有序位置中查找以确定主题(在第一次找到时停止):
-
用户指定
-
消息类型默认
-
全局默认值
通过默认机制之一找到主题时,无需在 produce or consume API 上指定该主题。
当找不到主题时,API 将相应地抛出异常。
1. 用户指定
传递到正在使用的 API 的主题具有最高优先级(例如。 或 )。PulsarTemplate.send("my-topic", myMessage)
@PulsarListener(topics = "my-topic"
2. 消息类型默认
当没有主题传递到 API 中时,系统会查找为正在生成或使用的消息类型配置的消息类型到主题的映射。
可以使用该属性配置映射。
以下示例用于配置在使用或生成消息时使用的默认主题:spring.pulsar.defaults.type-mappings
application.yml
Foo
Bar
spring:
pulsar:
defaults:
type-mappings:
- message-type: com.acme.Foo
topic-name: foo-topic
- message-type: com.acme.Bar
topic-name: bar-topic
是邮件类的完全限定名称。message-type |
如果消息(或输入的第一条消息)是 ,则框架将无法从中确定主题。如果应用程序可能发送消息,则应使用另一种方法指定主题。Publisher null null |
2.1. 通过注解指定
当没有主题传递到 API 中并且没有配置自定义主题映射时,系统会查找正在生成或使用的消息类的注释。
可以通过注释上的属性指定默认主题。@PulsarMessage
topic
以下示例配置在生成或使用以下类型的消息时使用的默认主题:Foo
@PulsarMessage(topic = "foo-topic")
record Foo(String value) {
}
属性占位符和 SpEL 表达式在注释中受支持,
例如:@PulsarMessage
@PulsarMessage(topic = "${app.topics.foo}")
record Foo(String value) {
}
@PulsarMessage(topic = "#{someBean.getTopic()}")
record Bar(String value) {
}
是邮件类的完全限定名称。message-type |
如果消息(或输入的第一条消息)是 ,则框架将无法从中确定主题。如果应用程序可能发送消息,则应使用另一种方法指定主题。Publisher null null |
3. 生产者全局默认
(生产时)参考的最终位置是系统范围的生产者默认主题。
使用命令式 API 时通过属性配置,使用反应式 API 时通过属性配置。spring.pulsar.producer.topic-name
spring.pulsar.reactive.sender.topic-name
4. 消费者全局默认
咨询的最终位置(使用时)是系统范围的使用者默认主题。
使用命令式 API 时通过 or 属性配置它,使用反应式 API 时通过 or 属性之一配置它。spring.pulsar.consumer.topics
spring.pulsar.consumer.topics-pattern
spring.pulsar.reactive.consumer.topics
spring.pulsar.reactive.consumer.topics-pattern