首页 > 解决方案 > 无法通过 SpringBoot (docker) 应用程序连接到 kafka

问题描述

在本地启动了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 连接时是否需要任何额外的配置?

标签: spring-bootdockerapache-kafka

解决方案


可能您连接到错误的端口。做一个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 网络。


推荐阅读