首页 > 解决方案 > Symfony 如何将 Mercure 与 Messenger + RabbitMQ 一起使用?

问题描述

我希望将 Mercury 与 RabbitMQ 一起使用。这是我第一次使用 Mercury 和 RabbitMQ,所以我还不是很好。

这是我的位置:

我已经安装了 Mercure 和 Messenger。

Messenger.yaml

framework:
    messenger:
        # Uncomment this (and the failed transport below) to send failed messages to this transport for later handling.
        failure_transport: failed

        transports:
            # https://symfony.com/doc/current/messenger.html#transport-configuration
             async: '%env(MESSENGER_TRANSPORT_DSN)%'
             failed: '%env(MESSENGER_TRANSPORT_FAILED_DSN)%'
            # sync: 'sync://'

        routing:
            # Route your messages to the transports
            # 'App\Message\YourMessage': async

.env

MERCURE_PUBLISH_URL=http://localhost:3000/.well-known/mercure
MERCURE_JWT_TOKEN=aVerySecretKey
MESSENGER_TRANSPORT_DSN=amqp://bastien:mypassword@localhost:5672/%2f/messages
MESSENGER_TRANSPORT_FAILED_DSN=amqp://bastien:mypassword@localhost:5672/%2f/failed

在我的控制器中,我在本地应用程序的 URL 上模拟了 50 次 ping:

    /**
     * @Route("/ping", name="ping", methods={"POST"})
     */
    public function ping(MessageBusInterface $bus)
    {
        for($i=0;$i<=50;$i++)
        {
            $update = new Update("http://monsite.com/ping", "[]");
            $bus->dispatch($update);
        }
        return $this->redirectToRoute('home');
    }

我已经成功启动了我的 Mercury 实例以及 Messenger 实例,因此它与我的 RabbitMQ 连接良好。

但是当我测试发送 ping 时,它可以工作,但没有通过我的 RabbitMQ。我错过了什么?我在路由部分想到了我的 Messenger.yaml,但如果是这种情况,我不知道该放什么

标签: symfonyrabbitmqamqpmessengermercure

解决方案


默认情况下,消息在 messenger 中同步执行。

您需要在 messenger.yaml 中配置更新消息以使用异步传输:

Symfony\Component\Mercure\Update: async

推荐阅读