gpt4 book ai didi

php - 无法解析 'kafka:9092' : Name or service not known - docker/php-rdkafka

转载 作者:IT老高 更新时间:2023-10-28 21:35:34 30 4
gpt4 key购买 nike

我正在尝试让 php 在 docker 容器中连接到 kafka。

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

kafka docker 容器 - 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,但没有任何乐趣。我也有 connection denied 但我不知道那是更好还是更糟?

如果有帮助,我的 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 吗?

最佳答案

代理将使用adverted.listeners(似乎在该docker镜像中用KAFKA_ADVERTISED_HOST_NAME抽象)来宣传自己,因此客户端将尝试连接到这些宣传的主机和端口。

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

关于php - 无法解析 'kafka:9092' : Name or service not known - docker/php-rdkafka,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43103167/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com