如需使用最新稳定版本,请使用 Spring Integration 7.0.4spring-doc.cadn.net.cn

为端点添加行为

在 Spring Integration 2.2 之前,您可以通过向轮询器的 <advice-chain/> 元素添加 AOP Advice,为整个集成流添加行为。 然而,假设您只想重试某个 REST Web Service 调用,而不重试任何下游端点。spring-doc.cadn.net.cn

例如,考虑以下流程:spring-doc.cadn.net.cn

inbound-adapter->poller->http-gateway1->http-gateway2->jdbc-outbound-adapter

如果您在轮询器的建议链中配置了一些重试逻辑,并且对 http-gateway2 的调用因网络故障而失败,则重试会导致 http-gateway1http-gateway2 被再次调用。 类似地,在 jdbc-outbound-adapter 发生暂时性故障后,两个 HTTP 网关都会被再次调用,然后才会再次调用 jdbc-outbound-adapterspring-doc.cadn.net.cn

Spring Integration 2.2 新增了对各个端点添加行为的能力。 这是通过向许多端点添加 <request-handler-advice-chain/> 元素来实现的。 以下示例展示了如何在 outbound-gateway 中使用 <request-handler-advice-chain/> 元素:spring-doc.cadn.net.cn

<int-http:outbound-gateway id="withAdvice"
    url-expression="'http://localhost/test1'"
    request-channel="requests"
    reply-channel="nextChannel">
    <int-http:request-handler-advice-chain>
        <ref bean="myRetryAdvice" />
    </int-http:request-handler-advice-chain>
</int-http:outbound-gateway>

在这种情况下,myRetryAdvice 仅本地应用于此网关,且在回复发送到 nextChannel 之后,不适用于下游采取的进一步操作。 建议的作用范围仅限于端点本身。spring-doc.cadn.net.cn

目前,您无法为整个 <chain/> 端点提供建议。 该架构不允许将 <request-handler-advice-chain> 作为链本身的子元素。spring-doc.cadn.net.cn

然而,可以在 <chain> 元素内的各个生成回复的端点中添加一个 <request-handler-advice-chain>。 例外情况是,在一个不生成回复的链中,由于链中的最后一个元素是 outbound-channel-adapter,因此该最后一个元素无法被增强(advised)。 如果您需要增强此类元素,必须将其移出链外(此时链的 output-channel 变为适配器的 input-channel)。 之后,适配器便可像往常一样被增强。 对于生成回复的链,每个子元素都可以被增强。spring-doc.cadn.net.cn