centos9 stream 下 rabbitmq高可用集群搭建及使用_centos stream 9

#安装时会默认安装erlang相关依赖包。
#加入系统自动启动并立即运行rabbit
systemctl enable rabbitmq-server.service --now


## 2. 配置hosts文件


在所有节点上编辑hosts文件,将各节点的IP地址和主机名映射到一起。这样可以通过主机名进行通信,而不是依赖IP地址。


这个就不在讲了,直接编辑 /etc/hosts这个文件即可


个人主机内容参考如下:



127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

110.110.10.5 host1
110.110.10.6 host2
110.110.10.7 host3


## 3. 配置rabbit集群


一般情况下一个节点的rabbit也差不多够用了,但为了稳定性使用高可用集群还是有必要的,配置集群可以直接编辑每个节点上的RabbitMQ配置文件,可以在/etc/rabbitmq/rabbitmq.conf中修改。常见的配置项:  
 # 节点名称  
 node\_name = rabbit@node1


# 集群节点列表  
 cluster\_nodes = {['rabbit@node1', 'rabbit@node2', 'rabbit@node3'], disc}


# Cookie值,用于节点间通信  
 erlang\_cookie = abcde12345fghij


还有自定义rabbit的ip,各类服务端口,还有ssl等高级配置在这里不说了。


注意,这些配置项必须在所有节点上保持一致。


### 命令方式配置集群


**默认情况下rabbit会直接获取hostname作为节点名,所以不用去配置文件中修改,这里主要讲使用命令直接配置和启动rabbit集群。**



先配置管理节点,再将管理节点cookie复制到其他节点

for i in {host1,host2,host3};do ssh $i systemctl stop rabbitmq-server;done
for i in {host1,host2,host3};do scp /var/lib/rabbitmq/.erlang.cookie $i:/var/lib/rabbitmq/.erlang.cookie;done
for i in {host1,host2,host3};do ssh $i chown rabbitmq:rabbitmq /var/lib/rabbitmq/.erlang.cookie;done
for i in {host1,host2,host3};do ssh $i chmod 400 /var/lib/rabbitmq/.erlang.cookie;done
for i in {host1,host2,host3};do ssh $i systemctl start rabbitmq-server;done

###节点加入集群,从拟定的主节点以外的其他节点操作,新加入节点操作也一样。;
rabbitmqctl stop_app
rabbitmqctl join_cluster --ram rabbit@host1
rabbitmqctl start_app

查看rabbitmq集群服务状态,每加入一个节点都可以查看一下是否加入成功

rabbitmqctl cluster_status

单个新节点加入集群操作,在新节点上操作,与上面多节点操作基本一致。

systemctl stop rabbitmq-server
scp host1:/var/lib/rabbitmq/.erlang.cookie /var/lib/rabbitmq/.erlang.cookie
chown rabbitmq:rabbitmq /var/lib/rabbitmq/.erlang.cookie
chmod 400 /var/lib/rabbitmq/.erlang.cookie
systemctl start rabbitmq-server
rabbitmqctl stop_app
rabbitmqctl join_cluster --ram rabbit@host1
rabbitmqctl start_app


### 读写测试



写入和读取测试:

python

import pika

连接到 rabbitmq 服务器

connection = pika.BlockingConnection(pika.ConnectionParameters(‘localhost’))
channel = connection.channel()

定义队列名称

queue_name = ‘test_queue’

向队列写入消息

channel.queue_declare(queue=queue_name)
channel.basic_publish(exchange=‘’,
routing_key=queue_name,
body=‘Hello World!’)
print(" [x] Sent ‘Hello World!’")

从队列中读取消息

method_frame, header_frame, body = channel.basic_get(queue=queue_name, auto_ack=True)
if method_frame:
print(" [x] Received %r" % body)
else:
print(‘No message returned’)

关闭连接

connection.close()

在上述代码中,我们首先连接到了本地的 rabbitmq 服务器。然后定义了一个名为 test_queue 的队列,并向队列中写入了一条消息:Hello World!。接着,我们又从队列中读取了一条消息,并将其打印出来。


### 在Node.js中使用RabbitMQ



在Node.js中使用RabbitMQ需要先安装amqplib库,可以通过npm进行安装:
npm install amqplib

##以下是使用RabbitMQ的基本步骤:
##建立与RabbitMQ服务器的连接
const amqp = require(‘amqplib’);
amqp.connect(‘amqp://localhost’).then(function(conn) {
//执行后续操作
});

###创建通道(channel)
conn.createChannel().then(function(ch) {
//执行后续操作
});

