Spring boot and Apache Kafka via Docker compose throw LEADER_NOT_AVAILABLE of topic and Send failed error

I just wanted to implement Spring boot and Apache Kafka via docker-compose. Firstly I installed the Apache Kafka Docker image via this link: https://hub.docker.com/r/ches/kafka/ and then install jplock/zookeeper via this command shown below.

docker run -d — name zookeeper — publish 2181:2181 jplock/zookeeper:latest

Then I run this command (docker ps) to determine if all images work. their status of all these images is up. That's why they work flawlessly.

Images

Then I run the app and sent a message via Postman. The post url is localhost:8080/kafkamessage.

JSON object is here

{
    "message" : "Hello World"
}

When I sent a request, it throws an error shown below.

"message": "Send failed; nested exception is org.apache.kafka.common.errors.TimeoutException: Topic k-topic not present in metadata after 60000 ms."

Expect for this, I defined an auto-created topic but the consumer and producer couldn't produce the topic in the console

[Consumer clientId=consumer-k-group-1, groupId=k-group] Error while fetching metadata with correlation id 728 : {k-topic=LEADER_NOT_AVAILABLE}
[Producer clientId=producer-1] Error while fetching metadata with correlation id 681 : {k-topic=LEADER_NOT_AVAILABLE}

Here is my error

enter image description here

Here is the link: https://github.com/Rapter1990/springbootkafka

How can I fix the issue?

enter image description here

Upvotes: 0

Views: 956

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191681

Term1

docker run --rm --name zookeeper -p 2181:2181 jplock/zookeeper

Term2

(Where your error is)

docker run --rm -p 9092:9092 -e KAFKA_ADVERTISED_HOST_NAME=127.0.0.1 --link zookeeper:zookeeper ches/kafka

Note: ches/kafka has had no modifications since 2017 and is not maintained with latest improvments in Kafka

Term3

mvn clean package
java -jar ./target/springbootkafka-0.0.1-SNAPSHOT.jar

Term4

curl -X POST -H 'Content-Type: application/json' --data '{"message" : "Hello World"}' localhost:8080/kafkamessage

The producer worked, and so does a consumer

$ kafkacat -C -t k-topic -b localhost:9092 -o beginning
{"message":"Hello World","id":"ef0e7883-640c-473b-9b3d-7919d59594a1","messageDate":[2021,2,11,13,10,30,11737000]}

Upvotes: 1

Related Questions