前のチュートリアルでは、ファンアウトスイッチを作成しました。ブロードキャストの形で複数の消費者にメッセージを配信できます。
何をするか?ルーティング
このチュートリアルでは、メッセージの一部のみを購読できる新しい機能を追加します。たとえば、興味のある色(「オレンジ」、「ブラック」、「グリーン」)のみが接続され、すべてのメッセージがコンソールに印刷されます。
バインド
スイッチとキューは拘束力のある関係です。簡単な理解は、キューがこの交換の情報に関心があることです。
バインディングは、追加のパラメーターRoutingKeyで追加できます。 Spring-AMQPは、シンプルでわかりやすいAPI(ビルダーモード)を使用して、それらの関係を非常に明確にします。スイッチとキューをBindingBuilderに配置し、ルーティングキー(RoutingKey)でキューをスイッチに簡単にバインドします。
@BeanPublic Binding Binding0A(DirectExchange DirectExchange、Queue autodeletequeue0){return binding bindingbuilder.bind(autodeletequeue0).o(directexchange).with( "orange");}これは、バインディングキーがスイッチタイプに依存し、ファンアウトスイッチがバインドできるオプションなしでは実行できないことを意味します。
直接接続スイッチ
前のチュートリアルでは、メッセージングシステムは放送の形ですべての消費者に配信されました。色の種類に基づいてフィルターを含めるように機能を拡張したいと考えています。たとえば、情報や警告ログを受信せずに、プログラムが詳細なエラーメッセージを受信し、ログとしてハードディスクに書き込みます。
3つのルーティングキー:オレンジ、黒、緑
上の図に示すように、直接接続交換xに結合した2つのキューがあります。最初のキューにはルーティングキーがオレンジ色で、2つ目にはブラックとグリーンの2つのルーティングキーがあります。
この設定では、ルーティングキーを使用したメッセージがスイッチにプッシュされると、メッセージはキューQ1にルーティングされます。メッセージで使用されるルーティングキーが黒または緑の場合、Q2にルーティングされます。ルーティングキーを使用しない残りのメッセージは破棄されます。
平行結合
平行結合
これにより、ファンアウトスイッチと同様の関数を実装できます。
ほとんど、コードを見てください
config.java
パッケージcom.zb.rabbitmqtest.t4routing.config; Import org.springframework.amqp.core。*; import org.springframework.context.annotation.bean; Import org.springframework.context.annotation.configuration; "T4Config")パブリッククラスconfig {/***作成:Zhang bo* time:2018/3/5 10:45 am* @Apinote Define Direct-Connected Exchange*/@Bean Public DirectExchange DirectExchange(){return new DirectExchange( "Direct-Exchange"); }/***作成者:Zhang bo*時間:2018/3/5 10:48 am* @Apinote定義匿名キューを自動的に削除する*/@bean public queue autodeletequeue0(){return new Anonymousqueue(); }/***作成者:Zhang bo*時間:2018/3/5 10:48 am* @Apinote定義匿名キューを自動的に削除する*/@bean public queue autodeletequeue1(){return new Anonymousqueue(); }/***作成者:Zhang bo* time:2018/3/5 10:48 am* @param directexchangeダイレクト接続スイッチ* @param autodeletequeue0* @apinote binding @apinote binding @apinote binding binded direct-connected switch* @return binding* @return bindexchang autodeletequeue0){binding bindingbuilder.bind(autodeletequeue0).to(directexchange).with( "orange"); }/***作成者:Zhang bo* time:2018/3/5 10:48 am* @param directexchange directexchange* @param autodeletequeue0は、queueを自動的に削除します。 bindingbuilder.bind(autodeletequeue0).to(directexchange).with( "black"); } /** * Created by: Zhang Bo* Time: 2018/3/5 10:48 am * @param directExchange DirectExchange * @param autoDeleteQueue1 Automatically delete queue* @apiNote Binding using the autoDeleteQueue1 queue with the routing key black to the direct switch* @return Binding */ @Bean public Binding binding1a(DirectExchange directExchange, Queue autodeletequeue1){binding bindingbuilder.bind(autodeletequeue1).to(directexchange).with( "black"); }/***作成者:Zhang bo* time:2018/3/5 10:48 am* @param directexchange directexchange* @param autodeleteequeue1は、queueを自動的に削除します* @Apinoteバインディングダイレクトスイッチへのキーグリーンキューを使用してバインディングします。 bindingbuilder.bind(autodeletequeue1).to(directexchange).with( "green"); }} Receiver.java
パッケージcom.zb.rabbitmqtest.t4routing.receiver; Import org.springframework.amqp.rabbit.annotation.rabbitlistener; Import org.springframework.stereotype.component;/*** @author zhang bo*/ @componen @rabbitlistener(queues = "#{autodeletequeue0.name}")public void receiver0(string str){system.out.println( "Receiver0 ++++++++++++:"+str); } @rabbitlistener(queues = "#{autodeletequeue1.name}")public void receiver1(string str){system.out.println( "Receiver1 +++++++++++:"+str); }} send.java
パッケージcom.zb.rabbitmqtest.t4routing.send; import org.springframework.amqp.core.directexchange; Import org.springframework.amqp.rabbit.core.rabbittemplate; import org.springframework.beans.beans.beans.annotation.Annotation.Annotation. org.springframework.stereotype.component;/** * @author zhang bo【[email protected] @Autowiredプライベートrabbittemplate rabbittemplate; private string [] keys = {"orange"、 "black"、 "green"}; public void send(){string message = "hahaha"; for(int i = 0; i <5; i ++){system.out.println( "send ++++++++++++++:"。concat(message)); rabbittemplate.convertandsend(directexchange.getName()、keys [2]、message); }}}
sendtest.java
パッケージcom.zb.rabbitmqtest.t4routing.send; import org.junit.test; import org.junit.runner.runwith; import org.springframework.beans.factory.annotation.Autowired; Import org.springframework.test.context.context.context.context.context.context.context.context.context.context.context.context.context.context.contex org.springframework.test.context.junit4.springrunner;/*** @author zhang bo*/ @runwith(springrunner.class)@springboottestpublic class sendtest {@autowired private send; @test public void send()throws Exception {send.send(); }}テスト結果:キー[0]の場合、Receiver0のみがあります。キー[1]の場合、Receive0とReceed1を使用してブロードキャストに似ています。キー[2]の場合、receive1のみがあります。
キーの場合[0]
+++++++++++++++++:hahaha
+++++++++++++++++:hahaha
+++++++++++++++++:hahaha
+++++++++++++++++:hahaha
+++++++++++++++++:hahaha
Receiver0 +++++++++++++++:Hahahaha
Receiver0 +++++++++++++++:Hahahaha
Receiver0 +++++++++++++++:Hahahaha
Receiver0 +++++++++++++++:Hahahaha
Receiver0 +++++++++++++++:Hahahahaキーの場合[1]
+++++++++++++++++:hahaha
+++++++++++++++++:hahaha
+++++++++++++++++:hahaha
+++++++++++++++++:hahaha
+++++++++++++++++:hahaha
Receiver1 +++++++++++++++:Hahahaha
Receiver1 +++++++++++++++:Hahahaha
Receiver0 +++++++++++++++:Hahahaha
Receiver0 +++++++++++++++:Hahahaha
Receiver0 +++++++++++++++:Hahahaha
Receiver1 +++++++++++++++:Hahahaha
Receiver1 +++++++++++++++:Hahahaha
Receiver0 +++++++++++++++:Hahahaha
Receiver1 +++++++++++++++:Hahahaha
Receiver0 +++++++++++++++:Hahahahaキーの場合[2]
+++++++++++++++++:hahaha
+++++++++++++++++:hahaha
+++++++++++++++++:hahaha
+++++++++++++++++:hahaha
+++++++++++++++++:hahaha
Receiver1 +++++++++++++++:Hahahaha
Receiver1 +++++++++++++++:Hahahaha
Receiver1 +++++++++++++++:Hahahaha
Receiver1 +++++++++++++++:Hahahaha
Receiver1 +++++++++++++++:Hahahaha
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。