PHP操作RabbitMQ消息接收不到的問題【cannot publish to internal exchange 'push-apns' in vhost 'pushHost】

1:安裝和基本概念過程不表 【erlang和rabbitMQ都是用源碼安裝的】

wget  http://www.erlang.org/download/otp_src_17.3.tar.gz

記住安裝 [ yum install -y xmlto ]

http://www.yuansir-web.com/2013/05/31/rabbitmq%E5%AE%98%E6%96%B9%E4%B8%AD%E6%96%87%E5%85%A5%E9%97%A8%E6%95%99%E7%A8%8Bphp%E7%89%88-%E7%AC%AC%E4%BA%8C%E9%83%A8%E5%88%86%E5%B7%A5%E4%BD%9C%E9%98%9F%E5%88%97%EF%BC%88work-queues%EF%BC%89/


2:  消費者代碼 receive.php

  [這裏是我自己新建了一個自己的vhost,和自定義用戶

$rabbitmqctl  add_user pushServer  123456  //新建用戶

 $rabbitmqctl  add_vhost  pushHost                  //新建vhost

 $rabbitmqctl  set_permissions -p pushHost pushServer ".*" ".*" ".*"  //給用戶設置權限

]

#!/usr/local/php/bin/php -q
<?php

	$config			=	array('host'=>'localhost','port'=>'5672',
									'login'=>'pushServer',
									'password'=>'123456',
									'vhost'=>'pushHost');
	
	$exchange_name          =	<strong><span style="font-size:18px;color:#cc0000;">'push-apns';</span></strong>
	$queue_name		=	'push-q-1';
	$route_key		=	'push-r-1';
		
 
		function processMessage($envelope, $queue) {
			$msg = $envelope->getBody();
			$envelopeID = $envelope->getDeliveryTag();
			$pid = posix_getpid();
			
			$logStr	=	$msg.'|'.$envelopeID.''."\r\n";
			echo $logStr	;
			file_put_contents("./log{$pid}.log",$logStr	 ,FILE_APPEND);
			$queue->ack($envelopeID);
		}
 


	$conn 	=	new AMQPConnection ($config);
	
	$conn->connect() or die(" connect to BROKER");
	
	$channel =   new AMQPChannel($conn);
	
	$exchange 	= new AMQPExchange($channel);
	
	$exchange->setName($exchange_name);
	 
	$exchange->setType(AMQP_EX_TYPE_DIRECT);

	$exchange->declare();
	
	$queue	=	 new AMQPQueue($channel);
	
	$queue->setName($queue_name);
	
	$queue->setFlags(AMQP_DURABLE);
	
	$ret 	=	$queue->declare();
	var_dump($ret);
	$queue->bind($exchange_name,$route_key);
 
	var_dump('[*] Waiting for messages. To exit press CTRL+C');
	$I = 0;
	while (TRUE) {
			ECHO "$I ".PHP_EOL;
			$retC = $queue->consume('processMessage');
			var_dump($retC);
	} 
	$conn->disconnect();
?>

3:生產者代碼 send.php

#!/usr/local/php/bin/php -q
<?php

	$config			=	array('host'=>'localhost','port'=>'5672',
									'login'=>'pushServer',
									'password'=>'123456',
									'vhost'=>'pushHost');
	
	$exchange_name	<span style="white-space:pre">	</span>=	<strong><span style="color:#cc0000;">'push-<span style="font-family: Arial, Helvetica, sans-serif;">apns</span>';</span></strong>
	$queue_name		=	'push-q-1';
	$route_key		=	'push-r-1';
		
	$conn 	=	new AMQPConnection ($config);
	
	$conn->connect() or die(" connect to BROKER");
	
	$channel =   new AMQPChannel($conn);
	
	$exchange 	= new AMQPExchange($channel);
	
	$exchange->setName($exchange_name);
	
	$exchange->setType(AMQP_EX_TYPE_DIRECT);
	
	$queue	=	 new AMQPQueue($channel);
	
	$queue->setName($queue_name);
	
	$queue->setFlags(AMQP_DURABLE);
	
	$queue->declare();
	$i=0;
	$start = microtime(true);
	while($i++ < 100000){
		$s =$exchange->publish("assssss",$route_key);;
	}
	echo microtime(true)-$start;
	$conn->disconnect();
	
    
	$data	=	"func you";
	// $s = $MQ->send($data);
	var_dump($s );
?><strong>
</strong>

4::運行 receive.php 程序阻塞等待數據的來領

5:運行 send.php 發生數據,可是消費者沒有看到數據啊 ,爲毛啊······

看日誌 cat /var/lib/rabbitmq/mnesia/rabbit@host

"cannot publish to internal exchange 'push-apns' in vhost 'pushHost'",

臥槽,網上大家都是這麼寫的啊【php + amqp】,於是去Google一番··············

沒找到,算了  去 rabbitMQ的自帶管理後臺看看 Exchange信息



看到features居然是I,就是internal的意思,自己在代碼裏面declare的exchange就默認是  internal的?? 毛線啊


好吧我用用下面的 Add a new exchange 添加了一個新的 exchange【名字叫push-msg,type選中爲direct】, Internal就是默認是NO了,修改上面代碼的 

$exchange_name = ‘push-msg’;


再運行上面的 receive.php 和send.php 可以了 ,RabbitmQ在我 本機測試  19.5分鐘可以發送 1000W數據 【單個消費者,和生產者】


另外的 AMQP的php代碼裏面也沒有說  AMQPExchange 可以聲明爲 internal FALSE的參數或者函數  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章