RabbiMQ工作原理及简单使用

SpringBoot AMQP官方如下介绍:

The Spring AMQP project applies core Spring concepts to the development of AMQP-based messaging solutions. It provides a "template" as a high-level abstraction for sending and receiving messages. It also provides support for Message-driven POJOs with a "listener container". These libraries facilitate management of AMQP resources while promoting the use of dependency injection and declarative configuration. In all of these cases, you will see similarities to the JMS support in the Spring Framework.

The project consists of two parts; spring-amqp is the base abstraction, and spring-rabbit is the RabbitMQ implementation.

 

SpringBoot程序为我们自动装配了RabbitTemplate和连接工厂(默认使用SimpleRabbitListenerContainerFactory),简化了异步消息发布-订阅的使用

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

之前已介绍过docker如何安装rabbitmq,spring.io官方给出了docker-compose的安装推荐

You can also use Docker Compose to quickly launch a RabbitMQ server if you have docker running locally. There is a docker-compose.yml in the root of the "complete" project in Github. It is very simple:

docker-compose.yml

rabbitmq:
  image: rabbitmq:management
  ports:
    - "5672:5672"
    - "15672:15672"

RabbitMQ配置由spring.rabbitmq.*外部化配置所控制,

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=secret

RabbitMQ优势:

  1.  实现了AMQP标准高级消息通信,具有可靠性,持久化支持,保证了消息的稳定性
  2.  基于Erlang语言开发,天生自带高并发、高可用光环

 

RabbitMQ工作机制:

  MQ生成器是不会直接把消息发送到队列,消息被发送到交换机,交换机可以转到单个队列,也可以转到多个队列,要发送消息,除了SpringBoot为我们自动装配的RabbitTemplate外,还需queue()方法创建一个AMQP队列,exchange()方法创建一个交换机,binding()方法将这两者绑定在一起定义RabbitTemplate发送到交换机时的行为

引入简单的生产者-消费者-代理模型

RabbitMQ本身不生成消息,扮演"中转、中介"作用

 

RabbitMQ工作流程图解:

交换机有四中类型,分别是Direct、Toptics、Fanout、Headers

Direct: 直接交换机, 默认使用exchange.amq.direct, 默认binding所有队列

代码:

@Component
@Slf4j
public class RabbitDirectSender {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendDirectMessage(DirectQueueNameEnum routingKey, Object message) {
        Assert.notNull(message, "rabbit message不能为空!");
        CorrelationData correlation = new CorrelationData(UUID.randomUUID().toString());
        this.rabbitTemplate.convertAndSend(routingKey.getValue(), message, correlation);
        log.info("direct send success delayqueue:{},message:{},correlationId:{}", routingKey.getValue(),
                JsonUtils.obj2json(message), correlation.getId());
    }
    @Bean
    public Queue initUserGetCouponQueue() {
        return new Queue(RabbitConstants.DirectQueueName.USER_GET_COUPON);
    }
}

Toptics: 

 

 

Fanout: 广播消息发送队列,所有绑定在此交换器上的queue,都会接收到消息

代码:

@Slf4j
@Component
public class RabbitFanoutSender {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送广播消息
     * @param exchange 所有绑定在此交换器上的队列都可以接收到广播消息
     * @param message 发送的消息体
     */
    public void sendFanoutMessage(FanoutExchangeName exchange,Object message){
        Assert.notNull(exchange, "exchange can not be null");
        Assert.notNull(message, "message can not be null");
        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend(exchange.getValue(), "", message, correlationData);
        log.info("fanout send success exchange:{}, message:{}, correlationId:{}", exchange.getValue(),
                JsonUtils.obj2json(message), correlationData.getId());

    }


    @Bean
    public Queue initOrderPayNotifyQueue(){
        return new Queue(RabbitConstants.FanoutQueueName.ORDER_PAY_NOTIFY);
    }

    @Bean
    public FanoutExchange exchange(){
        return new FanoutExchange(RabbitConstants.FanoutExchangeName.ORDER_PAY_NOTIFY.getValue());
    }

    @Bean
    public Binding binding(){
        return BindingBuilder.bind(initOrderPayNotifyQueue()).to(exchange());
    }
}

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值