This article introduces the structure of a Go application that sends and receives events with Apache Kafka. It covers setting up Kafka, defining topics, writing producers and consumers, handling events and organizing application code. Exact commands and library APIs depend on the Kafka and Go versions used.
Set up Kafka
Begin by downloading and configuring Kafka using the installation guide for the version you choose. A Kafka broker is a server that accepts and stores records. Configuration includes listener ports, connection settings and operational security. Topics organize records for a particular stream of data; they can be created, deleted and configured as the application requires.
Local development and console tools
The source course introduces Docker Compose as a way to start Kafka and ZooKeeper for local development. Kafka’s console producer can send test records to a topic, while the console consumer can read records from it. Use these tools to verify basic message flow before adding application code.
Connect a Go application
A producer sends records to a Kafka topic. A consumer reads records. A consumer group lets several consumers share work from a topic and can be scaled according to the workload. The source course uses a Go library it calls Salamander for producer, consumer and event operations; check the library’s current documentation and maintenance status before selecting a dependency.
Organize event-processing code
- Event structure: define the data your application sends through Kafka.
- Repository: handle database access for event-related records.
- Service: apply application or business logic to the data.
- Event handler: process records received from Kafka.
- Consumer connection: connect the consumer to the handler so received events reach the right processing logic.
The article repeats these building blocks in its event-handling section. It also mentions MySQL as an example database for storing and retrieving event data.
Manage configuration with Viper
Viper is introduced as a Go configuration library that can read settings from files, environment variables and command-line arguments. The course applies it to application and database settings such as a connection string, username and password. Keep credentials in suitable secret-management mechanisms rather than committing them to source code.
Topics covered
- Install and configure a Kafka broker and create topics.
- Use Docker Compose and Kafka console producer/consumer tools for local experiments.
- Build Go producers, consumers and consumer groups.
- Structure event data and connect handlers, services, repositories and a database.
- Use a configuration library to manage application settings.
Want to discuss your finances? Add us on LINE for a free consultation.
Add us on LINE for a free consultation
Or scan the QR code to connect:
