基本的 Apache Camel LoadBalancer 故障转移示例 [英] Basic Apache Camel LoadBalancer Failover Example

查看:29
本文介绍了基本的 Apache Camel LoadBalancer 故障转移示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

To start I just want to let you know I am new to Camel and very recently I grasped its main concepts.

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

I am trying to create a basic working example using Apache-Camel with ActiveMQ as a broker and using jms-component as a client of a loadbalancer using the failover construct. All this is done using the Java DSL only (if possible).

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

The example consists of 4 main apps, called MyApp-A, MyApp-B, MyApp-C and MyApp-D. In a normal scenario MyApp-A reads a file from my computer and then transforms it into a message. Then it sends that message to MyApp-B and MyApp-B sends it to MyApp-C.

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

However, there is a fail scenario. In this scenario, MyApp-A fails to send the message to MyApp-B. It then send the message to MyApp-D, which in turn sends it to MyApp-C.

Bellow 是我的 MyApp-A 代码

Bellow is the my code for 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 中获取.

I have considered using the camel-ftp but it would not work because MyApp-C would not know that MyApp-B died and would not know that it had to fetch from MyApp-D.

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

Now I have several problems and questions:

  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. 我应该使用哪个骆驼组件?

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

If you could provide any feedback on my code and on how to fix the problem I would be more then grateful.

推荐答案

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

After much effort, I have found a way to implement this basing myself on the loadbalancer example provided by apache.

我已将 eclipse 项目上传到我的 github 帐户,您可以在这里检查它是否工作:

I have uploadded the eclipse project to my github account, you can check it working here:

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

Although my example does respect the overall intended architecture, it does have few differences as explained bellow:

  • 它使用 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 代码中指定,到目前为止没有人能够向我解释为什么会发生这种情况.

Furthermore, it is also not clear when, where or why MyApp-C replies to MyApp-A with the changed report. This behavior is not specified in the Spring DSL code and so far no one was able to explain to me why this is even happening.

所以仍然存在两个问题:

So two problems remain:

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

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

In case you are interested, Here is the README.txt I created, with an accurate description of the problem:

这个例子展示了如何轻松地使用 Camel-MINA 组件设计一个允许容错解决方案的解决方案当服务器关闭时重定向请求.这些服务器很简单由 Apache MINA 框架创建并运行的 TCP/IP 服务器独立的 JVM.

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.

在此示例中,负载均衡器客户端将每隔10 秒,然后将该报告发送到运行的 MINA 服务器本地主机:9991.该服务器然后将报告转发给 MINA在 localhost:9993 上运行的服务器,然后将报告返回给客户端,以便它可以在控制台上打印它.每个 MINA 服务器将更改消息的正文,以便您可以看到报告不得不使用.如果出于某种原因(假设您按了 CTRL+C),在 localhost:9991 上运行的 MINA 服务器已经死了,然后负载均衡器将自动开始使用运行的 MINA 服务器本地主机:9992.一旦这个 MINA 服务器收到报告,它会将其发送回在 localhost:9993 上运行的 MINA 服务器,例如什么都没有发生.如果 localhost:9991 再次备份,则负载均衡器将再次开始使用它.

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.

负载均衡器将始终尝试使用 localhost:9991无论如何都尝试使用 localhost:9992.

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

要在您的 Maven 存储库中编译和安装项目,请执行在项目根目录下执行以下命令

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

mvn 全新安装

要运行示例,然后在各自的文件夹:

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

mina1:
mvn exec:java -Pmina1

mina1:
mvn exec:java -Pmina1

mina2:mvn exec:java -Pmina2

mina2: mvn exec:java -Pmina2

mina3:mvn exec:java -Pmina3

mina3: mvn exec:java -Pmina3

负载均衡:mvn exec:java -Ploadbalancer

loadbalancing: mvn exec:java -Ploadbalancer

如果您遇到任何问题,请在 Camel 论坛上告诉我们
http://camel.apache.org/discussion-forums.html

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

佩德罗·马丁斯!

<小时>

编辑

在上一篇文章中,我有两个问题:1.如何在java dsl中做到这一点2.为什么mina服务器会发送回复.

In the previous post I had 2 questions: 1. how to do this in java dsl 2. why are the mina servers sending replies.

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

I will attack problem 1 eventually, but I just want to state that the solution to problem 2 is here: http://camel.465427.n5.nabble.com/Load-balancing-using-Mina-example-with-Java-DSL-td5742566.html#a5742585

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

Kudos to Mr. Claus for the answer and suggestions.

编辑

现在这两个问题都解决了,它们都在同一个 git 存储库中.我希望我的代码可以帮助人们.

Both problems are now solved and they are both in the same git repository. I hope my code helps people.

这篇关于基本的 Apache Camel LoadBalancer 故障转移示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