【问题标题】:Cannot connect to kafka through SpringBoot (docker) application无法通过 SpringBoot (docker) 应用程序连接到 kafka
【发布时间】:2019-03-20 17:18:11
【问题描述】:

本地启动 kafka,我编写了一个示例 Spring-boot producer。当我运行此应用程序时,它工作正常。但是当我通过 docker 容器启动应用程序时,出现以下日志“无法建立与节点 0 的连接。代理可能不可用。

2019-03-20 06:06:56.023  INFO 1 --- [  XNIO-2 task-1] o.a.k.c.u.AppInfoParser                  : Kafka version : 1.0.1
2019-03-20 06:06:56.023  INFO 1 --- [  XNIO-2 task-1] o.a.k.c.u.AppInfoParser                  : Kafka commitId : c0518aa65f25317e
2019-03-20 06:06:56.224  WARN 1 --- [ad | producer-1] o.a.k.c.NetworkClient                    : [Producer clientId=producer-1] Connection to node 0 could not be established. Broker may not be available.
2019-03-20 06:06:56.263  WARN 1 --- [ad | producer-1] o.a.k.c.NetworkClient                    : [Producer clientId=producer-1] Connection to node 0 could not be established. Broker may not be available.
2019-03-20 06:06:56.355  WARN 1 --- [ad | producer-1] o.a.k.c.NetworkClient                    : [Producer clientId=producer-1] Connection to node 0 could not be established. Broker may not be available.
2019-03-20 06:06:56.594  WARN 1 --- [ad | producer-1] o.a.k.c.NetworkClient                    : [Producer clientId=producer-1] Connection to node 0 could not be established. Broker may not be available.
2019-03-20 06:06:56.919  WARN 1 --- [ad | producer-1] o.a.k.c.NetworkClient                    : [Producer clientId=producer-1] Connection to node 0 could not be established. Broker may not be available.
2019-03-20 06:06:57.877  WARN 1 --- [ad | producer-1] o.a.k.c.NetworkClient                    : [Producer clientId=producer-1] Connection to node 0 could not be established. Broker may not be available.

请根据日志找到下面的 ProducerConfig 值

2019-03-20 06:06:55.953  INFO 1 --- [  XNIO-2 task-1] o.a.k.c.p.ProducerConfig                 : ProducerConfig values: 
    acks = 1
    batch.size = 16384
    bootstrap.servers = [192.168.0.64:9092]
    buffer.memory = 33554432
    client.id = 
    compression.type = none
    connections.max.idle.ms = 540000
    enable.idempotence = false
    interceptor.classes = null
    key.serializer = class org.apache.kafka.common.serialization.StringSerializer
    linger.ms = 0
    max.block.ms = 60000
    max.in.flight.requests.per.connection = 5
    max.request.size = 1048576
    metadata.max.age.ms = 300000
    metric.reporters = []
    metrics.num.samples = 2
    metrics.recording.level = INFO
    metrics.sample.window.ms = 30000
    partitioner.class = class org.apache.kafka.clients.producer.internals.DefaultPartitioner
    receive.buffer.bytes = 32768
    reconnect.backoff.max.ms = 1000
    reconnect.backoff.ms = 50
    request.timeout.ms = 30000
    retries = 0
    retry.backoff.ms = 100
    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.mechanism = GSSAPI
    security.protocol = PLAINTEXT
    send.buffer.bytes = 131072
    ssl.cipher.suites = null
    ssl.enabled.protocols = [TLSv1.2, TLSv1.1, TLSv1]
    ssl.endpoint.identification.algorithm = null
    ssl.key.password = null
    ssl.keymanager.algorithm = SunX509
    ssl.keystore.location = null
    ssl.keystore.password = null
    ssl.keystore.type = JKS
    ssl.protocol = TLS
    ssl.provider = null
    ssl.secure.random.implementation = null
    ssl.trustmanager.algorithm = PKIX
    ssl.truststore.location = null
    ssl.truststore.password = null
    ssl.truststore.type = JKS
    transaction.timeout.ms = 60000
    transactional.id = null
    value.serializer = class org.springframework.kafka.support.serializer.JsonSerializer

我的 ProducerConfig 如下

@Bean
public Map<String, Object> producerConfigs() {
    Map<String, Object> props = new HashMap<>();
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.0.64:9092");
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
    return props;
}

通过docker连接时是否需要额外配置?

【问题讨论】:

  • 这听起来像是配置问题。请确保您了解 Kafka / docker 网络要求。希望github.com/wurstmeister/kafka-docker/wiki/Connectivity 会有所帮助。
  • 我在没有 docker 的情况下启动了 kafka。当我使用 docker 容器启动 spring-boot 应用程序(生产者)时,我遇到了上述问题,当我在没有 docker 的情况下启动同一个 spring-boot 应用程序时,它工作正常。

标签: spring-boot docker apache-kafka


【解决方案1】:

您可能连接到了错误的端口。做一个docker ps:

例如

2ca7f0cdddd        confluentinc/cp-enterprise-kafka:5.1.2   "/etc/confluent/dock…"   2 weeks ago         Up 50 seconds       0.0.0.0:9092->9092/tcp, 0.0.0.0:29092->29092/tcp   broker

并在上面的示例中使用后来的代理端口:29092。

通常也可以从您的笔记本电脑访问本地主机上的 docker 网络。

【讨论】:

    猜你喜欢
    • 2021-10-25
    • 2022-01-12
    • 2017-10-10
    • 1970-01-01
    • 2020-02-29
    • 2021-04-18
    • 2018-11-28
    • 1970-01-01
    • 2020-01-21
    相关资源
    最近更新 更多