RabbitMQ a Series: Hello World

Life is more than the immediate struggling, there are always not read poetry and not to far away.

https://raw.githubusercontent.com/longfeizheng/longfeizheng.github.io/master/images/mq/mq01.jpg

Outline

RabbitMQ is to achieve the Advanced Message Queuing Protocol (AMQP) is an open source message broker software (also known as message-oriented middleware). RabbitMQ server is written in Erlang, and clustering and failover is built on an open telecommunications platform framework. All major programming languages ​​have to communicate with the agent interface client library.

RabbitMQ installation

RabbitMQDownload address , in this series we use dockerway to install RabbitMQ, RabbitMQthe dockermirror address . On how to dockerinstall and use can refer to the following link ;

start upRabbitMQ

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

After the installation is completed by http://localhost:15672/#/(username and password to access both the graphical interface admin)

RabbitMQ the organization chart below

https://raw.githubusercontent.com/longfeizheng/longfeizheng.github.io/master/images/mq/mq01.png

  1. Server It is simply the message queue server entity
  2. Exchange The message switch that specifies what the message according to the rules, which are routed to a queue
  3. Queue Vector message queue, each message will be put into one or more queues
  4. Binding: Binding, its role is to exchangeand queuebind them in accordance with the routing rules
  5. Routing Key: Route keyword, exchangea message delivered in accordance with this keyword
  6. VHost: Web Hosting, a brokerwhere you can set up multiple vhost, separate privilege as different users.
  7. Producer: News producer, is a program delivery message
  8. Consumer: News consumers, is that the program accepts messages
  9. Channel: Message channel, each client's connection can be established more channel, each channelrepresenting a session task

Note: the Exchange, Queue, RoutingKeythree to a decision from Exchangethe Queuesingle line.

RabbitMQ messaging model

About RabbitMQ Message Model

  • DirectSwitch: based entirely keyfor delivery.
  • TopicSwitch: In keyfor delivery after the pattern matching.
  • FanoutSwitch: It takes a broadcast mode, incoming messages, will be delivered to all queues and to change the switch of binding.

RabbitMQ Hello World

Producers 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 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);
        }
    }
}

Results are as follows:

Consumers first start, restart producers

https://raw.githubusercontent.com/longfeizheng/longfeizheng.github.io/master/images/mq/mq01.gif

Download

Guess you like

Origin blog.csdn.net/dandandeshangni/article/details/93297763