Set up RabbitMQ in the AppHost
यह कंटेंट अभी तक आपकी भाषा में उपलब्ध नहीं है।
This article is the reference for the Aspire RabbitMQ Hosting integration. It enumerates the AppHost APIs — with examples for both AppHost.cs and apphost.ts — that you use to model a RabbitMQ server resource in your AppHost project.
If you’re new to the RabbitMQ integration, start with the Get started with RabbitMQ integrations guide. For how consuming apps read the connection information this page exposes, see Connect to RabbitMQ.
Installation
Section titled “Installation”To start building an Aspire app that uses RabbitMQ, install the 📦 Aspire.Hosting.RabbitMQ NuGet package:
aspire add rabbitmqLearn more about aspire add in the command reference.
Or, choose a manual installation approach:
#:package Aspire.Hosting.RabbitMQ@*<PackageReference Include="Aspire.Hosting.RabbitMQ" Version="*" />aspire add rabbitmqLearn more about aspire add in the command reference.
This updates your aspire.config.json with the RabbitMQ hosting integration package:
{ "packages": { "Aspire.Hosting.RabbitMQ": "13.3.0" }}Add RabbitMQ server resource
Section titled “Add RabbitMQ server resource”Once you’ve installed the hosting integration in your AppHost project, you can add a RabbitMQ server resource as shown in the following examples:
var builder = DistributedApplication.CreateBuilder(args);
var rabbitmq = builder.AddRabbitMQ("messaging");
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice") .WithReference(rabbitmq);
// After adding all resources, run the app...import { createBuilder } from './.modules/aspire.js';
const builder = await createBuilder();
const rabbitmq = await builder.addRabbitMQ("messaging");
await builder.addNodeApp("api", "./api", "index.js") .withReference(rabbitmq);
// After adding all resources, run the app...-
When Aspire adds a container image to the AppHost, as shown in the preceding example with the
docker.io/library/rabbitmqimage, it creates a new RabbitMQ server instance on your local machine. -
The RabbitMQ server resource includes default credentials with a
usernameof"guest"and a randomly generatedpasswordusing theCreateDefaultPasswordParametermethod. -
The AppHost reference call configures a connection in the consuming project named after the referenced RabbitMQ resource, such as
messagingin the preceding example.
Add RabbitMQ server resource with data volume
Section titled “Add RabbitMQ server resource with data volume”Add a data volume to the RabbitMQ server resource as shown in the following examples:
var builder = DistributedApplication.CreateBuilder(args);
var rabbitmq = builder.AddRabbitMQ("messaging") .WithDataVolume(isReadOnly: false);
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice") .WithReference(rabbitmq);
// After adding all resources, run the app...import { createBuilder } from './.modules/aspire.js';
const builder = await createBuilder();
const rabbitmq = await builder.addRabbitMQ("messaging");await rabbitmq.withDataVolume({ isReadOnly: false });
await builder.addNodeApp("api", "./api", "index.js") .withReference(rabbitmq);
// After adding all resources, run the app...The data volume is used to persist 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”Add a data bind mount to the RabbitMQ server resource as shown in the following examples:
var builder = DistributedApplication.CreateBuilder(args);
var rabbitmq = builder.AddRabbitMQ("messaging") .WithDataBindMount( source: "/RabbitMQ/Data", isReadOnly: false);
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice") .WithReference(rabbitmq);
// After adding all resources, run the app...import { createBuilder } from './.modules/aspire.js';
const builder = await createBuilder();
const rabbitmq = await builder.addRabbitMQ("messaging");await rabbitmq.withDataBindMount("/RabbitMQ/Data", { isReadOnly: false });
await builder.addNodeApp("api", "./api", "index.js") .withReference(rabbitmq);
// After adding all resources, run the app...Data bind mounts rely on the host machine’s filesystem to persist 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 examples:
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);
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice") .WithReference(rabbitmq);
// After adding all resources, run the app...import { createBuilder } from './.modules/aspire.js';
const builder = await createBuilder();
const userName = await builder.addParameter("username", { secret: true });const password = await builder.addParameter("password", { secret: true });
const rabbitmq = await builder.addRabbitMQ("messaging", { userName, password });
await builder.addNodeApp("api", "./api", "index.js") .withReference(rabbitmq);
// After adding all resources, run the app...When no password parameter is provided, Aspire generates a strong password automatically using the CreateDefaultPasswordParameter method.
Add RabbitMQ server resource with management plugin
Section titled “Add RabbitMQ server resource with management plugin”To add the RabbitMQ management plugin as a sub-resource, call WithManagementPlugin (or withManagementPlugin). You’ll need the credentials you configured to log in to the management UI:
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();
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice") .WithReference(rabbitmq);
// After adding all resources, run the app...import { createBuilder } from './.modules/aspire.js';
const builder = await createBuilder();
const userName = await builder.addParameter("username", { secret: true });const password = await builder.addParameter("password", { secret: true });
const rabbitmq = await builder.addRabbitMQ("messaging", { userName, password });await rabbitmq.withManagementPlugin();
await builder.addNodeApp("api", "./api", "index.js") .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 using the docker.io/library/rabbitmq management variant to the AppHost that runs the management plugin on port 15672 by default. You can access the management UI from the Aspire dashboard by selecting the endpoint for your RabbitMQ resource.
To configure a custom host port for the management plugin, pass the port option:
var rabbitmq = builder.AddRabbitMQ("messaging") .WithManagementPlugin(port: 15672);await rabbitmq.withManagementPlugin({ port: 15672 });Pass custom environment variables
Section titled “Pass custom environment variables”By default, Aspire injects the RabbitMQ connection information using variable names derived from the resource name (for example, MESSAGING_URI, MESSAGING_HOST, MESSAGING_PORT, MESSAGING_USERNAME, MESSAGING_PASSWORD). If your consuming app expects a different set of environment variable names, pass individual connection properties from the AppHost:
var builder = DistributedApplication.CreateBuilder(args);
var rabbitmq = builder.AddRabbitMQ("messaging");
var app = builder.AddExecutable("my-app", "node", "app.js", ".") .WithReference(rabbitmq) .WithEnvironment(context => { context.EnvironmentVariables["RABBITMQ_HOST"] = rabbitmq.Resource.PrimaryEndpoint.Property(EndpointProperty.Host); context.EnvironmentVariables["RABBITMQ_PORT"] = rabbitmq.Resource.PrimaryEndpoint.Property(EndpointProperty.Port); context.EnvironmentVariables["RABBITMQ_USER"] = rabbitmq.Resource.UserNameParameter; context.EnvironmentVariables["RABBITMQ_PASSWORD"] = rabbitmq.Resource.PasswordParameter; });
builder.Build().Run();import { createBuilder, EndpointProperty } from './.modules/aspire.js';
const builder = await createBuilder();
const rabbitmq = await builder.addRabbitMQ("messaging");const rabbitmqEndpoint = await rabbitmq.getEndpoint("tcp");const rabbitmqHost = await rabbitmqEndpoint.property(EndpointProperty.Host);const rabbitmqPort = await rabbitmqEndpoint.property(EndpointProperty.Port);
await builder.addNodeApp("my-app", "./app", "index.js") .withReference(rabbitmq) .withEnvironment("RABBITMQ_HOST", rabbitmqHost) .withEnvironment("RABBITMQ_PORT", rabbitmqPort) .withEnvironment("RABBITMQ_USER", await rabbitmq.userNameParameter()) .withEnvironment("RABBITMQ_PASSWORD", await rabbitmq.passwordParameter());
await builder.build().run();Connect to an existing RabbitMQ instance
Section titled “Connect to an existing RabbitMQ instance”In C#, call AsExisting instead of AddRabbitMQ to reference an externally managed RabbitMQ instance:
var builder = DistributedApplication.CreateBuilder(args);
var rabbitmq = builder.AddRabbitMQ("messaging") .AsExisting(connectionStringParameter: builder.AddParameter("messaging-cs", secret: true));
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice") .WithReference(rabbitmq);
// After adding all resources, run the app...The TypeScript AppHost doesn’t currently expose an asExisting(...) API for RabbitMQ. To connect to an existing RabbitMQ instance from a TypeScript AppHost, use builder.addConnectionString(...) with a parameter and reference that connection string from your consuming apps instead.
Connection properties
Section titled “Connection properties”For the full reference of RabbitMQ connection properties — and how consuming apps in C#, TypeScript, Python, and Go read them — see Connect to RabbitMQ.
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.