Php 卡夫卡-如何观看新消息
我有一个使用Kafka消息的PHP应用程序。 问题是如何知道卡夫卡中有新的信息? 第一个解决方案是在PHP中创建consumer,然后在循环中运行它以检查新消息。像这样的Php 卡夫卡-如何观看新消息,php,apache-kafka,kafka-consumer-api,Php,Apache Kafka,Kafka Consumer Api,我有一个使用Kafka消息的PHP应用程序。 问题是如何知道卡夫卡中有新的信息? 第一个解决方案是在PHP中创建consumer,然后在循环中运行它以检查新消息。像这样的 <?php namespace MyAppBundle\Command; use MyAppBundle\EventSourcing\EventSerializer\JSONEventSerializer; use MyAppBundle\Service\EventProjectorService; use MyAp
<?php
namespace MyAppBundle\Command;
use MyAppBundle\EventSourcing\EventSerializer\JSONEventSerializer;
use MyAppBundle\Service\EventProjectorService;
use MyAppBundle\Service\KafkaService;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Exception\RuntimeException;
class EventCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('events:fetch');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
/** @var KafkaService $kafkaService */
$kafkaService = $this->getContainer()->get('store_locator.kafka_service');
/** @var EventProjectorService $eventProjector */
$eventProjector = $this->getContainer()->get('store_locator.event_projector');
while(1){
$messages = $kafkaService->fetchEvents();
foreach ($messages as $message) {
$eventProjector->aggregate($message);
}
}
$output->writeln("Finish");
}
}
据我所知,没有比无限循环并不断检查新消息更好的方法了。一种常见的方法是让任务在设定的时间量或迭代次数后自行终止,然后使用类似于
supervisord
的方法来检测死亡并使消费者复活,以防止它耗尽您的所有资源。我目前正在寻找Symfony/Kafka解决方案。你有没有可能分享你的卡夫卡服务代码?@Nealio我不知道你到底需要什么,但我正在使用nmred/kafka php
包,实现自述文件中描述的所有内容。有一个卡夫卡消费者包装器,它会继续查找新消息,并在一段时间后重新启动自己。我也有类似的结果,所以我可以假设这是最好的解决方案:1。使用supervisord确保服务正在运行2。正确配置记录器(可选)3。随时终止并重新运行服务。