"RabbitMQ" Getting Started Guide (Installation, Configuration, Application)

Preface

RabbitMQ is complete based on the AMQP (Advanced Message Queuing Protocol) protocol standard and can Reusableenterprise messaging system. It follows the Mozilla Public License open source agreement, uses Erlang to implement an industrial-grade message queue (MQ) server, and is built on the Erlang OTP platform (because it is developed in Erlang, RabbitMQ stability and reliability relatively high)

Other mainstream MQ products

  • ActiveMQ: Produced by Apache, the most popular and powerful open source message bus, based on the JMS (Java Message Service) specification
  • RocketMQ: Alibaba’s low-latency, high-concurrency, high-availability, and high-reliability distributed messaging middleware, based on JMS and currently maintained by the Apache Foundation
  • Kafka: distributed, partitioned, multi-copy, multi-subscriber message publishing and subscription system (distributed MQ system), which can be used for search logs, monitoring logs, access logs, etc.

This article isRabbitMQ introductory tutorial, which will mainly explainRabbitMQ installation and configuration (Windows), related Concept, and specific application in the project

image.png

Install

Erlang

Official website download link:Downloads - Erlang/OTP

The RabbitMQ server must first install the Erlang operating environment. At the same time, you need to pay attention to the Erlang version that RabbitMQ depends on during installation. We can check the official version corresponding information below

Version corresponding:RabbitMQ Erlang Version Requirements — RabbitMQ

The version used this time Erlang OTP 25.3 (click to jump to the download link)

image.png

Double-click to execute the exe installation program, and follow the default except for the installation path.

Then configure the environment variables

ERLANG_HOME = D:\Erlang\Erlang\Erlang OTP

And add the /bin directory to the Path environment variable, that is, add %ERLANG_HOME%\bin to Path

After installation and configuration, open CMD, enter erl and press Enter. The version information will pop up, indicating that Erlang is installed successfully.

RabbitMQ

Official download page:RabbitMQ Changelog — RabbitMQ

Download link: RabbitMQ 3.12.0

Install the exe file and execute the installation package. Also keep the defaults except for the installation path.

Configure environment variables

RABBITMQ_SERVER = D:\RabbitMQ\RabbitMQ\rabbitmq_server-3.12.0

Then add %RABBITMQ_SERVER%\sbin to the Path environment variable

View all plugins

rabbitmq-plugins list

Note: If problems occur, please refer to the last chapter Uninstall completely

image.png

After that, we need to install the rabbitmq_management plug-in, which can visually view the status of the RabbitMQ server instance and control the RabbitMQ server.

# 安装插件
rabbitmq-plugins enable rabbitmq_management

Access the management interface: http://localhost:15672/ (account password: guest / guest)

image.png

The preliminary installation and configuration is complete. You can study with the official introductory documentation below.

Official documentation:RabbitMQ Tutorials — RabbitMQ

message queue

definition

Message refers to the data passed between two applications. The data can come in many forms and may contain just text strings or embedded objects.

"Message Queue" is a container that saves messages during their transmission.. In a message queue, there are usually two roles: producer and consumer. The producer is only responsible for sending data to the message queue. He does not care who takes out the data from the message queue for processing. The consumer is only responsible for fetching data from the message queue and processing it. He does not care who sent the data

image.png

effect

Decoupling. as the picture shows. Assume that systems B, C, and D all need data from system A, so system A calls three methods to send data to B, C, and D. At this time, system D is no longer needed, so the relevant code needs to be deleted in system A. Assume that there is a new system E that needs data at this time, and system A needs to add code to call system E. In order to reduce this strong coupling, you can use MQ. System A only needs to send data to MQ. If other systems need data, they can obtain it from MQ.

image.png

Asynchronous. as the picture shows. When a client request is sent in, system A will call systems B, C, and D. If the request is made synchronously, the response time is the sum of systems A, B, C, and D, which is 800ms. If you use MQ, system A sends data to MQ, and then returns a response to the client. There is no need to wait for responses from systems B, C, and D, which can greatly improve performance. For some non-essential businesses, such as sending text messages, sending emails, etc., you can use MQ

