Symfony
sponsored by SensioLabs
Menu
  • About
  • Documentation
  • Screencasts
  • Cloud
  • Certification
  • Community
  • Businesses
  • News
  • Download
  1. Home
  2. Documentation
  3. Symfony: The Fast Track
  4. Japanese
  5. RabbitMQをメッセージのブローカーとして使う
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
Search by Algolia

Table of Contents

  • PostgreSQLからRabbitMQに切り替える
  • DockerにRabbitMQを追加する
  • Dockerサービスを再起動する
  • RabbitMQのWeb管理画面を使う
  • RabbitMQをデプロイする

RabbitMQをメッセージのブローカーとして使う

RabbitMQをメッセージのブローカーとして使う

RabbitMQはとても有名なメッセージブローカーで、PostgreSQLの代わりに使うことができます。

PostgreSQLからRabbitMQに切り替える

メッセージブローカーとしてPostgreSQLの代わりにRabbitMQを利用します:

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

メッセンジャー用にRabbitMQサポートを追加する必要があります:

1
$ symfony composer req amqp-messenger

DockerにRabbitMQを追加する

既に予想していたかもしれませんが、RabbitMQを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:

Dockerサービスを再起動する

Docker ComposeでRabbitMQ コンテナを扱わせるために、コンテナをすべて停止して再起動します:

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

RabbitMQのWeb管理画面を使う

RabbitMQを通っていくキューやメッセージを見たければ、RabbitMQのWeb管理画面を開きます:

1
$ symfony open:local:rabbitmq

あるいはWebデバッグツールバーからも見ることができます:

/

RabbitMQの管理画面には guest/guest アカウントを使ってログインします:

/

RabbitMQをデプロイする

RabbitMQをプロダクションサーバーに追加するには、サービスのリストに追加します:

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

Webコンテナの設定でも参照し、 amqp PHP拡張を有効にします:

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
@@ -42,6 +43,7 @@ mounts:
 relationships:
     database: "database:postgresql"
     redis: "rediscache:redis"
+    rabbitmq: "queue:rabbitmq"
     
 hooks:
     build: |

RabbitMQサービスがプロジェクトにインストールされると、トンネルを使ってWeb管理画面にアクセスできるようになります:

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

# when done
$ symfony cloud:tunnel:close

より深く学ぶために

  • RabbitMQ ドキュメント.
Previous page Redisにセッションを保存する
Next page 次にやること?
This work, including the code samples, is licensed under a Creative Commons BY-NC-SA 4.0 license.
We stand with Ukraine.
Version:
Locale:

This book is backed by:

see all backers

↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

Avatar of Maxwell Vandervelde, a Symfony contributor

Thanks Maxwell Vandervelde for being a Symfony contributor

1 commit • 18 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?
    • Symfony at a Glance
    • Symfony Components
    • Case Studies
    • 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
    • SymfonyConnect
    • Support
    • How to be Involved
    • Code of Conduct
    • Events & Meetups
    • Projects using Symfony
    • Downloads Stats
    • Contributors
    • Backers
  • Blog
    • Events & Meetups
    • A week of symfony
    • Case studies
    • Cloud
    • Community
    • Conferences
    • Diversity
    • Documentation
    • Living on the edge
    • Releases
    • Security Advisories
    • SymfonyInsight
    • Twig
    • SensioLabs
  • Services
    • SensioLabs services
    • Train developers
    • Manage your project quality
    • Improve your project performance
    • Host Symfony projects
    Deployed on
Follow Symfony
Search by Algolia