Life is more than the immediate struggling, there are always not read poetry and not to far away.
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
RabbitMQ
Download address , in this series we use docker
way to install RabbitMQ
, RabbitMQ
the docker
mirror address . On how to docker
install 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
Server
It is simply the message queue server entityExchange
The message switch that specifies what the message according to the rules, which are routed to a queueQueue
Vector message queue, each message will be put into one or more queuesBinding
: Binding, its role is toexchange
andqueue
bind them in accordance with the routing rulesRouting Key
: Route keyword,exchange
a message delivered in accordance with this keywordVHost
: Web Hosting, abroker
where you can set up multiplevhost
, separate privilege as different users.Producer
: News producer, is a program delivery messageConsumer
: News consumers, is that the program accepts messagesChannel
: Message channel, each client's connection can be established morechannel
, eachchannel
representing a session task
Note: the Exchange
, Queue
, RoutingKey
three to a decision from Exchange
the Queue
single line.
RabbitMQ messaging model
About RabbitMQ
Message Model
Direct
Switch: based entirelykey
for delivery.Topic
Switch: Inkey
for delivery after the pattern matching.Fanout
Switch: 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