RabbitMQ 从命令行创建队列和绑定

发布于 2024-10-09 14:04:43 字数 86 浏览 7 评论 0原文

如果我的机器上安装了 RabbitMQ,有没有办法从命令行创建消息队列并将其绑定到某个交换而不使用客户端?

我认为这是不可能的,但我想确定一下。

If I have RabbitMQ installed on my machine, is there a way to create a message queue from the command line and bind it to a certain exchange without using a client?

I think it is not possible, but I want to be sure.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

薄暮涼年 2024-10-16 14:04:43

摘要:

其他答案是所要求答案的很好替代方案。以下是您可以从命令行使用的命令。

首先,做好所有必要的准备工作,例如安装rabbit、rabbitmqadmin 和rabbitctl。这个想法是使用来自rabbitmqctlrabbitmqadmin的命令。您可以看到一些命令示例: https://www.rabbitmq.com/management-cli.html< /a>

示例命令/设置:

以下命令应该为您提供大部分(如果不是全部)所需的内容:

# Get the cli and make it available to use.
wget http://127.0.0.1:15672/cli/rabbitmqadmin
chmod +x rabbitmqadmin
mv rabbitmqadmin /etc/rabbitmq

添加用户和权限

rabbitmqctl add_user testuser testpassword
rabbitmqctl set_user_tags testuser administrator
rabbitmqctl set_permissions -p / testuser ".*" ".*" ".*"

创建虚拟主机并设置权限

rabbitmqctl add_vhost Some_Virtual_Host
rabbitmqctl set_permissions -p Some_Virtual_Host guest ".*" ".*" ".*"

进行交换

./rabbitmqadmin declare exchange --vhost=Some_Virtual_Host name=some_exchange type=direct

创建队列

./rabbitmqadmin declare queue --vhost=Some_Virtual_Host name=some_outgoing_queue durable=true

进行绑定

./rabbitmqadmin --vhost="Some_Virtual_Host" declare binding source="some_exchange" destination_type="queue" destination="some_incoming_queue" routing_key="some_routing_key"

使用 Python 进行绑定的替代方法

以下是命令行绑定的替代方案,因为我有时会遇到问题,并发现以下 python 代码更可靠。

#!/usr/bin/env python
import pika

rabbitmq_host = "127.0.0.1"
rabbitmq_port = 5672
rabbitmq_virtual_host = "Some_Virtual_Host"
rabbitmq_send_exchange = "some_exchange" 
rabbitmq_rcv_exchange = "some_exchange"
rabbitmq_rcv_queue = "some_incoming_queue"
rabbitmq_rcv_key = "some_routing_key"

outgoingRoutingKeys = ["outgoing_routing_key"]
outgoingQueues = ["some_outgoing_queue "]

# The binding area
credentials = pika.PlainCredentials(rabbitmq_user, rabbitmq_password)
connection = pika.BlockingConnection(pika.ConnectionParameters(rabbitmq_host, rabbitmq_port, rabbitmq_virtual_host, credentials))
channel = connection.channel()
channel.queue_bind(exchange=rabbitmq_rcv_exchange, queue=rabbitmq_rcv_queue, routing_key=rabbitmq_rcv_key)

for index in range(len(outgoingRoutingKeys)):
    channel.queue_bind(exchange=rabbitmq_send_exchange, queue=outgoingQueues[index], routing_key=outgoingRoutingKeys[index])

上面的代码可以作为使用 python 的脚本的一部分运行。请注意,我将传出的内容放入数组中,这将允许您迭代它们。这应该会使部署变得容易。

最后的想法

我认为上述内容应该让您朝着正确的方向前进,如果任何特定命令没有意义,请使用谷歌或使用rabbitmqadmin help subcommands阅读更多内容。我尝试使用能够解释自身的变量。

Summary:

Other answers are good alternatives to what was asked for. Below are commands you can use from the command line.

First, do all the necessary prep work, e.g. install rabbit, rabbitmqadmin, and rabbitctl. The idea is to use commands from rabbitmqctl and rabbitmqadmin. You can see some command examples: https://www.rabbitmq.com/management-cli.html

