visuddhinanda 3 lat temu
rodzic
commit
d2b37dd20f

+ 11 - 3
app/Console/Commands/TestMq.php

@@ -40,16 +40,24 @@ class TestMq extends Command
      */
     public function handle()
     {
-		$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
+        //一对一
+		$connection = new AMQPStreamConnection(MQ_HOST, MQ_PORT, MQ_USERNAME, MQ_PASSWORD);
 		$channel = $connection->channel();
-		$channel->queue_declare('hello', false, false, false, false);
+		$channel->queue_declare('hello', false, true, false, false);
 
 		$msg = new AMQPMessage('Hello World!');
 		$channel->basic_publish($msg, '', 'hello');
-		
+
 		echo " [x] Sent 'Hello World!'\n";
 		$channel->close();
 		$connection->close();
+
+        //一对多
+        $connection = new AMQPStreamConnection(MQ_HOST, MQ_PORT, MQ_USERNAME, MQ_PASSWORD);
+        $channel->exchange_declare('hello_exchange','fanout',false,true);
+        $channel->queue_declare('hello', false, true, false, false);
+        $channel->exchange_bind('hello','exchange',"");
+
         return 0;
     }
 }

+ 4 - 4
app/Console/Commands/TestMqWorker.php

@@ -39,19 +39,19 @@ class TestMqWorker extends Command
      */
     public function handle()
     {
-		$connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
+		$connection = new AMQPStreamConnection(MQ_HOST, MQ_PORT, MQ_USERNAME, MQ_PASSWORD);
 		$channel = $connection->channel();
 
-		$channel->queue_declare('hello', false, false, false, false);
+		$channel->queue_declare('hello', false, true, false, false);
 
 		echo " [*] Waiting for messages. To exit press CTRL+C\n";
 
 		$callback = function ($msg) {
 			echo ' [x] Received ', $msg->body, "\n";
 		  };
-		  
+
 		$channel->basic_consume('hello', '', false, true, false, false, $callback);
-		  
+
 		while ($channel->is_open()) {
 			  $channel->wait();
 		  }