前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >RabbitMQ消息中间件从入门到高级(一)

RabbitMQ消息中间件从入门到高级(一)

作者头像
用户1212940
发布2022-04-13 15:29:50
5360
发布2022-04-13 15:29:50
举报
文章被收录于专栏:Lambda

一、什么是RabbitMQ

MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法。应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链接它们。消息传递指的是程序之间通过在消息中发送数据进行通信,而不是通过直接调用彼此来通信,直接调用通常是用于诸如远程过程调用的技术。排队指的是应用程序通过 队列来通信。队列的使用除去了接收和发送应用程序同时执行的要求。其中较为成熟的MQ产品有IBM WEBSPHERE MQ等等。

二、什么是队列?

队列就像存放了商品的仓库或者商店,是生产商品的工厂和购买商品的用户之间的中转站

三、队列里存储了什么?

在rabbitMQ中,信息流从你的应用程序出发,来到Rabbitmq的队列,所有信息可以只存储在一个队列中。队列可以存储很多信息,因为它基本上是一个无限制的缓冲区,前提是你的机器有足够的存储空间。

四、队列和应用程序的关系?

多个生产者可以将消息发送到同一个队列中,多个消息者也可以只从同一个队列接收数据。

五、RabbitMQ原理图

11464886-081647857db8b93f.png
11464886-081647857db8b93f.png
  1. Message 消息。消息是不具名的,它由消息头消息体组成。消息体是不透明的,而消息头则由 一系列可选属性组成,这些属性包括:routing-key(路由键)、priority(相对于其他消息的优先权)、delivery-mode(指出消息可能持久性存储)等。
  2. Publisher 消息的生产者。也是一个向交换器发布消息的客户端应用程序。
  3. Consumer 消息的消费者。表示一个从消息队列中取得消息的客户端应用程序。
  4. Exchange 交换器。用来接收生产者发送的消息并将这些消息路由给服务器中的队列。 三种常用的交换器类型 1. direct(发布与订阅 完全匹配) 2. fanout(广播) 3. topic(主题,规则匹配)
  5. Binding 绑定。用于消息队列和交换器之间的关联。一个绑定就是基于路由键将交换器和消息队列连接起来的路由规则,所以可以将交换器理解成一个由绑定构成的路由表。
  6. Queue 消息队列。用来保存消息直到发送给消费者。它是消息的容器,也是消息的终点。一个消息可投入一个或多个队列。消息一直在队列里面,等待消费者链接到这个队列将其取走。
  7. Routing-key 路由键。RabbitMQ决定消息该投递到哪个队列的规则。 队列通过路由键绑定到交换器。 消息发送到MQ服务器时,消息将拥有一个路由键,即便是空的,RabbitMQ也会将其和绑定使用的路由键进行匹配。 如果相匹配,消息将会投递到该队列。 如果不匹配,消息将会进入黑洞。
  8. Connection 链接。指rabbit服务器和服务建立的TCP链接。
  9. Channel 信道。 1,Channel中文叫做信道,是TCP里面的虚拟链接。例如:电缆相当于TCP,信道是一个独立光纤束,一条TCP连接上创建多条信道是没有问题的。 2,TCP一旦打开,就会创建AMQP信道。 3,无论是发布消息、接收消息、订阅队列,这些动作都是通过信道完成的。
  10. Virtual Host 虚拟主机。表示一批交换器,消息队列和相关对象。虚拟主机是共享相同的身份认证和加密环境的独立服务器域。每个vhost本质上就是一个mini版的RabbitMQ服务器,拥有自己的队列、交换器、绑定和权限机制。vhost是AMQP概念的基础,必须在链接时指定,RabbitMQ默认的vhost是/
  11. Borker 表示消息队列服务器实体。
  12. 交换器和队列的关系 交换器是通过路由键和队列绑定在一起的,如果消息拥有的路由 键跟队列和交换器的路由键匹配,那么消息就会被路由到该绑定的队列中。 也就是说,消息到队列的过程中,消息首先会经过交换器,接下来交换器在通过路由键匹配分发消息到具体的队列中。路由键可以理解为匹配的规则。
  13. RabbitMQ为什么需要信道?为什么不是TCP直接通信?
      1. TCP的创建和销毁开销特别大。创建需要3次握手,销毁需要4次分手。
      1. 如果不用信道,那应用程序就会以TCP链接Rabbit,高峰时每秒成千上万条链接会造成资源巨大的浪费,而且操作系统每秒处理TCP链接数也是有限制的,必定造成性能瓶颈。
      1. 信道的原理是一条线程一条通道,多条线程多条通道同用一条TCP链接。一条TCP链接可以容纳无限的信道,即使每秒成千上万的请求也不会成为性能的瓶颈。