Example Commands/Setup:

The following commands should give you the majority if not all of what you need:

# Get the cli and make it available to use.
wget http://127.0.0.1:15672/cli/rabbitmqadmin
chmod +x rabbitmqadmin
mv rabbitmqadmin /etc/rabbitmq

Add a user and permissions

rabbitmqctl add_user testuser testpassword
rabbitmqctl set_user_tags testuser administrator
rabbitmqctl set_permissions -p / testuser ".*" ".*" ".*"

Make a virtual host and Set Permissions

rabbitmqctl add_vhost Some_Virtual_Host
rabbitmqctl set_permissions -p Some_Virtual_Host guest ".*" ".*" ".*"

Make an Exchange

./rabbitmqadmin declare exchange --vhost=Some_Virtual_Host name=some_exchange type=direct

Make a Queue

./rabbitmqadmin declare queue --vhost=Some_Virtual_Host name=some_outgoing_queue durable=true

Make a Binding

./rabbitmqadmin --vhost="Some_Virtual_Host" declare binding source="some_exchange" destination_type="queue" destination="some_incoming_queue" routing_key="some_routing_key"

Alternative Way to Bind with Python

The following is an alternative to command line binding, as I've had issues with it sometimes and found the following python code to be more reliable.

#!/usr/bin/env python
import pika

rabbitmq_host = "127.0.0.1"
rabbitmq_port = 5672
rabbitmq_virtual_host = "Some_Virtual_Host"
rabbitmq_send_exchange = "some_exchange" 
rabbitmq_rcv_exchange = "some_exchange"
rabbitmq_rcv_queue = "some_incoming_queue"
rabbitmq_rcv_key = "some_routing_key"

outgoingRoutingKeys = ["outgoing_routing_key"]
outgoingQueues = ["some_outgoing_queue "]

# The binding area
credentials = pika.PlainCredentials(rabbitmq_user, rabbitmq_password)
connection = pika.BlockingConnection(pika.ConnectionParameters(rabbitmq_host, rabbitmq_port, rabbitmq_virtual_host, credentials))
channel = connection.channel()
channel.queue_bind(exchange=rabbitmq_rcv_exchange, queue=rabbitmq_rcv_queue, routing_key=rabbitmq_rcv_key)

for index in range(len(outgoingRoutingKeys)):
    channel.queue_bind(exchange=rabbitmq_send_exchange, queue=outgoingQueues[index], routing_key=outgoingRoutingKeys[index])

The above can be run as part of a script using python. Notice I put the outgoing stuff into arrays, which will allow you to iterate through them. This should make things easy for deploys.

Last Thoughts

I think the above should get you moving in the right direction, use google if any specific commands don't make sense or read more with rabbitmqadmin help subcommands. I tried to use variables that explain themselves.

想你的星星会说话 2024-10-16 14:04:43

安装RabbitMQ 管理插件。它附带了一个命令行工具,您可以使用它来配置所有队列/交换器等。

Install the RabbitMQ management plugin. It comes with a command line tool which you can use to configure all of your queues/exchanges/etc.

鯉魚旗 2024-10-16 14:04:43

创建Exchange:

rabbitmqadmin -u {user} -p {password} -V {vhost} declare exchange name={name} type={type}

创建队列:

rabbitmqadmin -u {user} -p {password} -V {vhost} declare queue name={name}

将队列绑定到Exchange:

rabbitmqadmin -u {user} -p {password} -V {vhost} declare binding source={Exchange} destination={queue}

Create Exchange:

rabbitmqadmin -u {user} -p {password} -V {vhost} declare exchange name={name} type={type}

Create Queue:

rabbitmqadmin -u {user} -p {password} -V {vhost} declare queue name={name}

Bind Queue to Exchange:

rabbitmqadmin -u {user} -p {password} -V {vhost} declare binding source={Exchange} destination={queue}
恰似旧人归 2024-10-16 14:04:43

也许有点晚了,但我已经使用 CURL 做到了。

对于队列:

curl -i -u RABBITUSER:RABBITPASSWORD -H "content-type:application/json" \
-XPUT -d'{"durable":true}' \
http://192.168.99.100:15672/api/queues/%2f/QUEUENAME

