I have been facing some problems dealing with Kafka, I'm a begginer and I'm still learning, so I would appreciate any help.
In my particular case I'm working with Kafka and docker in a compose file. Here I present my compose file:
version: "3.2"
services:
####################################
zookeeper:
image: 'confluentinc/cp-zookeeper:latest'
container_name: zookeeper
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
ports:
- 2181:2181
###############################################################
broker:
image: 'confluentinc/cp-kafka:latest'
container_name: broker
depends_on:
- zookeeper
ports:
- 9092:9092
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
ALLOW_PLAINTEXT_LISTENER: "yes"
# Exposes 9092 for external connections to the broker
# Use kafka:29092 for connections internal on the docker network
# See https://rmoff.net/2018/08/02/kafka-listeners-explained/ for details
KAFKA_LISTENERS: "PLAINTEXT://:29092,PLAINTEXT_HOST://:9092"
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker:29092,PLAINTEXT_HOST://localhost:9092
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
KAFKA_DELETE_TOPIC_ENABLE: "true"
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
################################################################
kafdrop2:
image: 'obsidiandynamics/kafdrop:latest'
container_name: kafdrop2
ports:
- 9000:9000
environment:
KAFKA_BROKERCONNECT: broker:29092
Additionally, I'm using Kafdrop to see my topics and messages faster instead of using the command line.
I have created a topic called First_Topic, and I have the following code:
package com.nrich.curse.kafka.tutorial;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;
import java.util.Properties;
public class demo {
public static void main(String[] args) {
// Create the producer properties
String servers = "broker:29092";
Properties properties = new Properties();
// properties.setProperty("bootstrap.servers", "broker:9092");
properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, servers);
// Serializer helps the producer know what type of value is send
// and how to serialize it to bytes
properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
// Create the producer, using String in each one mean the key/value be a string
KafkaProducer<String, String> producer = new KafkaProducer<String, String>(properties);
// Creating the producer record
ProducerRecord<String, String> record =
new ProducerRecord<String, String>("First_Topic","hello world nuevo nuevo funcionando");
// Send Data - COnsider it is asyncronic
producer.send(record);
// flush the data
producer.flush();
// flush and close the producer
producer.close();
}
}
My problem is that when I set the bootstrap-server or direction using this:
String servers = "broker:29092";
and I try to run the jar file using docker it fails.
The jar file was compiled with maven, so what I did was to put inside the docker file the jar.
My docker file looks like this:
FROM openjdk:11
WORKDIR /
COPY kafka-course-1.0.jar ./
CMD java -Xmx10m -Xms10m -jar kafka-course-1.0.jar
I have build it, and when I run it,I always get the following error:
[main] WARN org.apache.kafka.clients.ClientUtils - Couldn't resolve server broker:29092 from bootstrap.servers as DNS resolution failed for broker
[main] INFO org.apache.kafka.clients.producer.KafkaProducer - [Producer clientId=producer-1] Closing the Kafka producer with timeoutMillis = 0 ms.
[main] INFO org.apache.kafka.common.metrics.Metrics - Metrics scheduler closed
[main] INFO org.apache.kafka.common.metrics.Metrics - Closing reporter org.apache.kafka.common.metrics.JmxReporter
[main] INFO org.apache.kafka.common.metrics.Metrics - Metrics reporters closed
[main] INFO org.apache.kafka.common.utils.AppInfoParser - App info kafka.producer for producer-1 unregistered
Exception in thread "main" org.apache.kafka.common.KafkaException: Failed to construct kafka producer
at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:442)
at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:292)
at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:319)
at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:304)
at com.nrich.curse.kafka.tutorial.demo.main(demo.java:24)
Caused by: org.apache.kafka.common.config.ConfigException: No resolvable bootstrap urls given in bootstrap.servers
at org.apache.kafka.clients.ClientUtils.parseAndValidateAddresses(ClientUtils.java:89)
at org.apache.kafka.clients.ClientUtils.parseAndValidateAddresses(ClientUtils.java:48)
at org.apache.kafka.clients.producer.KafkaProducer.<init>(KafkaProducer.java:416)
I have no idea why it happens, but it does not work.
What I saw is that if I run the code without docker, just running it locally it works, but it only works when I change the bootstrap-server like this: String servers = "localhost:9092";
. In fact, I have tried it using the new jar file inside the docker (changing the bootstrap-server and compiling it again) but even thought it does not work.
As I mentioned, if I run it locally, it works using the "localhost:9092", and the producer gets the message inside the topic as you can see in the picture below: Events inside a topic
If appreciate any solution or advice. Thank you!