MQ message queue-rabbitmq

Introduction to message queue

rabbitMQ kafka activeMQ rocketMQ


Split payment orders by business

Vertical Split
Alipay Pay Wechat Pay Cloud QuickPass

Several elements for implementing microservices.
How do microservices implement (docker)
? How do microservices discover each other?
How do microservices access each other?
Rapid expansion
, how to monitor,
upgrade and rollback (CI/CD),
how to view access logs (ELK)

spring boot
spring cloud

postgres@server01:~$ apt-cache madison rabbitmq-server

rabbitMQ stand-alone installation

Official website https://www.rabbitmq.com/install-debian.html#apt-quick-start-cloudsmith

官方安装脚本,ubuntu22.04系统
root@server01:~/sh# cat install_rabbitmq.sh 
#!/bin/sh

sudo apt-get install curl gnupg apt-transport-https -y

## Team RabbitMQ's main signing key
curl -1sLf "https://keys.openpgp.org/vks/v1/by-fingerprint/0A9AF2115F4687BD29803A206B73A36E6026DFCA" | sudo gpg --dearmor | sudo tee /usr/share/keyrings/com.rabbitmq.team.gpg > /dev/null
## Community mirror of Cloudsmith: modern Erlang repository
curl -1sLf https://ppa.novemberain.com/gpg.E495BB49CC4BBE5B.key | sudo gpg --dearmor | sudo tee /usr/share/keyrings/rabbitmq.E495BB49CC4BBE5B.gpg > /dev/null
## Community mirror of Cloudsmith: RabbitMQ repository
curl -1sLf https://ppa.novemberain.com/gpg.9F4587F226208342.key | sudo gpg --dearmor | sudo tee /usr/share/keyrings/rabbitmq.9F4587F226208342.gpg > /dev/null

## Add apt repositories maintained by Team RabbitMQ
sudo tee /etc/apt/sources.list.d/rabbitmq.list <<EOF
## Provides modern Erlang/OTP releases
##
deb [signed-by=/usr/share/keyrings/rabbitmq.E495BB49CC4BBE5B.gpg] https://ppa1.novemberain.com/rabbitmq/rabbitmq-erlang/deb/ubuntu jammy main
deb-src [signed-by=/usr/share/keyrings/rabbitmq.E495BB49CC4BBE5B.gpg] https://ppa1.novemberain.com/rabbitmq/rabbitmq-erlang/deb/ubuntu jammy main

# another mirror for redundancy
deb [signed-by=/usr/share/keyrings/rabbitmq.E495BB49CC4BBE5B.gpg] https://ppa2.novemberain.com/rabbitmq/rabbitmq-erlang/deb/ubuntu jammy main
deb-src [signed-by=/usr/share/keyrings/rabbitmq.E495BB49CC4BBE5B.gpg] https://ppa2.novemberain.com/rabbitmq/rabbitmq-erlang/deb/ubuntu jammy main

## Provides RabbitMQ
##
deb [signed-by=/usr/share/keyrings/rabbitmq.9F4587F226208342.gpg] https://ppa1.novemberain.com/rabbitmq/rabbitmq-server/deb/ubuntu jammy main
deb-src [signed-by=/usr/share/keyrings/rabbitmq.9F4587F226208342.gpg] https://ppa1.novemberain.com/rabbitmq/rabbitmq-server/deb/ubuntu jammy main

# another mirror for redundancy
deb [signed-by=/usr/share/keyrings/rabbitmq.9F4587F226208342.gpg] https://ppa2.novemberain.com/rabbitmq/rabbitmq-server/deb/ubuntu jammy main
deb-src [signed-by=/usr/share/keyrings/rabbitmq.9F4587F226208342.gpg] https://ppa2.novemberain.com/rabbitmq/rabbitmq-server/deb/ubuntu jammy main
EOF

## Update package indices
sudo apt-get update -y

## Install Erlang packages
sudo apt-get install -y erlang-base \
                        erlang-asn1 erlang-crypto erlang-eldap erlang-ftp erlang-inets \
                        erlang-mnesia erlang-os-mon erlang-parsetools erlang-public-key \
                        erlang-runtime-tools erlang-snmp erlang-ssl \
                        erlang-syntax-tools erlang-tftp erlang-tools erlang-xmerl

