Giter Site home page Giter Site logo

docker-rabbitmq-example's Introduction

Docker RabbitMQ Example

TL;DR: Install Docker, then run docker-compose up. Control-C will cleanly stop everything.

This is a minimal example that runs RabbitMQ and two small Python messages, all in a single Docker environment. The publisher program publishes a message into RabbitMQ every 5 seconds; the consumer program prints every message it recevies to its stdout.

Once you've brought it all up, you should see the publisher and consumer printing messages on their stdout. You can also point a browser at http://localhost:15672/ (guest/guest) to see the RabbitMQ management console. (If you are using a Docker Toolbox or local Docker Machine environment, try http://192.168.99.100:15672/.)

Another Publisher

You can run parts of this environment separately if you'd like. With the whole environment running as above, try running the following:

virtualenv vpy
. vpy/bin/activate
pip install pika

cd publisher
export AMQP_URL=amqp://localhost
./publisher.py

The new publisher will start sending its own messages from "Message 0"; both sets of messages will appear on the consumer's output.

In much the same way (continuing to use the same Python virtual environment) you can run the consumer by hand. RabbitMQ supports multiple consumers reading from the same queue and only one of the consumers will print each message.

Development

You can run single parts of this; for instance, to run just RabbitMQ and run it in the background, you can

docker-compose up -d rabbitmq

Now you can run both the publisher and consumer on the console if you'd like.

As a general development approach, my recommendation would be:

  • Develop on the host using your favorite text editor and a normally-installed Python, with libraries in a virtual environment

  • Write as much non-network code as you can, and use a unit test framework like pytest to write tests for that

  • Write simple mock message handlers that batch everything into an in-memory queue, and extend your pytest tests to test your message handlers.

  • Use an environment variable like AMQP_URL to indicate the location of the broker (if any) and write bigger integration tests that pytest.skip() themselves if that variable isn't set.

  • Only when all of this passes, docker build minimal images with your application and nothing else installed. Hand-test the combined application.

docker-rabbitmq-example's People

Contributors

dmaze avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

docker-rabbitmq-example's Issues

Connection errors with internal publisher and consumer

I have tried to use your well detailed example with docker-compose up, however I have connection timeout errors, that stops this demo.

Error is here:

pika.exceptions.ConnectionWrongStateError: Illegal close(320, "Timeout while
setting up AMQP to 'rabbitmq'/(2, 1, 6, 'rabbitmq', ('172.18.0.3', 5672));
ssl=False") request on <SelectConnection CLOSED transport=None
params=<URLParameters host=rabbitmq port=5672 virtual_host=/ ssl=False>>
because it was called while connection state=CLOSED.

The whole log can be seen here thanks to travis-ci.

Any help appreciated...

BTW: When I declare publisher and consumer outside docker-compose (with localhost URL), it works

on "docker-compose up" getting errors on both consumer and publisher

Hi, looks like a very detailed and great example. But on taking the TL;DR route -> docker-compose up both consumer and publisher fail to start with these errors:

Traceback (most recent call last):

File "/usr/src/app/publisher.py", line 37, in <module>

import pika

File "/usr/local/lib/python2.7/site-packages/pika/__init__.py", line 10, in <module>

from pika.connection import ConnectionParameters

File "/usr/local/lib/python2.7/site-packages/pika/connection.py", line 15, in <module>

import pika.callback

File "/usr/local/lib/python2.7/site-packages/pika/callback.py", line 8, in <module>

from pika import frame

File "/usr/local/lib/python2.7/site-packages/pika/frame.py", line 7, in <module>

from pika import spec

File "/usr/local/lib/python2.7/site-packages/pika/spec.py", line 18, in <module>

from pika.exchange_type import ExchangeType

File "/usr/local/lib/python2.7/site-packages/pika/exchange_type.py", line 1, in <module>

from enum import Enum

ImportError: No module named enum

cheers.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.