无法解析’kafka:9092’:名称或服务未知 – docker / php-rdkafka

我想在一个docker容器中将php连接到kafka.

kafka php lib – https://github.com/arnaud-lb/php-rdkafka/

kafkaDocker集装箱 – https://hub.docker.com/r/wurstmeister/kafka/

一切都在构建并运行正常,但是当我尝试与PHP生产者联系时,我得到以下内容:

httpd_1   | %3|1490816385.542|FAIL|rdkafka#producer-1| [thrd:kafka:9092/bootstrap]: kafka:9092/bootstrap: Failed to resolve 'kafka:9092': Name or service not known
httpd_1   | %3|1490816385.543|ERROR|rdkafka#producer-1| [thrd:kafka:9092/bootstrap]: kafka:9092/bootstrap: Failed to resolve 'kafka:9092': Name or service not known
httpd_1   | %3|1490816385.543|ERROR|rdkafka#producer-1| [thrd:kafka:9092/bootstrap]: 1/1 brokers are down

我在PHP中使用以下内容

    $rk = new RdKafka\Producer();
    $rk->setLogLevel(LOG_DEBUG);
    $rk->addBrokers("kafka");

    $topicConf = new RdKafka\TopicConf();
    $topicConf->set("message.timeout.ms", 1000);
    $topic = $rk->newTopic("DEV", $topicConf);

    $topic->produce(RD_KAFKA_PARTITION_UA, 0, "Message");

    $rk->poll(1000);
    $kafkaConf = new RdKafka\Conf();
    $kafkaConf->setErrorCb(function ($rk, $err, $reason) {
        printf("Kafka error: %s (reason: %s)\n", rd_kafka_err2str($err), $reason);
    });
    $kafkaConf->setDrMsgCb(function ($rk, $message) {
        if ($message->err) {
            print_r($message);
        } else {
            print_r("ok");
        }
    });

我玩过,尝试在docker-compose.yml和PHP代码中设置主机IP,但没有乐趣.我也拒绝连接,但我不知道那更好还是更糟?

如果它有帮助,我的docker-compose.yml

httpd:
  build: .
  ports:
  - 8180:80
  volumes:
  - ~/www:/var/www/html
zookeeper:
  image: wurstmeister/zookeeper
  ports:
  - "2181:2181"
kafka:
  build: ~/kafka-docker/.
  links:
  - zookeeper
  ports:
  - "9092:9092"
  environment:
    KAFKA_ADVERTISED_HOST_NAME: kafka
    KAFKA_ADVERTISED_PORT: 9092
    KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
  volumes:
  - /var/run/docker.sock:/var/run/docker.sock

> PHP版本:7.0.17
> librdkafka版本:0.9.5.0
> php-rdkafka版本:2.0.1

我的问题:我知道如何从php成功连接到kafka?

最佳答案
经纪人将使用advertised.listeners(在该docker镜像中使用KAFKA_ADVERTISED_HOST_NAME抽象出来)宣传他们,然后客户端将尝试连接到这些广告主机和端口.

因此,您需要确保客户端可以解析并访问此广告主机名,例如通过在客户端主机上的/ etc / hosts中添加“kafka”.

转载注明原文:无法解析’kafka:9092’:名称或服务未知 – docker / php-rdkafka - 代码日志