java - 使用 Mina 和 Java DSL 的 Camel 负载平衡示例

标签 java apache-camel load-balancing failover spring-dsl

所以,最近我开始学习Camel。作为该过程的一部分,我决定浏览所有示例(列出 HERE 并在您 DOWNLOAD 包含所有示例和文档的包时可用)并看看我能学到什么。

其中一个示例,Load Balancing using Mina引起了我的注意,因为它在不同的 JVM 中使用了 Mina,并且它模拟了一个循环负载均衡器。

我对这个例子有一些问题。首先,它使用 Spring DSL,而不是我的项目使用的 Java DSL,我发现 Java DSL 现在更容易理解(主要也是因为我已经习惯了)。所以第一个问题:这个示例是否有一个仅使用 Java DSL 而不是 Spring DSL 来处理路由和 bean 的版本?

我的第二个问题与代码相关。描述指出,我引用:

Within this demo every ten seconds, a Report object is created from the Camel load balancer server. This object is sent by the Camel load balancer to a MINA server where the object is then serialized. One of the two MINA servers (localhost:9991 and localhost:9992) receives the object and enriches the message by setting the field reply of the Report object. The reply is sent back by the MINA server to the client, which then logs the reply on the console.

因此,从我读到的内容来看,我了解到 MINA 服务器 1(每个示例)从负载均衡器接收报告,更改它,然后将该报告发送回某个不可见的客户端。检查代码后,我发现没有客户端 java 类或 XML,当我运行时,服务器只是将结果发布在命令行上。客户在哪里??这个客户端是什么?

此处介绍的 MINA-1 服务器代码:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:camel="http://camel.apache.org/schema/spring"
       xsi:schemaLocation="
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
         http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

  <bean id="service" class="org.apache.camel.example.service.Reporting"/>

  <camelContext xmlns="http://camel.apache.org/schema/spring">

    <route id="mina1">
      <from uri="mina:tcp://localhost:9991"/>
      <setHeader headerName="minaServer">
        <constant>localhost:9991</constant>
      </setHeader>
      <bean ref="service" method="updateReport"/>
    </route>

  </camelContext>

</beans>

我不明白 updateReport() 方法如何神奇地在我的控制台上打印该对象。如果我想向第三个 MINA 服务器发送消息怎么办?我该怎么做呢? (我必须添加一条新路由,并将其发送到第三个服务器的 URI,对吗?)

我知道这些问题中的大多数可能听起来很愚蠢,但如果有人可以帮助我,我将不胜感激。 Java DSL 版本确实对我有帮助。

最佳答案

客户端在此 route 。

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:camel="http://camel.apache.org/schema/spring"
   xsi:schemaLocation="
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

  <bean id="service" class="org.apache.camel.example.service.Generator"/>

  <camelContext xmlns="http://camel.apache.org/schema/spring">

  <route id="sendMessage">
    <from uri="timer://org.apache.camel.example.loadbalancer?period=10s"/>
    <bean ref="service" method="createReport"/>
    <to uri="direct:loadbalance"/>
  </route>

    <!-- use failover load balancer in round robin mode, to automatic failover to next server
         in case of failure -->
  <route id="loadbalancer">
      <from uri="direct:loadbalance"/>
      <loadBalance inheritErrorHandler="false">
        <failover roundRobin="true"/>
        <to uri="mina:tcp://localhost:9991?sync=true"/>
        <to uri="mina:tcp://localhost:9992?sync=true"/>
      </loadBalance>
    <log message="${body}"/>
  </route>
  </camelContext>
</beans>

请注意,有一个时间部分:

<from uri="timer://org.apache.camel.example.loadbalancer?period=10s"/>

该组件调用类类型为org.apache.camel.example.service.Generator的服务bean的createReport方法。这是客户端。

要添加额外的 MINA 服务器,请使用以下 Spring DSL。

 <loadBalance inheritErrorHandler="false">
    <failover roundRobin="true"/>
    <to uri="mina:tcp://localhost:9991?sync=true"/>
    <to uri="mina:tcp://localhost:9992?sync=true"/>
    <to uri="mina:tcp://localhost:9993?sync=true"/>
 </loadBalance>

然后创建第三个 MINA 消费者,如下所示:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:camel="http://camel.apache.org/schema/spring"
   xsi:schemaLocation="
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

  <bean id="service" class="org.apache.camel.example.service.Reporting"/>

  <camelContext xmlns="http://camel.apache.org/schema/spring">

   <route id="mina2">
    <from uri="mina:tcp://localhost:9993"/>
     <setHeader headerName="minaServer">
       <constant>localhost:9993</constant>
     </setHeader>
     <bean ref="service" method="updateReport"/>
    </route>

  </camelContext>

</beans>

请注意,这将要求您在运行示例时另外启动 MINA3 服务器。客户端和负载均衡器路由(2条路由)位于同一个camel文件中。

我建议您学习如何阅读 Spring DSL,因为它确实值得付出努力。另外,如果您不熟悉 Spring,您确实需要入门。依赖注入(inject)部分尤其重要。

最后一个建议是给自己买一本《Camel In Action》。这确实是开始使用 Camel 的好方法。

如果您需要进一步说明,请询问。

关于java - 使用 Mina 和 Java DSL 的 Camel 负载平衡示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19758591/

相关文章:

amazon-web-services - AWS ELB 或域 url 重定向到 EC2 实例公共(public) DNS

java - 从 ANTLR 中的星号/加号规则获取值

java - 签名 PADES 顺序或并行

java - 有关 apache Camel 路线的任何信息

java - Camel 将 REDELIVERY_MAX_COUNTER 重置为 0 或 n 不起作用

java - 在 apache Camel 中使用自定义注册表

jms - 集群应用服务器中的JMS主题订阅者如何接收消息?

java - 如何在java中将 "&lt;/body&gt;"转换为 "</body>"

java - 如果不满足启动要求,终止计划的良好做法是什么

rabbitmq - 负载均衡器在RabbitMQ中的工作方式