RabbitMQ hosting integration
Questi contenuti non sono ancora disponibili nella tua lingua.
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 add rabbitmqLa CLI Aspire è interattiva; seleziona il risultato corretto quando richiesto:
Select an integration to add:
> rabbitmq (Aspire.Hosting.RabbitMQ)> Other results listed as selectable options...#:package Aspire.Hosting.RabbitMQ@*<PackageReference Include="Aspire.Hosting.RabbitMQ" Version="*" />Add RabbitMQ server resource
Section titled “Add RabbitMQ server resource”In your AppHost project, call AddRabbitMQ on the builder instance to add a RabbitMQ server resource:
var builder = DistributedApplication.CreateBuilder(args);
var rabbitmq = builder.AddRabbitMQ("messaging");
builder.AddProject<Projects.ExampleProject>() .WithReference(rabbitmq);
// After adding all resources, run the app...-
When Aspire adds a container image to the app host, as shown in the preceding example with the
docker.io/library/rabbitmqimage, it creates a new RabbitMQ server instance on your local machine. A reference to your RabbitMQ server (therabbitmqvariable) is added to theExampleProject. -
The RabbitMQ server resource includes default credentials with a
usernameof"guest"and randomly generatedpasswordusing theCreateDefaultPasswordParametermethod. -
The
WithReferencemethod configures a connection in theExampleProjectnamed"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:
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:
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:
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:
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.
Hosting integration health checks
Section titled “Hosting integration health checks”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.