Tencent cloud server installation and configuration rabbitmq

1. Installation

The recent study of message queues has made me interested in message middleware, and the use of rabbitmq as a message queue is my first step in learning middleware.

Installation of rabbitmq and erlang

Check the environment before installing:

[root@VM-12-4-centos ~]# cat /proc/version
Linux version 3.10.0-1160.66.1.el7.x86_64 ([email protected]) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) ) #1 SMP Wed May 18 16:02:34 UTC 2022

Rabbitmq is a message middleware written based on Erlang language, so before installing rabbitmq, you need to install erlang as a support.
We can prepare the packages that need to be installed first (it should be noted that I installed the rpm package of the corresponding version of centos7)
erlang language The rpm package download entry
insert image description here
found the x86_64 corresponding to el7 as above, because my cloud server is the x86 architecture of centos7

The rpm package download entry of rabbitmq
insert image description here
Download the compressed package as above, upload it to the cloud server, and then install it

[root@VM-12-4-centos ~]# ls
erlang-23.3.4.8-1.el7.x86_64.rpm  rabbitmq-server-3.10.7-1.suse.noarch.rpm
#安装erlang的依赖
[root@VM-12-4-centos ~]# yum install socat
......
Transaction test succeeded
Running transaction
  Installing : socat-1.7.3.2-2.el7.x86_64                                               1/1
  Verifying  : socat-1.7.3.2-2.el7.x86_64                                               1/1

Installed:
  socat.x86_64 0:1.7.3.2-2.el7

Complete!
#rpm安装前面下载的两个包
[root@VM-12-4-centos ~]# rpm -ivh erlang-23.3.4.8-1.el7.x86_64.rpm
......
[root@VM-12-4-centos ~]# rpm -ivh rabbitmq-server-3.10.7-1.suse.noarch.rpm
......

This completes the installation.

Of course, in addition to the above methods, you can also operate directly on the cloud server

[root@VM-12-4-centos ~]# wget https://github.com/rabbitmq/erlang-rpm/releases/download/v23.3.4.8/erlang-23.3.4.8-1.el7.x86_64.rpm
......
[root@VM-12-4-centos ~]# wget https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.10.7/rabbitmq-server-3.10.7-1.suse.noarch.rpm
......
#上面两个包的下载url是我从对应地址复制,然后直接用来wget的,不代表url一直不变动,所以还是要自己去复制
[root@VM-12-4-centos ~]# ls
erlang-23.3.4.8-1.el7.x86_64.rpm  rabbitmq-server-3.10.7-1.suse.noarch.rpm
[root@VM-12-4-centos ~]# yum install socat
......
Transaction test succeeded
Running transaction
  Installing : socat-1.7.3.2-2.el7.x86_64                                               1/1
  Verifying  : socat-1.7.3.2-2.el7.x86_64                                               1/1

Installed:
  socat.x86_64 0:1.7.3.2-2.el7

Complete!
#rpm安装前面下载的两个包
[root@VM-12-4-centos ~]# rpm -ivh erlang-23.3.4.8-1.el7.x86_64.rpm
......
[root@VM-12-4-centos ~]# rpm -ivh rabbitmq-server-3.10.7-1.suse.noarch.rpm
......

2. Start the service

Start the service and set it to start automatically

