This service contains a Kafka consumer microservice that listens to a Kafka topic, processes messages, and forwards them to a backend service. It uses Docker for deployment and interacts with a Kafka cluster managed with Zookeeper and the Wurstmeister Kafka Docker image.
- Consumes messages from a Kafka topic
- Sends messages to a backend service via HTTP POST requests
- Handles JSON decoding and errors
- Manually commits Kafka offsets after processing each message
- Graceful shutdown on interrupt signals
- Docker
- Kafka (for message production and consumption)
- Zookeeper (for Kafka coordination)
- Backend service (listening on the URL specified by
BACKEND_URL)
To set up Zookeeper and Kafka using Docker, follow these steps:
-
Create a Docker Network: Create a network for your containers to communicate. This network helps in linking the Zookeeper and Kafka containers.
-
Start Zookeeper: Use the Wurstmeister Zookeeper Docker image to start a Zookeeper instance. Configure it to listen on port 2181.
-
Start Kafka: Use the Wurstmeister Kafka Docker image to start a Kafka instance. Configure it to connect to Zookeeper and listen on ports 9092 and 9093. Make sure to set the advertised listeners properly to facilitate communication between Kafka and clients.
-
Clone the Repository: Obtain the project source code by cloning the repository from the provided URL.
-
Create a
requirements.txtFile: Include the necessary Python packages, such asconfluent_kafkaandrequests, in this file. -
Build the Docker Image: Build the Docker image for the Kafka consumer using the Dockerfile provided in the repository.
-
Run the Docker Container: Launch the Docker container with environment variables specifying the Kafka bootstrap servers, Kafka topic, and backend URL. Ensure the container is connected to the same Docker network as the Kafka and Zookeeper containers.
The Kafka consumer application requires the following environment variables:
KAFKA_BOOTSTRAP_SERVERS: The address of the Kafka bootstrap servers (default:localhost:9092).KAFKA_TOPIC: The Kafka topic from which messages are consumed (default:analytics_topic).BACKEND_URL: The URL of the backend service where messages are sent (default:http://localhost:5050/events).
You can set these variables in your Docker container configuration.
Once the Docker container is running, the Kafka consumer will start processing messages from the specified Kafka topic and forward them to the backend service. Logs will be output to the standard output.
To make changes to the Kafka consumer code, rebuild the Docker image after making your modifications. Ensure that you have the latest code changes reflected in the image before deploying it.
If you encounter issues:
- Confirm that both Kafka and Zookeeper containers are running and properly connected.
- Ensure the backend service is reachable and capable of handling POST requests.
- Check the logs of the Docker containers for any errors or issues.