RabbitMQ introduction, installation and use commands

Read the table of contents

1 Introduction to RabbitMQ

       RabbitMQ is an open source implementation of AMQP (Advanced Message Queue) developed by erlang, the official website address: http://www.rabbitmq.com . As a message broker, RabbitMQ is mainly responsible for receiving, storing and forwarding messages. It provides reliable message mechanisms and flexible message routing, and supports message clustering and distributed deployment. It is often used for application decoupling, time-consuming task queues, and traffic reduction. Feng waiting for the scene. This series of articles will systematically introduce RabbitMQ's working mechanism, code driver and cluster configuration. This article mainly introduces some basic concepts in RabbitMQ, commonly used RabbitMQ Control commands, and finally writes a simple chestnut driven by C#. First look at the basic structure of RabbitMQ:

  The above picture is a basic structure of RabbitMQ. Producer and Consumer are both clients of RabbitMQ. Producer is responsible for sending messages, and Consumer is responsible for consuming messages.

Next we combine this picture to understand some concepts of RabbitMQ:

  Broker (Server) : The process of accepting client connections and realizing AMQP message queue and routing functions. We can call Broker a RabbitMQ server.

  Virtual Host : A virtual concept. There can be several Exchanges and Queues in a Virtual Host, which are mainly used for permission control and isolation of applications. If application A uses VhostA and application B uses VhostB, then we only store the exchange, queue, and messages of application A in VhostA. Users of application A can only access VhostA, not the data in VhostB.

  Exchange : Accept the message sent by the producer and route the message to the queue in the server according to the Binding rule. ExchangeType determines the behavior of Exchange routing messages. For example, in RabbitMQ, there are four ExchangeTypes: Direct, Fanout, Topic, and Header. Different types of Exchange routing rules are different (these will be described in detail later).

  Queue : The message queue is used to store messages that have not been consumed by consumers. The queue is first in, first out. By default, messages stored first are processed first.

  Message : It is a message, composed of Header and Body. Header is a collection of various attributes added by the producer, including whether the Message is persisted, which Message Queue accepts, and what is the priority. Body is the data that is actually transmitted. The content format is byte[].

  Connection : Connection, for RabbitMQ, is actually a TCP connection between the client and the Broker.

  Channel : Channel. After only the connection between the client and the Broker is created, the client cannot send messages. A Channel needs to be created on the basis of the Connection. The AMQP protocol stipulates that AMQP commands can only be executed through the Channel. A Connection can contain multiple Channels. Channel is needed because the establishment and release of TCP connections are very expensive.

2 RabbitMQ installation

  Because RabbitMQ is developed in erlang language, we must install erlang support before installing RabbitMQ.

1 Windows platform installation

1 Install erlang

  First download erlang and download the latest version directly. The current download is OTP 21.3 Windows 64-bit Binary File. After the download is complete, you can install it in the next step.

2 Install RabbitMQ

  Download RabbtMQ on the Windows platform . The current download is rabbitmq-server-3.7.14.exe. After the download is complete, you can install it in the next step.

3 Install the web management plugin

  Open RabbitMQ Command Prompt and execute the command rabbitmq-plugins enable rabbitmq_management to complete the installation of the web monitoring plug-in. 

  After the installation is complete, open the browser and enter http://127.0.0.1:15672/ and use the default account [name:guest / password:guest] to log in. The interface is as follows. Using this UI plugin, we can easily view the exchange in RabbitMQ (exchange ), queue and other content, you can also add, modify, and delete exchange, queue, and users.

  At this point, the Windows platform installation of RabbitMQ is complete. Open the service manager, RabbitMQ is already running normally, as follows:

2 Centos install RabbitMQ

1 Install RabbitMQ

  The virtual machine system here is Centos7, and the installation method adopted is yum installation. For simplicity, we directly use the official erlang and RabbitMQ-server automatic installation scripts ( official installation documents ), and execute the following code line by line to install erlang. And RabbitMQ.

Copy code

#Install socat 
yum install socat #Install 

erlang 
  curl -s https://packagecloud.io/install/repositories/rabbitmq/erlang/script.rpm.sh | sudo bash 
  yum -y install erlang 

#Install rabbitmq-server 
  curl -s https ://packagecloud.io/install/repositories/rabbitmq/rabbitmq-server/script.rpm.sh | sudo bash 
  yum -y install rabbitmq-server 

#Start rabbitmq service 
  systemctl start rabbitmq-server #Add 
web management plug-in 
  rabbitmq-plugins enable rabbitmq_management

Copy code

Supplement: If the RabbitMQ execution command is particularly slow after the installation is complete, or an error occurs [ rabbitmq unable to perform an operation on node xxx@xxx ], the solution:

  Edit hosts, execute the command vim /etc/hosts, add the local IP (or virtual machine IP)

  After the command is executed, use a browser to visit http://127.0.0.1:15672/ and the web management interface will also appear. RabbitMQ installed through the installation steps above will generate Unit files, so we can use Systemd to manage RabbitMQ services. Here are a few commonly used commands:

