RabbitMQ is a powerful message broker that's widely used in event-driven architectures. Quarkus, known for its lightweight and cloud-native features, pairs seamlessly with RabbitMQ—especially when you configure it using
application.yml
. This article will guide you through setting up RabbitMQ in a Quarkus application using Docker for local development and Kubernetes for production, with clean profile-specific YAML configurations.
Step 1: Add Required Dependencies
First, you need to include RabbitMQ-related dependencies in your
pom.xml
. In Quarkus, this is handled using the SmallRye Reactive Messaging AMQP connector:
<dependencies>
<!-- RabbitMQ via AMQP -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-reactive-messaging-amqp</artifactId>
</dependency>
<!-- YAML support -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-config-yaml</artifactId>
</dependency>
<!-- Kubernetes ConfigMap and Secret support -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-kubernetes-config</artifactId>
</dependency>
</dependencies>
These dependencies enable Quarkus to communicate with RabbitMQ using AMQP, support YAML configurations, and pull values from Kubernetes resources.
Step 2: Running RabbitMQ with Docker for Development
You can quickly get RabbitMQ running locally with this Docker command:
docker run -d --hostname rabbit-dev --name rabbitmq-dev \
-p 5672:5672 -p 15672:15672 rabbitmq:3-management
- AMQP messages will flow through port
5672
.
- The management UI will be available at
http://localhost:15672
with default credentials (guest
/ guest
).
Step 3: Create application-dev.yml
Now, define your development-specific configuration in
application-dev.yml
:
"%dev":
mp:
messaging:
incoming:
rabbit-queue:
connector: smallrye-amqp
address: dev-queue
host: localhost
port: 5672
username: guest
password: guest
outgoing:
rabbit-out:
connector: smallrye-amqp
address: dev-queue
host: localhost
port: 5672
username: guest
password: guest
This setup configures both an incoming consumer and an outgoing producer tied to
dev-queue
. You're ready to test messaging locally using Docker.
Run with the dev profile like this:
./mvnw quarkus:dev -Dquarkus.profile=dev
Step 4: Prepare for Production on Kubernetes
To avoid hardcoding configuration in production, Kubernetes provides an excellent way to manage environment-specific values via ConfigMaps and Secrets.
Step 4.1: Create a ConfigMap for RabbitMQ
apiVersion: v1
kind: ConfigMap
metadata:
name: rabbitmq-config
data:
rabbitmq.host: rabbitmq-service
rabbitmq.port: "5672"
rabbitmq.queue: prod-queue
Step 4.2: Create a Secret for Sensitive Data
apiVersion: v1
kind: Secret
metadata:
name: rabbitmq-secret
type: Opaque
data:
rabbitmq.username: cHJvZHVzZXI= # base64 for 'produser'
rabbitmq.password: cHJvZHBhc3M= # base64 for 'prodpass'
Step 5: Create application-prod.yml
Now define the production configuration using environment variables in
application-prod.yml
:
"%prod":
mp:
messaging:
incoming:
rabbit-queue:
connector: smallrye-amqp
address: ${rabbitmq.queue}
host: ${rabbitmq.host}
port: ${rabbitmq.port}
username: ${rabbitmq.username}
password: ${rabbitmq.password}
outgoing:
rabbit-out:
connector: smallrye-amqp
address: ${rabbitmq.queue}
host: ${rabbitmq.host}
port: ${rabbitmq.port}
username: ${rabbitmq.username}
password: ${rabbitmq.password}
quarkus:
kubernetes-config:
enabled: true
config-maps: rabbitmq-config
secrets: rabbitmq-secret
This tells Quarkus to pull values from the Kubernetes ConfigMap and Secret at runtime.
Step 6: Activate Profiles Based on the Environment
When deploying your app, make sure to set the appropriate profile in your deployment YAML:
env:
- name: QUARKUS_PROFILE
value: prod
This activates the
prod
profile and triggers Quarkus to load the
application-prod.yml
file.
Step 7: Test Messaging with RabbitMQ
To confirm everything is set up properly, you can implement a REST endpoint that sends messages to RabbitMQ:
import jakarta.inject.Inject;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import org.eclipse.microprofile.reactive.messaging.Channel;
import org.eclipse.microprofile.reactive.messaging.Emitter;
@Path("/send")
public class RabbitSender {
@Inject
@Channel("rabbit-out")
Emitter<String> emitter;
@POST
public void send(String msg) {
emitter.send(msg);
}
}
This lets you POST messages to the queue and monitor them via RabbitMQ’s management UI.
image quote pre code