All Products
Search
Document Center

ApsaraMQ for RabbitMQ:Use AMQProxy to solve connection reuse issues of clients such as a PHP client

Last Updated:May 16, 2024

AMQProxy is an open source AMQP proxy service that can reuse AMQP connections. AMQProxy enables a client, such as a PHP client, that normally can use only short-lived connections to use persistent connections. This reduces the consumption of network and ApsaraMQ for RabbitMQ resources.

Prerequisites

OpenSSL is installed on the client-side server if you want to connect AMQProxy to ApsaraMQ for RabbitMQ by using SSL. For more information, see Downloads.

Important

In VPC environments, you are not allowed to connect AMQProxy to ApsaraMQ for RabbitMQ by using SSL.

Background information

Clients in some programming languages, such as PHP clients, do not support persistent connections. If you frequently enable and disable connections for a client, a large number of network and ApsaraMQ for RabbitMQ resources are consumed. This greatly increases the workloads of ApsaraMQ for RabbitMQ.

dg_AMQP Proxy_1

AMQProxy

AMQProxy is an open source AMQP proxy developed by CloudAMQP. AMQProxy allows you to enable persistent connections between clients and ApsaraMQ for RabbitMQ. After you deploy AMQProxy on a client server, requests from the client to ApsaraMQ for RabbitMQ are first sent to AMQProxy and then forwarded to ApsaraMQ for RabbitMQ.

AMQProxy processes connection-related requests initiated by a client based on the following logic:

  • If the client sends a request to enable a connection, AMQProxy checks whether an existing connection can be reused based on the username, password, and vhost. If an existing connection meets the reuse requirements, the connection is reused. If no existing connections meet the reuse requirements, AMQProxy enables a new connection for the client and ApsaraMQ for RabbitMQ.

  • If the client sends a request to disable a connection, AMQProxy directly returns OK but does not disable the connection with ApsaraMQ for RabbitMQ. When the client requests to enable the connection again, AMQProxy directly reuses this connection.

dg_AMQP Proxy_2

For more information about AMQProxy, see AMQProxy.

Deploy AMQProxy

  1. Run the following command to download the AMQProxy compressed package:

    wget https://github.com/cloudamqp/amqproxy/releases/download/v0.4.4/amqproxy-0.4.4-1.linux-x86_64-static.tar.gz
    Note

    AMQProxy 0.4.4 is used as an example in this topic. For more AMQProxy versions, see Releases.

  2. Run the following command to decompress the downloaded package:

    tar -xzvf amqproxy-0.4.4-1.linux-x86_64-static.tar.gz
  3. Run the following command to open the AMQProxy folder:

    cd amqproxy/
  4. Run the following command to start AMQProxy:

    ./amqproxy -l LISTEN_ADDRESS -p LISTEN_PORT AMQP_URL

    Parameter

    Description

    LISTEN_ADDRESS

    The IP address of AMQProxy. You can set this parameter to 127.0.0.1 because AMQProxy is deployed on the server of the client.

    LISTEN_PORT

    The listening port of AMQProxy. The client sends requests to AMQProxy over this port. All available ports such as port 5673 can be used as the listening port.

    AMQP_URL

    The URL of the ApsaraMQ for RabbitMQ instance. The URL must be in the {amqp|amqps}://{endpoint} format. Valid values:

    • amqp: the AMQP protocol. Use this parameter if Secure Sockets Layer (SSL) is not used to establish the connection.

    • amqps: the AMQP and SSL protocols. Use this parameter if the SSL protocol is used to establish the connection.

    • endpoint: the endpoint of the ApsaraMQ for RabbitMQ instance. You can view the endpoint on the Instances page in the ApsaraMQ for RabbitMQ console. For more information, see View instance details.

    Sample command:

    ./amqproxy -l 127.0.0.1 -p 5673 amqps://188XXX420.mq-amqp.cn-hangzhou-a.aliyuncs.com

    Sample response:

    Proxy upstream: 188XXX420.mq-amqp.cn-hangzhou-a.aliyuncs.com:5671 TLS
    Proxy listening on 127.0.0.1:5673
    0 clients                0 upstreams

    Parameter

    Description

    clients

    The number of connections between the client and AMQProxy.

    upstreams

    The number of connections between AMQProxy and the ApsaraMQ for RabbitMQ instance.

  5. Change the host IP address and port number in the code of the client to the IP address and listening port of AMQProxy.

    factory.setHost("127.0.0.1");
    factory.setPort(5673);