RabbitMQ download, configuration, and Java implementation examples of producers and consumers under Windows

RabbitMQ is a lightweight message broker middleware that supports multiple message communication protocols, supports distributed deployment, supports running on multiple operating systems, and has features such as flexibility and high availability. RabbitMQ supports a variety of protocols, the most important of which is Advanced Message Queuing Protocol (AMQP), which defines the communication protocol between "message client" and "message broker middleware". Based on this protocol, message client and message broker Middleware can be free from the constraints of development languages ​​and specific products.

The rough model of AMQP is shown in the figure below:

  

The producer passes the message to the "message broker (RabbitMQ server)", a message channel (Channel) is established between them, and the message is processed by the exchange (Exchange) first, and then delivered to a message queue (Message Queue), and finally the message The queue sends the message to the message person.

1. Download RabbitMQ and Erlang

The highest version of RabbitMQ server under Windows is 3.8.1, download link: https://www.rabbitmq.com/install-windows.html

The RabbitMQ server uses Erlang language, so you also need to download Erlang, the current highest version is 22.1,
download address: http://erlang.org/download/, click on the file name otp_win64_22.1.exe inside to download.

Two, placement

1. RabbitMQ will start as a Windows service after installation.

2. Open plug-in management

cmd into the sbin directory of the installation directory, enter the following command to install:
rabbitmq-plugins enable rabbitmq_management

Enter the following command to view the status of the plug-in:
rabbitmq-plugins list

3. Log in to the web management interface

Step 2 After opening the plug-in, this address can be accessed: http://localhost:15672/
Enter the default user name and password (both are guest) to log in, the interface is as follows after login

Note: Another port 5672 of RabbitMQ is used for client communication.

Three , Java implementation of producer and consumer examples

For the server, both the message producer and consumer belong to the client, and they communicate with the server through the AMQP protocol. AMQP is not limited by language, and the client can be implemented in different programming languages. The client is written in Java below.

Create a new Maven project and add dependencies to pom.xml:

        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>5.7.3</version>
        </dependency>

1. Write the producer

Create a new message producer and send a message to the server.

package com.example.rabbittest;

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 Send {
    public static void main(String[] args) throws IOException, TimeoutException {
        //创建连接
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection = factory.newConnection();
        //建立通道
        Channel channel = connection.createChannel();
        //声明队列
        String queueName = "hello";
        channel.queueDeclare(queueName,false,false,false,null);
        String message = "hello world";
        //消息发布
        channel.basicPublish("", queueName, null, message.getBytes());
        //关闭通道和连接
        channel.close();
        connection.close();
    }
}

The above code does not declare a switch, and the default switch will be used.

After running the code, log in to http://localhost:15672/, click the Queues tab, and you can see that the hello queue is created.

2. Write consumers

Create a new message consumer (in the same project as the producer) and accept server messages.

package com.example.rabbittest;

import com.rabbitmq.client.*;

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

public class Receive {
    public static void main(String[] args) throws IOException, TimeoutException {
        //创建连接
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection = factory.newConnection();
        //建立通道
        Channel channel = connection.createChannel();
        //声明队列
        String queueName = "hello";
        channel.queueDeclare(queueName,false,false,false,null);
        String message = "hello world";
        //消息发布
        Consumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String message = new String(body, "UTF-8");
                System.out.println("接收的消息:" + message);
            }
        };
        channel.basicConsume(queueName, true, consumer);
    }
}

After running the code, open the RabbitMQ console, click the hello queue, and you can see that there are corresponding consumers.

 

Guess you like

Origin blog.csdn.net/gdjlc/article/details/115008489