image.png

Peak clipping. as the picture shows. This is actually a very important application of MQ. Assume that system A has a sudden increase in the number of requests during a certain period of time, and 5,000 requests are sent. System A will then send 5,000 SQL statements to MySQL for execution. Of course, MySQL cannot handle such a large number of requests, and MySQL will crash. Causing system paralysis. If MQ is used, System A no longer sends SQL directly to the database, but sends the data to MQ. It is acceptable for MQ to backlog data in a short period of time, and then the consumer pulls it each time 2000 items are processed to prevent a large number of requests from being sent directly to MySQL during the peak request period, causing the system to crash

image.png

Features

Reliability: Ensure reliability by supporting message persistence, supporting transactions, supporting ack for consumption and transmission, etc.

Routing mechanism: Supports mainstream subscription consumption models, such as broadcast, subscription, headers matching, etc.

Scalability: Multiple RabbitMQ nodes can form a cluster, or the nodes in the cluster can be dynamically expanded according to actual business conditions

High availability: The queue can be mirrored on the machines in the cluster, so that the queue is still available if some nodes have problems

Multiple protocols: In addition to natively supporting AMQP protocol, RabbitMQ also supports STOMP, MQTT and other message middleware protocols

Multi-language client: RabbitMQ supports almost all commonly used languages, such as Java, Python, Ruby, PHP, C#, JavaScript, etc.

Management interface: RabbitMQ provides an easy-to-use user interface that allows users to monitor and manage messages, nodes in the cluster, etc.

Plug-in mechanism: RabbitMQ provides many plug-ins to achieve expansion in many aspects. Of course, you can also write your own plug-ins

application

This chapter will integrate rabbitmq into SpringBoot, and use rabbitmq-provider (producer) and rabbitmq-consumer (consumer) two projects for detailed explanation. You can also create these two modules in the parent project (this article uses the parent-child module method )

All code samples have been uploaded to the GitHub repository

Warehouse address:ReturnTmp/rabbitmq-demo: rabbitmq example code (github.com)

producer

Configuration

Create submodule rabbitmq-provider

Dependency configuration (you can also check it directly in the IDEA initialization module)

        <!--rabbitmq-->
        <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>

application.yml

server:  
  port: 8021  
spring:  
  application:  
    name: rabbitmq-provider  
  rabbitmq:  
    host: 127.0.0.1  
    port: 5672  
    username: root  
    password: 111111  
    virtual-host: RootHost

The virtual host configuration item is not necessary. You need to create the vhost yourself. If you do not create it yourself, the default isvirtual-host: /

Note: vhost can be understood as a virtual broker, that is, mini-RabbitMQ server, which contains independent queue, bind, exchange, etc. The most important thing is that it has an independent permission system, which can achieve vhost-wide User control. Of course, from the global perspective of RabbitMQ, vhost can be used as a means of isolating different permissions

You can create a vhost by following the steps below

image.png

Then create user (administrator)

image.png

Then we need to assign permissions to the user and specify to use the vhost we just created

image.png

code

CreateDirect-connected switchConfiguration class

Note: RabbitMQ has four types of switches, namely: direct switch, sector switch, topic switch, and header switch. A direct-connected switch is used for demonstration here. Other readers can try it themselves.

@Configuration
public class DirectRabbitConfig {
    
    

    //队列 起名:TestDirectQueue
    @Bean
    public Queue TestDirectQueue() {
    
    
        // durable:是否持久化,默认是false,持久化队列:会被存储在磁盘上,当消息代理重启时仍然存在,暂存队列:当前连接有效
        // exclusive:默认也是false,只能被当前创建的连接使用,而且当连接关闭后队列即被删除。此参考优先级高于durable
        // autoDelete:是否自动删除,当没有生产者或者消费者使用此队列,该队列会自动删除。
        //   return new Queue("TestDirectQueue",true,true,false);

        //一般设置一下队列的持久化就好,其余两个就是默认false
        return new Queue("TestDirectQueue", true);
    }

