背景:
由于需要将阿里云相关服务转移到华为云,项目配套服务rabbitmq也被列入迁移计划。阿里云rabbitmq是自建服务,采用5672端口,未开启ssl加密。华为云购买rabbitmq服务,开启了ssl加密。项目生产者发送消息时就报错"Broken pipe or closed connection"
经过一天不断调试终于找到解决方案,故记录如下。
若rabbitmq未启用ssl加密,百度出来的结果均是如下代码
$connection=new PhpAmqpLib\Connection\AMQPStreamConnection(
getConfig('rabbit','RABBIT_URL'),//mq ip
getConfig('rabbit','RABBIT_PORT'),//mq port
getConfig('rabbit','RABBIT_USER'),//mq user
getConfig('rabbit','RABBIT_PWD'),//mq pwd
getConfig('rabbit','RABBIT_VHOST')//mq vhost
);
但是rabbitmq启用了ssl加密,又无法获取到证书密钥信息的时候请尝试如下代码进行rabbitmq链接
//核心配置,对证书进行单边验证
$sslOptions = [
'verify_peer'=>false,
'verify_peer_name'=>false
];
$connection=new PhpAmqpLib\Connection\AMQPSSLConnection(
getConfig('rabbit','RABBIT_URL'),//mq ip
getConfig('rabbit','RABBIT_PORT'),//mq port
getConfig('rabbit','RABBIT_USER'),//mq user
getConfig('rabbit','RABBIT_PWD'),//mq pwd
getConfig('rabbit','RABBIT_VHOST'),//mq vhost
$sslOptions
);
请注意:新代码声明了$sslOptions 数组,链接方法由AMQPStreamConnection更换为AMQPSSLConnection
参考文献:
Connect to RabbitMQ from PHP over AMQPS | LornaJanehttps://lornajane.net/posts/2016/connect-to-rabbitmq-from-php-over-amqpsPHP: SSL context options - Manual https://www.php.net/manual/en/context.ssl.php
|