This project is a Go application that demonstrates how to consume Kafka messages using the Sarama library. It's containerized with Docker and includes configuration for both Kafka and the consumer application.
This application is designed as a flexible Kafka consumer that can process messages from multiple topics with dedicated handlers. It uses:
- Sarama library for Kafka integration
- Zap for structured logging
- Viper for configuration management
- Docker and Docker Compose for containerization and local development
The consumer connects to Kafka, registers handlers for specific topics, and processes messages according to the business logic in each handler.
- Docker
- Docker Compose
- Go (1.24+ for local development)
- Kafka (provided via Docker Compose)
-
Clone the project:
git clone https://gitlab.tygoinfra.com/technology/tgo/local-commerce/logistics/store/poc/golang-sarama-poc.git cd golang-sarama-poc
-
Build and run the application with Kafka using Docker Compose:
docker-compose up
This will start:
- Zookeeper
- Kafka broker
- Kafka topics setup
- Kafka UI (accessible at http://localhost:8080)
- The consumer application
-
Start Kafka infrastructure:
docker-compose up zookeeper kafka kafka-setup kafka-ui
-
Install dependencies:
go mod download
-
Run the application:
go run main.go
The application is configured using YAML files and environment variables:
kafka.brokers
: List of Kafka brokers to connect tokafka.group_id
: Consumer group IDkafka.topics
: Map of topics to consume fromkafka.init_offset
: Initial offset strategy (oldest
ornewest
)
logger.level
: Log level (info, debug, warn, error)
All configuration options can be overridden with environment variables using the prefix APP_
:
APP_KAFKA_BROKERS
APP_KAFKA_GROUP_ID
- etc.
.
├── config/ # Configuration files
│ └── config.yml # Main application configuration
├── consumer/ # Kafka consumer implementation
│ └── consumer.go # Consumer logic
├── handlers/ # Message handlers
│ └── handlers.go # Business logic for processing messages
├── models/ # Data models
│ └── product.go # Product event model
├── pkg/ # Shared packages
│ ├── config/ # Configuration loading
│ └── log/ # Logging setup
├── Dockerfile # Docker image configuration
├── docker-compose.yml # Docker Compose configuration for all services
├── go.mod # Go module dependencies
├── go.sum # Go module checksums
├── main.go # Main application entry point
└── README.md # Project documentation
The application follows these steps:
- Load configuration from files and environment variables
- Initialize the Kafka consumer with the specified broker(s)
- Register handlers for each configured topic
- Start consuming messages
- Process messages with the appropriate handler based on the topic
- Handle shutdown gracefully when receiving termination signals
The application is configured to handle these topics:
sarama-topic-1
: Product update eventssarama-topic-2
: Product delete events
[Add your license information here]