## Install rabbitmq-server and its dependencies
sudo apt-get install rabbitmq-server -y --fix-missing

List all sources of rabbitmq-server software
apt-cache madison rabbitmq-server

Check the rabbitmq version
root@server01:~/sh# rabbitmqctl status | grep “RabbitMQ version”
RabbitMQ version: 3.12.3

create user

# 1、创建用户并设置密码
[root@rabbit01 ~]# rabbitmqctl add_user admin 123456

#2、赋予其administrator角色
[root@rabbit01 ~]# rabbitmqctl set_user_tags admin administrator

#3、设置权限
[root@rabbit01 ~]# rabbitmqctl  set_permissions -p "/" admin '.*' '.*' '.*'


修改密码
root@server02:~# rabbitmqctl   change_password n80 12345

http://IP address:15672 access
insert image description here

root@server02:~# rabbitmqctl  add_vhost  n80 #是用来创建一个名为n80的虚拟主机
root@server02:~# rabbitmqctl  list_vhosts   #用来列出当前 RabbitMQ 服务器上的所有虚拟主机。
root@server02:~# rabbitmqctl list_queues   #列出当前 RabbitMQ 服务器上的所有队列及其相关信息。

Add users and configure permissions
insert image description here

rabbit cluster deployment

The rqbbit cluster is divided into two modes: normal mode and mirror mode

There are two types of nodes in the cluster:
disk node: saves data to memory and disk.
Memory node: saves data to
memory. Although memory node does not write to disk, it performs better than disk node. In the cluster, only one
disk It is enough for nodes to save data. If there are only memory nodes in the cluster, then they cannot all be stopped,
otherwise all data messages will be lost after all servers are stopped.

The three nodes use the official script to install rabbitmq, configure the host name and hosts resolution

vi /etc/hosts
192.168.74.70 node1.rabbit.org node1
192.168.74.71 node2.rabbit.org node2
192.168.74.72 node3.rabbit.org node3

Create a rabbitMQ cluster

Close RabbitMQ on each server:

The rabbitmq cluster relies on the erlang cluster to work, so the erlang cluster environment must be built first. Each node in the Erlang cluster is implemented through a magic cookie. This cookie is stored in /var/lib/rabbitmq/. In erlang.cookie, the file permission is 400, and the cookies of each node must be consistent. Otherwise, the nodes cannot communicate. Synchronize
.erlang.cookie on mq-server1 to the other two servers:
scp /var/lib/rabbitmq/.erlang.cookie 192.168.74.71: /var/lib/rabbitmq/
scp /var/lib/rabbitmq/.erlang.cookie 192.168.74.72:/var/lib/rabbitmq/

node1:
Add node1 to node3 as a memory node, and as a memory node, execute the following command on node1

root@node1:/etc/rabbitmq# rabbitmqctl stop_app  
Stopping rabbit application on node rabbit@node1 ...
root@node1:/etc/rabbitmq# rabbitmqctl reset 
Resetting node rabbit@node1 ...
root@node1:/etc/rabbitmq#  rabbitmqctl  join_cluster rabbit@node3 --ram
Clustering node rabbit@node1 with rabbit@node3
root@node1:/etc/rabbitmq# rabbitmqctl start_app
Starting node rabbit@node1 ...

node2:
Add node2 to node3 as a memory node, and as a memory node, execute the following command on node2:

root@node2:~# systemctl restart rabbitmq-server
root@node2:~# rabbitmqctl stop_app    ##停止 app 服务
Stopping rabbit application on node rabbit@node2 ...
root@node2:~# rabbitmqctl reset   ##清空元数据
Resetting node rabbit@node2 ...
root@node2:~# rabbitmqctl  join_cluster rabbit@node3 --ram
Clustering node rabbit@node2 with rabbit@node3
root@node2:~# rabbitmqctl start_app   ##启动 app 服务
Starting node rabbit@node2 ...


node3 view

root@node3:~# rabbitmqctl  cluster_status
............
Disk Nodes

rabbit@node3

RAM Nodes  #内存节点

