'127.0.0.1','port'=>'5672','login'=>'guest','password'=>'guest','vhost'=>'/') ; private $channel; private $e_name = 'glitzcloud'; public function __construct(){ //创建Exchannel $this->channel = $this->connrabbit($this->conn_args); if(!$this->channel){ echo 'Cannot Connect to the broker'; exit(); } } /* *创建RabbitMq Message */ public function setRabbit($message,$k_route,$e_name=''){ if(empty($e_name)){ $e_name = $this->e_name; } $channel = $this->channel; $res1=$channel->queue_declare($e_name, false, true, false, false);//声明 $msg = new AMQPMessage($message, array( 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT, // 'correlation_id' => 1111111111, // 'reply_to' => 'show' ) ); $res= $channel->basic_publish($msg,'yang',$e_name); $channel->close(); return true; } /* *获取RabbitMq Message */ public function getRabbit($k_route,$q_name){ //1.权限问题 //2.获取方法没有 if(empty($e_name)){ $e_name = $this->e_name; } $channel = $this->channel; // $channel->queue_declare($e_name, false, true, false, false);//声明 // $this->channel->queue_declare($e_name,true, true); // $channel->exchange_declare('yang', '', false, true, false); // $channel->queue_bind('', '',''); // $channel->basic_get(); // $messages = $channel->get(AMQP_AUTOACK); // if(is_object( $messages)) // return $messages->getBody(); // return $messages; } /** *链接RabbitMQ */ protected function connrabbit($conn_args){ $conn = new AMQPStreamConnection( \Yii::$app->params['rabbithost'], self::PORT, self::USER, self::PASS); //建立连接 if(!$conn->connectOnConstruct()){ return false; } $channel = new AMQPChannel($conn); return $channel; } }