我们可以在 camel-hystrix-eip 中的 onFallbackViaNetwork() 之前提供处理器吗

Can we provide a processor before onFallbackViaNetwork() in camel-hystrix-eip

我在我的项目中使用 camel-hystrix-eip,我想要在回退之前有一个处理器,但它不起作用

from("direct:sample").id("id:direct:sample").process(requestProcessor)
                .hystrix()
                .to(endPoint)
                .onFallbackViaNetwork()
                .to(fallback_endPoint)

我想使用处理器更改我的 fallback_endpoint,但似乎在 FallbackViaNetwork() 之后我们必须立即提供 to()。 请建议是否有任何方法。

我试过类似下面的方法,但它不起作用。

from("direct:sample").id("id:direct:sample").process(requestProcessor)
                .hystrix()
                .to(endPoint)
                .onFallbackViaNetwork()
                .process(fallbackProcessor)
                .to(fallback_endPoint)

实际上,我正在使用 requestProcessor 来覆盖实际的端点,并且在回退的情况下,fallback_endPoint 也会被覆盖,有什么办法可以避免这种情况。

您可以在 onFallbackViaNetwork() 之后拥有处理器。您还可以使用 toD EIP 将消息发送到动态端点。

根据您的代码,您可以设置包含新端点字符串的 Header MyEndpoint,然后使用 .toD("${header.MyEndpoint}") 引用它。每当您需要设置动态端点时重复此模式。

例如:

from("direct:sample")
    .process(new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {
            // do something
            exchange.getIn().setHeader("EndpointHystrix", "mock:hystrix");
        }
    })
    .hystrix()
        .toD("${header.EndpointHystrix}")
    .onFallbackViaNetwork()
        .process(new Processor() {
            @Override
            public void process(Exchange exchange) throws Exception {
                // do something more
                exchange.getIn().setHeader("EndpointFallback", "mock:fallback");
            }
        })
        .toD("${header.EndpointFallback}")
    .end()
    .to("...");

我已经在 Camel 2.20.0 和 2.21.0 中测试过了。