1

それで、最近Camelを学び始めました。プロセスの一環として、すべての例 (ここにリストされておりすべての例とドキュメントを含むパッケージをダウンロードすると利用可能になります) を調べて、学べることを確認することにしました。

例の 1 つであるMina を使用したロード バランシングは、異なる JVM で Mina を使用し、ラウンド ロビンでロード バランサーをシミュレートするため、私の注意を引きました。

この例にはいくつか問題があります。まず、私のプロジェクトで使用している Java DSL の代わりに、Spring DSL を使用します。これは、今でははるかに理解しやすくなっています (主に、これに慣れているためでもあります)。最初の質問: ルートと Bean に Spring DSL の代わりに Java DSL のみを使用するこの例のバージョンはありますか?

2 番目の質問はコード関連です。説明には次のように記載されています。

このデモでは、Camel ロード バランサー サーバーから Report オブジェクトが 10 秒ごとに作成されます。このオブジェクトは、Camel ロード バランサーによって MINA サーバーに送信され、そこでオブジェクトがシリアル化されます。2 つの MINA サーバー (localhost:9991 と localhost:9992) の 1 つがオブジェクトを受信し、Report オブジェクトのフィールド応答を設定してメッセージを充実させます。応答は MINA サーバーによってクライアントに送り返され、クライアントは応答をコンソールに記録します。

したがって、私が読んだことから、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()メソッドがコンソールにオブジェクトを魔法のように出力する方法がわかりません。3 番目の MINA サーバーにメッセージを送信したい場合はどうすればよいですか? どうすればいいですか?(新しいルートを追加して、それを 3 番目のサーバーの URI に正しく送信する必要がありますか?)

これらの質問のほとんどはばかげているように聞こえるかもしれませんが、誰かが私を助けてくれれば幸いです. これの Java DSL バージョンが本当に役に立ちます。

4

2 に答える 2

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.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"/>

このコンポーネントは、クラス type: であるサービス Bean のメソッド createReport を呼び出しますorg.apache.camel.example.service.Generator。これがクライアントです。

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>

次に、次のように 3 番目の 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 に慣れていない場合は、入門書が必要です。依存性注入の部分は特に重要です。

最後の推奨事項は、Camel In Action のコピーを自分で購入することです。これは、Camel を使い始めるのに最適な方法です。

さらに明確にする必要がある場合は、質問してください。

于 2013-11-04T06:05:24.677 に答える