Document updated on Mar 16, 2023
AMQP Consumer Integration in the API Gateway (RabbitMQ)
The AMQP component allows to send and receive messages to and from a queue through the API Gateway.
The configuration of the queue is a straightforward process. To connect the endpoints to the messaging system you only need to include the extra_config
key with the namespaces backend/amqp/consumer
or backend/amqp/producer
.
To create Async agents that consume messages asynchronously and without requiring a user request, see Async Agents.
The parameters of this integration follow the AMQP specification. To understand what are the implications of a certain parameter, see the AMQP Complete Reference Guide.
KrakenD creates both the exchange and the queue for you.
Configuration
The consumer retrieves messages from the queue when a KrakenD endpoint plugs to its AMQP backend. The recommendation is to connect consumers to GET
endpoints.
A single endpoint can consume messages from N queues, or can consume N messages from the same queue by adding N backends with the proper queue name.
See Async Agents to consume messages without an endpoint.
The needed configuration to run a consumer is:
{
"backend": [{
"host": ["amqp://guest:[email protected]:5672"],
"disable_host_sanitize": true,
"extra_config": {
"backend/amqp/consumer": {
"name": "queue-1",
"exchange": "some-exchange",
"durable": true,
"delete": false,
"no_wait": true,
"no_local": false,
"routing_key": ["#"],
"prefetch_count": 10,
"auto_ack": false,
"backoff_strategy": "exponential-jitter"
}
}
}]
}
Fields of AMQP Consumer
auto_ack
boolean- When KrakenD retrieves the messages, regardless of the success or failure of the operation, it marks them as
ACK
nowledge.Defaults tofalse
backoff_strategy
- When the connection to your event source gets interrupted for whatever reason, KrakenD keeps trying to reconnect until it succeeds or until it reaches the
max_retries
. The backoff strategy defines the delay in seconds in between consecutive failed retries. Check the meaning of each strategy.Possible values are:"linear"
,"linear-jitter"
,"exponential"
,"exponential-jitter"
,"fallback"
Defaults to"fallback"
delete
boolean- When
true
, AMQP deletes the queue when there are no remaining connections. This option is not recommended in most of the scenarios. If for instance, the connectivity between KrakenD and AMQP is lost for whatever reason and it’s the only client, AMQP will delete the queue no matter the number of messages there are inside, and when KrakenD gets the connection again the queue won’t exist and future connections will recreate it again.Defaults tofalse
durable
boolean- Durable queues will survive server restarts and remain when there are no remaining consumers or bindings.
true
is recommended, but depends on the use case.Defaults tofalse
exchange
* string- The exchange name (must have a topic type if already exists).Example:
"some-exchange"
exclusive
boolean- When
true
, AMQP will allow a single KrakenD instance to access the queue. This option is not recommended in environments where the gateway needs high availability and you have several instances running.Defaults tofalse
max_retries
integer- The maximum number of times you will allow KrakenD to retry reconnecting to a broken messaging system. During startup KrakenD will wait for a maximum of 3 retries before starting to use this policy. Use 0 for unlimited retries.Defaults to
0
name
* string- Queue name.
no_local
boolean- The no_local flag is not supported by RabbitMQ.
no_wait
boolean- When true, do not wait for the server to confirm the request and immediately begin deliveries. If it is not possible to consume, a channel exception will be raised and the channel will be closed.
prefetch_count
integer- The number of messages you want to prefetch prior to consume them.Defaults to
0
routing_key
* array- The list of routing keys you will use to consume messages.Example:
"#"
Connection retries
The gateway will try to reconnect to the AMQP server if the connection is lost for any reason. You can set the back-off strategy that better fits your needs.