Connect Aspire apps to LavinMQ
이 콘텐츠는 아직 번역되지 않았습니다.
This page describes how consuming apps connect to a LavinMQ resource. For broker ports, storage, management, and health checks, see Set up LavinMQ in the Aspire AppHost.
LavinMQ implements AMQP 0-9-1 and is wire-compatible with RabbitMQ. There is no LavinMQ-specific Community Toolkit client package; use a RabbitMQ-compatible client.
Connection properties
Section titled “Connection properties”Aspire exposes each property as an environment variable named [RESOURCE]_[PROPERTY]. A resource named lavinmq provides:
| Property | Environment variable | Description |
|---|---|---|
Host | LAVINMQ_HOST | Broker hostname or IP address |
Port | LAVINMQ_PORT | AMQP endpoint port |
Username | LAVINMQ_USERNAME | Username; defaults to guest |
Password | LAVINMQ_PASSWORD | Password; defaults to guest |
Uri | LAVINMQ_URI | amqp://{Username}:{Password}@{Host}:{Port}/ |
Unlike the Aspire RabbitMQ resource, the LavinMQ resource doesn’t expose a VirtualHost property. Its URI uses /.
For an external broker, model the connection string in the AppHost instead of adding a LavinMQ container:
var builder = DistributedApplication.CreateBuilder(args);
var lavinmq = builder.AddConnectionString("lavinmq");
builder.AddProject<Projects.Worker>("worker") .WithReference(lavinmq);
builder.Build().Run();import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.aspire/modules/aspire.mjs';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const lavinmq: IResourceWithConnectionString
lavinmq = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addConnectionString(name: string, options?: { environmentVariableNameOrExpression?: ReferenceExpression;}): IResourceWithConnectionString (+1 overload)
Adds a connection string resource
addConnectionString('lavinmq');
await const builder: IDistributedApplicationBuilder
builder .IDistributedApplicationBuilder.addNodeApp(name: string, appDirectory: string, scriptPath: string): NodeAppResource
Adds a node application to the application model. Node should be available on the PATH.
addNodeApp('worker', './worker', 'index.js') .ExecutableResource.withReference(source: EndpointReference | string | uri, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): NodeAppResource (+1 overload)
Adds a reference to another resource
withReference(const lavinmq: IResourceWithConnectionString
lavinmq);
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.build(): DistributedApplication
Builds the distributed application
build().DistributedApplication.run(cancellationToken?: cancellationToken): void
Runs the distributed application
run();C# configuration reads ConnectionStrings:lavinmq; environment-variable consumers read ConnectionStrings__lavinmq. The individual LavinMQ properties aren’t injected for an external connection-string resource.
Connect from your app
Section titled “Connect from your app”C# apps can use the Aspire RabbitMQ client integration because LavinMQ is wire-compatible with RabbitMQ. Install:
dotnet add package Aspire.RabbitMQ.Client#:package Aspire.RabbitMQ.Client@*<PackageReference Include="Aspire.RabbitMQ.Client" Version="*" />Register the client with the LavinMQ resource name:
builder.AddRabbitMQClient(connectionName: "lavinmq");Then resolve RabbitMQ.Client.IConnection through dependency injection.
To use the URI directly with the RabbitMQ .NET client:
using RabbitMQ.Client;
var uri = Environment.GetEnvironmentVariable("LAVINMQ_URI") ?? throw new InvalidOperationException("LAVINMQ_URI is not set.");
var factory = new ConnectionFactory { Uri = new Uri(uri) };using var connection = await factory.CreateConnectionAsync();using var channel = await connection.CreateChannelAsync();Install amqplib and its TypeScript declarations:
npm install amqplibnpm install --save-dev @types/amqplibimport amqp from 'amqplib';
const uri = process.env.LAVINMQ_URI;if (!uri) { throw new Error('LAVINMQ_URI is not set.');}
const connection = await amqp.connect(uri);const channel = await connection.createChannel();
// Publish or consume messages with channel.
await channel.close();await connection.close();Install pika:
pip install pikaimport os
import pika
parameters = pika.URLParameters(os.environ["LAVINMQ_URI"])connection = pika.BlockingConnection(parameters)channel = connection.channel()
# Publish or consume messages with channel.
connection.close()Install the official RabbitMQ AMQP 0-9-1 Go client:
go get github.com/rabbitmq/amqp091-gopackage main
import ( "os"
amqp "github.com/rabbitmq/amqp091-go")
func main() { connection, err := amqp.Dial(os.Getenv("LAVINMQ_URI")) if err != nil { panic(err) } defer connection.Close()
channel, err := connection.Channel() if err != nil { panic(err) } defer channel.Close()
// Publish or consume messages with channel.}Health checks and telemetry
Section titled “Health checks and telemetry”Aspire.RabbitMQ.Client adds a connection health check and RabbitMQ client tracing for C# apps. Other language clients provide their own logging, recovery, and instrumentation options.