I'm trying to connect to a RabbitMQ docker container from PHP. The error that I'm getting is Uncaught exception 'ErrorException' with message 'stream_socket_client(): php_network_getaddresses: getaddrinfo failed: Name or service not known'
. I think this is an issue with how I point my program to the RabbitMQ process.
The snippet of code that I think is causing the error is
$connection = new AMQPStreamConnection(getenv('MESSAGE_BROKER_HOST'), 5672, 'guest', 'guest');
The environment variable I'm accessing is defined in .env as
MESSAGE_BROKER_HOST=amqp://rabbitmq
I'm not very used to docker, but I was under the impression that this should work since the process that this code runs on has RabbitMQ linked in the docker-compose file as "rabbitmq".
The MESSAGE_BROKER_HOST
env should be rabbitmq
without amqp://
because phpamqp-lib does not support DSNs.
If you'd like to deal with real DSN look at enqueue/amqp-lib .
The code would look like:
<?php
use Enqueue\AmqpLib\AmqpConnectionFactory;
$factory = new AmqpConnectionFactory('amqp://guest:guest@rabbitmq:5672/%2f');
Your approach looks fine. The only problem that I can think of, is the insufficient permissions of guest
user. The default guest
user of RabbitMQ can only be accessed from localhost. Although insufficient permissions shouldn't throw unknown service
error.
Try creating a new user may be that will fix the problem. For details about the permissions of the guest user, you can see this link: https://www.rabbitmq.com/access-control.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.