Перейти до вмісту

RabbitMQ hosting integration

Цей контент ще не доступний вашою мовою.

RabbitMQ logo

The RabbitMQ hosting integration models a RabbitMQ server as the RabbitMQServerResource type. To access this type and its APIs add the 📦 Aspire.Hosting.RabbitMQ NuGet package in the AppHost project.

Aspire CLI — Додати пакет Aspire.Hosting.RabbitMQ
aspire add rabbitmq

Aspire CLI інтерактивний; оберіть відповідний результат пошуку:

Aspire CLI — Приклад виводу
Select an integration to add:
> rabbitmq (Aspire.Hosting.RabbitMQ)
> Other results listed as selectable options...

In your AppHost project, call AddRabbitMQ on the builder instance to add a RabbitMQ server resource:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var rabbitmq = builder.AddRabbitMQ("messaging");
builder.AddProject<Projects.ExampleProject>()
.WithReference(rabbitmq);
// After adding all resources, run the app...
  1. When Aspire adds a container image to the app host, as shown in the preceding example with the docker.io/library/rabbitmq image, it creates a new RabbitMQ server instance on your local machine. A reference to your RabbitMQ server (the rabbitmq variable) is added to the ExampleProject.

  2. The RabbitMQ server resource includes default credentials with a username of "guest" and randomly generated password using the CreateDefaultPasswordParameter method.

  3. The WithReference method configures a connection in the ExampleProject named "messaging".

Add RabbitMQ server resource with data volume

Section titled “Add RabbitMQ server resource with data volume”

To add a data volume to the RabbitMQ server resource, call the WithDataVolume method on the RabbitMQ server resource:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var rabbitmq = builder.AddRabbitMQ("messaging")
.WithDataVolume(isReadOnly: false);
builder.AddProject<Projects.ExampleProject>()
.WithReference(rabbitmq);
// After adding all resources, run the app...

The data volume is used to persist the RabbitMQ server data outside the lifecycle of its container. The data volume is mounted at the /var/lib/rabbitmq path in the RabbitMQ server container and when a name parameter isn’t provided, the name is generated at random. For more information on data volumes and details on why they’re preferred over bind mounts, see Docker docs: Volumes.

Add RabbitMQ server resource with data bind mount

Section titled “Add RabbitMQ server resource with data bind mount”

To add a data bind mount to the RabbitMQ server resource, call the WithDataBindMount method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var rabbitmq = builder.AddRabbitMQ("messaging")
.WithDataBindMount(
source: @"/RabbitMQ/Data",
isReadOnly: false);
builder.AddProject<Projects.ExampleProject>()
.WithReference(rabbitmq);
// After adding all resources, run the app...

Data bind mounts rely on the host machine’s filesystem to persist the RabbitMQ server data across container restarts. The data bind mount is mounted at the C:\RabbitMQ\Data on Windows (or /RabbitMQ/Data on Unix) path on the host machine in the RabbitMQ server container. For more information on data bind mounts, see Docker docs: Bind mounts.

Add RabbitMQ server resource with parameters

Section titled “Add RabbitMQ server resource with parameters”

When you want to explicitly provide the username and password used by the container image, you can provide these credentials as parameters. Consider the following alternative example:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var username = builder.AddParameter("username", secret: true);
var password = builder.AddParameter("password", secret: true);
var rabbitmq = builder.AddRabbitMQ("messaging", username, password);
builder.AddProject<Projects.ExampleProject>()
.WithReference(rabbitmq);
// After adding all resources, run the app...

Add RabbitMQ server resource with management plugin

Section titled “Add RabbitMQ server resource with management plugin”

To add the RabbitMQ management plugin to the RabbitMQ server resource, call the WithManagementPlugin method. Remember to use parameters to set the credentials for the container. You’ll need these credentials to log into the management plugin:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var username = builder.AddParameter("username", secret: true);
var password = builder.AddParameter("password", secret: true);
var rabbitmq = builder.AddRabbitMQ("messaging", username, password)
.WithManagementPlugin();
builder.AddProject<Projects.ExampleProject>()
.WithReference(rabbitmq);
// After adding all resources, run the app...

The RabbitMQ management plugin provides an HTTP-based API for management and monitoring of your RabbitMQ server. Aspire adds another container image docker.io/library/rabbitmq-management to the AppHost that runs the management plugin. You can access the management plugin from the Aspire dashboard by selecting an endpoint for your RabbitMQ resource.

The RabbitMQ hosting integration automatically adds a health check for the RabbitMQ server resource. The health check verifies that the RabbitMQ server is running and that a connection can be established to it.

The hosting integration relies on the 📦 AspNetCore.HealthChecks.Rabbitmq NuGet package.