camel故障转移负载平衡器不会故障转移到下一个目标

lmvvr0a8  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(289)

我使用故障转移负载平衡器定义了驼峰路由,如下所示:

from(activemq:foo)
    .errorHandler(deadLetterChannel(activemq:foo.dlq).onPrepareFailure(failureProcessor))
    .process(processor)
    .loadBalance()
        .failover(2, true, true)
            .to(activemq:queue1, activemq:queue2)
    .end();

使用上面定义的路由,如果传递到queue1失败,异常将由错误处理程序处理,消息将直接放入foo.dlq,而负载平衡器不会故障转移到下一个目标。
如何定义路线:
它应该故障转移到所有路由,如果传递到所有路由失败(完全失败),它应该将控制发送到错误处理程序,错误处理程序应该将消息放入dlq。

pgx2nnw8

pgx2nnw81#

我想你应该转身 inheritErrorHandler (第2个参数)在负载平衡器中关闭,即:

.loadBalance()
   .failover(2, false, true)

这样,两个节点之一的任何故障都会传播到loadbalancer处理器,而不是dlq。
但是如果整个负载平衡过程失败(即:如果两个节点都失败),dlq将被踢入。
提示:如果缩进代码,传播逻辑会更清晰一些:

from("...")
  .errorHandler(...)
  .loadBalance().failover(2, false, true) // level 1
    .to("...") // level 2
    .to("...") // level 2
  .end();

您有两个识别级别;每个级别都有自己的错误处理:
级别2的错误由负载平衡器管理
级别1的错误由dlq管理

gudnpqoy

gudnpqoy2#

我很惊讶。我刚刚使用两个虚拟http端点运行了以下测试(在camel 3.9中):

from("timer://demo?period=60s")
    .errorHandler( deadLetterChannel("log:DLQ") )
    .log("Run")
    .loadBalance().failover(2, false, false)
        .to("direct:node1")
        .to("direct:node2")
    .end()
    .log("success");

from("direct:node1")
    .errorHandler( noErrorHandler() )
    .log("Attempt on node#1")
    .to("http://localhost:8881/ws");

from("direct:node2")
    .errorHandler( noErrorHandler() )
    .log("Attempt on node#2")
    .to("http://localhost:8882/ws");

下面是输出:

INFO  | route1                         | Camel (Test) thread #0 - t | Run
INFO  | route2                         | Camel (Test) thread #0 - t | Attempt on node#1
INFO  | route3                         | Camel (Test) thread #0 - t | Attempt on node#2
INFO  | DLQ                            | Camel (Test) thread #0 - t | Exchange[ExchangePattern: InOnly, BodyType: null, Body: [Body is null]]

您可以清楚地看到,每个节点上都有一次尝试,并且交换最终转到dlq
如果启用inheriterrorhandler,输出现在是:

INFO  | route1                         | Camel (Test) thread #0 - t | Run
INFO  | route2                         | Camel (Test) thread #0 - t | Attempt on node#1
INFO  | DLQ                            | Camel (Test) thread #0 - t | Exchange[ExchangePattern: InOnly, BodyType: null, Body: [Body is null]]

相关问题