This is a simple example of a Kafka producer and consumer written in Go using the Sarama library. The producer sends messages to a Kafka topic, and the consumer reads and processes those messages.
Before running this application, ensure that you have the following installed:
- Go (Golang) - You can download and install Go from the official website.
- Kafka - You need a running Kafka broker. You can download and set up Apache Kafka from the official website.
- Clone this repository to your local machine
git clone https://github.com/shaikrasheed99/golang-kafka-producer-consumer.git
- Install the required Go packages
go get .
-
Update the Kafka broker URL in the producer and consumer code if your Kafka broker is running on a different host and port.
-
Build the producer and consumer executables
go build producer.go
go build consumer.go
- Start the Kafka producer by running the producer executable
./producer
-
The producer will prompt you to enter messages. Type a message and press Enter. To exit the producer, type "exit" and press Enter.
-
The producer will send the messages to the Kafka topic specified in the code (default is "test").
- Start the Kafka consumer by running the consumer executable
./consumer
-
The consumer will subscribe to the Kafka topic specified in the code (default is "test") and start reading messages.
-
The consumer will print received messages to the console, or it will log any errors encountered during message processing.
-
To stop the consumer, press
Ctrl+C
.