Sign inSign up

ivanfranchin/springboot-kafka-producer-native

By ivanfranchin

Updated 5 months ago

Spring Boot app that produces news messages to Kafka. It runs in Native mode

Image
0

1.4K

ivanfranchin/springboot-kafka-producer-native repository overview

springboot-kafka-producer-native

springboot-kafka-producer-native is a Java web application developed with Spring Boot, leveraging GraalVM Native Image to achieve native compilation. The source code for this project is available in the ivangfr/graalvm-quarkus-micronaut-springboot repository.

springboot-kafka-producer-native is the producer for the springboot.news.native Kafka topic. On the other hand, springboot-kafka-consumer-native is the consumer.

Author

Ivan Franchin (LinkedIn) (Github) (Medium) (Twitter)

REST API

The application has the following endpoint:

  • POST /api/news {"source": "...", "title": "..."}

Environment Variables

  • KAFKA_HOST

    Specify host of the Kafka message broker to use (default localhost)

  • KAFKA_PORT

    Specify port of the Kafka message broker to use (default 29092)

Running example

  1. Create network

    docker network create springboot-producer-consumer-native-network
    
  2. Start Zookeeper container

    docker run -d --rm --name zookeeper \
      -p 2181:2181 \
      -e ZOOKEEPER_CLIENT_PORT=2181 \
      --network springboot-producer-consumer-native-network \
      confluentinc/cp-zookeeper:7.3.1
    
  3. Start Kafka container

    docker run -d --rm --name kafka \
      -p 9092:9092 \
      -e KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181 \
      -e KAFKA_LISTENER_SECURITY_PROTOCOL_MAP=PLAINTEXT:PLAINTEXT \
      -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://kafka:9092 \
      -e KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1 \
      --network springboot-producer-consumer-native-network \
      confluentinc/cp-kafka:7.3.1
    
  4. Start springboot-kafka-producer-native

    docker run --rm --name springboot-kafka-producer-native \
      -p 9080:8080 \
      -e SPRING_PROFILES_ACTIVE=native \
      -e KAFKA_HOST=kafka \
      -e KAFKA_PORT=9092 \
      --network springboot-producer-consumer-native-network \
      ivanfranchin/springboot-kafka-producer-native:1.0.0
    
  5. Start springboot-kafka-consumer-native

    docker run --rm --name springboot-kafka-consumer-native \
      -p 9081:8080 \
      -e SPRING_PROFILES_ACTIVE=native \
      -e KAFKA_HOST=kafka \
      -e KAFKA_PORT=9092 \
      --network springboot-producer-consumer-native-network \
      ivanfranchin/springboot-kafka-consumer-native:1.0.0
    

    To stop press Ctrl+C

  6. Make post news request

    curl -i -X POST localhost:9080/api/news \
      -H 'Content-Type: application/json' \
      -d '{"source": "CNN", "title": "Palmeiras wins Copa Libertadores in 2021"}'
    

    To stop press Ctrl+C

  7. Cleanup

    docker stop kafka zookeeper
    docker network rm springboot-producer-consumer-native-network
    

Tag summary

Content type

Image

Digest

sha256:80c205729

Size

65 MB

Last updated

5 months ago

docker pull ivanfranchin/springboot-kafka-producer-native