Issue
We really tried a lot but it still can not get the Symfony RabbitMQ bundle (https://github.com/php-amqplib/RabbitMqBundle) running in AWS (with Docker). AWS only allows AMQPS and port 5671 to be opened in the AWS managed service.
This is our current configuration in detail:
old_sound_rabbit_mq:
connections:
default:
# nevermind
secure:
url: 'amqps://%env(RABBITMQ_USER)%:%env(RABBITMQ_PASSWORD)%@%env(RABBITMQ_HOST)%:%env(RABBITMQ_PORT)%'
vhost: '%env(RABBITMQ_VHOST)%'
lazy: true
connection_timeout: 6
read_write_timeout: 6
ssl_context:
verify_peer: false
keepalive: true
heartbeat: 60
use_socket: true # default false
producers:
# use 'old_sound_rabbit_mq.[my-producer-name]_producer' service to send data.
article_create:
connection: secure
exchange_options: { name: pimcore.article.create, type: topic }
queue_options:
name: article_create
Note that we use the "url" config value because it seems like it is the only way to set AMQPS for the bundle.
The relevant parts of docker-compose.yml:
php-fpm:
container_name: php-fpm
environment:
- RABBITMQ_HOST=my-rabbitmq # usually the docker container (otherwise localhost or server address)
- RABBITMQ_PORT=5671 # locally it seems to work with 5672
- RABBITMQ_USER=user
- RABBITMQ_PASSWORD=password
- RABBITMQ_VHOST=/
rabbitmq:
container_name: my-rabbitmq
image: rabbitmq:3.8-management
ports:
- 127.0.0.1:15672:15672
- 127.0.0.1:5672:5672
- 127.0.0.1:5671:5671
environment:
- RABBITMQ_DEFAULT_USER=pimcore
- RABBITMQ_DEFAULT_PASS=pimcore
volumes:
- rabbitmq:/var/lib/rabbitmq
This is how the messages are sent from a Symfony Event Listener:
$this->appKernel->getContainer()->get('old_sound_rabbit_mq.article_create_producer')->publish(
serialize($objToSend),
"article_create",
[
'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT,
'message_id' => md5(uniqid("prefix", true))
]
);
It all seems to work locally without any issues. In AWS we get the following errors:
Without sockets:
Broken pipe or closed connection
With sockets:
[2021-06-08 15:59:45] request.CRITICAL: Uncaught PHP Exception ErrorException: "Warning: socket_recv(): unable to read from socket [104]: Connection reset by peer" at /var/www/vendor/php-amqplib/php-amqplib/PhpAmqpLib/Wire/IO/SocketIO.php line 121 {"exception":"[object] (ErrorException(code: 0): Warning: socket_recv(): unable to read from socket [104]: Connection reset by peer at /var/www/vendor/php-amqplib/php-amqplib/PhpAmqpLib/Wire/IO/SocketIO.php:121)"} []
Solution
Finally solved - you have to define a custom AMQPChannel with a custom AMQPConnection with SSL options and then set this AMQPChannel to the producer:
// SSL
if ($port === 5671) {
$sslOptions = array(
'cafile' => CERTS_PATH . '/ca_certificate.pem',
'local_cert' => CERTS_PATH . '/client_certificate.pem',
'local_pk' => CERTS_PATH . '/client_key.pem',
'verify_peer' => true,
'verify_peer_name' => false,
);
$amqpSslConnection = new AMQPSSLConnection(
getenv('RABBITMQ_HOST'),
$port,
getenv('RABBITMQ_USER'),
getenv('RABBITMQ_PASSWORD'),
getenv('RABBITMQ_VHOST'),
$sslOptions
);
$amqpChannel = new AMQPChannel($amqpSslConnection);
$producer->setChannel($amqpChannel);
return $producer;
}
Answered By - Blackbam
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.