rabbitMq 常见问题

1.Server channel error: 406, message: PRECONDITION_FAILED - inequivalent arg 'durable' for exchange 'delay-lxw' in vhost '/': received 'false' but current is 'true'

这是由于服务端和客户端设置的交换机类型不一致所致

rabbitmq 的exchange有以下几种类型:

  • fanout类型 (广播)

  • direct类型 (严格指定)

  • topic类型 (宽松指定)

  • headers类型

2.rabbitmq系列问题解决:406, "PRECONDITION_FAILED - inequivalent arg 'durable'

 错误信息翻译过来大致意思就是:当前的queue的durable属性是true,但是我们声明queue时过去的参数确实是false.

 这个错误造成的原因是现在页面创建了queue和exchanges,但是代码中声明要使用的queue时,durable属性传递的是false.

要注意的是,生产者和消费者对queue的声明函数里,这个durable必须保持一致。

$exchange->setFlags(AMQP_DURABLE); //添加这行

参考文章:https://www.cnblogs.com/zhaohuaxishi/p/12107392.html

完整实例(生产者)

//推送消息到Mq
	public function pushTaskMq()
	{
		//$exchangeName = 'AMQP default'; //交换机名
		//$routeKey = 'delay-lxw-333'; //路由key
		$exchangeName = 'task-ems'; //交换机名
		$routeKey = 'delay-lxw'; //路由key
		$message = 'Hello World 123!'; //消息内容

		// 建立TCP连接,配置信息
		$connection = new AMQPConnection([
			'host' => 'localhost',
			'port' => '5672',
			'vhost' => '/',
			'login' => 'guest',
			'password' => 'guest',
			'heartbeat' => 30,
		]);

		//var_dump($connection->connect());
		//创建链接
		$connection->connect() or die("Cannot connect to the broker!\n");

		try {
			$channel = new AMQPChannel($connection);
			//创建交换机对象
			$exchange = new AMQPExchange($channel);
			$exchange->setName($exchangeName);
			$exchange->setType(AMQP_EX_TYPE_TOPIC);
			$exchange->setFlags(AMQP_DURABLE); //添加这行
			//AMQP_EX_TYPE_FANOUT  //广播
			//AMQP_EX_TYPE_DIRECT //严格指定
			//AMQP_EX_TYPE_TOPIC 宽松指定)
			//AMQP_EX_TYPE_HEADERS  //headers类型
			$exchange->declareExchange();

			//发送消息
			echo 'Send Message: ' . $exchange->publish($message, $routeKey) . "\n";
			echo "Message Is Sent: " . $message . "\n";
		} catch (\AMQPConnectionException $e) {
			var_dump($e);
		}

		$connection->disconnect();// 断开连接
	}

posted @ 2022-10-20 23:42  码农编程进阶笔记  阅读(238)  评论(0编辑  收藏  举报
返回顶部 有事您Q我