RabbitMQ快速整合SpringBoot

  1. 导入springboot的rabbitmq依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
  1. 配置文件
spring:
  rabbitmq:
   host: 127.0.0.1
   port: 5672
   username: guest
   password: guest
   virtualHost: /
  1. 生产者:rabbit配置类
    使用topic工作模式,注:RabbitMQ几种工作模式介绍
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {
    /**
     * 队列名
     */
    public static final String QUEUE_INFORM_A = "queue_inform_A";
    public static final String QUEUE_INFORM_B = "queue_inform_B";
    /**
     * 交换机名
     */
    public static final String EXCHANGE_TOPICS_INFORM = "exchange_topics_inform";

    /**
     * 交换机配置
     * ExchangeBuilder提供了fanout、direct、topic、header交换机类型的配置
     *
     * @return the exchange
     */
    @Bean(EXCHANGE_TOPICS_INFORM)
    public Exchange EXCHANGE_TOPICS_INFORM() {
        //durable(true)持久化,消息队列重启后交换机仍然存在
        return ExchangeBuilder.topicExchange(EXCHANGE_TOPICS_INFORM).durable(true).build();
    }

    /**
     * 声明队列
     *
     * @return
     */
    @Bean(QUEUE_INFORM_A)
    public Queue QUEUE_INFORM_A() {
        Queue queue = new Queue(QUEUE_INFORM_A);
        return queue;
    }

    /**
     * 声明队列
     *
     * @return
     */
    @Bean(QUEUE_INFORM_B)
    public Queue QUEUE_INFORM_B() {
        Queue queue = new Queue(QUEUE_INFORM_B);
        return queue;
    }

    /** channel.queueBind(INFORM_QUEUE_A,"inform_exchange_topic","inform.#.a.#");
     * 绑定队列到交换机 .
     *
     * @param queue the queue
     * @param exchange the exchange
     * @return the binding
     */
    @Bean
    public Binding BINDING_QUEUE_INFORM_SMS(@Qualifier(QUEUE_INFORM_A) Queue queue,
                                            @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("inform.#.a.#").noargs();
    }

    @Bean
    public Binding BINDING_QUEUE_INFORM_EMAIL(@Qualifier(QUEUE_INFORM_B) Queue queue,
                                              @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("inform.#.b.#").noargs();
    }

}
  1. 生产者:发送消息测试
@SpringBootTest
@RunWith(SpringRunner.class)
public class RabbitMqProviderTest {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Test
    public void testSendByTopics(){
        for (int i=0;i<100;i++){
            String message = "a inform to user"+i;
            rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_TOPICS_INFORM,"inform.a.b",message);
            System.out.println("Send Message is:'" + message + "'");
        }
    }

}

查看

  1. 消费者:编写消费监听器
@Component
public class RabbitHandler {

    /**
     * 监听a队列
     */
    @RabbitListener(queues = {RabbitConfig.QUEUE_INFORM_A})
    public void receiveA(String msg, Message message, Channel channel){
        System.out.println(msg);
    }

    /**
     * 监听b队列
     */
    @RabbitListener(queues = {RabbitConfig.QUEUE_INFORM_B})
    public void receiveB(String msg,Message message,Channel channel){
        System.out.println(msg);
    }

}

结果输出
在这里插入图片描述


代码地址

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值