java - 基本 Apache Camel LoadBalancer 故障转移示例

标签 java ftp apache-camel load-balancing failover

首先,我只想让您知道我是 Camel 的新手,最近我掌握了它的主要概念。

我正在尝试创建一个基本的工作示例,使用 Apache-Camel 和 ActiveMQ 作为代理,并使用 jms-component 作为使用故障转移构造的负载均衡器的客户端。所有这些都是仅使用 Java DSL 完成的(如果可能)。

该示例包含 4 个主要应用程序,分别称为 MyApp-A、MyApp-B、MyApp-C 和 MyApp-D。在正常情况下,MyApp-A 从我的计算机读取文件,然后将其转换为消息。然后它将该消息发送到 MyApp-B,MyApp-B 将其发送到 MyApp-C。

enter image description here

但是,有一个失败的场景。在这种情况下,MyApp-A 无法将消息发送到 MyApp-B。然后它将消息发送到 MyApp-D,MyApp-D 又将其发送到 MyApp-C。

enter image description here

下面是我的 MyApp-A 代码

public class MyApp-A {

    public static void main(String args[]) throws Exception {
        // create CamelContext
        CamelContext context = new DefaultCamelContext();

        // connect to embedded ActiveMQ JMS broker
        ConnectionFactory connectionFactory = 
            new ActiveMQConnectionFactory("vm://localhost");
        context.addComponent("jms",
            JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));

        // add our route to the CamelContext
        context.addRoutes(new RouteBuilder() {
            @Override
            public void configure() {
                from("file:data/inbox?noop=true")loadbalancer().failover().to("MyApp-B:incomingOrders").to("MyApp-D:incomingOrders").end();
            }
        });

        // start the route and let it do its work
        context.start();
        Thread.sleep(10000);

        // stop the CamelContext
        context.stop();
    }
}

我考虑过使用 camel-ftp但它不会工作,因为 MyApp-C 不知道 MyApp-B 死了,也不知道它必须从 MyApp-D 获取数据。

现在我有几个问题和疑问:

  1. 如何从 MyApp-A 向另一个应用程序 MyApp-B 发送消息(在本例中为文件)?我实际上应该在 Java DSL 的 .to(String) 方法中放入什么?
  2. 我实际上如何编写 MyApp-B 代码?我如何让它从 A(这是一个不同的应用程序,可能在不同的机器上)接收消息并将其发送到 MyApp-C(我假设如果我发现如何从 MyApp-A 发送到 MyApp-B,我会知道如何从 MyApp-B 发送到 MyApp-C)?
  3. MyApp-A 将如何检测到 MyApp-B 失败?
  4. 我应该使用哪个 Camel 组件?

如果您能就我的代码以及如何解决问题提供任何反馈,我将不胜感激。

最佳答案

经过一番努力,我找到了一种基于apache提供的负载均衡器示例实现这一点的方法。

我已经将 eclipse 项目上传到我的 github 帐户,你可以在这里查看它的工作情况:

虽然我的示例确实尊重整体预期架构,但它确实有一些差异,如下所述:

  • 它使用 Spring DSL 而不是 Java DSL
  • MyApp-A 是负载均衡器。每 10 个它生成一个报告(而不是读取文件)并将其发送到 MyApp-B。
  • MyApp-B 对应 localhost:9991 上的 MINA 服务器 1
  • MyApp-C 对应 localhost:9993 上的 MINA 服务器 3
  • MyApp-D 对应 localhost:9992 上的 MINA 服务器 2
  • 当 MyApp-C 收到报告时,它会将其发送回 MyApp-A

此外,也不清楚 MyApp-C 何时、何地或为何用更改后的报告回复 MyApp-A。 Spring DSL 代码中未指定此行为,到目前为止,没有人能够向我解释为什么会发生这种情况。

所以还有两个问题:

  1. 这将如何使用 Java DSL 完成
  2. 为什么 MyApp-C 会回复 MyApp-A,它是如何回复的?

如果您有兴趣,这是我创建的 README.txt,其中包含对问题的准确描述:

Load balancing with MINA Example

This example shows how you can easily use the Camel-MINA component to design a solution allowing for a fault tolerant solution that redirects requests when a server is down. These servers are simple TCP/IP servers created by the Apache MINA framework and run in separate JVMs.

In this example, the load balancer client will generate a report every 10 seconds and send that report to the MINA server running on localhost:9991. This server then forwards the report to the MINA server running on localhost:9993, which then returns the report to the client so it can print it on the console. Each MINA server will change the body of the message so you can see the routes that the report had to use. If for some reason (lets say you pressed CTRL+C), the MINA server running on localhost:9991 is dead, then the loadbalancer will automatically start using the MINA server running on localhost:9992. Once the this MINA server receives the report, it will send it back to the MINA server running on localhost:9993 like nothing has ever happened. If localhost:9991 gets back up again, then the loadbalancer will start using it again.

The load balancer will always attempt to use localhost:9991 before trying to use localhost:9992 no matter what.

Running the example

To compile and install the project in your maven repo, execute the following command on the root of the project

mvn clean install

To run the example, then execute the following command in the respective folder:

mina1:
mvn exec:java -Pmina1

mina2: mvn exec:java -Pmina2

mina3: mvn exec:java -Pmina3

loadbalancing: mvn exec:java -Ploadbalancer

If you hit any problems please let us know on the Camel Forums
http://camel.apache.org/discussion-forums.html


Pedro Martins !


编辑

在上一篇文章中我有两个问题: 1. 如何在 java dsl 中执行此操作 2.为什么mina服务器会回复。

我最终会攻击问题 1,但我只想声明问题 2 的解决方案在这里: http://camel.465427.n5.nabble.com/Load-balancing-using-Mina-example-with-Java-DSL-td5742566.html#a5742585

感谢克劳斯先生的回答和建议。


编辑

这两个问题现在都已解决,并且它们都在同一个 git 存储库中。我希望我的代码对人们有所帮助。

关于java - 基本 Apache Camel LoadBalancer 故障转移示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19745773/

相关文章:

java - 从系统外部与 Akka Actor 通信的多种方式?

java - Camel EIP 过滤重复项

scripting - 可编写脚本的 FTP 客户端

java - ftp4j 客户端下载文件时 FTP 服务器死机

spring - @CamelSpringTest 和@CamelSpringBootTest 有什么区别?

java - Apache Camel Netty4内存使用率高

java - Spring Boot 分页

java - ExpandAbleListView 出现 Cannot instantiate the type ExpandableListAdapter 错误

java - 错误 sequenceiq/hadoop-docker 写入文件

服务器名称不允许 FTP 连接