springboot2.x統合のRabbitMQ

まず、その依存関係

        <依存性> 
            <のgroupId> org.springframework.boot </のgroupId> 
            <たartifactId>ばねブートスタータAMQP </たartifactId> 
        </依存> 
        <依存性> 
            <のgroupId> com.alibaba </のgroupId> 
            <たartifactId> fastjson </たartifactId> 
            <バージョン> 1.2.49 </バージョン> 
        </依存> 
        <依存性> 
            <のgroupId> org.apache.commons </のgroupId> 
            <たartifactId>コモンズ-lang3 </たartifactId> 
        </依存>    

 

第二に、設定を追加

春:
    RabbitMQの:
        ホスト: 127.0.0.1の
        ポートは: 5672は
        、ユーザ名:管理者向け
        管理者:パスワードを
        仮想 -hostを:/ test_rabbitMq

 

 

第三に、キューおよびスイッチとバインドを作成します

パッケージcom.liangjian.config。

輸入org.springframework.amqp.core.Binding。
輸入org.springframework.amqp.core.BindingBuilder。
輸入org.springframework.amqp.core.FanoutExchange。
輸入org.springframework.amqp.core.Queue。
輸入org.springframework.context.annotation.Bean。
輸入org.springframework.context.annotation.Configuration。


@Configuration 
パブリック クラスRabbitMQConfig {
  
//キュー名
  のパブリック静的な文字列FANOUT_SMS_QUEUE = fanout_sms_queue
  //キュー作成
    @Bean 
    パブリックキューfanoutSmsQueueを(){
         返す 新しい新しいキュー(FANOUT_SMS_QUEUEを); 
    } 

  //スイッチを作成 @Bean
公共{)FanoutExchange fanoutExchange1を( 返す 新しい新しい(FanoutExchangeを" fanoutExchange1 " ;) }
  
  のキューを切り替えると結合// @Bean 結合bindingSms(){
戻りBindingBuilder.bind(fanoutSmsQueue())(fanoutExchange1());.へ } }

 

四、プロデューサープロデューサーがメッセージを送信

パッケージcom.liangjian.producer。

輸入com.liangjian.config.RabbitMQConfig。
輸入com.liangjian.util.SmsUtil。
輸入org.springframework.amqp.rabbit.core.RabbitTemplate。
輸入org.springframework.beans.factory.annotation.Autowired;
輸入org.springframework.scheduling.annotation.Async;
輸入org.springframework.stereotype.Component。

輸入java.util.Map; 

@Component 
パブリック クラスFanoutProducer { 

    @Autowired 
    プライベートRabbitTemplateのrabbitTemplate。

    公共 ボイド送信(文字列のマッサージ){ 
        String型のMSG= "Creatorは発表:" +マッサージ; 
        rabbitTemplate.convertAndSend(RabbitMQConfig.FANOUT_SMS_QUEUE、MSG); 
    } 

}

 

五、消費者の消費者は、メッセージが表示されます

パッケージcom.liangjian.consumer。

輸入lombok.extern.slf4j.Slf4j。
輸入org.springframework.amqp.rabbit.annotation.RabbitHandler。
輸入org.springframework.amqp.rabbit.annotation.RabbitListener。
輸入org.springframework.stereotype.Component。

@Component 

@ SLF4J 
パブリック クラスFanoutSmsConsumer {
  
  @RabbitListener(キュー= "fanout_sms_queue" 
  @RabbitHandler
  公共ボイドプロセス(メッセージのマッサージは)にUnsupportedEncodingExceptionをスロー{
      文字列ID = massage.getMessageProperties()getMessageId()。
   文字列MSG =新しい文字列(massage.getBody()、 "UTF-8");
  log.info(ID + ">>>>>>>>>>" + MSG)。
  JSONObject jsonObject = JSONObject.parseObject(MSG)。
  整数フィルムID = jsonObject.getInteger(「ユーザID」)。
  文字列のNUMS = jsonObject.getString( "電話");
  log.info( "フィルムID =" +フィルムID + ">>>>>>>>>>>>>>>>>>>>>>>> NUMS =" + NUMS)。
  }
 }

 

六、コントローラは、生産者が発表された呼び出し、

パッケージcom.liangjian.controller。

輸入com.alibaba.fastjson.JSONObject。
輸入com.liangjian.producer.FanoutProducer。
輸入lombok.extern.slf4j.Slf4j。
輸入org.springframework.beans.factory.annotation.Autowired;
輸入org.springframework.stereotype.Controller。
輸入org.springframework.transaction.annotation.Transactional;
輸入org.springframework.web.bind.annotation.GetMapping。
輸入org.springframework.web.bind.annotation.RequestMapping。
輸入org.springframework.web.bind.annotation.ResponseBody。


@RequestMapping( "/チケット"  
@Controller 
@ SLF4J 
パブリック クラスTicketController { 

    @Autowired 
    プライベートFanoutProducer fanoutProducer。


    (@GetMapping "/ getTicketが" 
    @ResponseBody 
    @Transactional(rollbackFor =例外。クラスパブリック文字列getTicketが(整数ユーザーID、文字列電話){ 

           JSONObject jsonObject = 新しいJSONObject()。
            jsonObject.put( "ユーザーID" ユーザID)); 
            jsonObject.put( "電話" 、携帯電話); 
            fanoutProducer.sendTicketMsg(jsonObject.toJSONString())。
               
        リターン   「に成功したメッセージを送って!」; 
    } 

}

 

七つの概要

スイッチの異なるタイプの、単に適切なスイッチを作成します。

@Bean 
    DirectExchange directExchange(){ 
        戻り 新しい DirectExchange( "directExchangeを" )。
    } 

    @Bean 
    結合bindingSms(){ 
        戻り BindingBuilder.bind(directSmsQueue())に(directExchange())( "key.sms"と。
    }
 

メッセージを送信し、携帯routingKey

 公共 ボイド送信(文字列QUEUENAME){ 
        文字列のMSG = "my_fanout_msg:" + 新しい日付(); 
        System.out.println( "生产者发布消息MSG:" + MSG)。
        rabbitTemplate.convertAndSend(QUEUENAME、 "key.sms" 、MSG)。
    }

 

トピックスイッチroutingkeyサポートワイルドカード一致:

Routingkey文字列=「#テストトピック「;
Routingkey文字列= "テストトピック*。";

    • * 1ワードのみを一致させるには
    • #マッチつ以上の単語を指定します

おすすめ

転載: www.cnblogs.com/castlechen/p/11145134.html