These steps cover writing and running your application:
- From your Terminal or console application, create a new directory called ~/projects/go-programming-cookbook/chapter12/synckafka and navigate to this directory.
- Run this command:
$ go mod init github.com/PacktPublishing/Go-Programming-Cookbook-Second-Edition/chapter12/synckafka
You should see a file called go.mod that contains the following:
module github.com/PacktPublishing/Go-Programming-Cookbook-Second-Edition/chapter12/synckafka
- Copy the tests from ~/projects/go-programming-cookbook-original/chapter12/synckafka, or use this as an exercise to write some of your own code!
- Ensure that Kafka is up and running on localhost:9092.
- Create a file called main.go in a directory named consumer with the following content:
package main
import (
"log"
sarama "github.com/Shopify/sarama"
)
func main() {
consumer, err :=
sarama.NewConsumer([]string{"localhost:9092"}, nil)
if err != nil {
panic(err)
}
defer consumer.Close()
partitionConsumer, err :=
consumer.ConsumePartition("example", 0,
sarama.OffsetNewest)
if err != nil {
panic(err)
}
defer partitionConsumer.Close()
for {
msg := <-partitionConsumer.Messages()
log.Printf("Consumed message: \"%s\" at offset: %d\n",
msg.Value, msg.Offset)
}
}
- Create a file called main.go in a directory named producer with the following content:
package main
import (
"fmt"
"log"
sarama "github.com/Shopify/sarama"
)
func sendMessage(producer sarama.SyncProducer, value string) {
msg := &sarama.ProducerMessage{Topic: "example", Value:
sarama.StringEncoder(value)}
partition, offset, err := producer.SendMessage(msg)
if err != nil {
log.Printf("FAILED to send message: %s\n", err)
return
}
log.Printf("> message sent to partition %d at offset %d\n",
partition, offset)
}
func main() {
producer, err :=
sarama.NewSyncProducer([]string{"localhost:9092"}, nil)
if err != nil {
panic(err)
}
defer producer.Close()
for i := 0; i < 10; i++ {
sendMessage(producer, fmt.Sprintf("Message %d", i))
}
}
- Navigate up a directory.
- Run go run ./consumer.
- In a separate Terminal from the same directory, run go run ./producer.
- In the producer Terminal, you should see the following:
$ go run ./producer
2017/05/07 11:50:38 > message sent to partition 0 at offset 0
2017/05/07 11:50:38 > message sent to partition 0 at offset 1
2017/05/07 11:50:38 > message sent to partition 0 at offset 2
2017/05/07 11:50:38 > message sent to partition 0 at offset 3
2017/05/07 11:50:38 > message sent to partition 0 at offset 4
2017/05/07 11:50:38 > message sent to partition 0 at offset 5
2017/05/07 11:50:38 > message sent to partition 0 at offset 6
2017/05/07 11:50:38 > message sent to partition 0 at offset 7
2017/05/07 11:50:38 > message sent to partition 0 at offset 8
2017/05/07 11:50:38 > message sent to partition 0 at offset 9
In the consumer Terminal, you should see this:
$ go run ./consumer
2017/05/07 11:50:38 Consumed message: "Message 0" at offset: 0
2017/05/07 11:50:38 Consumed message: "Message 1" at offset: 1
2017/05/07 11:50:38 Consumed message: "Message 2" at offset: 2
2017/05/07 11:50:38 Consumed message: "Message 3" at offset: 3
2017/05/07 11:50:38 Consumed message: "Message 4" at offset: 4
2017/05/07 11:50:38 Consumed message: "Message 5" at offset: 5
2017/05/07 11:50:38 Consumed message: "Message 6" at offset: 6
2017/05/07 11:50:38 Consumed message: "Message 7" at offset: 7
2017/05/07 11:50:38 Consumed message: "Message 8" at offset: 8
2017/05/07 11:50:38 Consumed message: "Message 9" at offset: 9
- The go.mod file may be updated and the go.sum file should now be present in the top-level recipe directory.
- If you have copied or written your own tests, go up one directory and run go test. Ensure all tests pass.