MessagingGateway请求/响应最佳方法



我正在尝试让一个webflux引导应用程序调用一个单独的SCS引导应用程序。我之前发现的最适用的问题是这个。有没有一种方法可以创建一个不使用(@Input、@Output、@EnableBinding(不推荐使用的注释的请求/响应消息网关?

如果可能的话,我想在两个应用程序中使用更新的功能风格。到目前为止,这个maven项目是我最好/唯一的工作成果。我已经尝试了各种技术,包括让网关直接返回WebFlux消息类型。如果需要的话,我可以把它们挖出来,但我想,与其把这个问题弄得一团糟,不如把我现有的东西推出来。

谢谢,格伦

以下是如何使用Spring Cloud Stream配置请求-回复网关的示例:

@SpringBootApplication
public class SpringCloudStreamRequestReplyApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudStreamRequestReplyApplication.class, args);
}
@Bean
IntegrationFlow requestFlow(StreamBridge streamBridge) {
return IntegrationFlows.from(UpperCaseGateway.class)
.enrichHeaders(HeaderEnricherSpec::headerChannelsToString)
.handle(m -> streamBridge.send("requests", m))
.get();
}
@Bean
IntegrationFlow repliesFlow(HeaderChannelRegistry channelRegistry) {
return IntegrationFlows.from(MessageConsumer.class, gateway -> gateway.beanName("replies"))
.filter(Message.class,
m -> channelRegistry.channelNameToChannel(m.getHeaders().getReplyChannel().toString()) != null,
filterEndpointSpec -> filterEndpointSpec.throwExceptionOnRejection(true))
.get();
}
public interface UpperCaseGateway {
String toUpperCase(String payload);
}
public interface MessageConsumer extends Consumer<Message<String>> {
}
}

因此,在这种情况下,我们调用UpperCaseGateway.toUpperCase()API,它将通过SpringCloudStream绑定器向requests目的地发送请求。

单向MessageConsumerreplies网关表示具有以下属性的Spring Cloud Stream函数绑定:

spring.cloud.stream.bindings.replies-in-0.destination=replies

在这一点上,我们将回复与他们的请求关联起来,并使用Spring Integration的内置回复功能。

另一方(我称之为"服务器"(必须处理Message以执行所需的相关报头,并且必须接收来自requests目的地的请求并回复replies目的地。

相关内容

最新更新