1。序文
メッセージミドルウェアには、メッセージブローカーと宛先という2つの重要な概念があります。メッセージ送信者がメッセージを送信すると、メッセージがメッセージブローカーによって引き継がれ、メッセージが指定された宛先に配信されることが保証されます。
一般的に使用されるメッセージブローカーには、JMSおよびAMQP仕様が含まれます。それに対応して、それらの一般的な実装はActivemqとrabbitmqです。
2。ActiveMQを統合します
2.1依存関係を追加します
<Dependency> groupId> org.springframework.boot </groupid> <artifactid> spring-boot-starter-activemq </artifactid> </dependency> <! - 次の依存関係を追加する必要がある場合、依存関係を追加する必要がある場合、 </依存関係>
2.2構成を追加します
#activemq configure spring.activemq.broker-url = tcp://192.168.2.12:61616spring.activemq.user = adminspring.activemq.password構成は、truespring.jms.pub-sub-domain = falseに設定する必要があります
ここでspring.activemq.pool.enabled = falseは、接続プールを閉じることを意味します。
2.3コーディング
構成クラス:
@configurationPublic class jmsconfirguration {public static final string queue_name = "Activemq_queue"; public static final string topic_name = "Activemq_topic"; @bean public queue queue(){return new ActivemQqueue(queue_name); } @bean public topic(){return new ActivemQtopic(topic_name); }}キューとトピックの作成を担当します。
メッセージプロデューサー:
@componentpublic class jmssender {@autowired private queue queue; @Autowiredプライベートトピックトピック。 @AutowiredプライベートJMSMESSAGINGTEMPLATE JMSTEMPLATE; public void sendbyqueue(string message){this.jmstemplate.convertandsend(queue、message); } public void sendbytopic(string message){this.jmstemplate.convertandsend(topic、message); }}メッセージ消費者:
@componentPublic class jmsreceiver {@jmslistener(destination = jmsconfirguration.queue_name)public void receivebyqueue(string message){system.out.println( "receive queue message:" + message); } @jmslistener(destination = jmsconfirguration.topic_name)public void receivebytopic(string message){system.out.println( "トピックメッセージを受信:" +メッセージ); }}メッセージ消費者は、@jmslistener annotationを使用してメッセージを聴きます。
2.4テスト
@runwith(springrunner.class)@springboottestpublic class jmstest {@autowired private jmssender sender; @test public void testsendbyqueue(){for(int i = 1; i <6; i ++){this.sender.sendbyqueue( "hello activemq queue"+i); }} @test public void testsendbytopic(){for(int i = 1; i <6; i ++){this.sender.sendbytopic( "hello activemqトピック"+i); }}}印刷結果:
キューメッセージの受信:こんにちはActivemqキュー1
キューの受信メッセージ:Hello ActiveMQキュー2
キューの受信メッセージ:こんにちはActivemqキュー3
キューメッセージの受信:こんにちはActivemqキュー4
キューメッセージの受信:こんにちはActivemqキュー5
spring.jms.pub-sub-domain = Trueを公開/購読モードのテスト時に設定します
トピックメッセージを受け取る:こんにちはActivemqトピック1
トピックメッセージを受信する:こんにちはActivemqトピック2
トピックメッセージの受信:こんにちはActiveMQトピック3
トピックメッセージの受信:こんにちはActivemqトピック4
トピックメッセージの受信:こんにちはActivemqトピック5
3。rabbitmqを統合します
3.1依存関係を追加します
<Dependency> groupId> org.springframework.boot </groupid> <artifactid> spring-boot-starter-amqp </artifactid> </dependency>
3.2構成を追加します
spring.rabbitmq.host = 192.168.2.30spring.rabbitmq.port = 5672spring.rabbitmq.username = lightspring.rabbitmq.password = lightspring.rabbitmq.virtual-host =/テスト
3.3コーディング
構成クラス:
@configurationPublic class amqpconfirguration {// ============================================================== @bean public queue queue(){return new queue(simple_queue、true); } // ================ wublic static final string ps_queue_1 = "ps_queue_1"; public static final string ps_queue_2 = "ps_queue_2"; public static final string fanout_exchange = "fanout_exchange"; @bean public queue psqueue1(){return new queue(ps_queue_1、true); } @bean public queue psque2(){return new queue(ps_queue_2、true); } @bean public fanoutexchange fanoutexchange(){return new fanoutexchange(fanout_exchange); } @bean public binding fanoutbinding1(){return bindingbuilder.bind(psque1())。to(fanoutexchange()); } @bean public binding fanoutbinding2(){return bindingbuilder.bind(psque2())。to(fanoutexchange()); } // ========================= [public static final String routing_queue_1 = "routing_queue_1"; public static final string routing_queue_2 = "routing_queue_2"; public static final string direct_exchange = "direct_exchange"; @bean public queue routingqueue1(){return new queue(routing_queue_1、true); } @bean public queue routingqueue2(){return new queue(routing_queue_2、true); } @bean public directExchange directExchange(){return new DirectExchange(direct_exchange); } @bean public binding directbinding1(){bindingbuildbuilder.bind(routingqueue1())。to(directexchange())。with( "user"); } @bean public binding directbinding2(){return bindingbuilder.bind(routingqueue2())。to(directExchange())。 } // ======================================= [パブリックサイチックファイナル文字列topic_queue_1 = "topic_queue_1"; public static final string topic_queue_2 = "topic_queue_2"; public static final string topic_exchange = "topic_exchange"; @bean public queue topicqueue1(){return new queue(topic_queue_1、true); } @bean public queue topicqueue2(){return new queue(topic_queue_2、true); } @bean public topicexchange topicexchange(){return new topicexchange(topic_exchange); } @bean public binding topicbinding1(){return bindingbuilder.bind(topicqueue1())。to(topicexchange())。 } @bean public binding topicbinding2(){return bindingbuilder.bind(topicqueue2())。to(topicexchange())。 }}RabbitMQには複数の作業モードがあるため、多くの構成があります。関連するコンテンツについて知りたい読者は、「RabbitMQワークモードの紹介」またはBaidu関連情報を自分でチェックすることができます。
メッセージプロデューサー:
@componentPublic class amqpsender {@autowired private amqptemplate amqptemplate; / ** * simpleモード送信 * * @paramメッセージ */ public void simplesend(string message){this.amqptemplate.convertandsend(amqpconfirguration.simple_queue、message); }/** * publish/subscribe mode send * * @param message */public void pssend(string message){this.amqptemplate.convertandsend(amqpconfirguration.fanout_exchange、 ""、message); } / ** *ルーティングモードを送信 * * @param message * / public void routingend(string routingkey、string message){this.amqptemplate.convertandsend(amqpconfirguration.direct_exchange、routingkey、message); } / ** *テーマモードを送信 * * @param routingKey * @param message * / public void topicsend(string routingkey、string message){this.amqptemplate.convertandsend(amqpconfirguration.topic_exchange、routingkey、message); }}メッセージ消費者:
@componentPublic class amqpreceiver { / ** *シンプルモード受信 * * @paramメッセージ * / @RabbitListener(queues = amqpconfirguration.simple_queue)public void simplereceive(string message){system.out.println( "受信メッセージ:" +メッセージ); }/** *公開/購読モードレセプション * * @paramメッセージ */@RabbitListener(queues = amqpconfirguration.ps_queue_1)public void psreceive1(string message){system.out.println(amqpconfirguration.ps_queue_1 + "メッセージ:" +メッセージ); } @rabbitlistener(queues = amqpconfirguration.ps_queue_2)public void psreceive2(string message){system.out.println(amqpconfirguration.ps_queue_2 + "メッセージ:" +メッセージ); } / ** *ルーティングモードレセプション * * @paramメッセージ * / @RabbitListener(Queues = amqpconfirguration.Routing_Queue_1)public void routingReceive1(System.out.Out.println(amqpconfirguration.routing_queue_1 + "メッセージの受信:" +メッセージ); } @rabbitlistener(queues = amqpconfirguration.routing_queue_2)public void routingreceive2(string message){system.out.println(amqpconfirguration.routing_que_2 + "受信メッセージ:" +メッセージ); } / ** *トピックモードレセプション * * @paramメッセージ * / @RabbitListener(queues = amqpconfirguration.topic_queue_1)public void topopeRece1(System.out.out.print.println(amqpconfirguration.topic_queue_1 + "メッセージを受信:" +メッセージ); } @rabbitlistener(queues = amqpconfirguration.topic_queue_2)public void topicreceive2(string message){system.out.println(amqpconfirguration.topic_queue_2 + "受信メッセージ:" +メッセージ); }}メッセージ消費者は、@RabbitListener Annotationを使用してメッセージを聞きます。
3.4テスト
@runwith(springrunner.class)@springboottestpublic class amqptest {@autowired private amqpsender sender; @test public void testsimplesend(){for(int i = 1; i <6; i ++){this.sender.simplesend( "test simplesend"+i); }} @test public void testpssend(){for(int i = 1; i <6; i ++){this.sender.pssend( "test pssend"+i); }} @test public void testpssend(){for(int i = 1; i <6; i ++){this.sender.pssend( "test pssend"+i); }} @test public void testroutingsend(){for(int i = 1; i <6; i ++){this.sender.routingsend( "order"、 "test routingend"+i); }} @test public void testtopicsend(){for(int i = 1; i <6; i ++){this.sender.topicsend( "user.add"、 "test topicsend"+i); }}}テスト結果がスキップされます。 。 。
リマインダー1:Access_Refusedログインは、認証メカニズムプレーンを使用して拒否されました
解決:
1)ユーザー名とパスワードが正しいことを確認してください。ユーザー名とパスワードの値にスペースまたはタブが含まれているかどうかに注意することが重要です(パスワードにもう1つのタブ文字があり、認証障害が発生したため、著者がテストしました)。
2)テストアカウントがゲストを使用している場合、rabbitmq.confファイルを変更する必要があります。 「loopback_users = none」構成をファイルに追加します。
リスナーのキューを準備できません。キューが存在しないか、ブローカーが私たちにそれを使用することを許可しません
解決:
rabbitmq管理インターフェイスにログインして、キューオプションに対応するキューを手動で追加できます。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。