对于绑定

curl -i -u RABBITUSER:RABBITPASSWORD -H "content-type:application/json" \
-XPOST -d"{\"routing_key\":\"QUEUENAME\"}" \
http://192.168.99.100:15672/api/bindings/%2f/e/EXCHANGENAME/q/QUEUENAME

注意 192.168.99.100:15672 指向我的 RMQ 管理

Maybe a little late to the party but I've done so using CURL.

For queues:

curl -i -u RABBITUSER:RABBITPASSWORD -H "content-type:application/json" \
-XPUT -d'{"durable":true}' \
http://192.168.99.100:15672/api/queues/%2f/QUEUENAME

And for bindings

curl -i -u RABBITUSER:RABBITPASSWORD -H "content-type:application/json" \
-XPOST -d"{\"routing_key\":\"QUEUENAME\"}" \
http://192.168.99.100:15672/api/bindings/%2f/e/EXCHANGENAME/q/QUEUENAME

Note 192.168.99.100:15672 points to my RMQ Management

尐偏执 2024-10-16 14:04:43

如果您使用的是 Linux Debian,则有一个名为 amqp-tools 的软件包。安装它

apt-get install amqp-tools

然后您可以使用命令行(例如 amqp-publish)将消息发送到您的队列

amqp-publish -e exchange_name -b "your message"

从队列中收集消息

amqp-get -q queue_name

然后您可以使用或

amqp-consume -q queue_name

还有来自 rabbitmq-c 包/库。构建后,您可以通过命令行发送消息,例如

amqp_sendstring localhost 5672 amq.direct test "hello world"

Have fun ...

If you are using Linux Debian, there's a package called amqp-tools. Install it with

apt-get install amqp-tools

You can then use command line such as amqp-publish to send messages to your queue

amqp-publish -e exchange_name -b "your message"

Then you can collect message(s) from the queue using

amqp-get -q queue_name

or

amqp-consume -q queue_name

There are also (command line) examples from rabbitmq-c package / library. After you build it, you can send messages through command line such as

amqp_sendstring localhost 5672 amq.direct test "hello world"

Have fun ...

私野 2024-10-16 14:04:43

提供的命令行界面rabbitmqctl 不公开创建队列并绑定它的功能。

然而,使用快速脚本来完成此操作非常简单,RabbitMQ 入门指南在发布者和消费者方面都显示了几个示例。

#do some work to connect
#do some work to open a channel
channel.queue_declare(queue='helloworld')

我掩盖了连接,但它实际上是创建队列的一个衬垫。该操作也是幂等的,这意味着您可以将该语句包含在脚本中并且安全,因为知道它不会不断重新创建队列或清除现有的同名队列。

rabbitmqctl, the provided command line interface, doesn't expose the ability to create a queue and bind it.

It, however, is quite trivial to do it with a quick script though, and the RabbitMQ getting started guide shows several examples of it, both on the publisher as well as the consumer side.

#do some work to connect
#do some work to open a channel
channel.queue_declare(queue='helloworld')

I'm glossing over connecting, but it's a literal one liner to create a queue. The operation is also idempotent, meaning you can include the statement in a script and be safe, knowing that it won't keep recreating the queue or blowing out an existing one of the same name.

無處可尋 2024-10-16 14:04:43

从 Windows 上的 CLI 动态创建 RabbitMq 交换、队列和绑定

我已经安装了 RabbitMQ 服务器并运行多个队列和交换,现在想从命令行动态创建它。我知道这是一个老问题,但我认为提供这些信息会有所帮助。

以下是我所做的:

安装

  1. 下载并安装了Python 2.6.6-201008-24 Windows x86-64 MSI安装程序,任何版本的python,
  2. 下载RabbitMqAdmin:RabbitMq Web用户界面有一个链接命令行,可以导航到http://server -name:15672/cli/ (server-name: 安装rabbitmq的服务器)或者,使用上面的url并在python中将文件保存为rabbitmqadmin.exe exe位置

例如:C:\Python26
C:\Python26\python
C:\Python26\rabbitmqadmin.exe

