简体   繁体   中英

Symfony RabbitMQ Bundle in AWS (managed service): Any way to get this working?

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 AMQP S 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)"} []

It may be late but.

For SSL connection with Aws MQ (Rabbit) you need to add the.perm in the path.

'%env(MESSENGER_TRANSPORT_DSN)%cacert=%env(resolve:MESSENGER_TRANSPORT_CERT)%'

you can get the perm at this aws url: AmazonRootCA1.pem

I hope it helps.

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;
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM