Spring Boot 入门(九)使用RabbitMQ

maven

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-amqpartifactId>
dependency>

配置文件

spring.rabbitmq.host=192.168.233.128
spring.rabbitmq.port=5672
spring.rabbitmq.username=root
spring.rabbitmq.password=123456

创建生产者

@Autowired
RabbitTemplate rabbitTemplate;

/**
* sendRabbitMQ
* 默认Exchange: (AMQP default)
* direct模式:通过routingKey关联
*/
@RequestMapping("/sendRabbitMQ")
public String sendRabbitMQ(String msg) {
    rabbitTemplate.convertAndSend("queue.A", msg);
    return "发送成功";
}

创建消费者

package com.example.demo.rabbitmq;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

@Component
public class QueueListener {
    @RabbitListener(queuesToDeclare = @Queue("queue.A"))
    public void receiveMsgA(String msg, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, Channel channel) {
        System.out.println("收到消息A:" + msg);
    }
}

发送消息

调用接口”sendRabbitMQ”

Spring Boot 入门(九)使用RabbitMQ

控制台打印

Spring Boot 入门(九)使用RabbitMQ

RabbitMQ管理端查看队列信息

Spring Boot 入门(九)使用RabbitMQ

fanout模式

将多个Queue(队列)绑定到一个Exchange(交换机),当Exchange收到消息时,与之绑定的Queue都将收到消息。

//发送代码指定一个Exchange
rabbitTemplate.convertAndSend("fanoutExchange", "", msg);
package com.example.demo.rabbitmq;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

@Component
public class QueueListener {
    @RabbitListener(bindings = @QueueBinding(value = @Queue("queue.A"), exchange = @Exchange("fanoutExchange")))
    public void receiveMsgA(String msg, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, Channel channel) {
        System.out.println("收到消息A:" + msg);
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue("queue.B"), exchange = @Exchange("fanoutExchange")))
    public void receiveMsgB(String msg, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, Channel channel) {
        System.out.println("收到消息B:" + msg);
    }
}

Spring Boot 入门(九)使用RabbitMQ

Spring Boot 入门(九)使用RabbitMQ

topic模式(用得比较多)

类似路由模式,但是routing_key支持模糊匹配,按规则转发消息(最灵活)。符号”#”匹配一个或多个词,符号”*”匹配不多不少一个词。

//发送代码指定一个Exchange,并且routingKey=A
rabbitTemplate.convertAndSend("topicExchange", "A", msg);
package com.example.demo.rabbitmq;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

@Component
public class QueueListener {
    @RabbitListener(bindings = @QueueBinding(value = @Queue("queue.A"), key = {"A"}, exchange = @Exchange(value = "topicExchange", type = ExchangeTypes.TOPIC)))
    public void receiveMsgA(String msg, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, Channel channel) {
        System.out.println("收到消息A:" + msg);
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue("queue.B"), key = {"B"}, exchange = @Exchange(value = "topicExchange", type = ExchangeTypes.TOPIC)))
    public void receiveMsgB(String msg, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, Channel channel) {
        System.out.println("收到消息B:" + msg);
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue("queue.A"), key = {"A"}, exchange = @Exchange(value = "topicExchange", type = ExchangeTypes.TOPIC)))
    public void receiveMsgC(String msg, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, Channel channel) {
        System.out.println("收到消息C:" + msg);
    }
}

由于消费者是轮询消费,所以发送两次消息,A和C依次收到消息(routingKey=A,B消费者不会收到消息)

Spring Boot 入门(九)使用RabbitMQ

Spring Boot 入门(九)使用RabbitMQ

Original: https://www.cnblogs.com/xiaoxiaoyu0707/p/15684444.html
Author: 小小渔
Title: Spring Boot 入门(九)使用RabbitMQ

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/572291/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球