1. Concept
1. Message middleware
Message middleware can also be called message queue, which refers to the use of efficient and reliable message delivery mechanism for platform-independent data exchange, and the integration of distributed systems based on data communication. By providing message passing and message queuing models, process communication can be extended in a distributed environment.
2. RabbitMQ concept
RabbitMQ is an open source message queue system developed using the Erlang language and implemented based on the AMQP protocol. The main features of AMQP are message-oriented, queue-oriented, routing (including point-to-point and publish/subscribe), reliability, and security. The AMQP protocol is more commonly used in enterprise systems that require high data consistency, stability, and reliability. The requirements for performance and throughput are secondary.
2. Characteristics of message middleware
-
Redundancy (storage)
-
Scalability
-
recoverability
-
Order guaranteed
-
buffer
-
Asynchronous communication
3. Two modes of message middleware
1. P2P mode
The P2P model contains three roles: message queue (Queue), sender (Sender), and receiver (Receiver). Each message is sent to a specific queue and the receiver gets the message from the queue. The queue holds messages until they are consumed or time out.
Features of P2P:
-
Each message has only one consumer (Consumer), that is, once consumed, the message is no longer in the message queue.
-
There is no time dependency between the sender and the receiver. That is to say, after the sender sends the message, whether the receiver is running or not, it will not affect the message being sent to the queue.
-
After successfully receiving the message, the receiver needs to respond successfully to the queue.
-
If you want every message sent to be processed successfully, you need P2P mode
2. Pub/Sub mode
The Pub/Sub model includes three roles: Topic, Publisher, and Subscriber. Multiple publishers send messages to a Topic, and the system delivers these messages to multiple subscribers.
Pub/Sub features:
-
Each message can have multiple consumers
-
There is a time dependency between publishers and subscribers. For a subscriber of a certain topic (Topic), it must create a subscriber before it can consume the publisher’s messages.
-
In order to consume messages, the subscriber must remain running
-
If you want to send a message without any processing, or only be processed by one messager, or can be processed by multiple consumers, then you can use the Pub/Sub model
4. Ordinary cluster construction
1. Environment preparation
#Three virtual hosts:
192.168.10.133 rabbitmq-1
192.168.10.134 rabbitmq-2
192.168.10.135 rabbitmq-3
Note: The three virtual machines are on the same network. If they span the WAN, the effect will become worse
2. Take a virtual machine as an example to install rabbitmq service
①Configure domain name resolution for all three devices
[root@rabbitmq-1 ~]# vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.10.133 rabbitmq-1 192.168.10.134 rabbitmq-2 192.168.10.135 rabbitmq-3
②Environment installation, rabbitmq service installation
#Install dependent environment [root@rabbitmq-1 ~]# yum install -y epel-release gcc-c + + unixODBC unixODBC-devel openssl-devel ncurses-devel #yuminstallerlang [root@rabbitmq-1 ~]# curl -s https://packagecloud.io/install/repositories/rabbitmq/erlang/script.rpm.sh | sudo bash [root@rabbitmq-1 ~]# yum install erlang-21.3.8.21-1.el7.x86_64 #erlEnvironment testing; [root@rabbitmq-1 ~]# erl Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:1:1] [ds:1:1:10] [async-threads:10] [hipe] [kernel-poll:false] Eshell V9.3 (abort with ^G) 1> #Install rabbitmq https://github.com/rabbitmq/rabbitmq-server/releases/tag/v3.7.10 [root@rabbitmq-1 ~]# yum install rabbitmq-server-3.7.10-1.el7.noarch.rpm
Note: The rabbitmq version and the erlang version must correspond to each other. The specific corresponding versions can be viewed on the rabbitmq official website: https://www.rabbitmq.com/news.html
# erlang official website
https://packagecloud.io/rabbitmq/erlang
3. rabbitmq enables remote login
[root@rabbitmq-1 ~]# cp /usr/share/doc/rabbitmq-server-3.7.10/rabbitmq.config.example /etc/rabbitmq/rabbitmq.config #Open the comment of {loopback_users, []} and delete the following comma [root@rabbitmq-1 ~]# vim /etc/rabbitmq/rabbitmq.config %% The default "guest" user is only permitted to access the server %% via a loopback interface (e.g. localhost). %% {loopback_users, [<<"guest">>]}, %% %% Uncomment the following line if you want to allow access to the %% guest user from anywhere on the network. {loopback_users, []}
4. Start rabbitmq service
[root@rabbitmq-1 ~]# systemctl daemon-reload [root@rabbitmq-1 ~]# systemctl start rabbitmq-server [root@rabbitmq-1 ~]# systemctl enable rabbitmq-server #Join self-start
#Open web access
[root@rabbitmq-1 ~]# rabbitmq-plugins enable rabbitmq_management
5. Check port status
[root@rabbitmq-1 ~]# ss -lntp | grep beam
Port 25672: rabbitmq service internal communication call port
Port 15672: web interface login port
Port 5672: Development call port
Port 4369: erlang discovery port
6. rabbitmq user creation
After the rabbitmq service is set up, there is a default user, account: guest, password: guest
#Add a user
#admin is the account number, 123456 is the password [root@rabbitmq-1 ~]# rabbitmqctl add_user admin 123456 #Set admin as the management account [root@rabbitmq-1 ~]# rabbitmqctl set_user_tags admin administrator #View users [root@rabbitmq-1 ~]# rabbitmqctl list_users #Set admin permissions [root@rabbitmq-1 ~]# rabbitmqctl set_permissions -p "/" newrain ".*" ".*" ".*" #Three '.*' represent conf permissions, read permissions and write permissions
7. Visit rabbitmq’s web interface: http://192.168.10.133:15672
#Log in with account number: guest, password: guest
#The remaining two are deployed in the same way
5. rabbitmq cluster deployment
1. First create the data storage directory and log storage directory: do all three
#Directory location can be customized [root@rabbitmq-1 ~]# mkdir -p /data/rabbitmq/data [root@rabbitmq-1 ~]# mkdir -p /data/rabbitmq/logs #Authorize or change directory attributes #[root@rabbitmq-1 ~]# chmod 777 -R /data/rabbitmq [root@rabbitmq-1 ~]# chown rabbitmq.rabbitmq /data/ -R #Create configuration file: [root@rabbitmq-1 ~]# vim /etc/rabbitmq/rabbitmq-env.conf [root@rabbitmq-1 ~]# cat /etc/rabbitmq/rabbitmq-env.conf #Data storage location RABBITMQ_MNESIA_BASE=/data/rabbitmq/data #Log storage location RABBITMQ_LOG_BASE=/data/rabbitmq/logs #Restart service [root@rabbitmq-1 ~]# systemctl restart rabbitmq-server
2.Copy?erlang.cookie
The Rabbitmq cluster relies on the Erlang cluster to operate, so the Erlang cluster environment must be built first. In the Erlang cluster, each node is implemented through a magic cookie. This cookie is stored in /var/lib/rabbitmq/.erlang.cookie, and the file has 400 permissions. Therefore, it is necessary to ensure that the cookies of each node are consistent, otherwise the nodes will not be able to communicate with each other.
Note: We use rabbitmq-1 as the main machine and copy its .erlang.cookie file to rabbitmq-2 and rabbitmq-3
#Copy to rabbitmq-2 [root@rabbitmq-1 ~]# scp /var/lib/rabbitmq/.erlang.cookie 192.168.10.134:/var/lib/rabbitmq/ #Copy to rabbitmq-3 [root@rabbitmq-1 ~]# scp /var/lib/rabbitmq/.erlang.cookie 192.168.10.135:/var/lib/rabbitmq/ #Restart rabbitmq-2 and rabbitmq-3 respectively [root@rabbitmq-2 ~]# systemctl restart rabbitmq-server [root@rabbitmq-3 ~]# systemctl restart rabbitmq-server
3. Add rabbitmq-2 and rabbitmq-3 as memory and disk nodes to the rabbitmq-1 node cluster
#Execute the following commands on rabbitmq-2 and rabbitmq-3: #Stop node [root@rabbitmq-2 ~]# rabbitmqctl stop_app [root@rabbitmq-2 ~]# rabbitmqctl reset #If there is data that needs to be reset, if not, don’t use it. #Add to memory node. If you want to add disk node, just remove the --ram parameter. [root@rabbitmq-2 ~]# rabbitmqctl join_cluster --ram rabbit@rabbitmq-1 Clustering node 'rabbit@rabbitmq-2' with 'rabbit@rabbitmq-1' ... #Start node [root@rabbitmq-2 ~]# rabbitmqctl start_app Starting node 'rabbit@rabbitmq-2' ...
(1) By default, rabbitmq is a disk node after startup. Under this cluster command, rabbitmq-2 and rabbitmq-3 are memory nodes.
mq-1 is the disk node.
(2) If you want to make mq-2 and mq-3 both disk nodes, remove the –ram parameter.
(3) If you want to change the node type, you can use the command rabbitmqctl change_cluster_node_type
disc(ram), the premise is that the rabbit application must be stopped?
4. Check the cluster status on the rabbitmq-1 disk node
[root@rabbitmq-1 ~]# rabbitmqctl cluster_status
5. Log in to the rabbitmq web management console and check the status:
rabbithttp-1: http://192.168.10.133:15672
rabbithttp-2: http://192.168.10.134:15672
rabbithttp-3: http://192.168.10.135:15672
6. rabbitmq cluster mirror configuration
The RabbitMQ default cluster mode has been completed above, but it does not guarantee the high availability of the queue. Although the switches and bindings can be copied to any node in the cluster, the queue contents will not be copied. However, the downtime of the queue node directly causes the queue to be unavailable and can only wait for restart. Therefore, if you want to be able to apply it normally even when the queue node is down or malfunctions, you must copy the queue content to each node in the cluster, and you must create a mirror queue. .
The mirror queue is based on the ordinary cluster mode, and then some policies are added, so you still have to configure the ordinary cluster first, and then you can set up the mirror queue. We will continue with the above cluster.
1. Execute the following commands on all three virtual machines
[root@rabbitmq-1 ~]# rabbitmqctl set_policy ha-all "^" '{"ha-mode":"all"}'
Note: “-p Vhost”: vhost name, “^” matches all queues, ha-all policy name is ha-all, ‘{“ha-mode”:”all “}’ If the policy mode is all, it will be copied to all nodes, including new nodes.
2. Log in to the web interface to view
7. Rabbitmq load balancing construction, using haproxy
1. Open a new virtual machine and install the haproxy service
[root@haproxy ~]# yum -y install haproxy
2. Modify the haproxy configuration file /etc/haproxy/haproxy.cfg
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg global log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy nbproc 4 daemon # turn on stats unix socket stats socket /var/lib/haproxy/stats #------------------------------------------------ ------------------- defaults mode http log global retries 3 timeout connect 10s timeout client 1m timeout server 1m timeout check 10s maxconn 2048 #------------------------------------------------ ------------------- ##Monitor and view local status##### listenadmin_stats bind *:80 #web platform port mode http option httplog option httpclose log 127.0.0.1 local0 err stats uri /haproxy stats auth admin:123456 stats refresh 30s #################################### ###Anti-generation monitoring frontend server bind *:5670 #Agent port log global mode tcp #option forwardfor default_backend rabbitmq maxconn 3 backend rabbitmq mode tcp log global balance roundrobin server rabbitmq-1 192.168.10.133:5672 check inter 2000s rise 2 fall 3 server rabbitmq-2 192.168.10.134:5672 check inter 2000s rise 2 fall 3 server rabbitmq-3 192.168.10.135:5672 check inter 2000s rise 2 fall 3
3. Log in to the web to view the proxy: http://192.168.10.136/haproxy
8. Frequently Asked Questions
1. When using rabbitmq-server -detached command to start rabbitmq, the following prompt appears: Warning: PID file not written; -detached was passed. At this time, use rabbitmqctl status to prompt that the service has been started. It can be seen that this problem does not need to be solved.
2. Due to the change of the hostname file, every time rabbitmqctl stop or rabbitmqctl cluster_status, etc., an error is reported as long as it is a rabbitmq command. The prompt is as follows:
Cluster status of node rabbit@web2 ... Error: unable to connect to node rabbit@web2: nodedown DIAGNOSTICS =========== attempted to contact: [rabbit@web2] rabbit@web2: * connected to epmd (port 4369) on web2 * epmd reports node 'rabbit' running on port 25672 * TCP connection succeeded but Erlang distribution failed * Hostname mismatch: node "rabbit@mq2" believes its host is different. Please ensure that hostnames resolve the same way locally and on "rabbit@mq2" current node details: - node name: 'rabbitmq-cli-11@web2' - home dir: /root - cookie hash: SGwxMdJ3PjEXG1asIEFpBg==
At this time, first ps aux | grep mq
, then kill -9
the process, and then rabbitmq-server -detached
to solve the problem. (That is, force kill first, and then restart); or exit the terminal and reconnect.
3. After restarting using rabbitmqctl stop
and rabbitmq-server -detached
, the originally added user admin, virtual host coresystem, etc. will be lost and need to be added again.
4. returned an error: shutdown: failed to start child: Logger.ErrorHandler
Reason: The erlang version does not correspond to the RabbitMQ version
Solution: Check the version according to RabbitMQ Erlang Version Requirements https://www.rabbitmq.com/which-erlang.html
9. Commonly used commands
1. Plug-in management
#Open a plug-in rabbitmq-plugins enable xxx #Close a plug-in rabbitmq-plugins disable xxx #Note: It will take effect after restarting the server.
2. virtual_host management
#New virtual_host:rabbitmqctl add_vhost xxx #undo virtual_host:rabbitmqctl delete_vhost xxx
3. User management
#New user rabbitmqctl add_user xxxpwd #delete users rabbitmqctl delete_user xxx #View users rabbitmqctl list_users #change Password rabbimqctl change_password {username} {newpassword} #Set user role rabbitmqctlset_user_tags {username} {tag ...} #Tag can be administrator, monitoring, management
4. Other commands
rabbitmq command: rabbitmq-plugins list ----View installed plug-ins rabbitmq-server -detached -----------Start the RabbitMQ node rabbitmqctl start_app ----------Start the RabbitMQ application, not the node rabbitmqctl stop_app ------Stop rabbitmqctl status ------View status rabbitmqctl add_user mq 123456 -------Set user and password rabbitmqctl set_user_tags mq administrator ------------------Add an account and set it as administrator rabbitmq-plugins enable rabbitmq_management --------------------Enable RabbitMQ_Management rabbitmqctl cluster_status ------------------Cluster status rabbitmqctl forget_cluster_node rabbit@rabbit3 -------------------Node removal rabbitmqctl reset application----------------------reset rabbitmqctl set_permissions -p "/" 123456 ".*" ".*" ".*" ---------------Authorization -------------------------- #View Connection, Queue, Channel, User rabbitmqctl list_connections #List all connections rabbitmqctl list_queues #List all queues rabbitmqctl list_channels #List all channels rabbitmqctl list_users #List all users #Set node type #If you want to change the node type, you can modify it through the command, as follows: rabbitmqctl stop_app rabbitmqctl change_cluster_node_type dist rabbitmqctl change_cluster_node_type ram rabbitmqctl start_app ================================================== = #Remove node #If you want to remove the node from the cluster, you can use the following command to achieve this: rabbitmqctl stop_app rabbitmqctl restart rabbitmqctl start_app ============================================= #Cluster restart sequence #The order of cluster restart is fixed and reversed. As stated below: #Startup sequence: disk node => memory node #Close sequence: memory node => disk node #The last shutdown must be the disk node, otherwise it may cause abnormal situations such as cluster startup failure and data loss.
The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Cloud native entry-level skills treeHomepageOverview 16,989 people are learning the system