0

I have kafka running in docker container as: enter image description here

I had the message produced to this kafka and now would like to consume through spring boot. I used the codebase from this repo:

https://github.com/lokeshgupta1981/Kafka-Tutorials/tree/master/spring-boot-kafka-app/src/main/java/com/howtodoinjava/kafka/demo

while running the consumer, I am getting flooding of flowing error.

[Consumer clientId=consumer-group_id-1, groupId=group_id] Bootstrap broker kafka:9093 (id: -1 rack: null) disconnected

I have kafka hostname resolved through hostfile.

I even added ssl properties as:

spring.kafka.ssl.key-store-location=file:/tmp/secrets/client_keystore.jks
spring.kafka.ssl.trust-store-location=file:/tmp/secrets/truststore.jks
spring.kafka.ssl.key-store-password=foobar
spring.kafka.ssl.trust-store-password=foobar
spring.kafka.ssl.key-store-type=JKS
spring.kafka.ssl.trust-store-type=JKS

but couldn't figure out why I am not able to connect to topic from spring-boot. However, I am able to consume from python as:

# Setup consumer properties
    broker_host = properties.get("client.consumer.broker.hosts") #kafka
    broker_port = properties.get("client.consumer.broker.port")  #9093
    sink_topic = properties.get("client.consumer.topic") # my-private-topic
   

    # Load SSL properties
    ssl_security_protocol = properties.get("ssl.security.protocol") #SSL
    ssl_ca_location = properties.get("ssl.ca.location") #/tmp/secrets/fake-ca-crt.pem
    ssl_cert_location = properties.get("ssl.cert.location") #tmp/secrets/client_cert.pem
    ssl_key_location = properties.get("ssl.key.location") #/tmp/secrets/client_key.pem


    # Load record avro schema
    record_schema_path = os.path.join(dirname, properties["schema.location"])
    record_schema = loads(open(record_schema_path).read())

    deserializer = SdpAvroDeserializer(record_schema)

    # Create a kafka consumer
    # Set enable.auto.commit to False to consume from the beginning of time
    # Set enable.auto.commit to True to only consume what hasn't been consumed yet
    props = {"bootstrap.servers": broker_host + ":" + str(broker_port),
             "default.topic.config": {"auto.offset.reset": "earliest"},
             "group.id": "python-consumer-example-group",
             "enable.auto.commit": False,
             "security.protocol": ssl_security_protocol,
             "ssl.ca.location": ssl_ca_location,
             "ssl.certificate.location": ssl_cert_location,
             "ssl.key.location": ssl_key_location
             }
    consumer = KafkaConsumer(props)
    consumer.subscribe([sink_topic])

    print(f"Start consuming from {sink_topic} at {broker_host} for {listenTimeoutSeconds} seconds")

    t_end = time.time() + listenTimeoutSeconds
    while (time.time() < t_end):
        message = consumer.poll(10)
        # print(message)
        # print("test\n")
        # print(message.value())
        if message and message.value() and not message.error():
            header, payload = deserializer.decode(message.value(), deserialize_header=True)
            if include_record_metadata.lower() == 'true':
                print("Message metadata: %s" % header)
            print("Message payload: %s" % payload)

so what I am missing in spring boot to consume the message from kafka?

OUTPUT:

    2022-02-03 00:36:50.431  INFO 20840 --- [           main] c.h.k.d.SpringBootKafkaAppApplication    : No active profile set, falling back to default profiles: default
