Git Product home page Git Product logo

python-kafka-docker's Introduction

python-kafka-docker

The main objective in this project was to learn how to create an application that sends and receives a message from Kafka, using Docker and docker-compose tools.

Stack

  • aiokafka
  • Docker
  • FastAPI
  • Kafka-python
  • Python 3.8

How to use

Using Docker Compose

You will need Docker installed to follow the next steps. To create and run the image use the following command:

> docker-compose up --build

The configuration will create a cluster with 3 containers:

  • Consumer container
  • Publisher container
  • kafka container
  • kafdrop container
  • zookeeper container

The Publisher container will create a simple RESTful API application that sends data to Kafka. It will take a few seconds to come up, then will be accessible at http://localhost:8000.

The Consumer container is a script that aims to wait and receive messages from Kafka.

And the kafdrop container will provide acess to web UI for viewing Kafka topics and browsing consumer groups that can be accessed at http://localhost:19000.

API

  • Send Message

Send message to Kafka, below is an example request:

POST http://localhost:8000/producer
Accept: application/json
Content-Type: application/json
Body:
{
    "name": "value",
    "description": "value",
}
  • Health check

Checks if the app is available.

GET http://localhost:8000/
Accept: application/json
Content-Type: application/json

Swagger

The swagger, an automatic interactive API documentation, will be accessible at http://localhost:8000/docs

Project Structure

Below is a project structure created:

.
├── README.md
├── docker-compose.yml
├── consumer
│   ├── Dockerfile
│   ├── app
│   │   ├── __init__.py
│   │   ├── __main__.py
│   │   └── enum.py
│   ├── app.py
│   └── requirements.txt
└── publisher
    ├── Dockerfile
    ├── app
    │   ├── __init__.py
    │   ├── core
    │   │   ├── __init__.py
    │   │   ├── gateways
    │   │   │   ├── __init__.py
    │   │   │   └── kafka.py
    │   │   └── models
    │   │       ├── __init__.py
    │   │       └── message.py
    │   ├── dependencies
    │   │   ├── __init__.py
    │   │   └── kafka.py
    │   ├── enum.py
    │   ├── main.py
    │   └── routers
    │       ├── __init__.py
    │       └── publisher.py
    └── requirements.txt

Environment Variables

Listed below are the environment variables needed to run the application. They can be included in docker-compose or to run locally, it's necessary to create an .env file in the root of the Publisher and Consumer service folders.

  • Publisher:
KAFKA_TOPIC_NAME=
KAFKA_SERVER=
KAFKA_PORT=
  • Consumer:
KAFKA_TOPIC_NAME=
KAFKA_SERVER=
KAFKA_PORT=

Help and Resources

You can read more about the tools documentation:

python-kafka-docker's People

Contributors

fernandoblima avatar

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.