RabbitMQ那些事-SpringBoot集成

Direct模式

引入依賴:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

配置:

spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

然后寫個(gè)配置類:

@Configuration
public class MqConfig {
     @Bean
     public Queue queue() {
          return new Queue("queue");
     }
}

然后生產(chǎn)者類:

@Component
public class HelloSender {
    @Autowired
    private AmqpTemplate template;
    
    public void send() {
    template.convertAndSend("queue","Hello,MQ");
    }
}

然后再另一個(gè)應(yīng)用程序?qū)懴M(fèi)者類:

@Component
public class HelloReceive {

    @RabbitListener(queues="queue")    //監(jiān)聽器監(jiān)聽指定的Queue
    public void receive(String str) {
        System.out.println("Receive:"+str);
    }

}

跑生產(chǎn)者程序的測試,消費(fèi)者程序就可以收到了:

@Autowired
private HelloSender helloSender;

@Test
public void testRabbit() {
    helloSender.send();
}

默認(rèn)情況下,RabbitMq的虛擬機(jī)是 "/" ,它默認(rèn)有交換機(jī)和路由鍵,交換機(jī)配置類型就是Direct

image.png

而template.convertAndSend("queue","Hello,MQ"),默認(rèn)就是通過默認(rèn)的虛擬機(jī)處理消息,默認(rèn)的虛擬機(jī)會自動(dòng)綁定所有的隊(duì)列,默認(rèn)的隊(duì)列名就是路由鍵Key,可以看到:

/**
 * Convert a Java object to an Amqp {@link Message} and send it to a default exchange
 * with a specific routing key.
 *
 * @param routingKey the routing key
 * @param message a message to send
 * @throws AmqpException if there is a problem
 */
void convertAndSend(String routingKey, Object message) throws AmqpException;

自定義虛擬機(jī)

配置

@Bean
public Queue myQueue() {
  return new Queue("myQueue");
}

@Bean
public CustomExchange customExchange(){
 return new CustomExchange("CustomExchange",ExchangeTypes.DIRECT);
}

@Bean
Binding customExchangeBinding( Queue myQueue, CustomExchange customExchange) {
  return BindingBuilder.bind(myQueue).to(customExchange).with("Apple").noargs();
}

發(fā)送者

@Component
public class HelloSender {
    @Autowired
    private AmqpTemplate template;

    public void send() {

        //template.convertAndSend("queue","Hello,MQ");
        template.convertAndSend("CustomExchange", "Apple", "Hello,MQ");

    }
}

接收者

@Component
public class HelloReceive {

    @RabbitListener(queues="myQueue")    //監(jiān)聽器監(jiān)聽指定的Queue
    public void receive(String str) {
        System.out.println("Receive:"+str);
    }

}

Topic模式

同樣的首先配置Topic交換機(jī)并綁定隊(duì)列

@Bean
public Queue myTopicQueue() {
  return new Queue("myTopicQueue");
}


@Bean
public TopicExchange topicExchange(){
  return new TopicExchange("TopicExchange");
}

@Bean
Binding topicExchangeBinding( Queue myTopicQueue, TopicExchange topicExchange) {

  return BindingBuilder.bind(myTopicQueue).to(topicExchange).with("Apple.#");
}

然后發(fā)生消息

@Component
public class HelloSender {
    @Autowired
    private AmqpTemplate template;

    public void send() {

        //template.convertAndSend("queue","Hello,MQ");
        // template.convertAndSend("CustomExchange", "Apple", "Hello,MQ");

        template.convertAndSend("TopicExchange", "Apple.666", "Hello,MQ");

    }
}

接收消息:

@Component
public class HelloReceive {

    @RabbitListener(queues="myTopicQueue")    //監(jiān)聽器監(jiān)聽指定的Queue
    public void receive(String str) {
        System.out.println("Receive:"+str);
    }
}

Fanout模式

同樣的套路

@Bean
public Queue myFanoutQueue() {
  return new Queue("myFanoutQueue");
}


@Bean
public FanoutExchange fanoutExchange(){
  return new FanoutExchange("FanoutExchange");
}

@Bean
Binding fanoutExchangeBinding( Queue myFanoutQueue, FanoutExchange fanoutExchange) {

  return BindingBuilder.bind(myFanoutQueue).to(fanoutExchange);
}
@Component
public class HelloSender {
    @Autowired
    private AmqpTemplate template;

    public void send() {

        template.convertAndSend("FanoutExchange", "", "Hello,MQ");

    }
}
@Component
public class HelloReceive {

    @RabbitListener(queues="myFanoutQueue")    //監(jiān)聽器監(jiān)聽指定的Queue
    public void receive(String str) {
        System.out.println("Receive:"+str);
    }

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 轉(zhuǎn)載2017年11月01日 09:54:03 2595 RabbitMQ 即一個(gè)消息隊(duì)列,主要是用來實(shí)現(xiàn)應(yīng)用程序的...
    楊傳池chris閱讀 6,442評論 1 0
  • 1、安裝 1.1、Erlang: Erlang下載地址,下載后安裝即可。 1.2、RabbitMQ安裝 Rabbi...
    木石前盟Caychen閱讀 997評論 0 12
  • http://liuxing.info/2017/06/30/Spring%20AMQP%E4%B8%AD%E6%...
    sherlock_6981閱讀 16,209評論 2 11
  • 從出生的那天起 便懂得了選擇 總想得到 最完美的取舍 世上本來 就會有對錯(cuò) 要不怎么 有那么多因果 得到和失去 別...
    人參果子閱讀 245評論 0 5
  • 何穎穎堅(jiān)持讀書第602天 《孩子,把你的手給我? 二》與十幾歲孩子實(shí)現(xiàn)真正有效溝通的方法 第12章 學(xué)習(xí),成長,改...
    何穎穎h閱讀 163評論 0 0

友情鏈接更多精彩內(nèi)容