如何在 spring-集成中从网关发送响应

How to send response from a Gateway in spring-integration

我正在按照下面的示例来实施 spring-集成网关。

问题:无法在网关上获得从服务激活器发送的响应。

  1. 网关回复通道是“bestQuoteResponseChannel”。
  2. 服务激活器写入同一通道。

执行了所有自定义方法并编写了响应,但从未发送出去。知道这里缺少什么吗? 我的配置在流程中也有分散聚集。

<!--Gateway:-->
<channel id="bestQuoteResponseChannel"/>
<gateway id="loanBrokerGateway"
        default-request-channel="loanRequestsChannel"
        service-interface="org.springframework.integration.samples.loanbroker.LoanBrokerGateway">
    <method name="getBestLoanQuote" reply-channel="bestQuoteResponseChannel">
        <header name="RESPONSE_TYPE" value="BEST"/>
    </method>
</gateway>

<chain input-channel="loanRequestsChannel">
    <header-enricher>
        <header name="creditScore" expression="@creditBureau.getCreditReport(payload).score"/>
    </header-enricher>
    <recipient-list-router apply-sequence="true">
        <recipient selector-expression="headers.creditScore > 800" channel="exclusiveBankChannel"/>
        <recipient selector-expression="headers.creditScore > 750" channel="premiereBankChannel"/>
        <recipient selector-expression="headers.creditScore > 700" channel="qualityBankChannel"/>
        <recipient selector-expression="headers.creditScore > 650" channel="friendlyBankChannel"/>
        <recipient channel="easyBankChannel"/>
    </recipient-list-router>
</chain>

<!-- Messages are sent to the banks via bank channels and will be received and processed by an aggregator -->

<aggregator input-channel="loanQuotesChannel" output-channel="input" method="aggregateQuotes">
    <beans:bean class="org.springframework.integration.samples.loanbroker.LoanQuoteAggregator"/>
</aggregator>

<service-activator ref="findBestQuoteService" method="findBestQuote" input-channel="input" output-channel="bestQuoteResponseChannel"/>

首先,对于这种情况,您不需要网关上的 reply-channel,因此您不需要服务激活器上的 output-channel。所有回复逻辑都应落在 replyChannel header 上,当调用网关方法时,框架始终填充该 replyChannel header。

很高兴看到您的 findBestQuoteService.findBestQuote 做了什么,因为 LoanQuoteAggregator 已经为我们做了一切:

/**
 * Aggregates LoanQuote Messages to return a single reply Message.
 *
 * @param quotes list of loan quotes received from upstream lenders
 * @param responseType header that indicates the response type
 * @return the best {@link LoanQuote} if the 'RESPONSE_TYPE' header value is 'BEST' else all quotes
 */
public Object aggregateQuotes(List<LoanQuote> quotes,
        @Header(value="RESPONSE_TYPE", required=false) String responseType) {
    Collections.sort(quotes);
    return ("BEST".equals(responseType)) ? quotes.get(0) : quotes;
}

那么,您之后的定制服务有什么意义呢?

还可以考虑为 org.springframework.integration 类别打开 DEBUG 级别,以查看您的消息传输情况。可能我们也会在日志中看到一些问题。

我不明白你的逻辑怎么会失去 replyChannel header,但我们需要更多信息来调查。

我在 spring 集成流程中有 scatter-gather,然后才向网关发送回复。那不知何故阻止了它向网关发送响应。

所做的更改: 我用 publish-subscribe channelaggregator 组合替换了 scatter-gather 组件。 此更改后它工作正常。

https://docs.spring.io/spring-integration/docs/5.3.0.M1/reference/html/scatter-gather.html#scatter-gather-namespace