Skip to content

NATS Aspire container - #1175

Merged
Sébastien Ros (sebastienros) merged 57 commits into
microsoft:mainfrom
mtmk:nats
Mar 7, 2024
Merged

NATS Aspire container#1175
Sébastien Ros (sebastienros) merged 57 commits into
microsoft:mainfrom
mtmk:nats

Conversation

@mtmk

@mtmk Ziya Suzen (mtmk) commented Dec 2, 2023

Copy link
Copy Markdown
Contributor

This PR is an effort to introduce NATS container and client support.

Implemented most of the features as much as I could gather from other implementations. There are a few open tasks as far as I can see which may be left for another PR:

@ghost ghost added the area-integrations Issues pertaining to Aspire Integrations packages label Dec 2, 2023
Comment thread src/Aspire.Hosting.Nats/Aspire.Hosting.Nats.csproj Outdated
@oising

Oisin Grehan (oising) commented Dec 8, 2023

Copy link
Copy Markdown
Contributor

Ziya Suzen (@mtmk) I'm rebasing this on latest main to fix merge conflicts and update any broken integration points. Also updating to use centralized package management /cc Reuben Bond (@ReubenBond)

@joperezr Jose Perez Rodriguez (joperezr) added the community-contribution Indicates that the PR has been added by a community member label Dec 11, 2023
@stebet

Copy link
Copy Markdown

Oooh exciting! Does it also have cluster/JetStream support?

@mtmk

Ziya Suzen (mtmk) commented Jan 14, 2024

Copy link
Copy Markdown
Contributor Author

Oooh exciting! Does it also have cluster/JetStream support?

Not yet 😅 but I think you can add the argument yourself for now if you like:

var nats = builder.AddNatsContainer("nats-server").WithArgs("-js");

Edit: also added options to AddNatsContainer which essentially does the above, even though I'm not sure if that's the correct approach. (Really need to look at others to see how they've done it)

var nats = builder.AddNatsContainer("nats-server", enableJetStream = true);

@mtmk

Copy link
Copy Markdown
Contributor Author

Oisin Grehan (@oising) do you think we're ready for review?

@mtmk

Copy link
Copy Markdown
Contributor Author

@azure-pipelinesazure-pipelines
/ dotnet.aspire
src/Aspire.Hosting.Nats/Aspire.Hosting.Nats.csproj#L0
src/Aspire.Hosting.Nats/Aspire.Hosting.Nats.csproj(0,0): error NU1102: (NETCORE_ENGINEERING_TELEMETRY=Restore) Unable to find package NATS.Net with version (>= 2.1.0)

Mitch Denny (@mitchdenny), i'm not sure why ci is failing NATS.Net v2.1.0 in up on NuGet https://www.nuget.org/packages/NATS.Net/2.1.0

@@ -0,0 +1,17 @@
using NATS.Client.Core;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this app a little more real? Can it use pubsub as well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, let me have a go!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a simple JetStream example since it's easier demonstrate persisted messages using Web API. As for pubsub I suppose we'd need a frontend using SignalR or Blazor. Maybe a simple chat or something?

Comment thread playground/nats/Nats.ApiService/Program.cs Outdated
Comment thread playground/nats/Nats.Backend/Program.cs Outdated
{
await foreach (var msg in nats.SubscribeAsync<AppEvent>("events.>", cancellationToken: _cts.Token).ConfigureAwait(false))
{
Console.WriteLine($"Processing event: {msg.Data}");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ILogger?


var manifest = await ManifestUtils.GetManifest(nats.Resource);

Assert.Equal("container.v0", manifest["type"]?.ToString());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ziya Suzen (@mtmk) we made some changes here https://github.com/dotnet/aspire/blob/f6a2f9f0b5aca2553aeab6d09ec657cffc30d9fd/tests/Aspire.Hosting.Tests/Kafka/AddKafkaTests.cs#L75-L95.

BTW I'm sorry that these changes must feel like a roller coaster 😄. FWIW, it's helping us land on solid patterns that seem to scale well 🚀

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no worries, my fault really having the PR open so long. thanks for bearing with me 🙏 also loving to witness the evolution 😍

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<PackageReference Include="Microsoft.Extensions.Configuration.Binder" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" />
<PackageReference Include="NATS.Net" />

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our naming rules are https://github.com/dotnet/aspire/tree/main/src/Components#naming.

The name of this component should be Aspire.NATS.Net to align with those rules.

@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This project and folder should be named Nats.AppHost.

@eerhardt Eric Erhardt (eerhardt) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread tests/Aspire.Nats.Client.Tests/ConfigurationTests.cs Outdated
Comment on lines +43 to +44
protected override void SetHealthCheck(NatsClientSettings options, bool enabled)
=> throw new NotImplementedException();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have health checks in the component. We should enable these tests.


namespace Aspire.Nats.Client;

public class NatsHealthCheck(INatsConnection connection) : IHealthCheck

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be public.

Comment on lines +13 to +16
return Task.FromResult(connection.ConnectionState == NatsConnectionState.Open
? HealthCheckResult.Healthy()
: HealthCheckResult.Unhealthy());
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated to be a bit more granular i.e. added degraded state. But description and stats only exist on the old client Nats.Client v1. One we're using here is v2 and it's a rewrite and most of that is not exposed yet.

"properties": {
"Net": {
"type": "object",
"properties": {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I shall look into this in another PR

@sebastienros

Sébastien Ros (sebastienros) commented Mar 7, 2024

Copy link
Copy Markdown
Contributor

Fixed conflicts, added manifest.json and renamed AddNats() to AddNatsClient to follow a recent change we did in other libraries to prevent conflict with the distributed app extension methods.

Tried the sample, worked for me, then using the Swagger I found an issue, it's not taking the custom json converter for TimeSpan properties in the config object, I believe it's an issue in SwaggerUI. (found domaindrivendev/Swashbuckle.AspNetCore#2500)

Ziya Suzen (@mtmk) if you are ok with my changes I'll merge it.

@sebastienros
Sébastien Ros (sebastienros) merged commit 44cde5d into microsoft:main Mar 7, 2024
@mtmk
Ziya Suzen (mtmk) deleted the nats branch March 7, 2024 07:24
@davidfowl

Copy link
Copy Markdown
Collaborator

Thank you Ziya Suzen (@mtmk) ! I look forward to future contributions!

@github-actions github-actions Bot locked and limited conversation to collaborators Apr 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-integrations Issues pertaining to Aspire Integrations packages community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants