Amqp.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace components\service;
  3. use components\Exception;
  4. use PhpAmqpLib\Connection\AMQPStreamConnection;
  5. /**
  6. * Amqp 服务组件
  7. * Class Amqp
  8. * @package components\service
  9. */
  10. class Amqp extends AmqpConfig
  11. {
  12. /* 以下即将废弃:因为使用跟踪不方便 */
  13. /**
  14. * [获取连接]
  15. * @author: libingke
  16. * @return AMQPStreamConnection
  17. */
  18. public function getConnect()
  19. {
  20. $conn = new \PhpAmqpLib\Connection\AMQPStreamConnection(
  21. $this->host,
  22. $this->port,
  23. $this->user,
  24. $this->pass,
  25. $this->vhost
  26. );
  27. return $conn;//->channel()->queue_declare();
  28. }
  29. /**
  30. * [php拓展自带的连接]
  31. * @author: libingke
  32. * @return \AMQPConnection
  33. */
  34. public function AMQPConnection()
  35. {
  36. $config = [
  37. 'host' => $this->host,
  38. 'port' => $this->port,
  39. 'login'=> $this->user,
  40. 'password' => $this->pass,
  41. 'vhost' => $this->vhost
  42. ];
  43. $conn = new \AMQPConnection($config);
  44. if (!$conn->connect())
  45. throw new Exception(2000);
  46. return $conn;
  47. }
  48. /**
  49. * @param string $key
  50. * @return mixed
  51. */
  52. public function getConfig($key = '')
  53. {
  54. return is_string($key) && $key && isset($this->$key) ?
  55. $this->$key : json_decode(json_encode($this), true);
  56. }
  57. }