跳转到内容

NATS hosting integration

此内容尚不支持你的语言。

NATS logo

To get started with the Aspire NATS hosting integration, install the Aspire.Hosting.Nats NuGet package in the app host project.

Aspire CLI — 添加 Aspire.Hosting.Nats 包
aspire add nats

Aspire CLI 是交互式的;按提示选择合适的搜索结果:

Aspire CLI — 输出示例
Select an integration to add:
> nats (Aspire.Hosting.Nats)
> Other results listed as selectable options...

In your app host project, register and consume a NATS server integration using the AddNats extension method to add the NATS server resource to the builder:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var nats = builder.AddNats("nats");
builder.AddProject<Projects.ExampleProject>()
.WithReference(nats);
// After adding all resources, run the app...

To add NATS with JetStream enabled, which provides streaming capabilities, use the WithJetStream extension method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var nats = builder.AddNats("nats")
.WithJetStream();
builder.AddProject<Projects.ExampleProject>()
.WithReference(nats);

To add data volumes for persisting NATS data, call the WithDataVolume method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var nats = builder.AddNats("nats")
.WithJetStream()
.WithDataVolume();
builder.AddProject<Projects.ExampleProject>()
.WithReference(nats);

The data volume is used to persist the NATS data outside the lifecycle of the container.

For development scenarios, you may want to use bind mounts instead of data volumes. To add a data bind mount, call the WithDataBindMount method:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var nats = builder.AddNats("nats")
.WithJetStream()
.WithDataBindMount(source: @"C:\Nats\Data");
builder.AddProject<Projects.ExampleProject>()
.WithReference(nats);

To add NATS with username and password authentication, use the WithNatsUser and WithNatsPassword parameters:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var user = builder.AddParameter("natsUser");
var pass = builder.AddParameter("natsPass", secret: true);
var nats = builder.AddNats("nats", userName: user, password: pass);
builder.AddProject<Projects.ExampleProject>()
.WithReference(nats);