Spring Boot AMQP-RabbitMQ 进阶

作者: 慢慢来了 | 来源:发表于2017-08-24 16:59 被阅读55次

本章延伸一下RabbitMq的其它队列应用

1. Work Queues

WorkQueues.png

生产者(P)生产消息并投递到Queue中,多个消费者(C1 、C2)可以订阅一个Queue,这个时候Queue中的消息会被平均的分摊给多个消费进行处理,而不是每个消费者都收到所有消息并处理。

Java 代码示例-生产者:
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Description: AMQP  JAVA消息生产者
 * author: 慢慢来了
 * date:  2017/3/31 11:38
 */
@Component
public class RabbitMqProducter {

    @Autowired
    private AmqpTemplate amqpTemplate;

    public void sendMsg(String content){
        for(int i = 0 ; i < 3 ; i++) {
            amqpTemplate.convertAndSend("S.Q.CRM.TO.MSG" , content + "_" +i);
        }
    }
}
Java 代码示例-消费者
package com.amqp;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Description: AMQP  JAVA消息消费者
 * author: 慢慢来了
 * date:  2017/3/31 11:38
 */
@Component
public class RabbitMqConsumer {

    @Autowired
    private AmqpTemplate amqpTemplate;


    @RabbitListener(queues="S.Q.CRM.TO.MSG")
    public void getMsg(String msg){
        System.out.println("**************Receiver: " + msg);
    }

    @RabbitListener(queues="S.Q.CRM.TO.MSG")
    public void getMsg2(String msg){
        System.out.println("--------------Receiver2: " + msg);
    }
}

2. Exchanges

Exchage.png

上节说的Queue 与Work Queues 都是直接把消息投递到Queue中的,但是RabbitMQ消息传递模型的核心思想是,生产者不发送任何信息直接到队列。( The core idea in the messaging model in RabbitMQ is that the producer never sends any messages directly to a queue. )而且生产都不知道消息会投递到哪个Queue上;生产都只需要知道消息发送给Exchage上面就好。
RabbitMQ中的Exchange有四种类型,不同的类型有着不同的路由策略:direct, topic, headers and fanout。

2.1 Binding

RabbitMQ 中通过Binding 将Exchange 与Quque关联起来,这样RabbitMQ就知道如何正确地将消息路由到指定的Quque了。

Binding.png
2.2 Routing key

生产者在将消息发送给Exchange的时候,一般会指定一个routing key,来指定这个路由规则,而这个routing key需要与 Exchange Type 与binding key 联合使用才能最终生效。
在Exchange Tpye 与binding key 固定的情况下(在正常使用情况下,这些内容都是配置好的),我们的生产者就可以在发送消息给Exchange的时候,通过指定routing key 来指定消息流的流向哪里。

Java代码示例
amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(Exchange).with(routeKey));
2.3 Fanout

fanout类型的Exchange路由规则非常简单,它会把所有发送到该Exchange的消息路由到所有与它绑定的Queue中。

image.png
2.4 Direct

direct类型的Exchange路由规则也很简单,它会把消息路由到那些binding key与routing key完全匹配的Queue

Direct.png
2.5 Topic

前面讲到direct类型的Exchange路由规则是完全匹配binding key与routing key,但这种严格的匹配方式在很多情况下不能满足实际业务需求。topic类型的Exchange在匹配规则上进行了扩展,它与direct类型的Exchage相似,也是将消息路由到binding key与routing key相匹配的Queue中,但这里的匹配规则有些不同,它约定:
routing key为一个句点号“. ”分隔的字符串(我们将被句点号“. ”分隔开的每一段独立的字符串称为一个单词),如“queue.user.aaa”、“q.av”、“queue.user.bbb”
binding key与routing key一样也是句点号“. ”分隔的字符串
binding key中可以存在两种特殊字符“”与“#”,用于做模糊匹配,其中“”用于匹配一个单词,“#”用于匹配多个单词(可以是零个)】

topic.png
Java 示例:
amqpTemplate.convertSendAndReceive(String exchange, String routingKey, Object message);

相关文章

网友评论

    本文标题:Spring Boot AMQP-RabbitMQ 进阶

    本文链接:https://www.haomeiwen.com/subject/vsgsdxtx.html