Spring Boot整合RabbitMQ (自动应答模式)

1. 自动应答模式

  • 第1步,编写application.properties
spring.rabbitmq.host = 127.0.0.1
spring.rabbitmq.port = 5672
spring.rabbitmq.password = test
spring.rabbitmq.username = test
spring.rabbitmq.virtual-host = /test

-第2步,编写RabbitMQ配置

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMQ配置
 */
@Configuration
public class SpringBootRabbitMQConfiguration {

    
    // ======== 生产者配置 ========

    /**
     * 声明了一个TopicExchange
     *
     * @return
     */
    @Bean
    public TopicExchange testTopicExchange() {
        return new TopicExchange("testTopicExchange");
    }


    // ======== 消费者配置 ========

    /**
     * 声明了一个Queue
     *
     * @return
     */
    @Bean
    public Queue testQueue() {
        return new Queue("testQueue");
    }


    /**
     * 声明了另一个Queue
     *
     * @return
     */
    @Bean
    public Queue testQueue2() {
        return new Queue("testQueue2");
    }


    // ======== 实际项目中为了解耦,绑定队列是放在RabbitMQ的Web端操作========

    /**
     * 队列1绑定TopicExchange并关联到RoutingKey
     *
     * @param testQueue
     * @param testTopicExchange
     * @return
     */
    @Bean
    public Binding bindingTestQueueToTestTopicExchange(@Qualifier("testQueue") Queue testQueue, @Qualifier("testTopicExchange") TopicExchange testTopicExchange) {
        return BindingBuilder.bind(testQueue).to(testTopicExchange).with("foo.*");
    }

    /**
     * 队列2绑定TopicExchange并关联到RoutingKey
     */
    @Bean
    public Binding bindingTestQueue2ToTestTopicExchange(@Qualifier("testQueue2") Queue testQueue, @Qualifier("testTopicExchange") TopicExchange testTopicExchange) {
        return BindingBuilder.bind(testQueue).to(testTopicExchange).with("foo.bar");
    }
}
  • 第3步,编写消费者类
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class Consumer {

    @RabbitListener(queues = "testQueue")
    public void receiveTestQueue(String msg) {
        System.out.println("消费者1:" + msg);
    }

    @RabbitListener(queues = "testQueue2")
    public void receiveTestQueue2(String msg) {
        System.out.println("消费者2:" + msg);
    }

}
  • 第4步,编写生产者
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Producer {

    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * 直接发送消息到Queue
     *
     * @param msg
     */
    public void sendToQueue(String routingKey, String msg) {
        // 如果没有经过交换机,routingKey即queue
        this.amqpTemplate.convertAndSend(routingKey, msg);
    }

    /**
     * 发送消息到TopicExchange
     *
     * @param msg
     */
    public void sendToTopicExchange(String exchange, String routingKey,String msg) {
        this.amqpTemplate.convertAndSend(exchange, routingKey, msg);
    }

}
  • 第5步,编写测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootRabbitMQTest {

    @Autowired
    private Producer producer;

    /**
     * 直接发送消息到Queue
     */
    @Test
    public void sendToQueue() {
        producer.sendToQueue("testQueue", "'hello world' through queue");
    }

    /**
     * 发送消息到TopicExchange
     */
    @Test
    public void sendToTopicExchange() {
        // TopicExchange绑定了2个队列
        // - 如果routingKey为foo.bar,两个队列(两个消费者)都会收到消息
        // - 如果routingKey为foo.bar2(其它任意标识),只有一个队列(一个消费者)才会收到消息
        producer.sendToTopicExchange("testTopicExchange", "foo.bar2", "hello world!!!");
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值