rabbitMQ.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * rabbitmq的配置
  4. */
  5. $rabbitMQ = [
  6. /*'on before_consume' => function ($event) {
  7. if (isset(\Yii::$app->db)) {
  8. $db = \Yii::$app->db;
  9. if ($db->getIsActive()) {
  10. $db->close();
  11. }
  12. $db->open();
  13. }
  14. },
  15. 'logger' => [
  16. 'enable' => true,
  17. 'category' => 'amqp',
  18. 'print_console' => true,
  19. ],*/
  20. 'class' => 'mikemadisonweb\rabbitmq\Configuration',
  21. 'connections' => [
  22. [
  23. 'host' => getenv('RABBIT_MQ_HOST'),
  24. 'port' => getenv('RABBIT_MQ_PORT'),
  25. 'user' => 'guest',
  26. 'password' => 'guest',
  27. 'vhost' => '/', //虚拟主机
  28. ]
  29. ],
  30. /**
  31. * 交换器
  32. * 交换类型(type):direct、topic、headers、fanout
  33. * 列出交换器: rabbitmqctl list_exchanges
  34. */
  35. 'exchanges' => [
  36. [
  37. 'name' => 'exCommon',
  38. 'type' => 'direct' // direct
  39. ],
  40. [
  41. 'name' => 'exInform',
  42. 'type' => 'direct' // direct
  43. ],
  44. /*
  45. [
  46. 'name' => 'topic_ex_1',
  47. 'type' => 'topic', // topic
  48. 'passive' => false,
  49. 'durable' => true,
  50. 'auto_delete' => false,
  51. 'internal' => false,
  52. 'nowait' => false,
  53. 'arguments' => null,
  54. 'ticket' => null,
  55. 'declare' => true,
  56. ],
  57. */
  58. ],
  59. /**
  60. * 队列
  61. */
  62. 'queues' => [
  63. [
  64. 'name' => 'queueCommon',
  65. 'passive' => false,
  66. ],
  67. [
  68. 'name' => 'queueInform',
  69. 'passive' => false,
  70. ],
  71. /*
  72. [
  73. 'name' => 'queue_2',
  74. 'passive' => false,
  75. 'durable' => true,
  76. 'exclusive' => false,
  77. 'auto_delete' => false,
  78. 'nowait' => false,
  79. 'arguments' => null,
  80. 'ticket' => null,
  81. 'declare' => true,
  82. ],
  83. [
  84. 'name' => 'queue_3',
  85. 'passive' => false,
  86. 'durable' => true,
  87. 'exclusive' => false,
  88. 'auto_delete' => false,
  89. 'nowait' => false,
  90. 'arguments' => null,
  91. 'ticket' => null,
  92. 'declare' => true,
  93. ],
  94. */
  95. ],
  96. /**
  97. * 交换和队列之间的关系称为绑定
  98. * 列出绑定: rabbitmqctl list_bindings
  99. */
  100. 'bindings' => [
  101. [
  102. 'queue' => 'queueCommon',
  103. 'exchange' => 'exCommon',
  104. 'routing_keys' => ['routeCommon'],
  105. ],
  106. [
  107. 'queue' => 'queueInform',
  108. 'exchange' => 'exInform',
  109. 'routing_keys' => ['routeInform'],
  110. ],
  111. /*
  112. [
  113. 'queue' => 'queue_2',
  114. 'exchange' => 'topic_ex_1',
  115. 'routing_keys' => ['route_1', 'route_prefix.*', '*.route_suffix', '*.*.last', '*'],
  116. ],
  117. [
  118. 'queue' => 'queue_3',
  119. 'exchange' => 'topic_ex_1',
  120. 'routing_keys' => ['route_3', 'route3_prefix.*', '*.route_suffix', '*.*.last'],
  121. ],
  122. */
  123. ],
  124. /**
  125. * 生产者
  126. */
  127. 'producers' => [
  128. [
  129. //常规生产者
  130. 'name' => 'producerCommon',
  131. ],
  132. [
  133. //通知模块生产者
  134. 'name' => 'producerInform',
  135. ],
  136. /*
  137. [
  138. 'name' => 'producer_2',
  139. 'connection' => 'default',
  140. 'safe' => true,
  141. 'content_type' => 'text/plain',
  142. 'delivery_mode' => 2,
  143. 'serializer' => 'serialize',
  144. ]
  145. */
  146. ],
  147. /**
  148. * 消费者
  149. */
  150. 'consumers' => [
  151. [
  152. //常规消费者
  153. 'name' => 'consumerCommon',
  154. 'callbacks' => [
  155. 'queueCommon' => '\common\components\rabbitmq\consumer\ImportDataConsumer', //设定队列的数据给谁转回调而以,只是个配置
  156. ],
  157. ],
  158. [
  159. //通知模块消费者
  160. 'name' => 'consumerInform',
  161. 'callbacks' => [
  162. 'queueInform' => '\common\components\rabbitmq\consumer\InformConsumer', //设定队列的数据给谁转回调而以,只是个配置
  163. ],
  164. ],
  165. /*
  166. [
  167. 'name' => 'consumer_2',
  168. 'connection' => 'default',
  169. 'callbacks' => [
  170. 'queue_2' => '\common\components\rabbitmq\consumer\PrintMsgConsumer',
  171. ],
  172. 'qos' => [
  173. 'prefetch_size' => 0,
  174. 'prefetch_count' => 0,
  175. 'global' => false,
  176. ],
  177. 'idle_timeout' => 0,
  178. 'idle_timeout_exit_code' => null,
  179. 'proceed_on_exception' => false,
  180. 'deserializer' => 'unserialize',
  181. ],
  182. */
  183. ],
  184. ];
  185. return $rabbitMQ;