2022-02-03 00:36:51.110  INFO 20840 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 9000 (http)
2022-02-03 00:36:51.118  INFO 20840 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-02-03 00:36:51.118  INFO 20840 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.35]
2022-02-03 00:36:51.178  INFO 20840 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2022-02-03 00:36:51.178  INFO 20840 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 714 ms
2022-02-03 00:36:51.326  INFO 20840 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2022-02-03 00:36:51.475  INFO 20840 --- [           main] o.a.k.clients.consumer.ConsumerConfig    : ConsumerConfig values: 
        allow.auto.create.topics = true
        auto.commit.interval.ms = 5000
        auto.offset.reset = earliest
        bootstrap.servers = [kafka:9093]
        check.crcs = true
        client.dns.lookup = default
        client.id = 
        client.rack = 
        connections.max.idle.ms = 540000
        default.api.timeout.ms = 60000
        enable.auto.commit = false
        exclude.internal.topics = true
        fetch.max.bytes = 52428800
        fetch.max.wait.ms = 500
        fetch.min.bytes = 1
        group.id = group_id
        group.instance.id = null
        heartbeat.interval.ms = 3000
        interceptor.classes = []
        internal.leave.group.on.close = true
        isolation.level = read_uncommitted
        key.deserializer = class org.apache.kafka.common.serialization.StringDeserializer
        max.partition.fetch.bytes = 1048576
        max.poll.interval.ms = 300000
        max.poll.records = 500
        metadata.max.age.ms = 300000
        metric.reporters = []
        metrics.num.samples = 2
        metrics.recording.level = INFO
        metrics.sample.window.ms = 30000
        partition.assignment.strategy = [class org.apache.kafka.clients.consumer.RangeAssignor]
        receive.buffer.bytes = 65536
        reconnect.backoff.max.ms = 1000
        reconnect.backoff.ms = 50
        request.timeout.ms = 30000
        retry.backoff.ms = 100
        sasl.client.callback.handler.class = null
        sasl.jaas.config = null
        sasl.kerberos.kinit.cmd = /usr/bin/kinit
        sasl.kerberos.min.time.before.relogin = 60000
        sasl.kerberos.service.name = null
        sasl.kerberos.ticket.renew.jitter = 0.05
        sasl.kerberos.ticket.renew.window.factor = 0.8
        sasl.login.callback.handler.class = null
        sasl.login.class = null
        sasl.login.refresh.buffer.seconds = 300
        sasl.login.refresh.min.period.seconds = 60
        sasl.login.refresh.window.factor = 0.8
        sasl.login.refresh.window.jitter = 0.05
        sasl.mechanism = GSSAPI
        security.protocol = PLAINTEXT
        security.providers = null
        send.buffer.bytes = 131072
        session.timeout.ms = 10000
        ssl.cipher.suites = null
        ssl.enabled.protocols = [TLSv1.2]
        ssl.endpoint.identification.algorithm = https
        ssl.key.password = null
        ssl.keymanager.algorithm = SunX509
        ssl.keystore.location = /tmp/secrets/client_keystore.jks
        ssl.keystore.password = [hidden]
        ssl.keystore.type = JKS
        ssl.protocol = TLSv1.2
        ssl.provider = null
        ssl.secure.random.implementation = null
        ssl.trustmanager.algorithm = PKIX
        ssl.truststore.location = /tmp/secrets/truststore.jks
        ssl.truststore.password = [hidden]
        ssl.truststore.type = JKS
        value.deserializer = class org.springframework.kafka.support.serializer.JsonDeserializer

2022-02-03 00:36:51.550  INFO 20840 --- [           main] o.a.kafka.common.utils.AppInfoParser     : Kafka version: 2.5.0
2022-02-03 00:36:51.551  INFO 20840 --- [           main] o.a.kafka.common.utils.AppInfoParser     : Kafka commitId: 66563e712b0b9f84
2022-02-03 00:36:51.551  INFO 20840 --- [           main] o.a.kafka.common.utils.AppInfoParser     : Kafka startTimeMs: 1643866611549
2022-02-03 00:36:51.553  INFO 20840 --- [           main] o.a.k.clients.consumer.KafkaConsumer     : [Consumer clientId=consumer-group_id-1, groupId=group_id] Subscribed to topic(s): my-private-topic
2022-02-03 00:36:51.554  INFO 20840 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService
2022-02-03 00:36:51.576  INFO 20840 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 9000 (http) with context path ''
2022-02-03 00:36:51.583  INFO 20840 --- [           main] c.h.k.d.SpringBootKafkaAppApplication    : Started SpringBootKafkaAppApplication in 1.405 seconds (JVM running for 1.727)
2022-02-03 00:36:52.036  WARN 20840 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient   : [Consumer clientId=consumer-group_id-1, groupId=group_id] Bootstrap broker kafka:9093 (id: -1 rack: null) disconnected
2022-02-03 00:36:52.302  WARN 20840 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient   : [Consumer clientId=consumer-group_id-1, groupId=group_id] Bootstrap broker kafka:9093 (id: -1 rack: null) disconnected
2022-02-03 00:36:52.569  WARN 20840 --- [ntainer#0-0-C-1] org.apache.kafka.clients.NetworkClient   : [Consumer clientId=consumer-group_id-1, groupId=group_id] Bootstrap broker kafka:9093 (id: -1 rack: null) disconnected
2
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Programmer
  • 117
  • 2
  • 14
  • I doubt Spring is the problem. Have you tried using `kafka-console-consumer`? Unless you've edited /etc/hosts file, your host doesn't know what `kafka` hostname is. You'd need to run the client code on the same Docker network, in a container, for that to work. Also, I'd recommend not using a container that has both Zookeeper and Kafka in it – OneCricketeer Feb 03 '22 at 13:41
  • I have the kafka hostname edited in hostfile. – Programmer Feb 03 '22 at 14:10
  • That's only going to cause problems. Edit your container advertised listeners to correctly expose its address – OneCricketeer Feb 03 '22 at 14:21
  • Other problem - notice `ssl.key.password = null`... Should this be set? – OneCricketeer Feb 03 '22 at 14:27
  • I have ssl.truststore.password = [hidden] and ssl.keystore.password = [hidden] – Programmer Feb 03 '22 at 14:28
  • Sure, those are the stores, not the password of the key itself. I assume you either added/removed that JKS key password when you were using PEM files with Python? – OneCricketeer Feb 03 '22 at 15:47
  • For example, see this post that `ssl.key.password` is set https://docs.confluent.io/platform/current/kafka/authentication_ssl.html – OneCricketeer Feb 03 '22 at 15:50

0 Answers0