Set up Mosquitto with the Aspire integration
Konten ini belum tersedia dalam bahasa Anda.
Use the Mosquitto hosting integration to run Eclipse Mosquitto as an Aspire container resource. For a short introduction, see Get started with the Mosquitto integration.
Installation
Section titled “Installation”aspire add communitytoolkit-mosquittoAspire CLI interaktif; pilih hasil pencarian yang sesuai saat diminta:
Select an integration to add:
> communitytoolkit-mosquitto (CommunityToolkit.Aspire.Hosting.Mosquitto)> Other results listed as selectable options...#:package CommunityToolkit.Aspire.Hosting.Mosquitto@*<PackageReference Include="CommunityToolkit.Aspire.Hosting.Mosquitto" Version="*" />Add a Mosquitto resource
Section titled “Add a Mosquitto resource”import { createBuilder } from './.aspire/modules/aspire.mjs';
const builder = await createBuilder();const mqtt = await builder.addMosquitto('mqtt');
const api = await builder.addProject('api', '../Api/Api.csproj');await api.withReference(mqtt);await api.waitFor(mqtt);
await builder.build().run();var builder = DistributedApplication.CreateBuilder(args);var mqtt = builder.AddMosquitto("mqtt");
builder.AddProject<Projects.Api>("api") .WithReference(mqtt) .WaitFor(mqtt);
// After adding all resources, run the app...builder.Build().Run();The resource exposes the MQTT protocol endpoint on container port 1883. Aspire assigns a host port unless you specify port. The generated connection string uses mqtt://host:port. The integration’s health check attempts an MQTT connection and reports healthy only after the broker accepts it.
Persist broker data
Section titled “Persist broker data”The container stores state under /mosquitto/data. A volume preserves it independently of the container. A bind mount makes the files accessible from the host.
const mqtt = await builder.addMosquitto('mqtt');await mqtt.withDataVolume();// Or use a host directory:// await mqtt.withDataBindMount('./mosquitto-data');var mqtt = builder.AddMosquitto("mqtt") .WithDataVolume();// Or use a host directory:// var mqtt = builder.AddMosquitto("mqtt")// .WithDataBindMount("./mosquitto-data");Customize broker configuration
Section titled “Customize broker configuration”The default mosquitto.conf listens on port 1883, permits anonymous connections, and persists state under /mosquitto/data. Replace it with a configuration appropriate to your environment by copying a local configuration directory into the container:
var mqtt = builder.AddMosquitto("mqtt") .WithContainerFiles("/mosquitto/config", "./mosquitto-config");Keep authentication and listener settings aligned with the applications that connect to the broker. The default anonymous configuration is intended for local development, not an exposed production broker.
AppHost API
Section titled “AppHost API”| Method | Purpose |
|---|---|
AddMosquitto / addMosquitto | Add an MQTT broker, optionally pinning the host port. |
WithDataVolume / withDataVolume | Persist Mosquitto data in a named volume. |
WithDataBindMount / withDataBindMount | Persist Mosquitto data in a host directory. |