六、RabbitMQ 交换机详解

  • Direct 交换机(发布与订阅 完全匹配)
  1. 业务需求
11464886-fe687719c05f3fc4.png
11464886-fe687719c05f3fc4.png
  1. 修改customer 配置文件
代码语言:javascript
复制
spring.application.name=rabbitmq-direct-consumer

spring.rabbitmq.host=10.136.196.159
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=123456

# 设置交换机 exchange 名称
mq.config.exchange=log.direct

# info 路由键
mq.config.queue.info.routing.key=log.info.routing.key
# info 队列名称
mq.config.queue.info=log.info


# error 路由键
mq.config.queue.error.routing.key=log.error.routing.key
# error 队列名称
mq.config.queue.error=log.error
  1. 修改 provider 配置文件
代码语言:javascript
复制
spring.application.name=rabbitmq-direct-provider

spring.rabbitmq.host=10.136.196.159
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=123456

# 设置交换机 exchange 名称
mq.config.exchange=log.direct

# info 路由键
mq.config.queue.info.routing.key=log.info.routing.key

# error 路由键
mq.config.queue.error.routing.key=log.error.routing.key
  1. 编写 Customer

InfoReceiver

代码语言:javascript
复制
package com.pyy.mq.service;


import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 消息接收者
 * @RabbitListener bindings:绑定队列
 *   @QueueBinding  value:绑定队列的名称
 *                exchange:配置交换器
 * 
 *     @Queue value:配置队列名称
 *        autoDelete:是否是一个可删除的临时队列
 * 
 *     @Exchange value:为交换器起个名称
 *           type:指定具体的交换器类型
 */
@Component
@RabbitListener(
        bindings = @QueueBinding(
                value = @Queue(value = "${mq.config.queue.info}", autoDelete = "true"),
                exchange = @Exchange(value = "${mq.config.exchange}", type = ExchangeTypes.DIRECT),
                key = "${mq.config.queue.info.routing.key}"
        )
)
public class InfoReceiver {

    /**
     * 接收消息方法,采用消息队列监听机制
     * @param msg
     */
    @RabbitHandler
    public void process(String msg) {
        System.out.println("Info receiver:" + msg);
    }
}

ErrorReceiver

代码语言:javascript
复制
package com.pyy.mq.service;


import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 消息接受者
 * @author wolf
 */

@Component
@RabbitListener(
        bindings = @QueueBinding(
                value = @Queue(value = "${mq.config.queue.error}", autoDelete = "true"),
                exchange = @Exchange(value = "${mq.config.exchange}", type = ExchangeTypes.DIRECT),
                key = "${mq.config.queue.error.routing.key}"
        )
)
public class ErrorReceiver {

    /**
     * 接收消息方法,采用消息队列监听机制
     * @param msg
     */
    @RabbitHandler
    public void process(String msg) {
        System.out.println("Error receiver:" + msg);
    }
}
  1. 编写 provider
代码语言:javascript
复制
package com.pyy.mq.service;


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

/**
 * 消息发送者
 * @author wolf
 */

@Component
public class Sender {

    @Autowired
    private AmqpTemplate rabbitAmqpTemplate;

    // 交换机名称
    @Value("${mq.config.exchange}")
    private String exchange;

    // 路由键
    @Value("${mq.config.queue.info.routing.key}")
    private String routingkey;



    /**
     * 发送消息方法
     * @param msg
     */
    public void send(String msg) {
        // 向指定交换机 exchange  中通过执行的路由键 routingkey 中发送消息
        //参数一:交换器名称。
        //参数二:路由键
        //参数三:消息
        this.rabbitAmqpTemplate.convertAndSend(exchange, routingkey, msg);
    }
}
  • Topic 交换机(主题 规则匹配)
  1. 业务需求
11464886-4377b8e2b9a28094.png
11464886-4377b8e2b9a28094.png
  1. 修改配置文件

customer 配置文件

代码语言:javascript
复制
spring.application.name=rabbitmq-topic-consumer

spring.rabbitmq.host=10.136.196.159
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=123456