Copy code


  #Start the RabbitMQ service systemctl start rabbitmq-server #Stop the 
RabbitMQ service 
  systemctl stop rabbitmq-server #View the 
running status of RabbitMQ 
  systemctl status rabbitmq-server #Restart the 
RabbitMQ service 
  systemctl restart rabbitmq-server

Copy code

2 RabbitMQ Control tool

  Most of the common functions of RabbitMQ can be realized by using the Web management interface, but some functions cannot be achieved by the WebUI, such as opening/closing the RabbitMQ application and cluster management. RabbitMQ Control is a command line management tool of RabbitMQ, which can call all the built-in functions of RabbitMQ. The main command is rabbitmqctl. Below is a command to query the user list. Note that you need to switch to the sbin directory to execute:

  In order to use the RabbitMQ Control tool conveniently, we'd better add an environment variable. When Windows is installed by default, add one to the PATH: C:\Program Files\RabbitMQ Server\rabbitmq_server-3.7.14\sbin, if it is not installed by default, find the corresponding Add PATH to the installation directory. According to the installation method above, Centos can directly use RabbitMQ Control tool without extra configuration. If you want to learn more about the RabbitMQ Control tool, you can refer to the official documentation of RabbitMQ Control .

  Here is a summary of some of the most commonly used RabbitMQ Controll commands. Interested friends can try to run these commands. For example, use the command rabbitmqctl add_user <username> <password> in the command line tool to add a new user.

1 Basic control commands

  Basic control commands are mainly used to start and stop applications, runtime, etc.

Copy code

#Stop rabbitmq and runtime 
  rabbitmqctl shutdown #Stop 
erlang node 
  rabbitmqctl stop 
#Enable 
  rabbitmq rabbitmqctl start_app #Stop rabbitmq 
rabbitmqctl 
  stop_app #View 
status 
  rabbitmqctl reset status 
  #View environment 
rabbitmqctl 
environment #rabbitmqctl environment #rabbitmqctlrabbit and the internal exchanges are cleared 
  .

Copy code

2 Service status management

  These commands are mainly used to view exchange, channel, binding, queue, consumers:

Copy code


  #Return to queue information list_queues [-p <vhostpath>] [<queueinfoitem> ...] 
  #Return 
to exchange information list_exchanges [-p <vhostpath>] [<exchangeinfoitem> ...] 
  #Return 
to binding information list_bindings [- p <vhostpath>] [<bindinginfoitem> ...] 
  #Return 
 link information list_connections [<connectioninfoitem> ...] 
  #Return 
all current channels list_channels [<channelinfoitem> ...]    
  #Return 
consumers list_consumers [-p <vhostpath >]

Copy code

3 User management commands

  These commands are mainly used to add, modify, delete users and manage user permissions

Copy code

#Add user in rabbitmq's internal database 
  add_user <username> <password>   
  #Delete 
a user delete_user <username> #Change 
user password 
  change_password <username> <newpassword> 
  #Clear 
user password and prohibit user login clear_password <username> #Set 
user tags , Is to set the user role 
  set_user_tags <username> <tag> 
# View user list 
  list_users #Create 
a vhost 
  add_vhost <vhostpath> #Delete 
a vhosts 
  delete_vhost <vhostpath>     
  #List 
vhosts list_vhosts [<vhostinfoitem> ...] #For 
a vhosts Give users relevant permissions 
  set_permissions [-p <vhostpath>] <user> <conf> <write> <read>
  #Clear 
a user's permissions on vhost clear_permissions [-p <vhostpath>] <username> 
all users' permissions on a vhost
  list_permissions [-p <vhostpath>] #List   
the access permissions of a user 
  list_user_permissions <username>

Copy code

4 cluster management commands

Copy code

#clusternode indicates the name of the node, --ram indicates that the node joins the cluster as a ram node. By default, a node joins the cluster as a disc node. Before a node joins the cluster, the rabbitmq application of the node must be stopped, that is, stop_app must be executed first. 
  join_cluster <clusternode> [--ram] #Display    
all nodes in the cluster 
  cluster_status 
  #Set the 
cluster name set_cluster_name <clustername> 
  #Modify the 
cluster name rename_cluster_node <oldname> <newname> #Change 
the mode of a node in a cluster, the node is before conversion It must be stopped first, and the only disk node in a cluster cannot be converted to ram node 
  change_cluster_node_type <disc | ram> 
  #Remotely 
delete a node, the node must be stopped before deleting rabbitmqctl forget_cluster_node rabbit@rabbit1 #Synchronization 
mirror queue 
  sync_queue <queuename> 
  #Cancel 
synchronization queue cancel_sync_queue <queuename>   
  #Empty 
all messages in the queue purge_queue [-p vhost] <queuename>

Copy code

  Many of the commands listed here are not used at this stage, such as cluster control related commands. The usage of these commands will be gradually understood in the following chapters.

Guess you like

Origin blog.csdn.net/u014748504/article/details/108535431