TestMq.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use PhpAmqpLib\Connection\AMQPStreamConnection;
  5. use PhpAmqpLib\Message\AMQPMessage;
  6. class TestMq extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'test:mq ';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'Command description';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return int
  33. */
  34. public function handle()
  35. {
  36. //一对一
  37. $connection = new AMQPStreamConnection(MQ_HOST, MQ_PORT, MQ_USERNAME, MQ_PASSWORD);
  38. $channel = $connection->channel();
  39. $channel->queue_declare('hello', false, true, false, false);
  40. $msg = new AMQPMessage('Hello World!');
  41. $channel->basic_publish($msg, '', 'hello');
  42. echo " [x] Sent 'Hello World!'\n";
  43. $channel->close();
  44. $connection->close();
  45. //一对多
  46. $connection = new AMQPStreamConnection(MQ_HOST, MQ_PORT, MQ_USERNAME, MQ_PASSWORD);
  47. $channel->exchange_declare('hello_exchange','fanout',false,true);
  48. $channel->queue_declare('hello', false, true, false, false);
  49. $channel->exchange_bind('hello','exchange',"");
  50. return 0;
  51. }
  52. }