Connect Kafka with Golang
Introduction
If you need to know the basics of Kafka, such as its key features, components, and advantages, I have an article covering that here. Please review it and follow the steps until you've completed the Kafka installation using Docker to proceed with the following sections.
Connecting to Kafka with Golang
Similar to the example in the article about connecting Kafka with NodeJS, this source code also includes two parts: initializing a producer to send messages to Kafka and using a consumer to subscribe to messages from a topic.
I'll break down the code into smaller parts for better understanding. First, let's define the variable values.
- Here, the package `github.com/confluentinc/confluent-kafka-go/kafka` is used to connect to Kafka.
- The broker is the host address; if you are using ZooKeeper, replace the host address accordingly.
- The `groupId` and `topic` can be changed as needed.
Next is initializing the producer.
The above code is used to send an array of messages `{"message 1", "message 2", "message 3"}` to a topic and uses a go-routine to iterate through events with `for e := range p.Events()` and print out the delivery result, whether it's a success or failure.
Next is creating a consumer to subscribe to the topic and receive messages.
Finally, since this is a simple example, call the functions to create the producer and consumer for use. In a real-world scenario, the deployment of the producer and consumer is typically done on two different servers in a microservices system.
Result |
Comments
Post a Comment