基本Apache的骆驼负载平衡器故障转移实例 [英] Basic Apache Camel LoadBalancer Failover Example

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

问题描述

要开始我只想让你知道我是新来的骆驼和最近我就抓住它的主要概念。

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

我尝试使用Apache的骆驼和ActiveMQ作为经纪人和使用JMS组件为使用故障切换结构负载均衡器的客户端创建一个基本工作示例。所有这一切都是使用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.

贝娄是我的code为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();
    }
}

我已经使用考虑骆驼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
  2. 。要(字符串)方法
  3. 如何实际code MyApp的-B?我如何使它收到来自A的消息(这是一个不同的应用程序,可能在不同的机器),并将其发送到MyApp的-C(我认为如果我发现如何从MyApp的-A发送到MyApp的-B,我知道如何发送的MyApp从-B的MyApp到-C)?

  4. 如何将MyApp的-A检测MyApp的-B失败了吗?

  5. 我应该使用哪种骆驼组成部分?

如果你能提供任何反馈对我的code和如何解决我会更感激随后的问题。

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

推荐答案

经过一番努力,我已经找到一种方法来实现这一由阿帕奇提供的负载均衡器的例子立足自己。

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

我已经uploadded 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对应MINA服务器1在localhost:9991

  • MyApp的-C相当于MINA服务器3在localhost:9993

  • MyApp的-D对应于MINA服务器2在localhost:9992

  • 当MyApp的-C接到报告,它发回给MyApp的-A

此外,也不清楚何时,何地或为什么MyApp的-C回复MyApp的-A与更改的报告。是不是在Spring DSL code规定这种行为,至今没有人能向我解释这是为什么,即使发生了。

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:

此示例显示了如何轻松地使用骆驼MINA组件
  设计一个解决方案,允许容错方案,
  当一个服务器关闭重定向请求。这些服务器很简单
  TCP / IP服务器由Apache MINA框架创建并运行
  独立的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的报告
  服务器上的本地主机上运行:9993,然后返回报告
  客户端,以便它可以在控制台上打印出来。 MINA每个服务器将
  改变邮件的正文,所以你可以看到路由的
  报告不得不使用。如果由于某种原因(假设你pressed CTRL + C),
  在本地主机上运行的服务器MINA:9991是死的,那么
  负载均衡器会自动开始使用的MINA服务器上运行
  本地主机:9992。一旦这个MINA服务器收到的报告,它会
  发送回本地主机上运行的服务器MINA:9993像
  一切都没有发生过。如果本地主机: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

如果碰到任何问题,请让我们知道骆驼论坛结果
   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

佩德罗马丁斯!

修改

在previous后我有2个问题:
1.如何做到这一点在Java DSL
2.为什么米娜服务器发送回复。

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是在这里:
<一href=\"http://camel.465427.n5.nabble.com/Load-balancing-using-Mina-example-with-Java-DSL-td5742566.html#a5742585\" rel=\"nofollow\">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仓库。我希望我的code可以帮助的人。

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

这篇关于基本Apache的骆驼负载平衡器故障转移实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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