명령 줄에서 대기열과 바인딩을 생성하는 RabbitMQ
내 컴퓨터에 RabbitMQ가 설치되어있는 경우 명령 줄에서 메시지 큐를 생성하고 클라이언트를 사용하지 않고 특정 거래소에 바인딩하는 방법이 있습니까?
불가능하다고 생각하지만 확실히하고 싶습니다.
RabbitMQ 관리 플러그인을 설치합니다 . 모든 큐 / 교환 / 등을 구성하는 데 사용할 수있는 명령 줄 도구가 함께 제공됩니다.
요약:
다른 답변은 요청한 것에 대한 좋은 대안입니다. 다음은 명령 줄에서 사용할 수있는 명령입니다.
먼저 필요한 모든 준비 작업을 수행합니다 (예 : rabbit 설치 rabbitmqadmin
, 및 rabbitctl
. 아이디어는 rabbitmqctl
및의 명령을 사용하는 것입니다 rabbitmqadmin
. 몇 가지 명령 예제를 볼 수 있습니다 : https://www.rabbitmq.com/management-cli.html
예제 명령 / 설정 :
다음 명령은 필요한 모든 것이 아니라면 대부분을 제공합니다.
# 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으로 바인딩하는 다른 방법
다음은 명령 줄 바인딩의 대안입니다. 가끔 문제가 발생하고 다음 파이썬 코드가 더 안정적이라는 것을 알았습니다.
#!/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])
위는 파이썬을 사용하여 스크립트의 일부로 실행할 수 있습니다. 나가는 항목을 배열에 넣습니다. 그러면 배열을 반복 할 수 있습니다. 이렇게하면 배포가 쉬워집니다.
마지막 생각
위의 내용이 올바른 방향으로 움직 이도록해야한다고 생각합니다. 특정 명령이 의미가없는 경우 Google을 사용하거나 rabbitmqadmin help subcommands
. 나는 스스로를 설명하는 변수를 사용하려고했다. 행운을 빕니다 :)
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}
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"
재미를 ...
제공된 명령 줄 인터페이스 인 rabbitmqctl은 대기열을 생성하고 바인딩하는 기능을 노출하지 않습니다.
그러나 빠른 스크립트로 수행하는 것은 매우 사소하며 rabbit mq 시작 안내서는 게시자와 소비자 측 모두에서 몇 가지 예를 보여줍니다.
#do some work to connect
#do some work to open a channel
channel.queue_declare(queue='helloworld')
나는 연결에 대해 글을 쓰고 있지만 대기열을 만드는 것은 문자 그대로 하나의 라이너입니다. 이 작업은 멱 등성이므로 스크립트에 문을 포함 할 수 있으며 대기열을 계속 재생성하거나 동일한 이름의 기존 대기열을 날려 버리지 않을 것임을 알 수 있습니다.
Windows의 CLI에서 동적으로 RabbitMq 교환, 대기열 및 바인딩 생성
나는 이미 RabbitMQ 서버를 설치하고 여러 큐 및 교환과 함께 실행했으며 이제 명령 줄에서 즉시 생성하고 싶었습니다. 나는 그것이 오래된 질문이라는 것을 알고 있지만이 정보를 제공하는 것이 도움이 될 것이라고 생각했습니다.
다음은 내가 한 일입니다.
설정
- Python 2.6.6-201008-24 Windows x86-64 MSI 설치 프로그램, 2.X보다 크지 만 3.X가 아닌 모든 버전의 Python을 다운로드하여 설치했습니다.
- 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
Create exchange:
c:\python26\python.exe rabbitmqadmin.exe declare exchange name=ExchangeName1 type=topic durable=true
Create queue:
c:\python26\python.exe rabbitmqadmin.exe declare queue name=NameofQueue1 durable=true
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
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
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!')
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.
helps to bind the exchange while you're at it:
channel.queue_bind(queueName, exchange)
C-;
'UFO ET IT' 카테고리의 다른 글
언제 save !, create!를 사용합니까? (0) | 2020.12.03 |
---|---|
NSTimer가 호출 한 메소드에 매개 변수 전달 (0) | 2020.12.03 |
Heroku H18 오류를 어떻게 해석해야합니까? (0) | 2020.12.03 |
jQuery.attr () 함수를 사용하여 여러 데이터 속성을 설정할 수 있습니까? (0) | 2020.12.03 |
관리자를 사용할 수 없습니다. (0) | 2020.12.03 |