#发送消息
const queueName = “hello”;
ch.assertQueue(queueName, { durable: false });
ch.sendToQueue(queueName, new Buffer(‘Hello World!’));

接收消息

const queueName = “hello”;
ch.assertQueue(queueName, { durable: false });
ch.consume(queueName, function(msg) {
console.log(“Received message: %s”, msg.content.toString());
}, { noAck: true });

#######完整示例代码:

const amqp = require(‘amqplib’);

amqp.connect(‘amqp://localhost’).then(function(conn) {
conn.createChannel().then(function(ch) {
const queueName = “hello”;
ch.assertQueue(queueName, { durable: false });
ch.sendToQueue(queueName, new Buffer(‘Hello World!’));

ch.assertQueue(queueName, { durable: false });
ch.consume(queueName, function(msg) {
  console.log("Received message: %s", msg.content.toString());
}, { noAck: true });

});
}).catch(function(err) {
console.log(‘Error:’, err);
});


## 


## 4. 常用管理命令



#添加新用户
sudo rabbitmqctl add_user username password

#删除用户
sudo rabbitmqctl delete_user username

#分配用户权限
sudo rabbitmqctl set_permissions -p / virtual-hostname ‘username’ ‘.’ '.’ ‘.*’

#查看用户列表
sudo rabbitmqctl list_users

#查看队列列表
sudo rabbitmqctl list_queues

#查看交换机列表
sudo rabbitmqctl list_exchanges

#查看绑定列表
sudo rabbitmqctl list_bindings

#查看 vhost 列表
sudo rabbitmqctl list_vhosts

#查看某个 vhost 的权限控制列表
sudo rabbitmqctl list_permissions -p virtual-hostname

#查看 RabbitMQ 服务器信息
sudo rabbitmqctl status


## 5. 设置集群policy设置



##语句格式
rabbitmqctl set_policy [-p ]
rabbitmqctl clear_policy [-p ]
rabbitmqctl list_policies [-p ]

[ host1 ] 设置 ha-mode 高可用模式

rabbitmqctl set_policy ha-all ‘^(?!amq.).*’ ‘{“ha-mode”: “all”}’;

置一个队列的最大长度为1000条消息:

rabbitmqctl set_policy max-length-1000 “^my-queue$” ‘{“max-length”:1000}’ --apply-to queues


## 6. 启用web面板插件



启用web插件

rabbitmq-plugins enable rabbitmq_management

##在本服务器或者同网段其他主机打开浏览器即可访问rabbitmq集群状态和管理页面


![](https://img-blog.csdnimg.cn/e393bc34c237446ebc6c6687347a4d9e.png)


使用前面命令添加用户并设置为管理员即可登陆web界面。 


![](https://img-blog.csdnimg.cn/73962817eaad4832aac7c4aa9d3c706c.png)


## 7. rabbitmq配置文件参考



###一般情况下不用在这里修改配置文件,但自定义参数的时候还是很有用的。
cat /etc/rabbitmq/rabbitmq.conf

This example configuration file demonstrates various settings

available via rabbitmq.conf. It primarily focuses core broker settings

but some tier 1 plugin settings are also covered.

This file is AN EXAMPLE. It is NOT MEANT TO BE USED IN PRODUCTION. Instead of

copying the entire (large!) file, create or generate a new rabbitmq.conf for the target system

and populate it with the necessary settings.

See https://rabbitmq.com/configure.html to learn about how to configure RabbitMQ,

the ini-style format used by rabbitmq.conf, how it is different from advanced.config,

how to verify effective configuration, and so on.

See https://rabbitmq.com/documentation.html for the rest of RabbitMQ documentation.

In case you have questions, please use RabbitMQ community Slack and the rabbitmq-users Google group

instead of GitHub issues.

======================================

Core broker section

======================================

####这下面的5672,5671如果已经被占用时或者为了安全考虑可修改为其他端口,在服务应用时也需要调整成新的端口

Networking

====================

Related doc guide: https://rabbitmq.com/networking.html.

By default, RabbitMQ will listen on all interfaces, using

the standard (reserved) AMQP 0-9-1 and 1.0 port.

listeners.tcp.default = 5672

To listen on a specific interface, provide an IP address with port.

For example, to listen only on localhost for both IPv4 and IPv6:

IPv4

listeners.tcp.local = 127.0.0.1:5672

IPv6

listeners.tcp.local_v6 = ::1:5672

You can define multiple listeners using listener names

listeners.tcp.other_port = 5673

listeners.tcp.other_ip = 10.10.10.10:5672

TLS listeners are configured in the same fashion as TCP listeners,

including the option to control the choice

  • 7
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值