无法通过SpringBoot(docker)应用程序连接到kafka

时间:2019-03-20 06:15:30

标签: spring-boot docker apache-kafka

在本地启动 kafka ,我写了一个示例 Spring-boot生产者。当我运行此应用程序时,它运行良好。但是,当我通过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连接时是否需要其他配置?

1 个答案:

答案 0 :(得分:0)

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

通常也可以从笔记本电脑上通过localhost访问docker网络。