# 设置交换机 exchange 名称
mq.config.exchange=log.topic

# info 队列名称
mq.config.queue.info=log.info

# error 队列名称
mq.config.queue.error=log.error

# logs 队列名称
mq.config.queue.logs=log.all

provider 配置文件

代码语言:javascript
复制
spring.application.name=rabbitmq-topic-provider

spring.rabbitmq.host=10.136.196.159
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=123456

# 设置交换机 exchange 名称
mq.config.exchange=log.topic
  1. 编写 Customer InfoReceiver
代码语言:javascript
复制
package com.pyy.mq.service;


import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 消息接受者
 * @author wolf
 */

@Component
@RabbitListener(
        bindings = @QueueBinding(
                value = @Queue(value = "${mq.config.queue.info}", autoDelete = "true"),
                exchange = @Exchange(value = "${mq.config.exchange}", type = ExchangeTypes.TOPIC),
                key = "*.log.info"
        )
)
public class InfoReceiver {

    /**
     * 接收消息方法,采用消息队列监听机制
     * @param msg
     */
    @RabbitHandler
    public void process(String msg) {
        System.out.println("Info receiver:" + msg);
    }
}

ErrorReceiver

代码语言:javascript
复制
package com.pyy.mq.service;


import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 消息接受者
 * @author wolf
 */

@Component
@RabbitListener(
        bindings = @QueueBinding(
                value = @Queue(value = "${mq.config.queue.error}", autoDelete = "true"),
                exchange = @Exchange(value = "${mq.config.exchange}", type = ExchangeTypes.TOPIC),
                key = "*.error.info"
        )
)
public class ErrorReceiver {

    /**
     * 接收消息方法,采用消息队列监听机制
     * @param msg
     */
    @RabbitHandler
    public void process(String msg) {
        System.out.println("Error receiver:" + msg);
    }
}

LogsReceiver

代码语言:javascript
复制
package com.pyy.mq.service;


import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 消息接受者
 * @author wolf
 */

@Component
@RabbitListener(
        bindings = @QueueBinding(
                value = @Queue(value = "${mq.config.queue.info}", autoDelete = "true"),
                exchange = @Exchange(value = "${mq.config.exchange}", type = ExchangeTypes.TOPIC),
                key = "*.log.*"
        )
)
public class LogsReceiver {

    /**
     * 接收消息方法,采用消息队列监听机制
     * @param msg
     */
    @RabbitHandler
    public void process(String msg) {
        System.out.println("All receiver:" + msg);
    }
}
  1. 编写 Provider UserProvider
代码语言:javascript
复制
package com.pyy.mq.service;


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

/**
 * 消息发送者
 * @author wolf
 */

@Component
public class UserSender {

    @Autowired
    private AmqpTemplate rabbitAmqpTemplate;

    // 交换机名称
    @Value("${mq.config.exchange}")
    private String exchange;
    

    /**
     * 发送消息方法
     * @param msg
     */
    public void send(String msg) {
        // 向指定交换机 exchange  中通过执行的路由键 routingkey 中发送消息
        this.rabbitAmqpTemplate.convertAndSend(exchange, "user.log.info", "user.og.info-->" + msg);
        this.rabbitAmqpTemplate.convertAndSend(exchange, "user.log.error", "user.log.error-->" + msg);
        this.rabbitAmqpTemplate.convertAndSend(exchange, "user.log.debug", "user.log.debug-->" + msg);
        this.rabbitAmqpTemplate.convertAndSend(exchange, "user.log.warn", "user.log.warn-->" + msg);
    }
}

ProductProvider

代码语言:javascript
复制
package com.pyy.mq.service;


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

/**
 * 消息发送者
 * @author wolf
 */

@Component
public class ProductSender {

    @Autowired
    private AmqpTemplate rabbitAmqpTemplate;

    // 交换机名称
    @Value("${mq.config.exchange}")
    private String exchange;
    

    /**
     * 发送消息方法
     * @param msg
     */
    public void send(String msg) {
        // 向指定交换机 exchange  中通过执行的路由键 routingkey 中发送消息
        this.rabbitAmqpTemplate.convertAndSend(exchange, "product.log.info", "product.log.info-->" + msg);
        this.rabbitAmqpTemplate.convertAndSend(exchange, "product.log.error", "product.product.log.error-->" + msg);
        this.rabbitAmqpTemplate.convertAndSend(exchange, "product.log.debug", "product.log.debug-->" + msg);
        this.rabbitAmqpTemplate.convertAndSend(exchange, "product.log.warn", "product.log.warn-->" + msg);
    }
}
  • Fanout 交换机(广播 )
  1. 业务需求
