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

1:安装和基本概念过程不表 【erlang和rabbitMQ都是用源码安装的】

wget https://www.rabbitmq.com/releases/rabbitmq-server/v3.4.1/rabbitmq-server-3.4.1.tar.gz
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信息


PHP操作RabbitMQ消息接收不到的问题【cannot publish to internal exchange 'push-apns' in vhost 'pushHost】_第1张图片

看到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的参数或者函数  

你可能感兴趣的:(PHP,linux,rabbitmq)