基于SpringBoot操作RabbitMQ

1.添加依赖

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

 

 

2.配置application.yml

spring:
    rabbitmq:
        host: 127.0.0.1
        port: 5672
        username: guest
        password: guest
        virtualHost: /    

 

 

 

3.定义RabbitConfig类,配置Exchange、Queue、及绑定交换机

package com.xuecheng.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Configuration
public class RabbitMqConfig {
  
    public static final String EXCHANGE_TOPIC_INFORM = "exchange_topic_inform";
    public static final String QUEUE_INFORM_SMS = "queue_inform_sms";
    public static final String QUEUE_INFORM_EMAIL = "queue_inform_email";

    /**
     * 交换机配置
     * ExchangeBuilder提供了fanout、direct、topic、header交换机类型的配置
     * @return the exchange
     */
    @Bean(EXCHANGE_TOPIC_INFORM)
    public Exchange EXCHANGE_TOPIC_INFORM(){
        return ExchangeBuilder.topicExchange(EXCHANGE_TOPIC_INFORM).durable(true).build();
    }

    //申明队列
    @Bean(QUEUE_INFORM_SMS)
    public Queue QUEUE_INFORM_SMS(){
        return new Queue(QUEUE_INFORM_SMS);
    }

    //申明队列
    @Bean(QUEUE_INFORM_EMAIL)
    public Queue QUEUE_INFORM_EMAIL(){
        return new Queue(QUEUE_INFORM_EMAIL);
    }

    //绑定队列到交换机上
    @Bean
    public Binding BINDING_QUEUE_SMS_TO_EXCHANGE(@Qualifier(QUEUE_INFORM_SMS) Queue queue
            ,@Qualifier(EXCHANGE_TOPIC_INFORM) Exchange exchange){
        return BindingBuilder
                .bind(queue).to(exchange)
                .with("inform.#.sms.#").noargs();
    }

    @Bean
    public Binding BINDING_QUEUE_EMAIL_TO_EXCHANGE(@Qualifier(QUEUE_INFORM_EMAIL) Queue queue
            ,@Qualifier(EXCHANGE_TOPIC_INFORM) Exchange exchange){
        return BindingBuilder
                .bind(queue).to(exchange)
                .with("inform.#.email.#").noargs();
    }


}

 

 

 

4.消息生产端

使用RabbitTemplate 即可生产消息

  @Autowired
    private RabbitTemplate rabbitTemplate;
public void produceMsg(){ String msg = "Hello world!!!!!!!!!!!!!!!!!!";
     //使用该方法发送消息
     //参数1:交换机名称,参数2:路由key,参数3:消息 rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_TOPIC_INFORM,
"inform.sms.email",msg); }

 

 

 

4.消息消费端

使用RabbitListener注解即可监听队列

    //使用RabbitListener注解监听队列,注解参数为队列名称
    @RabbitListener(queues = {队列名称})
    public void receiveMsg(String msg, Message message){
            System.out.println(msg);
    }

 

posted @ 2021-01-29 20:18  zddsl  阅读(76)  评论(0)    收藏  举报