RabbitMQ 系列一:Hello World

栏目: 后端 · 发布时间: 7年前

内容简介:RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件(亦称面向消息的中间件)。RabbitMQ服务器是用Erlang语言编写的,而集群和故障转移是构建在开放电信平台框架上的。所有主要的编程语言均有与代理接口通讯的客户端库。启动

RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件(亦称面向消息的中间件)。RabbitMQ服务器是用Erlang语言编写的,而集群和故障转移是构建在开放电信平台框架上的。所有主要的编程语言均有与代理接口通讯的客户端库。

安装RabbitMQ

RabbitMQ 下载地址,本系列我们采用 docker 的方式来安装 RabbitMQ , RabbitMQ 的 docker 镜像地址。关于如何 docker 的安装和使用可参考以下链接;

启动 RabbitMQ

docker run -d -p 15672:15672 -p 5672:5672 -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=admin --name rabbitmq --hostname=rabbitmqhostone daocloud.io/library/rabbitmq:3.7.14-management-alpine

安装完成之后可通过 http://localhost:15672/#/ 访问图形界面(用户名密码均为 admin )

RabbitMQ 架构图如下

RabbitMQ 系列一:Hello World
https://user-gold-cdn.xitu.io/2019/6/19/16b6ea0613842390?w=640&h=434&f=jpeg&s=103636
  1. Server 简单来说就是消息队列服务器实体
  2. Exchange 消息交换机,它指定消息按什么规则,路由到哪个队列
  3. Queue 消息队列载体,每个消息都会被投入到一个或多个队列
  4. Binding : 绑定,它的作用就是把 exchange 和 queue 按照路由规则绑定起来
  5. Routing Key : 路由关键字, exchange 根据这个关键字进行消息投递
  6. VHost : 虚拟主机,一个 broker 里可以开设多个 vhost ,用作不同用户的权限分离。
  7. Producer : 消息生产者,就是投递消息的程序
  8. Consumer : 消息消费者,就是接受消息的程序
  9. Channel : 消息通道,在客户端的每个连接里,可建立多个 channel ,每个 channel 代表一个会话任务

注意:由 Exchange 、 Queue 、 RoutingKey 三个才能决定一个从 Exchange 到 Queue 的唯一的线路。

RabbitMQ 消息模型

关于 RabbitMQ 消息模型

  • Direct 交换机:完全根据 key 进行投递。
  • Topic 交换机:在 key 进行模式匹配后进行投递。
  • Fanout 交换机:它采取广播模式,消息进来时,将会被投递到与改交换机绑定的所有队列中。

RabbitMQ Hello World

生产者 Procuder

package com.niocoder.quickstart;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Procuder {

    public static void main(String[] args) throws IOException, TimeoutException {

        // 1. 创建ConnectionFactory
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("admin");
        factory.setVirtualHost("/");

        // 2. 通过链接工厂创建连接
        Connection connection = factory.newConnection();

        // 3. 通过connection 创建一个channel
        Channel channel = connection.createChannel();

        // 4. 通过channel发送数据
        for (int i = 0; i < 3; i++) {
            String msg = "Hello World";
            /**
             *      * @param exchange the exchange to publish the message to
             *      * @param routingKey the routing key
             *      * @param props other properties for the message - routing headers etc
             *      * @param body the message body
             */
            channel.basicPublish("", "hello", null, msg.getBytes());
        }

        // 5. 关闭连接
        channel.close();
        connection.close();
    }
}

复制代码

消费者 Consumer

package com.niocoder.quickstart;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;

public class Consumer {
    public static void main(String[] args) throws Exception {
        // 1. 创建ConnectionFactory
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("admin");
        factory.setVirtualHost("/");

        // 2. 通过链接工厂创建连接
        Connection connection = factory.newConnection();

        // 3. 通过connection 创建一个channel
        Channel channel = connection.createChannel();

        // 4. 创建一个队列
        String queueName = "hello";
        /**
         *      * @param queue the name of the queue
         *      * @param durable true if we are declaring a durable queue (the queue will survive a server restart)
         *      * @param exclusive true if we are declaring an exclusive queue (restricted to this connection)
         *      * @param autoDelete true if we are declaring an autodelete queue (server will delete it when no longer in use)
         *      * @param arguments other properties (construction arguments) for the queue
         */
        channel.queueDeclare(queueName, true, false, false, null);

        // 5. 创建消费者
        QueueingConsumer consumer = new QueueingConsumer(channel);

        // 6. 设置channel
        channel.basicConsume(queueName,true,consumer);

        while (true){
            // 7. 获取消息
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String msg = new String(delivery.getBody());
            System.err.println("消费端: " + msg);
        }
    }
}

复制代码

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

新物种爆炸

新物种爆炸

吴声 / 中信出版社 / 2017-7-30 / 58.00元

宝马为什么要重点发展共享汽车 Airbnb正试图成为内容和社交平台 不排队、不结账、没有收银员的颠覆传统超市 茑屋书店要打造全新生活方式 基于新的商业环境与技术条件的变化,必须会产生新的品类和商业模式,这就是新物种! 大数据与人工智能等技术正在创建新的商业话语体系,创建新的权力架构,引领第四新物种爆炸。商业规则正在快速发生变化,新的模式与业态层出不穷。 要么成为......一起来看看 《新物种爆炸》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具