[root@VM-12-4-centos ~]# systemctl start rabbitmq-server
[root@VM-12-4-centos ~]# systemctl enable rabbitmq-server
Created symlink from /etc/systemd/system/multi-user.target.wants/rabbitmq-server.service to /usr/lib/systemd/system/rabbitmq-server.service.
[root@VM-12-4-centos ~]# systemctl status rabbitmq-server
● rabbitmq-server.service - RabbitMQ broker
   Loaded: loaded (/usr/lib/systemd/system/rabbitmq-server.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2022-08-16 10:58:38 CST; 1min 57s ago
 Main PID: 4261 (beam.smp)
   CGroup: /system.slice/rabbitmq-server.service
           ├─4261 /usr/lib64/erlang/erts-11.2.2.7/bin/beam.smp -W w -MBas ageffcbf -MHas ageffcbf -MBlmbcs 512 -MHlmbcs 512 -MMmcs 30 -P 1048576 -t 5000000 -stbt db -zdbbl 128000 -sbwt none -sbwtdcpu non...
           ├─4276 erl_child_setup 32768
           ├─4301 /usr/lib64/erlang/erts-11.2.2.7/bin/epmd -daemon
           ├─4324 inet_gethost 4
           └─4325 inet_gethost 4
......
[root@VM-12-4-centos ~]# rabbitmqctl status
Status of node rabbit@VM-12-4-centos ...
Runtime
......
Plugins

Enabled plugin file: /etc/rabbitmq/enabled_plugins
Enabled plugins:


Data directory

Node data directory: /var/lib/rabbitmq/mnesia/rabbit@VM-12-4-centos
Raft data directory: /var/lib/rabbitmq/mnesia/rabbit@VM-12-4-centos/quorum/rabbit@VM-12-4-centos

Config files


Log file(s)

 * /var/log/rabbitmq/[email protected]
 * /var/log/rabbitmq/rabbit@VM-12-4-centos_upgrade.log
 * <stdout>
......
Listeners

Interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Interface: [::], port: 5

The status information above is very important. Pay attention to the config files field and the log files field. Regarding custom configuration and operation logs, these two field information are the focus objects, and then open the foreground access and add access users

#添加admin用户配置密码为admin
[root@VM-12-14-centos ~]# rabbitmqctl add_user admin admin
Adding user "admin" ...
Done. Don't forget to grant the user permissions to some virtual hosts! See 'rabbitmqctl help set_permissions' to learn more.
# 设置admin用户为管理员
[root@VM-12-14-centos ~]# rabbitmqctl set_user_tags admin administrator
Setting tags for user "admin" to [administrator] ...
# 启用网页后台管理插件
[root@VM-12-4-centos ~]# rabbitmq-plugins enable rabbitmq_management
Enabling plugins on node rabbit@VM-12-4-centos:
rabbitmq_management
The following plugins have been configured:
  rabbitmq_management
  rabbitmq_management_agent
  rabbitmq_web_dispatch
Applying plugin configuration to rabbit@VM-12-4-centos...
The following plugins have been enabled:
  rabbitmq_management
  rabbitmq_management_agent
  rabbitmq_web_dispatch

started 3 plugins.
[root@VM-12-4-centos ~]# rabbitmqctl status
Status of node rabbit@VM-12-4-centos ...
Runtime
......
Listeners

Interface: [::], port: 25672, protocol: clustering, purpose: inter-node and CLI tool communication
Interface: [::], port: 5672, protocol: amqp, purpose: AMQP 0-9-1 and AMQP 1.0
Interface: [::], port: 15672, protocol: http, purpose: HTTP API

After the above is done, the web page access function is actually enabled, but for the server, it is actually necessary to enable the firewall to intercept. From our inspection above, we can see that the port providing the service is port 5672, which is provided by rabbitmq to The access port of the client, and port 15672, which is the ui port of the background management interface. The url to access the ui is usually: http://[ip]:15672/

The first is the firewall settings of the server itself:

# 检查firewalld运行状态和开放端口
[root@VM-12-14-centos ~]# firewall-cmd --state
running
[root@VM-12-14-centos ~]# firewall-cmd --permanent --zone=public --list-ports
20/tcp 21/tcp 22/tcp 80/tcp 8888/tcp 39000-40000/tcp 8099/tcp 443/tcp 15672/tcp 5672/tcp

From the above, you can see that the important ports 15672 and 5672 have been released. If not, open them manually.

[root@VM-12-14-centos ~]# firewall-cmd --permanent --zone=public --add-port=15672/tcp
success
[root@VM-12-14-centos ~]# firewall-cmd --permanent --zone=public --add-port=5672/tcp
success

After firewalld has opened the access to the corresponding port, let's try to visit the front desk
insert image description here

You can see that the access failed, so let's check the connectivity of the port

D:\Users\User\Desktop>ping x.x.x.x -t

正在 Ping x.x.x.x 具有 32 字节的数据:
来自 x.x.x.x 的回复: 字节=32 时间=13ms TTL=248
来自 x.x.x.x 的回复: 字节=32 时间=13ms TTL=248
来自 x.x.x.x 的回复: 字节=32 时间=13ms TTL=248

x.x.x.x 的 Ping 统计信息:
    数据包: 已发送 = 3,已接收 = 3,丢失 = 0 (0% 丢失)
往返行程的估计时间(以毫秒为单位):
    最短 = 13ms,最长 = 13ms,平均 = 13ms
Control-C
^C
D:\Users\User\Desktop>telnet x.x.x.x 15672
正在连接x.x.x.x...无法打开到主机的连接。 在端口 15672: 连接失败

D:\Users\User\Desktop>

It can be seen that our ip access is of course no problem, but the connection to the corresponding port fails, so the configuration is still missing, and we use Tencent Cloud Server, so we need to open the corresponding port access on the console I won’t go into details about the official documentation of Tencent Cloud server’s open port access. Just follow the documentation and it’s ok. Insert a sentence: If you are sure that the service is enabled correctly, but the external PC cannot access it, you often need to check the connectivity of the peer IP first
.
Then there is the connectivity detection of the open port of the corresponding service. After the connectivity is no problem, consider whether the external PC is compatible with accessing the service.

Back to the topic, when we fix the firewall problem, check the connectivity, and then we can re-visit the front desk service if it is feasible
insert image description here
, enter the user and password added earlier to log in successfully
insert image description here

Three, python operation rabbitmq

Regarding the remote service of rabbitmq, we have enabled and confirmed that it is accessible. Now we use python to operate it. As a python with rich third-party libraries and easy-to-use features, operating mq can be said to be particularly suitable.

First, install and operate the third-party library of rabbitmq. Pay attention to use python3 to correspond to pip, because python2 is not maintained at present, and the installation and use of pika as a third-party library is also prone to problems.

# 因为我是在windows下进行的python练习,它的python3对应的各种命令都是pip和python即可
D:\Users\User\Desktop>pip install pika
。。。。。。
D:\Users\User\Desktop>pip show pika
Name: pika
Version: 1.3.0
Summary: Pika Python AMQP Client Library
Home-page: None
Author: None
Author-email: None
License: BSD-3-Clause
Location: d:\software\python38\lib\site-packages
Requires:
Required-by:
D:\Users\User\Desktop>python
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pika
>>> cred = pika.PlainCredentials('admin', 'admin')
>>> connection = pika.BlockingConnection(pika.ConnectionParameters(host='x.x.x.x',credentials=cred))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\software\python38\lib\site-packages\pika\adapters\blocking_connection.py", line 360, in __init__
    self._impl = self._create_connection(parameters, _impl_class)
  File "D:\software\python38\lib\site-packages\pika\adapters\blocking_connection.py", line 451, in _create_connection
    raise self._reap_last_connection_workflow_error(error)
pika.exceptions.ProbableAccessDeniedError: ConnectionClosedByBroker: (530) "NOT_ALLOWED - access to vhost '/' refused for user 'admin'"

The most important thing to use python is to learn to read the stack information of the error report. For example, it is very clear above that the admin user does not have access to the "/" vhost, so you have to add permissions

# centos服务器
[root@VM-12-4-centos ~]# rabbitmqctl set_permissions -p "/" admin ".*" ".*" ".*"
Setting permissions for user "admin" in vhost "/" ...
# 本地PC,承接上面python使用
>>> connection = pika.BlockingConnection(pika.ConnectionParameters(host='114.132.160.125',credentials=cred))
>>>

Rabbitmq instance of producer and consumer

Producers and consumers are typical of typical design patterns. For messaging systems, a simple example of implementing this design pattern is perfect as a test.

Open two new terminals on windows, one as a producer and one as a consumer

D:\Users\User\Desktop>python
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>>
>>>
>>> print("生产者")
生产者
>>> import pika
>>> cred = pika.PlainCredentials("admin", "admin")
#进行连接
>>> connection = pika.BlockingConnection(pika.ConnectionParameters("x.x.x.x", credentials=cred, heartbear=0))
>>> channel = connection.channel()
# 创建队列
>>> channel.queue_declare(queue='hello')
<METHOD(['channel_number=1', 'frame_type=1', "method=<Queue.DeclareOk(['consumer_count=0', 'message_count=0', 'queue=hello'])>"])>
# 往队列里面生产一条'test from queue of the rabbitmq'的信息
>>> channel.basic_publish(exchange='', routing_key='hello', body='test from queue of the rabbitmq')
>>>

After the "product" is produced above, check the information queue in the background

[root@VM-12-14-centos ~]# rabbitmqctl list_queues
Timeout: 60.0 seconds ...
Listing queues for vhost / ...
name    messages
hello   1
[root@VM-12-14-centos ~]#

In addition, you can see the queue information more intuitively at the foreground, which will not be shown here.

>>>
>>> print("消费者")
消费者
>>>
>>> def callf(ch, method, properties, mes):
...     print("Got {}".format(mes))
...
>>> print('Waiting for messages. To exit press CTRL+C')
Waiting for messages. To exit press CTRL+C
# 连接上hello通道,并连接处理函数
>>> channel.basic_consume('hello', callf, True)
'ctag1.e2826632074d436284b2b52096ab0dd2'
# 开始消费
>>> channel.start_consuming()
Got b'test from queue of the rabbitmq'

The above production and consumption are in one-to-one correspondence. The producer calls the basic_publish method to store a message in the channel, and the consumer continues to monitor the channel. When a message comes, the processing function callf is called once. This is the classic producer consumer, which is also The simplest application, but the actual application environment is not so straightforward and simple.

Come to the background to view the channel, we can view the following information

[root@VM-12-14-centos ~]# rabbitmqctl list_queues
Timeout: 60.0 seconds ...
Listing queues for vhost / ...
name    messages
hello   0
[root@VM-12-14-centos ~]#

4. Docker configures rabbitmq and makes it accessible to the outside world

Before that, first you have to install docker and open the corresponding access ports 15672 and 5672

[root@VM-12-14-centos ~]# docker run -d --hostname localhost --name rabbit-management --restart=always -p 15672:15672 -p 5672:5672 rabbitmq:3.6-management-alpine
......
[root@VM-12-14-centos ~]# docker ps
CONTAINER ID        IMAGE                            COMMAND                  CREATED             STATUS              PORTS                                                                                        NAMES
784571a3bbef        rabbitmq:3.6-management-alpine   "docker-entrypoint..."   5 minutes ago       Up 5 minutes        4369/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp, 15671/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp   rabbit-management

As above, well, docker is a bit fragrant, hahaha

The above is my configuration and remote call record of rabbitmq on Tencent Cloud Server. I'm Jack, over.

Guess you like

Origin blog.csdn.net/weixin_44948269/article/details/126360144