Code Monkey home page Code Monkey logo

spring-boot-3-apache-kafka-producer-consumer's Introduction

spring-boot-3-apache-kafka-producer-consumer

Purpose :
1- Send message to Kafka broker topic from kafka-producer-rest-api via kafka-template
2- Read message from Kafka broker topic to kafka-consumer-rest-api
3- Decrease coupling between microservices.

kafka-producer-consumer

Tech Stack

Java 17
Kafka Broker ( must be installed and started at localhost:9092 before starting producer and consumer apis )
H2 Database Engine
spring boot
spring boot starter data jpa
spring boot starter web
spring boot starter test
spring kafka
hibernate
logback
maven
maven-surefire-plugin
springfox-core
springfox-spring-web
springdoc openAPI starter webMVC UI
datasource-proxy
Docker

Install Kafka on Windows 10

1-) Download the latest Kafka release and extract it into "D:\DEV\kafka_2.13-3.3.1" directory :

https://downloads.apache.org/kafka/3.3.1/kafka_2.13-3.3.1.tgz

D:\DEV>cd kafka_2.13-3.3.1
D:\DEV\kafka_2.13-3.3.1>cd bin
D:\DEV\kafka_2.13-3.3.1\bin>cd windows
D:\DEV\kafka_2.13-3.3.1\bin\windows>

2-) Open command line window and start zookeeper

D:\DEV\kafka_2.13-3.3.1\bin\windows>.\zookeeper-server-start.bat ..\..\config\zookeeper.properties

3-) Open another command line window and start Kafka server

D:\DEV\kafka_2.13-3.3.1\bin\windows>.\kafka-server-start.bat ..\..\config\server.properties
[2022-12-26 11:42:24,857] INFO Kafka version: 3.3.1 (org.apache.kafka.common.utils.AppInfoParser)
[2022-12-26 11:42:24,867] INFO Kafka commitId: e23c59d00e687ff5 (org.apache.kafka.common.utils.AppInfoParser)
[2022-12-26 11:42:24,868] INFO Kafka startTimeMs: 1672044144839 (org.apache.kafka.common.utils.AppInfoParser)
[2022-12-26 11:42:24,891] INFO [KafkaServer id=0] started (kafka.server.KafkaServer)

4-) Open another command line window and create "orders" topic

D:\DEV\kafka_2.13-3.3.1\bin\windows>.\kafka-topics.bat --create --topic orders --bootstrap-server localhost:9092
Created topic orders.

D:\DEV\kafka_2.13-3.3.1\bin\windows>.\kafka-topics.bat --describe --topic orders --bootstrap-server localhost:9092
Topic: orders   TopicId: 5dz_M-ssT2ClPkdDtmWZcg PartitionCount: 1       ReplicationFactor: 1    Configs:
        Topic: orders   Partition: 0    Leader: 0       Replicas: 0     Isr: 0

5-) Write some events to the "orders" topic by running the console producer client

D:\DEV\kafka_2.13-3.3.1\bin\windows>.\kafka-console-producer.bat --topic orders --bootstrap-server localhost:9092
>apple
>orange

6-) Open another command line window and run the console consumer client to read the events from the "orders" topic

D:\DEV\kafka_2.13-3.3.1\bin\windows>.\kafka-console-consumer.bat --topic orders --from-beginning --bootstrap-server localhost:9092
apple
orange

Note : In case of LEADER_NOT_AVAILABLE error add hostname and port into "\config\server.properties" file.

listeners=PLAINTEXT://localhost:9092

[2022-12-28 23:32:23,019] WARN [Producer clientId=console-producer] Error while fetching metadata with correlation id 23 : {orders=LEADER_NOT_AVAILABLE}
(org.apache.kafka.clients.NetworkClient)

spring-boot-3-apache-kafka-producer-consumer's People

Contributors

tufangorel avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.