代码:在批处理文件中使用以下命令

  1. 创建交换:
c:\python26\python.exe rabbitmqadmin.exe declare exchange name=*ExchangeName1* type=topic durable=true
  1. 创建队列:
c:\python26\python.exe rabbitmqadmin.exe declare queue name=*NameofQueue1* durable=true
  1. 创建绑定:
c:\python26\python.exe rabbitmqadmin.exe declare binding source=ExchangeName1 destination_type=queue destination=*NameofQueue1* routing_key=*RoutingKey1*

通过执行rabbitmqadmin.exe -help -subcommands 它列出了所有可用的命令,

例如:c:\python26\python .exerabbitmqadmin.exe-help-子命令

Create RabbitMq Exchange, Queue and Bindings dynamically from CLI on Windows

I already had a RabbitMQ Server installed and running with multiple queue and exchange and now wanted to create it on the fly from command line. I know it is an old question but I thought giving out this information will be helpful.

Following is what I did:

Setup

  1. Downloaded and installed Python 2.6.6-201008-24 Windows x86-64 MSI installer , any version of python,
  2. Download RabbitMqAdmin: RabbitMq Web User Interface has a link Command Line which navigates to http://server-name:15672/cli/ (server-name: server on which rabbitmq is installed) alternatively,use the above url and save the file as rabbitmqadmin.exe in the python exe location

eg: C:\Python26
C:\Python26\python
C:\Python26\rabbitmqadmin.exe

Code:in a batch file used the below commands

  1. Create exchange:
c:\python26\python.exe rabbitmqadmin.exe declare exchange name=*ExchangeName1* type=topic durable=true
  1. Create queue:
c:\python26\python.exe rabbitmqadmin.exe declare queue name=*NameofQueue1* durable=true
  1. Create binding:
c:\python26\python.exe rabbitmqadmin.exe declare binding source=ExchangeName1 destination_type=queue destination=*NameofQueue1* routing_key=*RoutingKey1*

by executing rabbitmqadmin.exe -help -subcommands it lists all the available commands

eg: c:\python26\python.exe rabbitmqadmin.exe -help -subcommands

简单爱 2024-10-16 14:04:43

对我来说,我的 RabbitMQ 管理交易一直试图重定向到 https 版本...我的设置中的所有内容都是普通的,我什至没有配置文件...无论如何,我的解决方法是手动创建rabbitmqadmin .py 在 sbin 文件夹中,然后填充 https://raw.githubusercontent.com/rabbitmq/rabbitmq-management/v3.8.1/bin/rabbitmqadmin

然后,确保 python 位于例如,您的 PATH 并运行它来添加交换:

python rabbitmqadmin.py declare exchange --vhost=/ name=CompletedMessageExchange type=direct

For me, my RabbitMQ Management deal kept trying to redirect to the https version... everything in my setup is vanilla, I don't even have a config file... anyways, my work around was to manually create rabbitmqadmin.py in the sbin folder, then fill it with https://raw.githubusercontent.com/rabbitmq/rabbitmq-management/v3.8.1/bin/rabbitmqadmin

Then, make sure that python is in your PATH and run this to, for example, add an exchange:

python rabbitmqadmin.py declare exchange --vhost=/ name=CompletedMessageExchange type=direct
深白境迁sunset 2024-10-16 14:04:43

这是一个更简单的 Python 示例,取自 RabbitMQ Python 教程

首先,安装 pika:

sudo easy_install pika
# (or use pip)

这就是向本地主机发送消息所需的全部内容:

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

channel.queue_declare(queue='test-queue')
channel.basic_publish(exchange='', routing_key='test-queue', body='Hello World!')

Here is a more minimal Python example, taken from the RabbitMQ Python tutorial.

First, install pika:

sudo easy_install pika
# (or use pip)

This is all you need to send a message to localhost:

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
channel = connection.channel()

channel.queue_declare(queue='test-queue')
channel.basic_publish(exchange='', routing_key='test-queue', body='Hello World!')
誰ツ都不明白 2024-10-16 14:04:43

如果任何 Windows 用户正在寻找基于 powershell 的解决方案,那么这里有我编写的函数。

