-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: code review taken into account
Signed-off-by: Damien Blanchet <[email protected]>
- Loading branch information
1 parent
bb5df0b
commit 1a03b27
Showing
19 changed files
with
241 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Nats transport provider for SlimMessageBus <!-- omit in toc --> | ||
|
||
Please read the [Introduction](intro.md) before reading this provider documentation. | ||
|
||
- [Underlying Nats client](#underlying-nats-client) | ||
- [Configuration](#configuration) | ||
- [Message Serialization](#message-serialization) | ||
|
||
## Underlying Nats client | ||
|
||
This transport provider uses [Nats.Nets](https://www.nuget.org/packages/NATS.Net) client to connect to the Nats broker. | ||
|
||
## Configuration | ||
|
||
The configuration is arranged via the `.WithProviderNats(cfg => {})` method on the message bus builder. | ||
|
||
@[:cs](../src/Samples/Sample.Nats.WebApi/Program.cs,ExampleConfiguringMessageBus) | ||
|
||
The `NatsMessageBusSettings` property is used to configure the underlying [Nats.Net library client](https://github.com/nats-io/nats.net). | ||
Please consult the Nats.Net library docs for more configuration options. | ||
|
||
## Message Serialization | ||
|
||
Nats offers native serialization functionality. This functionality conflicts with the serialization functionality provided by SlimMessageBus. We have chosen to leave the responsibility for serialization to SlimMessageBus and leave the default configuration of Nats serialization, which is raw serialization. This means that the message body is serialized as a byte array. |
7 changes: 7 additions & 0 deletions
7
src/Samples/Infrastructure/Nats-SingleNode/docker-compose.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
version: '2' | ||
services: | ||
nats: | ||
container_name: slim.nats | ||
image: nats:2.10 | ||
ports: | ||
- 4222:4222 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace Sample.Nats.WebApi; | ||
|
||
using SlimMessageBus; | ||
|
||
public class PingConsumer(ILogger<PingConsumer> logger) : IConsumer<PingMessage>, IConsumerWithContext | ||
{ | ||
private readonly ILogger _logger = logger; | ||
|
||
public IConsumerContext Context { get; set; } | ||
|
||
public Task OnHandle(PingMessage message) | ||
{ | ||
_logger.LogInformation("Got message {Counter} on topic {Path}", message.Counter, Context.Path); | ||
return Task.CompletedTask; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
namespace Sample.Nats.WebApi; | ||
|
||
public record PingMessage(int Counter, Guid Value); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using NATS.Client.Core; | ||
|
||
using Sample.Nats.WebApi; | ||
|
||
using SecretStore; | ||
|
||
using SlimMessageBus; | ||
using SlimMessageBus.Host; | ||
using SlimMessageBus.Host.Nats.Config; | ||
using SlimMessageBus.Host.Serialization.Json; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.Services.AddEndpointsApiExplorer(); | ||
builder.Services.AddSwaggerGen(); | ||
|
||
Secrets.Load(@"..\..\..\..\..\secrets.txt"); | ||
|
||
var endpoint = Secrets.Service.PopulateSecrets(builder.Configuration["Nats:Endpoint"]); | ||
var topic = Secrets.Service.PopulateSecrets(builder.Configuration["Nats:Topic"]); | ||
|
||
// doc:fragment:ExampleConfiguringMessageBus | ||
builder.Services.AddSlimMessageBus(messageBusBuilder => | ||
{ | ||
messageBusBuilder.WithProviderNats(cfg => | ||
{ | ||
cfg.Endpoint = endpoint; | ||
cfg.ClientName = $"MyService_{Environment.MachineName}"; | ||
cfg.AuthOpts = NatsAuthOpts.Default; | ||
}); | ||
|
||
messageBusBuilder | ||
.Produce<PingMessage>(x => x.DefaultTopic(topic)) | ||
.Consume<PingMessage>(x => x.Topic(topic).Instances(1)); | ||
|
||
messageBusBuilder.AddServicesFromAssemblyContaining<PingConsumer>(); | ||
messageBusBuilder.AddJsonSerializer(); | ||
}); | ||
// doc:fragment:ExampleConfiguringMessageBus | ||
|
||
var app = builder.Build(); | ||
|
||
if (app.Environment.IsDevelopment()) | ||
{ | ||
app.UseSwagger(); | ||
app.UseSwaggerUI(); | ||
} | ||
|
||
app.UseHttpsRedirection(); | ||
|
||
app.MapGet("/publish-message", (IMessageBus messageBus, CancellationToken cancellationToken) => | ||
{ | ||
PingMessage pingMessage = new(0, Guid.NewGuid()); | ||
messageBus.Publish(pingMessage, cancellationToken: cancellationToken); | ||
}); | ||
|
||
app.Run(); |
14 changes: 14 additions & 0 deletions
14
src/Samples/Sample.Nats.WebApi/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"profiles": { | ||
"Samples.Nats.WebApi": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "swagger", | ||
"applicationUrl": "https://localhost:7276;http://localhost:5044", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.2"/> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\SlimMessageBus.Host.Nats\SlimMessageBus.Host.Nats.csproj" /> | ||
<ProjectReference Include="..\..\SlimMessageBus.Host.Serialization.Json\SlimMessageBus.Host.Serialization.Json.csproj" /> | ||
<ProjectReference Include="..\..\Tools\SecretStore\SecretStore.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
12 changes: 12 additions & 0 deletions
12
src/Samples/Sample.Nats.WebApi/appsettings.Development.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"Nats": { | ||
"Endpoint": "nats://localhost:4222", | ||
"Topic": "test" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*", | ||
"Nats": { | ||
"Endpoint": "{{nats_endpoint}}", | ||
"Topic": "{{nats_subject}}" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.