Skip to content

Instantly share code, notes, and snippets.

@jasonsalas
Last active July 1, 2023 08:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonsalas/27bf3254dd0ba9095aa1ac71f089ca5d to your computer and use it in GitHub Desktop.
Save jasonsalas/27bf3254dd0ba9095aa1ac71f089ca5d to your computer and use it in GitHub Desktop.
Kafka-based pub/sub demo in Chapter 6 of "Microservices in Go" by Alexander Shuiskov

Running Chapter 6's pub/sub demo using Kafka

Here's how to setup Kafka as a pub/sub engine for Alexander Shuiskov's excellent Microservices with Go, and how to create a producer and consumer for your instance in a CLI

running Docker Compose

run Kafka in a Docker container (from movieapp/src/cmd/ratingingester):

  • docker-compose -d up
  • docker exec -it kafka /bin/sh
  • cd /opt/kafka_2.13-2.8.1/bin
  • kafka-topics.sh --create --zookeeper zookeeper:2181 --replication-factor 1 --partitions 1 --topic ratings

send a message (in a separate terminal):

  • docker exec -it kafka /bin/sh
  • cd /opt/kafka_2.13-2.8.1/bin
  • kafka-console-producer.sh --bootstrap-server localhost:9092 --topic ratings
  • (type and send your messages at the command prompt)

run the book's ingester application (in a separate terminal):

  • cd movieapp/src/cmd/ratingingester
  • go run main.go

consume messages (in a separate terminal):

  • docker exec -it kafka /bin/sh
  • cd /opt/kafka_2.13-2.8.1/bin
  • kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic ratings --from-beginning --max-messages 20
  • watch the messages appear! :)
version: '3'
services:
zookeeper:
image: wurstmeister/zookeeper
container_name: zookeeper
ports:
- "2181:2181"
kafka:
image: wurstmeister/kafka
container_name: kafka
ports:
- "9092:9092"
environment:
KAFKA_ADVERTISED_HOST_NAME: localhost
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment