Răsfoiți Sursa

rabbitmq 队列补全

shishao-home 6 ani în urmă
părinte
comite
0146e4e895

+ 9 - 2
common/components/rabbitmq/consumer/PrintMsgConsumer.php

@@ -23,11 +23,18 @@ class PrintMsgConsumer implements ConsumerInterface
 		print_r($data);
         print_r('PrintMsgConsumer done! ');
 
-		return ConsumerInterface::MSG_ACK;
+        if($data['status'] == 200){
+            print_r('suceess ');
+            return ConsumerInterface::MSG_ACK;
+        }elseif($data['status'] == 500){
+            sleep(5);
+            print_r('faile ');
+            return ConsumerInterface::MSG_REQUEUE;
+        }
 		/**
          * ConsumerInterface::MSG_ACK - Acknowledge message (mark as processed) and drop it from the queue
          * ConsumerInterface::MSG_REJECT - Reject and drop message from the queue
-         * ConsumerInterface::MSG_REJECT_REQUEUE - Reject and requeue message in RabbitMQ
+         * ConsumerInterface::MSG_REQUEUE - Reject and requeue message in RabbitMQ
          */
 	}
 

+ 26 - 14
common/config/rabbitmq.php

@@ -1,5 +1,4 @@
 <?php
-use mikemadisonweb\rabbitmq\Configuration;
 /**
  * rabbitmq的配置
  */
@@ -37,12 +36,12 @@ $rabbitmq = [
      */
     'exchanges' => [
         [
-            'name' => 'ex1',
-            'type' => 'direct'
+            'name' => 'ex_1',
+            'type' => 'direct' // direct
         ],
         [
-            'name' => 'ex2',
-            'type' => 'topic',
+            'name' => 'topic_ex_1',
+            'type' => 'topic', // topic
             'passive' => false,
             'durable' => true,
             'auto_delete' => false,
@@ -61,9 +60,6 @@ $rabbitmq = [
         [
             'name' => 'queue_1',
             'passive' => false,
-            // Queue can be configured here the way you want it:
-            //'durable' => true,
-            //'auto_delete' => false,
         ],
         [
             'name' => 'queue_2',
@@ -76,6 +72,17 @@ $rabbitmq = [
             'ticket' => null,
             'declare' => true,
         ],
+        [
+            'name' => 'queue_3',
+            'passive' => false,
+            'durable' => true,
+            'exclusive' => false,
+            'auto_delete' => false,
+            'nowait' => false,
+            'arguments' => null,
+            'ticket' => null,
+            'declare' => true,
+        ],
     ],
 
     /**
@@ -85,13 +92,18 @@ $rabbitmq = [
     'bindings' => [
         [
             'queue' => 'queue_1',
-            'exchange' => 'ex1',
-            'routing_keys' => ['route1'],
+            'exchange' => 'ex_1',
+            'routing_keys' => ['route_1'],
         ],
         [
             'queue' => 'queue_2',
-            'exchange' => 'ex2',
-            'routing_keys' => ['route1', 'route2'],
+            'exchange' => 'topic_ex_1',
+            'routing_keys' => ['route_1', 'route_prefix.*', '*.route_suffix', '*.*.last', '*'],
+        ],
+        [
+            'queue' => 'queue_3',
+            'exchange' => 'topic_ex_1',
+            'routing_keys' => ['route_3', 'route3_prefix.*', '*.route_suffix', '*.*.last'],
         ],
     ],
 
@@ -104,7 +116,7 @@ $rabbitmq = [
         ],
         [
             'name' => 'producer_2',
-            'connection' => Configuration::DEFAULT_CONNECTION_NAME,
+            'connection' => 'default',
             'safe' => true,
             'content_type' => 'text/plain',
             'delivery_mode' => 2,
@@ -124,7 +136,7 @@ $rabbitmq = [
         ],
         [
             'name' => 'consumer_2',
-            'connection' => Configuration::DEFAULT_CONNECTION_NAME,
+            'connection' => 'default',
             'callbacks' => [
                 'queue_2' => '\common\components\rabbitmq\consumer\PrintMsgConsumer',
             ],

+ 15 - 4
console/controllers/RabbitmqTestController.php

@@ -8,27 +8,38 @@ use yii\console\Controller;
 class RabbitmqTestController extends Controller
 {
     /**
+     * 消息生产者
      * @param $producerName 生产者名称 (producer.name) -- 从 config/rabbitmq.php 找
      * @param $exchangeName 交换器名称 (exchanges.name)
      * @param $routingKey 绑定路由键(bindings.routing_keys)
+     * @param $status 模拟执行状态 -- 200:成功 500:失败
+     *
+     * 例子:
+     * ./yii rabbitmq-test/publish
+     * ./yii rabbitmq-test/publish producer_2 ex_1 route_1
+     * ./yii rabbitmq-test/publish producer_1 topic_ex_1 route_prefix.*
+     * ./yii rabbitmq-test/publish producer_1 topic_ex_1 *.route_suffix
      */
-    public function actionPublish($producerName = '', $exchangeName = '', $routingKey = '')
+    public function actionPublish($producerName = '', $exchangeName = '', $routingKey = '', $status = 200)
     {
         if(empty($producerName)){
             $producerName = 'producer_1';
         }
         if(empty($exchangeName)){
-            $exchangeName = 'ex1';
+            $exchangeName = 'ex_1';
         }
         if(empty($routingKey)){
-            $routingKey = 'route1';
+            $routingKey = 'route_1';
         }
 
         $producer = \Yii::$app->rabbitmq->getProducer($producerName);
-        $data = ['id' => 123, 'status' => 200, ['producer'=>$producerName, 'exchange'=>$exchangeName, 'routingKey'=>$routingKey]];
+        $data = ['id' => 123, 'status' => $status, ['producer'=>$producerName, 'exchange'=>$exchangeName, 'routingKey'=>$routingKey]];
         $msg = serialize($data);
         $producer->publish($msg, $exchangeName, $routingKey);
     }
 
+    public function actionPublishtopic()
+    {
 
+    }
 }

+ 3 - 0
vendor/workerman/GatewayWorker/vendor/workerman/workerman.log

@@ -0,0 +1,3 @@
+2019-12-31 15:00:32 pid:5021 Workerman[start.php] start in DEBUG mode
+2019-12-31 15:00:54 pid:5021 Workerman[start.php] stopping ...
+2019-12-31 15:00:54 pid:5021 Workerman[start.php] has been stopped

+ 31 - 0
相关文档/rabbitMq.md

@@ -0,0 +1,31 @@
+#rrabbitMq 文档
+
+配置文件目录:
+`common/config/rabbitmq.php`
+
+rabbitMq 后台:
+http://127.0.0.1:15672
+帐号:guest
+密码:guest
+
+
+### 测试
+mikemadisonweb/yii2-rabbitmq 自带的命令:
+https://github.com/mikemadisonweb/yii2-rabbitmq#console-commands
+
+`rabbitmq/consume` - 运行一个消费者
+ ./yii rabbitmq/consume consumer_1
+
+`rabbitmq/publish` - 执行一生产者(从标准输入中获取输入值)
+echo 'a:3:{s:2:"id";i:123;s:6:"status";i:200;i:0;a:3:{s:8:"producer";s:10:"producer_2";s:8:"exchange";s:4:"ex_1";s:10:"routingKey";s:7:"route_1";}}' | ./yii rabbitmq/publish producer_1 ex_1 route_1
+
+### 自定义的生产者
+`rabbitmq-test/publish` - 运行一个生产者
+
+./yii rabbitmq-test/publish
+
+ ./yii rabbitmq-test/publish producer_2 ex_1 route_1
+ 
+ ./yii rabbitmq-test/publish producer_1 topic_ex_1 route_prefix.*
+ 
+ ./yii rabbitmq-test/publish producer_1 topic_ex_1 *.route_suffix