-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
50 lines (42 loc) · 1.4 KB
/
main.go
File metadata and controls
50 lines (42 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/segmentio/kafka-go"
)
// The entry point of the application.
// It configures the Kafka producer and sends a string message to a specified topic.
func main() {
// Define the Kafka broker address
brokerAddress := "localhost:9092"
// Define the Kafka topic to which the message will be sent
topic := "example-topic"
// Create a new Kafka writer (producer) with the specified configuration
// Balancer: Distributes messages to partitions using the LeastBytes strategy
writer := kafka.NewWriter(kafka.WriterConfig{
Brokers: []string{brokerAddress},
Topic: topic,
Balancer: &kafka.LeastBytes{},
BatchSize: 1, // Send each message immediately
})
// Ensure the writer is closed when the function exits
defer writer.Close()
// Create the message to send
message := kafka.Message{
Key: []byte("example-key"), // Optional key
Value: []byte("Hello, Kafka!"),
}
// Use a context with timeout to avoid indefinite blocking
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Attempt to send the message to the configured Kafka topic
log.Println("Sending message to Kafka...")
err := writer.WriteMessages(ctx, message)
if err != nil {
log.Fatalf("Failed to send message: %v", err)
}
log.Println("Message sent successfully.")
fmt.Println("Kafka producer has completed sending the message.")
}