|
|
@@ -0,0 +1,144 @@
|
|
|
+<?php
|
|
|
+use mikemadisonweb\rabbitmq\Configuration;
|
|
|
+/**
|
|
|
+ * rabbitmq的配置
|
|
|
+ */
|
|
|
+
|
|
|
+$rabbitmq = [
|
|
|
+ /*'on before_consume' => function ($event) {
|
|
|
+ if (isset(\Yii::$app->db)) {
|
|
|
+ $db = \Yii::$app->db;
|
|
|
+ if ($db->getIsActive()) {
|
|
|
+ $db->close();
|
|
|
+ }
|
|
|
+ $db->open();
|
|
|
+ }
|
|
|
+ },
|
|
|
+ 'logger' => [
|
|
|
+ 'enable' => true,
|
|
|
+ 'category' => 'amqp',
|
|
|
+ 'print_console' => true,
|
|
|
+ ],*/
|
|
|
+ 'class' => 'mikemadisonweb\rabbitmq\Configuration',
|
|
|
+ 'connections' => [
|
|
|
+ [
|
|
|
+ 'host' => '127.0.0.1',
|
|
|
+ 'port' => '5672',
|
|
|
+ 'user' => 'guest',
|
|
|
+ 'password' => 'guest',
|
|
|
+ 'vhost' => '/', //虚拟主机
|
|
|
+ ]
|
|
|
+ ],
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 交换器
|
|
|
+ * 交换类型(type):direct、topic、headers、fanout
|
|
|
+ * 列出交换器: rabbitmqctl list_exchanges
|
|
|
+ */
|
|
|
+ 'exchanges' => [
|
|
|
+ [
|
|
|
+ 'name' => 'ex1',
|
|
|
+ 'type' => 'direct'
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ 'name' => 'ex2',
|
|
|
+ 'type' => 'topic',
|
|
|
+ 'passive' => false,
|
|
|
+ 'durable' => true,
|
|
|
+ 'auto_delete' => false,
|
|
|
+ 'internal' => false,
|
|
|
+ 'nowait' => false,
|
|
|
+ 'arguments' => null,
|
|
|
+ 'ticket' => null,
|
|
|
+ 'declare' => true,
|
|
|
+ ],
|
|
|
+ ],
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 队列
|
|
|
+ */
|
|
|
+ 'queues' => [
|
|
|
+ [
|
|
|
+ 'name' => 'queue_1',
|
|
|
+ 'passive' => false,
|
|
|
+ // Queue can be configured here the way you want it:
|
|
|
+ //'durable' => true,
|
|
|
+ //'auto_delete' => false,
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ 'name' => 'queue_2',
|
|
|
+ 'passive' => false,
|
|
|
+ 'durable' => true,
|
|
|
+ 'exclusive' => false,
|
|
|
+ 'auto_delete' => false,
|
|
|
+ 'nowait' => false,
|
|
|
+ 'arguments' => null,
|
|
|
+ 'ticket' => null,
|
|
|
+ 'declare' => true,
|
|
|
+ ],
|
|
|
+ ],
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 交换和队列之间的关系称为绑定
|
|
|
+ * 列出绑定: rabbitmqctl list_bindings
|
|
|
+ */
|
|
|
+ 'bindings' => [
|
|
|
+ [
|
|
|
+ 'queue' => 'queue_1',
|
|
|
+ 'exchange' => 'ex1',
|
|
|
+ 'routing_keys' => ['route1'],
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ 'queue' => 'queue_2',
|
|
|
+ 'exchange' => 'ex2',
|
|
|
+ 'routing_keys' => ['route1', 'route2'],
|
|
|
+ ],
|
|
|
+ ],
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生产者
|
|
|
+ */
|
|
|
+ 'producers' => [
|
|
|
+ [
|
|
|
+ 'name' => 'producer_1',
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ 'name' => 'producer_2',
|
|
|
+ 'connection' => Configuration::DEFAULT_CONNECTION_NAME,
|
|
|
+ 'safe' => true,
|
|
|
+ 'content_type' => 'text/plain',
|
|
|
+ 'delivery_mode' => 2,
|
|
|
+ 'serializer' => 'serialize',
|
|
|
+ ]
|
|
|
+ ],
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 消费者
|
|
|
+ */
|
|
|
+ 'consumers' => [
|
|
|
+ [
|
|
|
+ 'name' => 'consumer_1',
|
|
|
+ 'callbacks' => [
|
|
|
+ 'queue_1' => '\common\components\rabbitmq\consumer\ImportDataConsumer', //设定队列的数据给谁转回调而以,只是个配置
|
|
|
+ ],
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ 'name' => 'consumer_2',
|
|
|
+ 'connection' => Configuration::DEFAULT_CONNECTION_NAME,
|
|
|
+ 'callbacks' => [
|
|
|
+ 'queue_2' => '\common\components\rabbitmq\consumer\PrintMsgConsumer',
|
|
|
+ ],
|
|
|
+ 'qos' => [
|
|
|
+ 'prefetch_size' => 0,
|
|
|
+ 'prefetch_count' => 0,
|
|
|
+ 'global' => false,
|
|
|
+ ],
|
|
|
+ 'idle_timeout' => 0,
|
|
|
+ 'idle_timeout_exit_code' => null,
|
|
|
+ 'proceed_on_exception' => false,
|
|
|
+ 'deserializer' => 'unserialize',
|
|
|
+ ],
|
|
|
+ ],
|
|
|
+];
|
|
|
+
|
|
|
+return $rabbitmq;
|