【问题标题】:Symfony how to use Mercure with Messenger + RabbitMQ?Symfony 如何将 Mercure 与 Messenger + RabbitMQ 一起使用?
【发布时间】:2020-12-02 13:57:08
【问题描述】:

我希望将 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,但如果是这种情况,我不知道该放什么

【问题讨论】:

  • 默认情况下,消息在messenger中同步执行。您可能需要在 messenger.yaml 中配置更新消息以使用异步传输
  • 噢噢好吧!我刚刚在messenger.yaml 中为路由添加了Symfony\Component\Mercure\Update: async,现在效果很好!我现在更好地理解它是如何工作的!非常感谢!

标签: symfony rabbitmq amqp messenger mercure


【解决方案1】:

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

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

Symfony\Component\Mercure\Update: async

【讨论】:

  • 是的,效果很好!另一方面,如果我有一个用 Python 编码的应用程序,并且我需要放入我的 RabbitMQ 元素,这些元素应该被我的 Symfony 应用程序与 Messenger 拦截,这可能吗?因为在我的 Python 应用程序中,我将无法使用 Messenger 或将“更新”对象放入 RabbitMQ 等待列表。
  • 您可以查看 Symfony 在发送更新消息时发送的消息。如果使用起来不容易/干净,您可以添加一个中间步骤:从 Python 推送到队列,消费并在该消费者中发送带有信封的格式良好的更新消息。这可以提供帮助:symfonycasts.com/screencast/messenger/external-transport
猜你喜欢
  • 2021-05-09
  • 2021-06-18
  • 2022-01-25
  • 2021-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-14
相关资源
最近更新 更多