rabbit@node1
rabbit@node2

Running Nodes   #运行的节点

rabbit@node1
rabbit@node2
rabbit@node3

Versions

rabbit@node1: RabbitMQ 3.11.15 on Erlang 25.3.1
rabbit@node2: RabbitMQ 3.11.15 on Erlang 25.3.1
rabbit@node3: RabbitMQ 3.11.15 on Erlang 25.3.1

CPU Cores

Node: rabbit@node1, available CPU cores: 2
Node: rabbit@node2, available CPU cores: 2
Node: rabbit@node3, available CPU cores: 2
............

Set the cluster to mirror mode

rabbitmqctl set_policy ha-all “^” ‘{“ha-mode”:“all”}’

Verify cluster status

Create an account on any node
rabbitmqctl add_user admin 123456
rabbitmqctl set_user_tags admin administrator
rabbitmqctl set_permissions -p "/" admin '. ' '. ' '.*'

Enable plugins on all nodes
sudo rabbitmq-plugins enable rabbitmq_management #Enable web plugins

All three nodes can log in
insert image description here

To remove a RabbitMQ cluster, follow these steps:

  1. Stop all RabbitMQ nodes: Run the following command on each node to stop the RabbitMQ service:

    rabbitmqctl stop_app
    rabbitmqctl reset
    
  2. Clear the cluster state between nodes: Run the following command on each node to clear the cluster state between nodes:

    rabbitmqctl force_reset
    
  3. Reset node name or clear configuration on each node: You can choose to restore the node to a standalone node, or clear the node's configuration information entirely. If you want to restore the node to a standalone node, delete all files in the RabbitMQ configuration directory (usually located at /etc/rabbitmq/or /var/lib/rabbitmq/). Then restart the RabbitMQ service.
    systemctl restart rabbitmq-server

Please note that removing the RabbitMQ cluster may result in data loss. Please make sure you have backed up important data before performing the operation.

cluster monitoring

RabbitMQ API:
curl -s -u guest:guest http://localhost:15672/api/nodes

Cluster status monitoring

root@node1:~/py# cat rabbitmq.py 
#!/bin/env python
#coding:utf-8
#Author: ZhangJie
import subprocess
running_list = []
error_list = []
false = "false"
true = "true" 
def get_status():
    obj = subprocess.Popen(("curl -s -u guest:guest http://localhost:15672/api/nodes &> /dev/null"), shell=True, stdout=subprocess.PIPE)
    data = obj.stdout.read()
    data1 = eval(data)
    for i in data1:
        if i.get("running") == "true":
            running_list.append(i.get("name"))
        else:
            error_list.append(i.get("name"))
def count_server():
    if len(running_list) < 3: # 可以判断错误列表大于 0 或者运行列表小于 3,3未总计的节点数量
        print(101) # 100 就是集群内有节点运行不正常了
    else:
        print(50) # 50 为所有节点全部运行正常
def main():
    get_status()
    count_server()
if __name__ == "__main__":
    main()

Memory usage monitoring

root@node1:~/py# python3 rabbitmq_memory.py rabbit@node2
139382784
root@node1:~/py# cat rabbitmq_memory.py 
#!/bin/env python
#coding:utf-8
#Author: ZhangJie
import subprocess
import sys
running_list = []
error_list = []
false = "false"
true = "true" 
def get_status():
    obj = subprocess.Popen(("curl -s -u guest:guest http://localhost:15672/api/nodes &> /dev/null"), shell=True, stdout=subprocess.PIPE)
    data = obj.stdout.read()
    data1 = eval(data)
    #print(data1)
    for i in data1:
        if i.get("name") == sys.argv[1]:
            print(i.get("mem_used"))
def main():
    get_status()
if __name__ == "__main__":
    main()

root@node1:~/py# python3  rabbitmq_memory.py rabbit-test
root@node1:~/py# python3 rabbitmq_memory.py rabbit@node1
138203136

View all connections list
curl -s -u admin:123456 http://localhost:15672/api/connections

View the queue
curl -s -u admin:123456 http://localhost:15672/api/queues/

Guess you like

Origin blog.csdn.net/m0_37749659/article/details/132484156