我试图让php连接到Kafka都在一个码头集装箱。
Kafkaphp库-https://github.com/arnaud-lb/php-rdkafka/
Kafka码头集装箱-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 refused
但我不知道这是好是坏?
如果有用的话,我的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吗?
1条答案
按热度按时间r7knjye21#
代理将使用advanded.listeners(在docker映像中似乎是用kafka\u advanded\u host\u名称抽象的)为自己做广告,因此客户端将尝试连接到这些广告的主机和端口。
因此,您需要确保客户机能够解析并访问此播发的主机名,例如,通过将“kafka”添加到客户机主机上的/etc/hosts。