• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

?Setting Up Kafka with Docker Compose and Node.js: A Practical Guide

Lomanu4 Оффлайн

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155
Apache Kafka is a powerful distributed event streaming platform widely used for building real-time data pipelines and applications. However, setting up Kafka can be daunting, especially when integrating it with a Node.js application. This article explores a GitHub project that simplifies this process by using Docker Compose to set up a Kafka environment and a Node.js Express application to test Kafka's functionality.

Overview of the Project
This project provides a ready-to-use Kafka environment using Docker Compose and a Node.js Express application (kafka-express-app) to test Kafka by producing and consuming messages. It is an excellent starting point for developers looking to integrate Kafka into their applications.

Project Structure
clone this github repo:

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.


The repository is well-organized, with the following structure:


kafka-docker/
├── docker-compose.yml # Docker Compose file for Kafka setup
├── kafka-express-app/ # Node.js Express application
│ ├── src/
│ │ ├── kafka/
│ │ │ ├── producer.ts # Kafka producer logic
│ │ │ └── consumer.ts # Kafka consumer logic
│ │ ├── routes/
│ │ │ └── index.ts # Express routes
│ │ └── app.ts # Express app entry point
│ ├── package.json # Node.js dependencies and scripts
│ ├── tsconfig.json # TypeScript configuration
│ └── .env # Environment variables
└── README.md # Project documentation

Kafka Setup with Docker Compose

The docker-compose.yml file defines the services required to run Kafka, including:

  • Zookeeper: Manages Kafka brokers.
  • Kafka Broker: The Kafka server.
  • Schema Registry: Manages Avro schemas for Kafka topics.
  • Kafka Connect: Connects Kafka to external systems.
  • Control Center: A web UI for managing Kafka.
  • kafka-express-app: The Node.js application for testing Kafka.

Steps to Start Kafka

  1. Start Docker Services: Run the following command to start all services:

docker-compose up -d
  1. Verify Services: Check if all services are running:

docker ps
  1. Access Control Center: Open the Confluent Control Center in your browser:


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



(If you don't see anything, then wait for 1 min. Let the containers spin up and connect with each other.)


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



On the Click of this page. It will open the cluster details page.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



here you can see numbers of broker running, topics, partitions and other details.

Node.js Application Setup
The kafka-express-app is a Node.js Express application that interacts with Kafka. It includes producer and consumer logic written in TypeScript.

Environment Variables
Create a .env file in the kafka-express-app directory with the following content:


KAFKA_CLIENT_ID=my-app
KAFKA_BROKERS=localhost:9092
KAFKA_GROUP_ID=test-group
KAFKA_TOPIC=test-topic

Install Dependencies
Navigate to the kafka-express-app directory and install the required dependencies:


cd kafka-express-app
npm install

Build the Application
Compile the TypeScript code to JavaScript:


npm run build

Run the Application
Start the application in development mode:


npm run dev

Or start the compiled application:


npm start

Testing Kafka
The Node.js application provides endpoints to test Kafka's producer and consumer functionality.

Produce a Message

Use a tool like curl or Postman to send a POST request to the /api/publish endpoint:


curl -X POST

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

\
-H "Content-Type: application/json" \
-d '{"topic": "test-topic", "message": "Hello Kafka!"}'

Consume Messages

Send a GET request to the /api/subscribe endpoint to start consuming messages:


curl

Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



You should see the consumed messages logged in the console.


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.




Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.



Stopping Services
To stop all Docker services, run:


docker-compose down

Troubleshooting

  1. Kafka Broker Connection Issues: Ensure the KAFKA_BROKERS in .env matches the broker address in docker-compose.yml (e.g., broker:29092).
  2. Port Conflicts: Ensure the ports defined in docker-compose.yml (e.g., 9092, 29092, 9021) are not in use by other applications.

Conclusion
This project simplifies the process of setting up Kafka with Docker Compose and integrating it with a Node.js application. Whether you're a beginner or an experienced developer, this repository provides a solid foundation for building real-time data streaming applications.
If you're looking to dive into Kafka and Node.js, this project is a great place to start. Try it out, and let us know your thoughts in the comments below!


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу