使用Apache Kafka作为队列传输的Symfony Messenger

时间:2019-10-10 07:33:54

标签: php symfony apache-kafka messenger

我不知道如何为Kafka配置Symfony messenger。一切都适用于rabbitmq(我创建了Messenger和Messenger处理程序):

环境:

MESSENGER_TRANSPORT_DSN=amqp://user:password@myhost:5672/%2f/messages

config / packages / messenger.yaml

framework:
  messenger:
    transports:
      async: "%env(MESSENGER_TRANSPORT_DSN)%"

.env

MESSENGER_TRANSPORT_DSN=enqueue://node-1.kafka.myhost.com:9092/%2f/messages

config / packages / messenger.yaml

framework:
  messenger:
    transports:
      async: "%env(MESSENGER_TRANSPORT_DSN)%"

请给我最好的例子。谢谢!

1 个答案:

答案 0 :(得分:2)

我的开发者:Docker + Centos 7 + PHP73,NGINX。

此配置的解决方案:

1。安装php-rdkafka(重要:版本3.1.x !,将路径更改为php;))

yum -y install make librdkafka-devel && git clone --branch 3.1.x https://github.com/arnaud-lb/php-rdkafka.git && cd php-rdkafka && /path/to/php73/root/bin/phpize && ./configure --with-php-config=/path/to/php73/root/bin/php-config && make all -j 5 && make install

2。将php扩展名添加到php.ini

[rdkafka]
extension=rdkafka.so

3。安装symfony的软件包:

composer req symfony/messenger enqueue/rdkafka enqueue/enqueue-bundle sroze/messenger-enqueue-transport

4。注册捆绑软件-添加到config / bundles.php

Enqueue\Bundle\EnqueueBundle::class => ['all' => true],
Enqueue\MessengerAdapter\Bundle\EnqueueAdapterBundle::class => ['all' => true],

5。添加文件config / packages / enqueue.yaml:

enqueue:
  default:
    transport:
      dsn: "rdkafka://"
      global:
        group.id: 'myapp'
        metadata.broker.list: "%env(KAFKA_BROKER_LIST)%"
      topic:
        auto.offset.reset: beginning
      commit_async: true
    client: ~

6。添加文件config / packages / messenger.yaml:

framework:
  messenger:
    failure_transport: failed

    transports:
      async:
        dsn:  "%env(MESSENGER_TRANSPORT_DSN)%"
      failed:
        dsn: "doctrine://default?queue_name=failed"

    routing:
      'App\Message\EmailNotification': asyn

7。添加到.env:

###> messenger ###
MESSENGER_TRANSPORT_DSN=enqueue://default
KAFKA_BROKER_LIST=node-1.kafka.host:9092,node-2.kafka.host:9092,node-3.kafka.host:9092
###< messenger ###

8。文档中的Messege和MessengeHandle: https://symfony.com/doc/current/messenger.html

9。运行消费者:

php bin/console messenger:consume async

祝你好运!