    //Direct交换机 起名:TestDirectExchange
    @Bean
    DirectExchange TestDirectExchange() {
    
    
        //  return new DirectExchange("TestDirectExchange",true,true);
        return new DirectExchange("TestDirectExchange", true, false);
    }

    //绑定  将队列和交换机绑定, 并设置用于匹配键:TestDirectRouting
    @Bean
    Binding bindingDirect() {
    
    
        return BindingBuilder.bind(TestDirectQueue()).to(TestDirectExchange()).with("TestDirectRouting");
    }


    @Bean
    DirectExchange lonelyDirectExchange() {
    
    
        return new DirectExchange("lonelyDirectExchange");
    }

}

Then write a simple interface for message push (can be written as a scheduled task depending on the situation)

@RestController
public class SendMessageController {
    
    

    @Autowired
    RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法

    @GetMapping("/sendDirectMessage")
    public String sendDirectMessage() {
    
    
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "test message, hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        //将消息携带绑定键值:TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);
        return "ok";
    }
}

Start the project and call the interface: http://localhost:8021/sendDirectMessage

Check the RabbitMQ management page to see if the push is successful.

image.png

image.png

consumer

Configuration

Create submodule rabbitmq-consumer

Depend on configuration

        <!--rabbitmq-->
        <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>

application.yml

server:  
  port: 8022  
spring:  
  application:  
    name: rabbitmq-consumer  
  rabbitmq:  
    host: 127.0.0.1  
    port: 5672  
    username: root  
    password: 111111  
    virtual-host: RootHost
code

Create a message receiving listening class

@Component
@RabbitListener(queues = "TestDirectQueue")
public class DirectReceiver {
    
    

    @RabbitHandler
    public void process(Map testMessage) {
    
    
        System.out.println("DirectReceiver receive message: " + testMessage.toString());
    }
}

Then start the project and check the reception status of consumers

image.png

Serialization

Sending and receiving messages may cause Failed to convert message problems, which can be solved by using JSON serialization to transmit information

producer
@Configuration
public class RabbitMQConfig implements InitializingBean {
    
    

    /**
     * 自动注入RabbitTemplate模板
     */
    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送消息JSON序列化
     */
    @Override
    public void afterPropertiesSet() {
    
    
        //使用JSON序列化
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
    }
}
consumer
@Configuration
public class RabbitMQConfig {
    
    

    @Bean
    public MessageConverter jsonMessageConverter(ObjectMapper objectMapper) {
    
    
        return new Jackson2JsonMessageConverter(objectMapper);
    }
}

Completely uninstall

Various problems may occur during our installation. Generally, the versions of RabbitMQ and Erlang do not correspond. You need to completely uninstall RabbitMQ and Erlang. You can uninstall according to the following steps.

Note: The blogger used Erlang 20.3 Rabbit 3.7.15 for the first installation. Later, it seems that the smaller version does not correspond. If a problem occurs, you need to uninstall and install again.

(1) Open the Windows Control Panel and double-click "Programs and Features".

(2) In the list of currently installed programs, right-click RabbitMQ Server and click "Uninstall".

(3) In the list of currently installed programs, right-click "Erlang OTP" and click "Uninstall".

(4) Open Windows Task Manager.

(5) In the task manager, find the process epmd.exe. If the process is still running, right-click the process and click End Process.

(6) Delete all installation directories of RabbitMQ and Erlang.

(7) Exclusion of sentence C:\Windows\System32\config\systemprofile.erlang.cookie(Result existence).

(8) Go to the user folder:C:\Users\[username] and delete the file .erlang.cookie.

(9) Also in the User folder, go to AppData \ Roaming \ RabbitMQ. Delete the RabbitMQ folder.

(10) Delete the subkey of registry HKEY_LOCAL_MACHINE\SOFTWARE\Ericsson\Erlang\ErlSrv.

(11) Open and run cmd->sc delete RabbitMQ.

(12) Open Run->regedit to find the RabbitMQ node and delete it (if it exists)

Reference link

This article is published by the blog post platform OpenWrite!

Guess you like

Origin blog.csdn.net/m0_63748493/article/details/134585920