rabbitmq deployment and configuration and verification

1. Scenario description

The friend project needs to get a test environment. With a little help, the system is not complicated, but you need to install mysql, Reids, Es, RabbitMq, etc. Mq is mainly used for synchronizing user information and sending in-site messages and text messages. RabbitMq has not been recorded before. I just went through it again, and recorded deployment, testing, and common problems. Friends in need, thank you for taking them!

2. Solutions

2.1 mq deployment

Installation instructions, the version does not use the latest version, using 3.6.15 in 2018, the latest download on github is too slow.

2.1.1 Install erlang language related packages
[root@ruanjianlaowang ~]# yum install epel-release
[root@ruanjianlaowang ~]# yum install erlang
[root@ruanjianlaowang ~]# wget https://packages.erlang-solutions.com/erlang-solutions-1.0-1.noarch.rpm
[root@ruanjianlaowang ~]# rpm -Uvh erlang-solutions-1.0-1.noarch.rpm
2.1.2 Install rabbitmq
[root@ruanjianlaowang ~]#  wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.15/rabbitmq-server-3.6.15-1.el7.noarch.rpm

[root@ruanjianlaowang ~]# yum install rabbitmq-server-3.6.15-1.el7.noarch.rpm

#Enable remote access
[root@ruanjianlaowang ~]# cat /etc/rabbitmq/rabbitmq.config
             [{rabbit, [{loopback_users, []}]}].

If the file or content does not exist, add the following (3.6.6 version no)
[root@ruanjianlaowang ~]# vi /etc/rabbitmq/rabbitmq.config
[root@ruanjianlaowang ~]# rabbitmq-plugins enable rabbitmq_management


#Install the delay plugin, specific scenarios, not required
[root@ruanjianlaowang ~]# cd /usr/lib/rabbitmq/lib/rabbitmq_server-3.6.15/plugins

[root@ruanjianlaowang ~]# wget https://dl.bintray.com/rabbitmq/community-plugins/rabbitmq_delayed_message_exchange-0.0.1.ez
[root@ruanjianlaowang ~]# rabbitmq-plugins enable rabbitmq_delayed_message_exchange

#Firewall release port number
[root@ruanjianlaowang ~]# firewall-cmd --add-port=15672/tcp --permanent
[root@ruanjianlaowang ~]# firewall-cmd --add-port=5672/tcp --permanent

#Start and close the view and other commands
[root@ruanjianlaowang ~]# service rabbitmq-server start
[root@ruanjianlaowang ~]# service rabbitmq-server status
[root@ruanjianlaowang ~]# service rabbitmq-server restart
[root@ruanjianlaowang ~]# service rabbitmq-server stop
Startup success graph:

2.1.3 Adding new users and virtual machines to the console

(1) Console address: http://45.122.146.23:15672/#/ (Alibaba cloud server, ip is changed to the address where mq is installed by yourself)

(2) Two ports, console: 15672, message communication: 5672

(3) Add users

(4) Add virtual host

In fact, similar to the files in linux, the default is / directory, creating a virtual host is easy to distinguish.

(5) Associate users and virtual host s

Click on the user, the empowerment page will pop up

(6) Configuration completed

2.2 rabbitmq verification

You can directly use the official verification code and change the configuration. One of the two classes sends messages and the other receives messages.

2.2.1 Official website demo address:

https://github.com/rabbitmq/rabbitmq-tutorials/blob/master/java/Send.java

2.2.2 Testing

(1) Test under idea, springboot project, two depencies are enough, pom file

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.laowang</groupId>
    <artifactId>mqtest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mqtest</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

The effect diagram is as follows:

(2) Message push class: Send.java

package com.laowang.mqtest.hello;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.nio.charset.StandardCharsets;
public class Send {
    private final static String QUEUE_NAME = "hello";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
//        factory.setHost("localhost");
        factory.setHost("45.122.146.23");
        factory.setPort(5672);
        factory.setVirtualHost("ruanjianlaowang");
        factory.setUsername("ruanjianlaowang");
        factory.setPassword("123");

        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            String message = "Hello World!";
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes(StandardCharsets.UTF_8));
            System.out.println(" [x] Sent '" + message + "'");
        }
    }
}

(3) Message accepting class Recv.java:

package com.laowang.mqtest.hello;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;
public class Recv {
    private final static String QUEUE_NAME = "hello";
    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
//        factory.setHost("localhost");
        factory.setHost("45.122.146.23");
        factory.setPort(5672);
        factory.setVirtualHost("ruanjianlaowang");
        factory.setUsername("ruanjianlaowang");
        factory.setPassword("123");
        
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            System.out.println(" [x] Received '" + message + "'");
        };
        channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> { });
    }
}

Description: Change the connection information to your own connection information.

(4) Execution effect diagram

Send class:

Class accepted, sent three times.

2.3 Summary of the problem:

(1) The virtual host can be created with "/" or without it. Pay attention to the configuration.

(2) The firewall remembers to release the port number.

I'm "Software Pharaoh", if you think it's okay, pay attention to the next one, and the follow-up update will be known in seconds! Welcome to the discussion area and the public account of the same name to leave a message and exchange!

Tags: RabbitMQ MQ

Posted by TobyRT on Mon, 30 May 2022 05:54:46 +0530