spring - Spring Cloud Stream Reactive,如何为生产者设置路由键
问题描述
Spring Cloud Stream 允许我们使用应用程序配置设置路由键,例如:
cloud.stream:
default:
contentType: "application/json"
rabbit.bindings:
output-resync-events:
producer:
prefix: "xxx."
routing-key-expression: '''command.service'''
bindings:
output-resync-events:
destination: "resync-requests"
但我需要routing-key
为每条消息发送动态设置。
我知道,BinderAwareChannelResolver
但似乎这只允许您创建一个新的Exchange
本身,这不是预期的结果。
我想使用相同TopicExchange
但不同routing-key
的s。
此外,是否可以使用 Spring Cloud Stream 的反应式 API 来实现这一点?
解决方案
它使用 SPEL,因此您可以像这样在运行时设置它:
messagingConfig.dataCopiedChannel().send(MessageBuilder.fromMessage(message).setHeader("type", "key-stuff").build());
messagingConfig 是您的接口,用于将绑定注入到类中以发送消息。
然后,您将 application.properties 配置为如下所示
spring.cloud.stream.bindings.data-copied.destination=data.topic.exchange
spring.cloud.stream.rabbit.bindings.data-copied.producer.routing-key-expression=headers.type
这会将其发送到 data.topic.exchange 并带有“key-stuff”的路由键。
推荐阅读
- python-3.x - matplotlib 图例位置令人费解
- unit-testing - 作为接口访问的单例的单元测试
- python-3.x - 使用 Python 通过 JSON 循环?
- c# - 如何调用方法 onEndEdit 输入字段或统一单击确定按钮?
- wpf - WPF - 按钮无法识别其图标路径
- ios - Objective C如何根据其他数组项从数组中删除项
- sql - 具有别名的同一列上的多个连接
- php - PHP使用特定标签删除div及其子元素
- php - Twilio 可编程视频:在小组会议室中是否可以拥有一对一的音频?
- r - 如何使用 stringr 包仅替换 R 中匹配项中的特定组?