fouriermourier/py_rmq_client

By fouriermourier

Updated almost 2 years ago

A flexible RabbitMQ client for publishing and consuming messages with customizable flow

Image
Message queues
0

330

fouriermourier/py_rmq_client repository overview

Flexible RabbitMQ Client written in Python

Publish and consume messages with customizable flow. Ideal for testing or experimenting with RabbitMQ configurations. Configurable via CLI arguments and environment variables.

Source code is available on GitHub.

Usage

CLI Arguments

When running the RabbitMQ client, you can specify options through CLI arguments to customize its behavior. Here's a summary of available CLI options:

ArgumentDescriptionDefaultChoices
--modeClient's behavior (publish or consume)consumerconsumer, publisher
--messageMessage to publish (required in publisher mode)None-
--delayDelay between operations in seconds1.0-
--countNumber of messages to publish (0 for infinite)0-
--log-levelSet the log verbositydebugcritical, error, warning, info, debug
--verboseEnable detailed outputTrueTrue, False

Note: If you're in publisher mode, the --message argument is required.

Note #2: It's better to set log-level to "warning" or higher so that you won't see too many messages regarding connection info

Environment Variables

Alternatively, you can configure RabbitMQ using environment variables. Here's the list of supported variables and their default values:

Environment VariableDescriptionDefault
RABBITMQ_HOSTThe RabbitMQ server hostnamelocalhost
RABBITMQ_PORTThe port RabbitMQ is running on5672
RABBITMQ_DEFAULT_USERUsername for connecting to RabbitMQguest
RABBITMQ_DEFAULT_PASSPassword for the userguest
RABBITMQ_QUEUE_NAMEName of the queue to consume/publish messages tomy_queue
RABBITMQ_SSL_ENABLEDWhether to enable SSL for the connectionFalse
RABBITMQ_SSL_CA_CERTSPath to the SSL CA certificateNone
EXCHANGE_NAMERabbitMQ exchange name""
EXCHANGE_TYPEExchange type (e.g., direct, fanout, topic, headers)direct
RABBITMQ_ROUTING_KEYRouting key for binding queue to exchangeValue of RABBITMQ_QUEUE_NAME
Running with Docker

In the docker-compose.yml, you can customize the entrypoint to control how the client runs. Below are two examples to configure the entrypoint.

  1. Publisher mode:

    services:
      rmq:
        image: fouriermourier/py_rmq_client:latest
        ## if you need to build from scratch: 
        # build:
        #   context: ./
        #   dockerfile: Dockerfile
        env_file:
          - .env
        entrypoint:
          - rmq
          - --mode=publisher
          - --message="hello there"
          - --delay=0.6
          - --count=10
          - --log-level=warning
          - --verbose=True
    
  2. Consumer mode:

    services:
      rmq:
        image: fouriermourier/py_rmq_client:latest
        ## if you need to build from scratch:
        # build:
        #   context: ./
        #   dockerfile: Dockerfile
        env_file:
          - .env
        entrypoint:
          - rmq
          - --mode=consumer
          - --delay=0.3
          - --log-level=warning
          - --verbose=True
    
Examples
Running the publisher

You can run the publisher mode with a custom message, delay, and count of messages to send:

docker-compose -f docker-compose.yaml run consumer --mode=publisher --message="hello world" --delay=1 --count=5 --log-level=warning

The client will publish "hello world" 5 times with a 1-second delay between each message.

Running the consumer

To run the consumer:

docker-compose -f docker-compose.yaml run consumer --mode=publisher --message="hello world" --delay=1 --count=5 --log-level=warning

The consumer will process messages with a 1.5-second delay between each one.


RabbitMQ client configuration

By default, the client uses the values below:

  • port: 5672
  • exchange name: ""
  • exchange type: direct
  • host: localhost
  • username: guest
  • password: guest
  • queue name: my_queue
  • ssl enabled: False
  • ssl CA certificates: None
  • routing key: value of RABBITMQ_QUEUE_NAME if specified, otherwise - value of the my_queue parameter

Feel free to adjust .env file or ConfigMap in case of running in k8s to your specific credentials.


Tag summary

Content type

Image

Digest

sha256:faba9eeaa

Size

23.3 MB

Last updated

almost 2 years ago

docker pull fouriermourier/py_rmq_client