11464886-274016b196fd7e58.png
11464886-274016b196fd7e58.png
  1. 修改配置文件 customer 配置文件
代码语言:javascript
复制
spring.application.name=springcloud-mq

spring.rabbitmq.host=192.168.70.131
spring.rabbitmq.port=5672
spring.rabbitmq.username=oldlu
spring.rabbitmq.password=123456

#设置交换器的名称
mq.config.exchange=order.fanout
 
#短信服务队列名称
mq.config.queue.sms=order.sms

#push服务队列名称
mq.config.queue.push=order.push

Provider

代码语言:javascript
复制
spring.application.name=springcloud-mq

spring.rabbitmq.host=192.168.70.131
spring.rabbitmq.port=5672
spring.rabbitmq.username=oldlu
spring.rabbitmq.password=123456
 
#设置交换器的名称
mq.config.exchange=order.fanout
  1. 编写 Customer

SmsReceiver

代码语言:javascript
复制
/**
 * 消息接收者
 * @author Administrator
 * @RabbitListener bindings:绑定队列
 * @QueueBinding  value:绑定队列的名称
 *                exchange:配置交换器
 *                key:路由键
 * 
 * @Queue value:配置队列名称
 *        autoDelete:是否是一个可删除的临时队列
 * 
 * @Exchange value:为交换器起个名称
 *           type:指定具体的交换器类型
 */
@Component
@RabbitListener(
bindings=@QueueBinding(           
  value=@Queue(
    value="${mq.config.queue.sms}",autoDelete="true"), 
    exchange=@Exchange(value="${mq.config.exchange}",type=ExchangeTypes.FANOUT)
   )
)
public class SmsReceiver {
   /**
    * 接收消息的方法。采用消息队列监听机制
    * @param msg
    */
   @RabbitHandler
   public void process(String msg){
     System.out.println("Sms........receiver: "+msg);
   }
}

PushReceiver

代码语言:javascript
复制
/**
 * 消息接收者
 * @author Administrator
 * @RabbitListener bindings:绑定队列
 * @QueueBinding  value:绑定队列的名称
 *                exchange:配置交换器
 * 
 * @Queue value:配置队列名称
 *        autoDelete:是否是一个可删除的临时队列
 * 
 * @Exchange value:为交换器起个名称
 *           type:指定具体的交换器类型
 */

@Component
@RabbitListener( 
bindings=@QueueBinding(value=@Queue(
    value="${mq.config.queue.push}",autoDelete="true"),exchange=@Exchange(value="${mq.config.exchange}",type=ExchangeTypes.FANOUT)
  )
)
public class PushReceiver {
   /**
    * 接收消息的方法。采用消息队列监听机制
    * @param msg
    */
   @RabbitHandler
   public void process(String msg){
      System.out.println("Push..........receiver: "+msg);
   }
}
  1. 编写 Provider
代码语言:javascript
复制
/**
 * 消息发送者
 * @author Administrator
 *
 */
@Component
public class Sender {
   @Autowired
   private AmqpTemplate rabbitAmqpTemplate;

   //exchange 交换器名称
   @Value("${mq.config.exchange}")
   private String exchange; 
   /*
    * 发送消息的方法
    */
   public void send(String msg){
      //向消息队列发送消息
      //参数一:交换器名称。
      //参数二:路由键
      //参数三:消息
     this.rabbitAmqpTemplate.convertAndSend(this.exchange,"", msg);
   }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018/10/10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、什么是RabbitMQ
  • 二、什么是队列?
  • 三、队列里存储了什么?
  • 四、队列和应用程序的关系?
  • 五、RabbitMQ原理图
  • 六、RabbitMQ 交换机详解
相关产品与服务
消息队列 CMQ 版
消息队列 CMQ 版(TDMQ for CMQ,简称 TDMQ CMQ 版)是一款分布式高可用的消息队列服务,它能够提供可靠的,基于消息的异步通信机制,能够将分布式部署的不同应用(或同一应用的不同组件)中的信息传递,存储在可靠有效的 CMQ 队列中,防止消息丢失。TDMQ CMQ 版支持多进程同时读写,收发互不干扰,无需各应用或组件始终处于运行状态。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档