简体   繁体   English

Spring 云 stream 合并来自两个不同函数的响应

[英]Spring cloud stream merge responses from two different functions

I'm trying to use spring cloud stream to solve the following problem:我正在尝试使用 spring 云 stream 来解决以下问题:

在此处输入图像描述

I have a class that calls two separated functions (Function A and B), both of those functions must work in parallel if the Function A finishes it must call the Function C, the same happens if Function B finish but this will call Function D, then I need to wait for Function C and Function D to finish and merge both responses in a single response, and then return this merged response object to the starting class that must be waiting to get that response.我有一个 class 调用两个单独的函数(函数 A 和 B),如果 886982359588 A 完成,这两个函数必须并行工作,它必须调用 Function C,如果 Function B 完成,也会发生同样的情况,但这将调用 888698882359然后我需要等待 Function C 和 Function D 完成并将两个响应合并为一个响应,然后将此合并响应 object 返回到必须等待获得该响应的起始 class。

The problems I have are:我遇到的问题是:

  • How do I call Function C to pass the Function A response?如何调用 Function C 传递 Function A 响应?
  • How to wait until Function C and Function D finish and get their responses in Function E?如何等到 Function C 和 Function D 完成并在 Function E 中得到他们的响应?
  • How to wait for the response of Function E in the controller, I'm using streamBridge.send which to start Function A and Function B at the same time.如何等待 controller 中 Function E 的响应,我正在使用streamBridge.send同时启动 Function A 和 Function B。

I'm using我正在使用

  • spring-cloud-stream-3.1.3
  • spring-cloud-stream-binder-rabbit Required spring-cloud-stream-binder-rabbit必需

I cannot use Kafka Required我不能使用Kafka必需

Sample code示例代码

ServiceClass服务等级

@Service
@RequiredArgsConstructor
public class ServiceClass {

    @NonNull
    private final StreamBridge streamBridge;

    @Override
    protected MergedResponse execute(Input input) {
        var send1 = streamBridge.send("functionA-in-0", input);
        var send2 = streamBridge.send("functionB-in-0", input);

        //TODO: Wait for Function E response object
    }
}

Function A Function 一

@Slf4j
@Configuration
public class FunctionAClass{

    @Bean
    public Function<Input, OutputFunctionA> functionA() {
        return input -> {
            //TODO: Invoke Function C to pass OutputFunctionA object
            return OutputFunctionA.builder.build();
        };
    }
}

I don't mind using Supplier or Consumer instead of Function .我不介意使用SupplierConsumer而不是Function

Edit Hi, @Oleg Zhurakousky thanks for your help, to answer your question my problem is: I have to create a REST endpoint that consume N different third-party REST endpoints (two at first, async is a must as it will be too slow to process every request sequentially) I don't need all the data from them, just a few fields to build a common object. I'm planning to used streamBridge to start the first two functions that will to build the required request for each third-party, then a function to invoke each of their endpoints, then a function to build a common object with each response, and finally a function that collects the commons objects and send them in the response of my service.编辑你好,@Oleg Zhurakousky 感谢你的帮助,回答你的问题我的问题是:我必须创建一个 REST 端点,它使用N个不同的第三方 REST 端点(首先是两个,异步是必须的,因为它太慢了顺序处理每个请求)我不需要他们的所有数据,只需要几个字段来构建一个通用的 object。我打算使用streamBridge启动前两个函数,这些函数将为每个三分之一构建所需的请求-party,然后是一个 function 来调用他们的每个端点,然后是一个 function 来为每个响应构建一个公共的 object,最后是一个 function 来收集公共对象并将它们发送到我的服务的响应中。 Let me know if you have another question.如果您还有其他问题,请告诉我。

Regards.问候。

Couple of points.几点。

  1. Since you are introducing a synchronization point you will have in the end a blocking call no matter what you try since synchronization (such as your aggregation requirement) would have to wait, for two responses, correlate then etc. On top there is an issue of state that you are going to deal with and how to recover such state in the event of a system crash during aggregation.由于您正在引入一个同步点,因此无论您尝试什么,您最终都会有一个阻塞调用,因为同步(例如您的聚合要求)将不得不等待,等待两个响应,然后关联等等。最重要的是有一个问题您将要处理的 state 以及如何在聚合期间发生系统崩溃时恢复此类 state。
  2. Aggregation is not really a use case for sc-stream so we don't have a framework-based support for it.聚合并不是 sc-stream 的真正用例,因此我们没有基于框架的支持。 I would consider using Spring Integration framework to create pipelines for two+ different endpoints, aggregate then using [Aggregator pattern](aggregator pattern support ) and then use StreamBridge to send data out to target destination.我会考虑使用 Spring 集成框架为两个以上的不同端点创建管道,然后使用 [聚合器模式](聚合器模式支持)进行聚合,然后使用 StreamBridge 将数据发送到目标目的地。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM