Mq.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace App\Http\Api;
  3. use PhpAmqpLib\Connection\AMQPStreamConnection;
  4. use PhpAmqpLib\Message\AMQPMessage;
  5. use PhpAmqpLib\Exchange\AMQPExchangeType;
  6. use Illuminate\Support\Facades\Log;
  7. class Mq{
  8. private static function connection(){
  9. $host = config("queue.connections.rabbitmq.host");
  10. $port = config("queue.connections.rabbitmq.port");
  11. $user = config("queue.connections.rabbitmq.user");
  12. $password = config("queue.connections.rabbitmq.password");
  13. $vhost = config("queue.connections.rabbitmq.password");
  14. if(empty($host) || empty($port) || empty($user) || empty($password) || empty($vhost)){
  15. Log::error('rabbitmq set error');
  16. return;
  17. }
  18. $connection = new AMQPStreamConnection($host,$port,$user,$password,$vhost);
  19. return $connection;
  20. }
  21. public static function publish(string $channelName, $message){
  22. //一对一
  23. try{
  24. $host = config("queue.connections.rabbitmq.host");
  25. $port = config("queue.connections.rabbitmq.port");
  26. $user = config("queue.connections.rabbitmq.user");
  27. $password = config("queue.connections.rabbitmq.password");
  28. $vhost = config("queue.connections.rabbitmq.virtual_host");
  29. if(empty($host) || empty($port) || empty($user) || empty($password) || empty($vhost)){
  30. Log::error('rabbitmq set error');
  31. return;
  32. }
  33. $connection = new AMQPStreamConnection($host,$port,$user,$password,$vhost);
  34. $channel = $connection->channel();
  35. $channel->queue_declare($channelName, false, true, false, false);
  36. $msg = new AMQPMessage(json_encode($message,JSON_UNESCAPED_UNICODE));
  37. $channel->basic_publish($msg, '', $channelName);
  38. $channel->close();
  39. $connection->close();
  40. }catch(\Exception $e){
  41. Log::error($e);
  42. return;
  43. }
  44. }
  45. /**
  46. * @param string $exchange
  47. * @param string $queue
  48. * @param callable|null $callback
  49. */
  50. public static function worker($exchange,$queue,$callback=null){
  51. $consumerTag = 'consumer';
  52. $host = config("queue.connections.rabbitmq.host");
  53. $port = config("queue.connections.rabbitmq.port");
  54. $user = config("queue.connections.rabbitmq.user");
  55. $password = config("queue.connections.rabbitmq.password");
  56. $vhost = config("queue.connections.rabbitmq.virtual_host");
  57. $connection = new AMQPStreamConnection($host,$port,$user,$password,$vhost);
  58. /*
  59. $connection = new AMQPStreamConnection(env("RABBITMQ_HOST"),
  60. env("RABBITMQ_PORT"),
  61. env("RABBITMQ_USER"),
  62. env("RABBITMQ_PASSWORD"),
  63. env("RABBITMQ_VIRTUAL_HOST"));
  64. */
  65. $channel = $connection->channel();
  66. /*
  67. The following code is the same both in the consumer and the producer.
  68. In this way we are sure we always have a queue to consume from and an
  69. exchange where to publish messages.
  70. */
  71. /*
  72. name: $queue
  73. passive: false
  74. durable: true // the queue will survive server restarts
  75. exclusive: false // the queue can be accessed in other channels
  76. auto_delete: false //the queue won't be deleted once the channel is closed.
  77. */
  78. $channel->queue_declare($queue, false, true, false, false);
  79. /*
  80. name: $exchange
  81. type: direct
  82. passive: false
  83. durable: true // the exchange will survive server restarts
  84. auto_delete: false //the exchange won't be deleted once the channel is closed.
  85. */
  86. $channel->exchange_declare($exchange, AMQPExchangeType::DIRECT, false, true, false);
  87. $channel->queue_bind($queue, $exchange);
  88. /**
  89. * @param \PhpAmqpLib\Message\AMQPMessage $message
  90. */
  91. $process_message = function ($message) use($callback,$connection,$exchange,$queue)
  92. {
  93. if($callback !== null){
  94. try{
  95. $result = $callback(json_decode($message->body));
  96. if($result !== 0){
  97. throw new \Exception('error');
  98. }
  99. }catch(\Exception $e){
  100. // push to issues
  101. $channelName = 'issues';
  102. $channelIssues = $connection->channel();
  103. $channelIssues->queue_declare($channelName, false, true, false, false);
  104. $msg = new AMQPMessage(json_encode(['exchange'=>$exchange,
  105. 'channel'=>$queue,
  106. 'message'=>json_decode($message->body),
  107. 'result'=>0,
  108. 'error'=>$e,
  109. ],JSON_UNESCAPED_UNICODE));
  110. $channelIssues->basic_publish($msg, '', $channelName);
  111. $channelIssues->close();
  112. }
  113. }
  114. $message->ack();
  115. // Send a message with the string "quit" to cancel the consumer.
  116. /*
  117. if ($message->body === 'quit') {
  118. $message->getChannel()->basic_cancel($message->getConsumerTag());
  119. }
  120. */
  121. };
  122. /*
  123. queue: Queue from where to get the messages
  124. consumer_tag: Consumer identifier
  125. no_local: Don't receive messages published by this consumer.
  126. no_ack: If set to true, automatic acknowledgement mode will be used by this consumer. See https://www.rabbitmq.com/confirms.html for details.
  127. exclusive: Request exclusive consumer access, meaning only this consumer can access the queue
  128. nowait:
  129. callback: A PHP Callback
  130. */
  131. $channel->basic_consume($queue, $consumerTag, false, false, false, false, $process_message);
  132. /**
  133. * @param \PhpAmqpLib\Channel\AMQPChannel $channel
  134. * @param \PhpAmqpLib\Connection\AbstractConnection $connection
  135. */
  136. $shutdown = function ($channel, $connection)
  137. {
  138. $channel->close();
  139. $connection->close();
  140. };
  141. register_shutdown_function($shutdown, $channel, $connection);
  142. // Loop as long as the channel has callbacks registered
  143. while ($channel->is_consuming()) {
  144. $channel->wait(null, true);
  145. // do something else
  146. usleep(300000);
  147. }
  148. }
  149. }