RabbitMQ creating queues and bindings from command line

后端 未结 11 1589
时光取名叫无心
时光取名叫无心 2020-12-12 15:25

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

相关标签:
11条回答
  • 2020-12-12 15:53

    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
    
    0 讨论(0)
  • 2020-12-12 15:57

    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. Good luck :)

    0 讨论(0)
  • 2020-12-12 16:03

    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}
    
    0 讨论(0)
  • 2020-12-12 16:05

    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 greater than 2.X but not 3.X
    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

    2. Create queue:

      c:\python26\python.exe rabbitmqadmin.exe declare queue name=NameofQueue1 durable=true

    3. 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

    0 讨论(0)
  • 2020-12-12 16:07

    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 ...

    0 讨论(0)
  • 2020-12-12 16:12

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

    0 讨论(0)
提交回复
热议问题