Function createQueue([string]$QueueName){
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("content-type", "application/json")
$headers.Add("Authorization", "Basic Z3Vlc3Q6Z3Vlc3Q=")

$body = "{
`n  `"vhost`": `"/`",
`n  `"name`": `"$QueueName`",
`n  `"durable`": `"true`",
`n  `"arguments`": {}
`n}"

# Write-Host $body

$url='http://localhost:15672/api/queues/%2f/'+$QueueName

# Write-Host  $url

$response = Invoke-RestMethod $url -Method 'PUT' -Headers $headers -Body $body
$response | ConvertTo-Json
}

将其保存到 helper.ps1 文件中并将其包含到您的脚本中,如下所示

$queueNames = 'my-queue-name'

. .\helper.ps1

createQueue($queueName)

If any windows user looking for powershell based solution then there is the function I have written.

Function createQueue([string]$QueueName){
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("content-type", "application/json")
$headers.Add("Authorization", "Basic Z3Vlc3Q6Z3Vlc3Q=")

$body = "{
`n  `"vhost`": `"/`",
`n  `"name`": `"$QueueName`",
`n  `"durable`": `"true`",
`n  `"arguments`": {}
`n}"

# Write-Host $body

$url='http://localhost:15672/api/queues/%2f/'+$QueueName

# Write-Host  $url

$response = Invoke-RestMethod $url -Method 'PUT' -Headers $headers -Body $body
$response | ConvertTo-Json
}

Save this into helper.ps1 file and include it into your script like this

$queueNames = 'my-queue-name'

. .\helper.ps1

createQueue($queueName)
陌伤ぢ 2024-10-16 14:04:43

在 RabbitMQ 中创建和删除队列的演练:

我找不到执行此操作的命令行命令。这是我用 java 代码实现的方法。

Ubuntu 上的 Rabbitmq-server 版本 3.3.5

列出队列,还没有队列:

sudo rabbitmqctl list_queues
[sudo] password for eric:
Listing queues ...
...done.

将其放入 CreateQueue.java

import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import java.util.*;
public class CreateQueue {
  public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    Map<String, Object> args = new HashMap<String, Object>();
    args.put("x-message-ttl", 60000);
    channel.queueDeclare("kowalski", false, false, false, args);
    channel.close();
    connection.close();
  }
}

提供您的rabbitmq安装附带的jar文件:

我正在使用rabbitmq-client.jar版本0.9.1,使用您的rabbitmq版本附带的版本。

编译并运行:

javac -cp .:rabbitmq-client.jar CreateQueue.java
java -cp .:rabbitmq-client.jar CreateQueue

它应该顺利完成,现在检查您的队列:

sudo rabbitmqctl list_queues
Listing queues ...
kowalski        0
...done.

kowalski 队列存在。

Walkthrough to Create and delete a queue in RabbitMQ:

I couldn't find a commandline command to do it. Here is how I did it in code with java.

Rabbitmq-server version 3.3.5 on Ubuntu.

List the queues, no queues yet:

sudo rabbitmqctl list_queues
[sudo] password for eric:
Listing queues ...
...done.

Put this in CreateQueue.java

import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import java.util.*;
public class CreateQueue {
  public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    Map<String, Object> args = new HashMap<String, Object>();
    args.put("x-message-ttl", 60000);
    channel.queueDeclare("kowalski", false, false, false, args);
    channel.close();
    connection.close();
  }
}

Supply the jar file that came with your rabbitmq installation:

I'm using rabbitmq-client.jar version 0.9.1, use the one that comes with your version of rabbitmq.

Compile and run:

javac -cp .:rabbitmq-client.jar CreateQueue.java
java -cp .:rabbitmq-client.jar CreateQueue

It should finish without errors, check your queues now:

sudo rabbitmqctl list_queues
Listing queues ...
kowalski        0
...done.

the kowalski queue exists.

望她远 2024-10-16 14:04:43

有助于在您进行交换时绑定交换:

channel.queue_bind(queueName, exchange)

C-;

helps to bind the exchange while you're at it:

channel.queue_bind(queueName, exchange)

C-;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文