21/02/2016 - RABBITMQ, SYMFONY
Use these RabbitMQ topic exchange configurations if you're using RabbitMqBundle for symfony application. If you're using more than one consumers in your configs, consider using multiple_consumers config option instead. I have an example post somewhere that you can find and have a look into.
When producing messages to a queue with $this->producer->publish(json_encode($message, 'xxxxx');
method, remember to replace xxxxx
(routing_key) parameters with routing_key values that you defined in config files. Example: create_order, bmw, audi, mercedes. Also, in the image below, RK stands for "routing_key" and BK stands for "binding_key".
*
and #
signs.#
sign without words in the case of topic exchange, topic exchange will behave like a fanout exchange and deliver messages randomly.After creating or updating queues/exchanges configs, you should always run $ bin/console rabbitmq:setup-fabric
so that all queues/exchanges from your symfony config are synchronized with rabbitmq server otherwise published messages won't reach queues as they won't exist.
All order "create" messages would go to single queue.
# You can run many workers like commands below.
$ app/console rabbitmq:consumer -m 100 order_create
$ app/console rabbitmq:consumer -m 100 order_create
old_sound_rabbit_mq:
connections:
default:
host: 127.0.0.1
port: 5672
user: guest
password: guest
vhost: /
lazy: true
producers:
order_create:
connection: default
exchange_options: { name: 'order_create_ex', type: topic }
consumers:
order_create:
connection: default
exchange_options: { name: 'order_create_ex', type: topic }
queue_options: { name: 'order_create_qu' }
callback: application_frontend.consumer.order_create
In this example, "bmw", "audi" and "mercedes" messages will be coming from a single producer but "bmw" orders get treated differently compared to "audi" and "mercedes" orders which get treated in same way.
# You can run many workers like commands below.
$ app/console rabbitmq:consumer -m 100 order_create_bmw
$ app/console rabbitmq:consumer -m 100 order_create_bmw
$ app/console rabbitmq:consumer -m 100 order_create_audi_mercedes
$ app/console rabbitmq:consumer -m 100 order_create_audi_mercedes
old_sound_rabbit_mq:
connections:
default:
host: 127.0.0.1
port: 5672
user: guest
password: guest
vhost: /
lazy: true
producers:
order_create:
connection: default
exchange_options: { name: order_create_ex, type: topic }
consumers:
order_create_bmw:
connection: default
exchange_options: { name: order_create_ex, type: topic }
queue_options:
name: order_create_bmw_qu
routing_keys:
- 'bmw.#'
callback: application_frontend.consumer.order_create_bmw
order_create_audi_mercedes:
connection: default
exchange_options: { name: order_create_ex, type: topic }
queue_options:
name: order_create_audi_mercedes_qu
routing_keys:
- 'audi.*'
- '#.mercedes.*'
callback: application_frontend.consumer.order_create_audi_mercedes
In this example, "bmw", "audi" and "mercedes" messages will be coming from two different producers. Producer X "bmw" messages get treated differently compared to the producer Y "audi" and "mercedes" messages which get treated in same way.
# You can run many workers like commands below.
$ app/console rabbitmq:consumer -m 100 order_create_bmw
$ app/console rabbitmq:consumer -m 100 order_create_bmw
$ app/console rabbitmq:consumer -m 100 order_create_audi_mercedes
$ app/console rabbitmq:consumer -m 100 order_create_audi_mercedes
old_sound_rabbit_mq:
connections:
default:
host: 127.0.0.1
port: 5672
user: guest
password: guest
vhost: /
lazy: true
producers:
order_create_bmw:
connection: default
exchange_options: { name: order_create_ex, type: topic }
queue_options:
name: order_create_bmw_qu
routing_keys:
- 'bmw.#'
order_create_audi_mercedes:
connection: default
exchange_options: { name: order_create_ex, type: topic }
queue_options:
name: order_create_audi_mercedes_qu
routing_keys:
- 'audi.*'
- '#.mercedes.*'
consumers:
order_create_bmw:
connection: default
exchange_options: { name: order_create_ex, type: topic }
queue_options:
name: order_create_bmw_qu
routing_keys:
- 'bmw.#'
callback: application_frontend.consumer.order_create_bmw
order_create_audi_mercedes:
connection: default
exchange_options: { name: order_create_ex, type: topic }
queue_options:
name: order_create_audi_mercedes_qu
routing_keys:
- 'audi.*'
- '#.mercedes.*'
callback: application_frontend.consumer.order_create_audi_mercedes