12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- <?php
- namespace components\service;
- use components\Exception;
- use PhpAmqpLib\Connection\AMQPStreamConnection;
- /**
- * Amqp 服务组件
- * Class Amqp
- * @package components\service
- */
- class Amqp extends AmqpConfig
- {
- /* 以下即将废弃:因为使用跟踪不方便 */
- /**
- * [获取连接]
- * @author: libingke
- * @return AMQPStreamConnection
- */
- public function getConnect()
- {
- $conn = new \PhpAmqpLib\Connection\AMQPStreamConnection(
- $this->host,
- $this->port,
- $this->user,
- $this->pass,
- $this->vhost
- );
- return $conn;//->channel()->queue_declare();
- }
- /**
- * [php拓展自带的连接]
- * @author: libingke
- * @return \AMQPConnection
- */
- public function AMQPConnection()
- {
- $config = [
- 'host' => $this->host,
- 'port' => $this->port,
- 'login'=> $this->user,
- 'password' => $this->pass,
- 'vhost' => $this->vhost
- ];
- $conn = new \AMQPConnection($config);
- if (!$conn->connect())
- throw new Exception(2000);
- return $conn;
- }
- /**
- * @param string $key
- * @return mixed
- */
- public function getConfig($key = '')
- {
- return is_string($key) && $key && isset($this->$key) ?
- $this->$key : json_decode(json_encode($this), true);
- }
- }
|