Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Symfony: The Fast Track
  4. Polish
  5. Używanie RabbitMQ jako pośrednika wiadomości

Używanie RabbitMQ jako pośrednika wiadomości

RabbitMQ jest bardzo popularnym pośrednikiem wiadomości (ang. message broker), który możesz wykorzystać jako alternatywę dla PostgreSQL.

Zmiana PostgreSQL na RabbitMQ

Wprowadź następujące zmiany, aby użyć RabbitMQ zamiast PostgreSQL jako pośrednika wiadomości:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
--- a/config/packages/messenger.yaml
+++ b/config/packages/messenger.yaml
@@ -5,10 +5,7 @@ framework:
         transports:
             # https://symfony.com/doc/current/messenger.html#transport-configuration
             async:
-                dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
-                options:
-                    use_notify: true
-                    check_delayed_interval: 60000
+                dsn: '%env(RABBITMQ_URL)%'
                 retry_strategy:
                     max_retries: 3
                     multiplier: 2

Musimy również dodać obsługę RabbitMQ dla Messengera:

1
$ symfony composer req amqp-messenger

Dodawanie RabbitMQ do stosu Dockera

Jak pewnie się domyślasz, musimy dodać RabbitMQ do stosu Docker Compose:

1
2
3
4
5
6
7
8
9
10
11
12
13
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -19,6 +19,10 @@ services:
     image: redis:5-alpine
     ports: [6379]

+  rabbitmq:
+    image: rabbitmq:3.7-management
+    ports: [5672, 15672]
+
 volumes:
 ###> doctrine/doctrine-bundle ###
   db-data:

Restartowanie usług Dockera

Aby Docker Compose zauważył RabbitMQ, musisz zatrzymać kontenery i je zrestartować:

1
2
$ docker-compose stop
$ docker-compose up -d
1
$ sleep 10

Odkrywanie webowego interfejsu do zarządzania RabbitMQ

Jeżeli chcesz zobaczyć kolejki i wiadomości przepływające przez RabbitMQ, otwórz webowy interfejs zarządzania:

1
$ symfony open:local:rabbitmq

lub wykorzystaj pasek narzędzi do debugowania:

/

Użyj kombinacji guest/guest aby zalogować się do webowego interfejsu zarządzania RabbitMQ.

/

Wdrażanie RabbitMQ

Aby dodać RabbitMQ do serwerów produkcyjnych, dodaj go do listy usług:

1
2
3
4
5
6
7
8
9
10
11
--- a/.platform/services.yaml
+++ b/.platform/services.yaml
@@ -18,3 +18,8 @@ files:

 rediscache:
     type: redis:5.0
+
+queue:
+    type: rabbitmq:3.7
+    disk: 1024
+    size: S

Dodaj odniesienie do RabbitMQ w konfiguracji kontenera oraz włącz rozszerzenie PHP o nazwie amqp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
--- a/.platform.app.yaml
+++ b/.platform.app.yaml
@@ -8,6 +8,7 @@ dependencies:

 runtime:
     extensions:
+        - amqp
         - apcu
         - blackfire
         - ctype
@@ -41,6 +42,7 @@ mounts:
 relationships:
     database: "database:postgresql"
     redis: "rediscache:redis"
+    rabbitmq: "queue:rabbitmq"
     
 hooks:
     build: |

Aby dostać się do webowego interfejsu zarządzania RabbitMQ, po tym kiedy zostanie on zainstalowany w Twoim projekcie, musisz najpierw otworzyć tunel:

1
2
3
4
5
$ symfony cloud:tunnel:open
$ symfony open:remote:rabbitmq

# when done
$ symfony cloud:tunnel:close

Idąc dalej

  • Dokumentacja RabbitMQ.
Previous page Używanie Redisa do przechowywania sesji
Next page Co dalej?
This work, including the code samples, is licensed under a Creative Commons BY-NC-SA 4.0 license.
TOC
    Version

    Symfony 5.4 is backed by

    Measure & Improve Symfony Code Performance

    Measure & Improve Symfony Code Performance

    Take the exam at home

    Take the exam at home

    Version:
    Locale:
    ebook

    This book is backed by:

    see all backers

    Symfony footer

    Avatar of Robin Lehrmann, a Symfony contributor

    Thanks Robin Lehrmann for being a Symfony contributor

    2 commits • 103 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony