๐ AMQP Sidecar
AMQP Sidecar is a lightweight, configurable Elixir application designed to act as a sidecar for consuming and publishing messages via AMQP (e.g., RabbitMQ). It integrates seamlessly with Kubernetes and supports dynamic configuration for exchanges, queues, and routing keys.
๐ Features
- ๐ฅ Consume Messages: Dynamically subscribe to AMQP exchanges and queues based on configuration.
- ๐ค Publish Messages: Publish messages to AMQP exchanges with optional headers.
- โ๏ธ Configurable: Define exchanges, queues, routing keys, and endpoints in a
broker.jsonfile. - ๐ณ Kubernetes-Friendly: Designed to run as a sidecar in Kubernetes with configuration mounted as a ConfigMap.
- ๐ HTTP API: Expose an HTTP API for publishing messages and health checks.
- ๐งฉ Extensible: Easily extendable to support custom message processing and forwarding logic.
๐ ๏ธ Installation
Prerequisites
- Elixir (1.12 or later)
- RabbitMQ (3.8 or later)
- Kubernetes (optional, for running as a sidecar)
Steps
-
Clone the repository:
git clone https://github.com/alesima/amqp_sidecar.git cd amqp_sidecar -
Install dependencies:
mix deps.get -
Configure the application:
- Copy the example config and edit it:
(or mount it as a ConfigMap โ see Kubernetes below).cp config/broker.json.example config/broker.json - Example
broker.json:{ "rules": [ { "exchange": "test_exchange", "exchangeType": "topic", "queue": "test_queue", "routingKeys": ["test.*"], "endpointUri": "http://test-endpoint" } ] }
Supported
exchangeTypevalues:topic(default),direct,fanout,x-delayed-message(requiresexchangeArguments: {"x-delayed-type": "<type>"}). Messages are matched by exchange plus AMQP topic patterns (*= one word,#= zero or more words). Unmatched messages are acknowledged and dropped with a warning; failed forwards are rejected with requeue. - Copy the example config and edit it:
-
Run the application:
mix run --no-halt
๐ฆ Usage
Consuming Messages
The sidecar automatically consumes messages from the configured exchanges and queues. Messages are forwarded to the specified endpointUri.
Publishing Messages
Use the HTTP API to publish messages:
curl -X POST http://localhost:4000/publish \
-H "Content-Type: application/json" \
-d '{
"exchange": "test_exchange",
"routingKey": "test.key",
"message": "Hello, world!",
"headers": {
"x-delay": 5000
}
}'
Health Check
Check the health of the sidecar:
curl http://localhost:4000/health
๐ณ Running in Kubernetes
-
Build the Docker image:
docker build -t amqp_sidecar:latest . -
Deploy to Kubernetes:
- Create a ConfigMap for
broker.json:apiVersion: v1 kind: ConfigMap metadata: name: amqp-sidecar-config data: broker.json: | { "rules": [ { "exchange": "test_exchange", "exchangeType": "topic", "queue": "test_queue", "routingKeys": ["test.*"], "endpointUri": "http://test-endpoint" } ] } - Deploy the sidecar:
apiVersion: apps/v1 kind: Deployment metadata: name: amqp-sidecar spec: replicas: 1 template: spec: containers: - name: amqp-sidecar image: amqp_sidecar:latest volumeMounts: - name: config-volume mountPath: /app/config volumes: - name: config-volume configMap: name: amqp-sidecar-config
- Create a ConfigMap for
๐งช Testing
Run the test suite:
mix test
๐ Project Structure
amqp_sidecar/
โโโ config/ # Configuration files
โโโ lib/ # Application code
โ โโโ amqp_sidecar/ # Core modules
โ โโโ web/ # HTTP API and routes
โโโ test/ # Tests
โโโ Dockerfile # Dockerfile for containerization
โโโ mix.exs # Project dependencies
โโโ README.md # This file
๐ค Contributing
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create a new branch (
git checkout -b feature/your-feature). - Commit your changes (
git commit -m 'Add some feature'). - Push to the branch (
git push origin feature/your-feature). - Open a pull request.
๐ License
This project is licensed under the MIT License. See the LICENSE file for details.
๐ Acknowledgments
- Elixir for being an awesome language.
- RabbitMQ for reliable message queuing.
- Kubernetes for making sidecar patterns easy.
Made with โค๏ธ by Alex Silva