|
| 1 | +# Trigger function from RabbitMQ |
| 2 | + |
| 3 | +Trigger OpenFaaS functions from RabbitMQ messages. |
| 4 | + |
| 5 | +> Note: This feature is included for [OpenFaaS Standard & For Enterprises](https://openfaas.com/pricing/) customers. |
| 6 | +
|
| 7 | +## Installation |
| 8 | + |
| 9 | +* Setup the connector |
| 10 | + |
| 11 | + You can install the RabbitMQ connector using its [helm chart](https://github.com/openfaas/faas-netes/tree/master/chart/rabbitmq-connector). |
| 12 | + |
| 13 | + The `values.yaml` file can be customised to suit your needs. |
| 14 | + |
| 15 | +* Configure the connector for you needs by defining a values.yaml file |
| 16 | + |
| 17 | + ```yaml |
| 18 | + rabbitmqURL: "amqp://rabbitmq.rabbitmq.svc.cluster.local:5672" |
| 19 | + |
| 20 | + queues: |
| 21 | + - name: queue1 |
| 22 | + durable: true |
| 23 | + ``` |
| 24 | +
|
| 25 | +Use the `queues` parameter to configure a list of queues to which the connector should subscribe. When a message is received on a queue, the connector will invoke any function that has the queue name listed in its `topic` annotation. |
| 26 | + |
| 27 | +Queue configuration: |
| 28 | + |
| 29 | +* `name` - The name of the queue. (Required) |
| 30 | +* `durable` - Specifies whether the queue should survive broker restarts. |
| 31 | +* `nowait` - Whether to wait for confirmation from the server when declaring the queue. |
| 32 | +* `autodelete` - Whether the queue should be deleted automatically when no consumers are connected. |
| 33 | +* `exclusive` - Specifies whether the queue should be exclusive to the connection that created it. |
| 34 | + |
| 35 | +If your RabbitMQ requires a TLS connection make sure to use `amqps://` as the scheme in the url. |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +To test the connector, you can deploy the [printer function](https://github.com/openfaas/store-functions/blob/master/printer/handler.go). It prints out the HTTP headers and body of any invocation. |
| 40 | + |
| 41 | +Add an annotation `topic=queue1` so that the function is invoked for any message published to the queue with name queue1. |
| 42 | +Each time a function is invoked by the connector it will receive the message from the queue as the HTTP body. |
| 43 | + |
| 44 | +```bash |
| 45 | +faas-cli store deploy printer \ |
| 46 | + --annotation topic=queue1 |
| 47 | +``` |
| 48 | + |
| 49 | +Publish a message on the queue for testing. We will be using the [RabbitMQ management CLI](https://www.rabbitmq.com/docs/management-cli) for this. |
| 50 | + |
| 51 | +```bash |
| 52 | +./rabbitmqadmin publish \ |
| 53 | + routing_key="queue1" payload='Hello, Task Queue!' properties='{"message_id":"1"}' |
| 54 | +``` |
| 55 | + |
| 56 | +Take a look at the logs of the printer function to inspect the invocations made by the connector. You should see the following output: |
| 57 | + |
| 58 | +```bash |
| 59 | +faas-cli logs printer |
| 60 | +
|
| 61 | +2024-12-02T16:58:49Z X-Forwarded-For=[10.42.0.4:58102] |
| 62 | +2024-12-02T16:58:49Z X-Msg-Id=[1] |
| 63 | +2024-12-02T16:58:49Z X-Start-Time=[1733158729048068067] |
| 64 | +2024-12-02T16:58:49Z X-Call-Id=[ea459d66-e1af-4846-9f73-18561f69074f] |
| 65 | +2024-12-02T16:58:49Z X-Connector=[connector-sdk] |
| 66 | +2024-12-02T16:58:49Z X-Topic=[queue1] |
| 67 | +2024-12-02T16:58:49Z User-Agent=[openfaas-gateway/0.4.34] |
| 68 | +2024-12-02T16:58:49Z Accept-Encoding=[gzip] |
| 69 | +2024-12-02T16:58:49Z Content-Type=[text/plain] |
| 70 | +2024-12-02T16:58:49Z X-Forwarded-Host=[gateway.openfaas:8080] |
| 71 | +2024-12-02T16:58:49Z |
| 72 | +2024-12-02T16:58:49Z Hello, Task Queue! |
| 73 | +2024-12-02T16:58:49Z |
| 74 | +2024-12-02T16:58:49Z 2024/12/02 16:58:49 POST / - 202 Accepted - ContentLength: 0B (0.0003s) |
| 75 | +``` |
| 76 | + |
| 77 | +Additional headers are made available to the request. These headers contain RabbitMQ message metadata. |
| 78 | + |
| 79 | +* `X-Topic` - topic that triggered the function. |
| 80 | +* `X-Msg-Id` - the message identifier. |
| 81 | + |
| 82 | +The default content-type is configured as `text/plain`, but can be changed to another content-type such as `application/json` or `application/octet-stream` by the [values.yaml file](https://github.com/openfaas/faas-netes/blob/master/chart/kafka-connector/values.yaml) for the connector. |
| 83 | + |
| 84 | +## Would you like a demo? |
| 85 | + |
| 86 | +Feel free to reach out to us for a demo or to ask any questions you may have. |
| 87 | + |
| 88 | +* [Talk to us](https://openfaas.com/pricing/) |
0 commit comments