Skip to content

Repository files navigation

go-kafka-sample

The sample about kafka, which use these libraries:

Similar libraries for GO

We also provide these libraries to support GO:

  • segmentio/kafka-go, IBM/sarama and confluent
  • kafka to wrap go-stomp
    • Simplify the way to initialize the consumer, publisher by configurations
      • Props: when you want to change the parameter of consumer or publisher, you can change the config file, and restart Kubernetes POD, do not need to change source code and re-compile.
  • core-go/mq to implement this flow, which can be considered a low code tool for message queue consumer:

kafka-sample

A complete TypeScript sample application demonstrating how to build an event-driven service using kafka-plus (which wraps kafkajs) and the core-ts ecosystem.

The project shows how to produce and consume Kafka messages, validate payloads, process messages with retry support, persist data into MySQL, and expose health check endpoints.

Rather than being a simple Kafka example, this project demonstrates how multiple reusable infrastructure libraries can be composed into a clean, maintainable microservice.


Features

  • Kafka producer
  • Kafka consumer
  • Message validation
  • Automatic retry processing
  • MySQL persistence
  • Structured logging
  • Health check endpoint
  • Environment-based configuration
  • Lightweight HTTP server
  • Clean dependency injection using a composition root

Architecture

                 HTTP Request
                      │
                      ▼
               HTTP Application
                      │
                      ▼
               Application Context
                      │
      ┌───────────────┼────────────────┐
      │               │                │
      ▼               ▼                ▼
  Producer        Consumer         Health Check
      │               │
      │               ▼
      │       Message Processor
      │               │
      │          Validation
      │               │
      │         Retry Policy
      │               │
      │          Repository
      │               │
      └───────────► MySQL

All application components are created in a single composition root (context.ts), making dependencies explicit and easy to maintain.


Technology Stack

  • TypeScript
  • Node.js
  • Kafka
  • MySQL

core-ts Libraries

  • kafka-plus
  • message-processing
  • logger-core
  • validation-core
  • mysql2-core
  • config-plus
  • health-service

Project Structure

src/
│
├── app.ts
├── context.ts
├── config.ts
├── model.ts
│
├── handler/
│   ├── validator.ts
│   ├── writer.ts
│   └── processor.ts
│
├── repository/
│   └── user-repository.ts
│
└── index.ts

Message Flow

HTTP Request

        │

        ▼

    Producer

        │

        ▼

   Kafka Topic

        │

        ▼

    Consumer

        │

        ▼

Message Processor

        │

        ▼

   Validation

        │

        ▼

      Retry

        │

        ▼

      MySQL

Getting Started

Install dependencies

npm install

Configure

Update the Kafka and MySQL configuration.

Kafka
-------
Broker
Topic
Client ID
Username
Password

MySQL
-------
Host
Port
Database
Username
Password

Run

npm start

or

npm run dev

REST Endpoints

Send Message

POST /send

Example

{
  "id": "u001",
  "name": "John Doe",
  "email": "john@example.com"
}

The application publishes the message to Kafka.


Health Check

GET /health

Returns the health status of:

  • Kafka
  • MySQL

Example

{
  "status": "UP",
  "checks": [
    {
      "name": "kafka",
      "status": "UP"
    },
    {
      "name": "mysql",
      "status": "UP"
    }
  ]
}

Processing Pipeline

Incoming Kafka messages pass through several stages.

Kafka Message

      │

      ▼

 Validation

      │

      ▼

    Retry

      │

      ▼

  Repository

      │

      ▼

    MySQL

This separation keeps business logic small while making infrastructure concerns reusable.

A common flow to consume a message from a message queue


Dependency Injection

The sample uses a simple composition root instead of a dependency injection framework.

createContext()

    │

    ├── Logger

    ├── Validator

    ├── Repository

    ├── Retry Policy

    ├── Processor

    ├── Producer

    ├── Consumer

    └── Health Check

This approach keeps dependencies explicit and easy to understand.


Why This Sample?

Most Kafka examples only demonstrate how to send and receive messages.

This sample demonstrates how to build a real service by combining reusable infrastructure libraries.

It shows how to:

  • publish Kafka events
  • consume Kafka events
  • validate messages
  • implement retry processing
  • persist data
  • expose health checks
  • organize application dependencies
  • keep business logic independent from infrastructure

Related Projects

Library Purpose
kafka-plus Kafka producer and consumer abstraction
message-processing Message processing pipeline with retry support
logger-core Structured logging
validation-core Data validation
mysql2-core MySQL data access
config-plus Configuration management
health-service Health check aggregation

License

MIT

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages