Skip to content

Commit

Permalink
Add support for vhosts in RabbitMQ Dev Services topology
Browse files Browse the repository at this point in the history
  • Loading branch information
subnova committed Oct 10, 2024
1 parent 7f22ff2 commit b889f39
Show file tree
Hide file tree
Showing 10 changed files with 373 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/native-tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
{
"category": "Messaging2",
"timeout": 80,
"test-modules": "reactive-messaging-amqp, reactive-messaging-rabbitmq, reactive-messaging-rabbitmq-dyn, reactive-messaging-pulsar",
"test-modules": "reactive-messaging-amqp, reactive-messaging-rabbitmq, reactive-messaging-rabbitmq-dyn, reactive-messaging-rabbitmq-devservices, reactive-messaging-pulsar",
"os-name": "ubuntu-latest"
},
{
Expand Down
1 change: 1 addition & 0 deletions docs/.vale/styles/Quarkus/CaseSensitiveTerms.yml
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ swap:
Qdmanage|QDMANAGE: qdmanage
Qdstat|QDSTAT: qdstat
Qeth|QETH: qeth
Rabbit\sMQ|rabbitMQ|Rabbitmq|Rabbit-mq: RabbitMQ
RAMdisk|ramdisk|RAM-disk: RAM disk
Ram|ram: RAM
RAW: raw
Expand Down
45 changes: 29 additions & 16 deletions docs/src/main/asciidoc/rabbitmq-dev-services.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Dev Services for RabbitMQ automatically starts a RabbitMQ broker in dev mode and
So, you don't have to start a broker manually.
The application is configured automatically.

== Enabling / Disabling Dev Services for RabbitMQ
== Enabling / disabling Dev Services for RabbitMQ

Check warning on line 16 in docs/src/main/asciidoc/rabbitmq-dev-services.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.Headings] Use sentence-style capitalization in 'Enabling / disabling Dev Services for RabbitMQ'. Raw Output: {"message": "[Quarkus.Headings] Use sentence-style capitalization in 'Enabling / disabling Dev Services for RabbitMQ'.", "location": {"path": "docs/src/main/asciidoc/rabbitmq-dev-services.adoc", "range": {"start": {"line": 16, "column": 4}}}, "severity": "INFO"}

Dev Services for RabbitMQ is automatically enabled unless:

Expand All @@ -22,15 +22,15 @@ Dev Services for RabbitMQ is automatically enabled unless:
- all the Reactive Messaging RabbitMQ channels have the `host` or `port` attributes set

Dev Services for RabbitMQ relies on Docker to start the broker.
If your environment does not support Docker, you will need to start the broker manually, or connect to an already running broker.
You can configure the broker access using the `rabbitmq-host`, `rabbitmq-port`, `rabbitmq-username` and `rabbitmq-password` properties.
If your environment does not support Docker, you must start the broker manually, or connect to an already running broker.
You can configure the broker access by using the `rabbitmq-host`, `rabbitmq-port`, `rabbitmq-username` and `rabbitmq-password` properties.

== Shared broker

Most of the time you need to share the broker between applications.
Most of the time you want to share the broker between applications.
Dev Services for RabbitMQ implements a _service discovery_ mechanism for your multiple Quarkus applications running in _dev_ mode to share a single broker.

NOTE: Dev Services for RabbitMQ starts the container with the `quarkus-dev-service-rabbitmq` label which is used to identify the container.
NOTE: Dev Services for RabbitMQ starts the container with the `quarkus-dev-service-rabbitmq` label, which is used to identify the container.

If you need multiple (shared) brokers, you can configure the `quarkus.rabbitmq.devservices.service-name` attribute and indicate the broker name.
It looks for a container with the same value, or starts a new one if none can be found.
Expand All @@ -48,7 +48,7 @@ You can set the port by configuring the `quarkus.rabbitmq.devservices.port` prop
== Configuring the image

Dev Services for RabbitMQ uses official images available at https://hub.docker.com/_/rabbitmq.
You can configure the image and version using the `quarkus.rabbitmq.devservices.image-name` property:
You can configure the image and version with the `quarkus.rabbitmq.devservices.image-name` property:

[source, properties]
----
Expand All @@ -58,14 +58,24 @@ quarkus.rabbitmq.devservices.image-name=rabbitmq:latest
== Access the management UI

Check warning on line 58 in docs/src/main/asciidoc/rabbitmq-dev-services.adoc

View workflow job for this annotation

GitHub Actions / Linting with Vale

[vale] reported by reviewdog 🐶 [Quarkus.Headings] Use sentence-style capitalization in 'Access the management UI'. Raw Output: {"message": "[Quarkus.Headings] Use sentence-style capitalization in 'Access the management UI'.", "location": {"path": "docs/src/main/asciidoc/rabbitmq-dev-services.adoc", "range": {"start": {"line": 58, "column": 4}}}, "severity": "INFO"}

By default, Dev Services for RabbitMQ use the official image with the `management` tag. This means you have the https://github.com/docker-library/docs/tree/master/rabbitmq#management-plugin[management plugin] available. You can use the xref:dev-ui.adoc[Dev UI] to find the HTTP port randomly affected
or configure a static one via `quarkus.rabbitmq.devservices.http-port`.
or configure a static one by using `quarkus.rabbitmq.devservices.http-port`.

== Predefined Topology
== Predefined topology

Dev Services for RabbitMQ supports defining topology upon broker start. You can define Exchanges, Queues, and
Bindings using standard Quarkus configuration.
Dev Services for RabbitMQ supports defining topology upon broker start. You can define Virtual Hosts, Exchanges, Queues,
and Bindings through standard Quarkus configuration.

=== Defining Exchanges
=== Defining virtual hosts

RabbitMQ uses a default virtual host of `/`. To define additional RabbitMQ virtual hosts, provide the names
of the virtual hosts in the `quarkus.rabbitmq.devservices.vhosts` key:

[source, properties]
----
quarkus.rabbitmq.devservices.exchanges.vhosts=my-vhost-1,my-vhost-2
----

=== Defining exchanges

To define a RabbitMQ exchange you provide the exchange's name after the `quarkus.rabbitmq.devservices.exchanges` key,
followed by one (or more) of the exchange's properties:
Expand All @@ -75,16 +85,17 @@ followed by one (or more) of the exchange's properties:
quarkus.rabbitmq.devservices.exchanges.my-exchange.type=topic # defaults to 'direct'
quarkus.rabbitmq.devservices.exchanges.my-exchange.auto-delete=false # defaults to 'false'
quarkus.rabbitmq.devservices.exchanges.my-exchange.durable=true # defaults to 'false'
quarkus.rabbitmq.devservices.exchanges.my-exchange.vhost=my-vhost # defaults to '/'
----

Additionally, any additional arguments may be provided to the exchange's definition by using the `arguments` key:
Additionally, any additional arguments can be provided to the exchange's definition by using the `arguments` key:

[source, properties]
----
quarkus.rabbitmq.devservices.exchanges.my-exchange.arguments.alternate-exchange=another-exchange
----

=== Defining Queues
=== Defining queues

To define a RabbitMQ queue you provide the queue's name after the `quarkus.rabbitmq.devservices.queues` key,
followed by one (or more) of the queue's properties:
Expand All @@ -93,16 +104,17 @@ followed by one (or more) of the queue's properties:
----
quarkus.rabbitmq.devservices.queues.my-queue.auto-delete=false # defaults to 'false'
quarkus.rabbitmq.devservices.queues.my-queue.durable=true # defaults to 'false'
quarkus.rabbitmq.devservices.queues.my-queue.vhost=my-vhost # defaults to '/'
----

Additionally, any additional arguments may be provided to the queue's definition by using the `arguments` key:
Additionally, any additional arguments can be provided to the queue's definition by using the `arguments` key:

[source, properties]
----
quarkus.rabbitmq.devservices.queues.my-queue.arguments.x-dead-letter-exchange=another-exchange
----

=== Defining Bindings
=== Defining bindings

To define a RabbitMQ binding you provide the binding's name after the `quarkus.rabbitmq.devservices.bindings` key,
followed by one (or more) of the binding's properties:
Expand All @@ -113,12 +125,13 @@ quarkus.rabbitmq.devservices.bindings.a-binding.source=my-exchange # defaul
quarkus.rabbitmq.devservices.bindings.a-binding.routing-key=some-key # defaults to '#'
quarkus.rabbitmq.devservices.bindings.a-binding.destination=my-queue # defaults to name of binding
quarkus.rabbitmq.devservices.bindings.a-binding.destination-type=queue # defaults to 'queue'
quarkus.rabbitmq.devservices.bindings.a-binding.vhost=my-vhost # defaults to '/'
----

NOTE: The name of the binding is only used for the purposes of the Dev Services configuration and is not part of the
binding defined in RabbitMQ.

Additionally, any additional arguments may be provided to the binding's definition by using the `arguments` key:
Additionally, any additional arguments can be provided to the binding's definition by using the `arguments` key:

[source, properties]
----
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.smallrye.reactivemessaging.rabbitmq.deployment;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.OptionalInt;
Expand Down Expand Up @@ -32,6 +33,12 @@ public static class Exchange {
@ConfigItem(defaultValue = "false")
public Boolean durable;

/**
* What virtual host should the exchange be associated with?
*/
@ConfigItem(defaultValue = "/")
public String vhost;

/**
* Extra arguments for the exchange definition.
*/
Expand All @@ -55,6 +62,12 @@ public static class Queue {
@ConfigItem(defaultValue = "false")
public Boolean durable;

/**
* What virtual host should the queue be associated with?
*/
@ConfigItem(defaultValue = "/")
public String vhost;

/**
* Extra arguments for the queue definition.
*/
Expand Down Expand Up @@ -90,6 +103,12 @@ public static class Binding {
@ConfigItem(defaultValue = "queue")
public String destinationType;

/**
* What virtual host should the binding be associated with?
*/
@ConfigItem(defaultValue = "/")
public String vhost;

/**
* Extra arguments for the binding definition.
*/
Expand Down Expand Up @@ -180,6 +199,12 @@ public static class Binding {
@ConfigDocMapKey("binding-name")
public Map<String, Binding> bindings;

/**
* Virtual hosts that should be predefined after starting the RabbitMQ broker.
*/
@ConfigItem
public Optional<List<String>> vhosts;

/**
* Environment variables that are passed to the container.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,13 @@ private RunningDevService startRabbitMQBroker(DockerStatusBuildItem dockerStatus
config.fixedExposedHttpPort,
launchMode.getLaunchMode() == LaunchMode.DEVELOPMENT ? config.serviceName : null);

config.exchanges.forEach(x -> container.withExchange(x.name, x.type, x.autoDelete, false, x.durable, x.arguments));
config.queues.forEach(x -> container.withQueue(x.name, x.autoDelete, x.durable, x.arguments));
config.vhosts.forEach(container::withVhost);
config.exchanges
.forEach(x -> container.withExchange(x.vhost, x.name, x.type, x.autoDelete, false, x.durable, x.arguments));
config.queues.forEach(x -> container.withQueue(x.vhost, x.name, x.autoDelete, x.durable, x.arguments));
config.bindings
.forEach(b -> container.withBinding(b.source, b.destination, b.arguments, b.routingKey, b.destinationType));
.forEach(b -> container.withBinding(b.vhost, b.source, b.destination, b.arguments, b.routingKey,
b.destinationType));

final Supplier<RunningDevService> defaultRabbitMQBrokerSupplier = () -> {

Expand Down Expand Up @@ -253,6 +256,7 @@ static class Exchange {
String type;
Boolean autoDelete;
Boolean durable;
String vhost;
Map<String, Object> arguments;

Exchange(Map.Entry<String, RabbitMQDevServicesBuildTimeConfig.Exchange> entry) {
Expand All @@ -264,6 +268,7 @@ static class Exchange {
this.type = source.type;
this.autoDelete = source.autoDelete;
this.durable = source.durable;
this.vhost = source.vhost;
this.arguments = source.arguments != null
? source.arguments.entrySet().stream().collect(Collectors.toMap(Entry::getKey, Entry::getValue))
: Map.of();
Expand All @@ -274,6 +279,7 @@ static class Queue {
String name;
Boolean autoDelete;
Boolean durable;
String vhost;
Map<String, Object> arguments;

Queue(Map.Entry<String, RabbitMQDevServicesBuildTimeConfig.Queue> entry) {
Expand All @@ -284,6 +290,7 @@ static class Queue {
this.name = name;
this.autoDelete = source.autoDelete;
this.durable = source.durable;
this.vhost = source.vhost;
this.arguments = source.arguments != null
? source.arguments.entrySet().stream().collect(Collectors.toMap(Entry::getKey, Entry::getValue))
: Map.of();
Expand All @@ -295,6 +302,7 @@ static class Binding {
String destination;
String routingKey;
String destinationType;
String vhost;
Map<String, Object> arguments;

Binding(Map.Entry<String, RabbitMQDevServicesBuildTimeConfig.Binding> entry) {
Expand All @@ -306,6 +314,7 @@ static class Binding {
this.routingKey = source.routingKey;
this.destination = source.destination.orElse(name);
this.destinationType = source.destinationType;
this.vhost = source.vhost;
this.arguments = source.arguments != null
? source.arguments.entrySet().stream().collect(Collectors.toMap(Entry::getKey, Entry::getValue))
: Map.of();
Expand All @@ -321,6 +330,7 @@ static class Binding {
private final List<Exchange> exchanges;
private final List<Queue> queues;
private final List<Binding> bindings;
private final List<String> vhosts;
private final Map<String, String> containerEnv;

public RabbitMQDevServiceCfg(RabbitMQDevServicesBuildTimeConfig devServicesConfig) {
Expand All @@ -339,6 +349,7 @@ public RabbitMQDevServiceCfg(RabbitMQDevServicesBuildTimeConfig devServicesConfi
this.bindings = devServicesConfig.bindings != null
? devServicesConfig.bindings.entrySet().stream().map(Binding::new).collect(Collectors.toList())
: Collections.emptyList();
this.vhosts = devServicesConfig.vhosts.orElse(Collections.emptyList());
this.containerEnv = devServicesConfig.containerEnv;
}

Expand Down
1 change: 1 addition & 0 deletions integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@
<module>reactive-messaging-pulsar</module>
<module>reactive-messaging-mqtt</module>
<module>reactive-messaging-rabbitmq</module>
<module>reactive-messaging-rabbitmq-devservices</module>
<module>reactive-messaging-rabbitmq-dyn</module>
<module>reactive-messaging-hibernate-reactive</module>
<module>reactive-messaging-hibernate-orm</module>
Expand Down
Loading

0 comments on commit b889f39

Please sign in to comment.