关于 Aspire 应用宿主的编排

This commit is contained in:
hello 2025-03-18 22:45:05 +08:00
parent c01868da0e
commit ade2f40524
3 changed files with 22 additions and 17 deletions

View File

@ -8,13 +8,12 @@ namespace HelloShop.AppHost.Extensions
{
public static class DaprSidecarResourceBuilderExtensions
{
public static IResourceBuilder<IDaprSidecarResource> WithReference(this IResourceBuilder<IDaprSidecarResource> builder, IResourceBuilder<IResourceWithConnectionString> resourceBuilder, int waitInSeconds = 10)
public static IResourceBuilder<IDaprSidecarResource> WithReferenceAndWaitFor(this IResourceBuilder<IDaprSidecarResource> builder, IResourceBuilder<IResourceWithConnectionString> resourceBuilder)
{
builder.WithAnnotation(new EnvironmentCallbackAnnotation(async context =>
{
var notificationService = context.ExecutionContext.ServiceProvider.GetRequiredService<ResourceNotificationService>();
await notificationService.WaitForResourceAsync(resourceBuilder.Resource.Name, KnownResourceStates.Running);
await Task.Delay(TimeSpan.FromSeconds(waitInSeconds));
await notificationService.WaitForResourceHealthyAsync(resourceBuilder.Resource.Name);
var connectionStringName = resourceBuilder.Resource.ConnectionStringEnvironmentVariable ?? $"ConnectionStrings__{resourceBuilder.Resource.Name}";
context.EnvironmentVariables[connectionStringName] = new ConnectionStringReference(resourceBuilder.Resource, false);
}));

View File

@ -6,9 +6,11 @@ using HelloShop.AppHost.Extensions;
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("cache", port: 6380).WithPersistence();
var cache = builder.AddRedis("cache", port: 6380).WithLifetime(ContainerLifetime.Persistent).WithPersistence();
var rabbitmq = builder.AddRabbitMQ("rabbitmq").WithManagementPlugin();
var rabbitmqUser = builder.AddParameter("rabbitmqUser", secret: true);
var rabbitmqPassword = builder.AddParameter("rabbitmqPassword", secret: true);
var rabbitmq = builder.AddRabbitMQ("rabbitmq", rabbitmqUser, rabbitmqPassword).WithLifetime(ContainerLifetime.Persistent).WithManagementPlugin();
var identityService = builder.AddProject<Projects.HelloShop_IdentityService>("identityservice")
.WithDaprSidecar();
@ -19,33 +21,33 @@ var orderingService = builder.AddProject<Projects.HelloShop_OrderingService>("or
.WithReference(identityService)
.WithDaprSidecar(options =>
{
options.WithOptions(daprSidecarOptions).WithReference(rabbitmq).WithReference(cache);
options.WithOptions(daprSidecarOptions).WithReferenceAndWaitFor(rabbitmq).WithReferenceAndWaitFor(cache);
});
var productService = builder.AddProject<Projects.HelloShop_ProductService>("productservice")
.WithReference(identityService)
.WithReference(identityService).WaitFor(identityService)
.WithDaprSidecar(options =>
{
options.WithOptions(daprSidecarOptions).WithReference(rabbitmq).WithReference(cache);
}); ;
options.WithOptions(daprSidecarOptions).WithReferenceAndWaitFor(rabbitmq).WithReferenceAndWaitFor(cache);
});
var basketService = builder.AddProject<Projects.HelloShop_BasketService>("basketservice")
.WithReference(identityService)
.WithReference(cache)
.WithReference(identityService).WaitFor(identityService)
.WithReference(cache).WaitFor(cache)
.WithDaprSidecar(options =>
{
options.WithOptions(daprSidecarOptions).WithReference(rabbitmq).WithReference(cache);
options.WithOptions(daprSidecarOptions).WithReferenceAndWaitFor(rabbitmq).WithReferenceAndWaitFor(cache);
});
var apiservice = builder.AddProject<Projects.HelloShop_ApiService>("apiservice")
.WithReference(identityService)
.WithReference(orderingService)
.WithReference(productService)
.WithReference(basketService)
.WithReference(identityService).WaitFor(identityService)
.WithReference(orderingService).WaitFor(orderingService)
.WithReference(productService).WaitFor(productService)
.WithReference(basketService).WaitFor(basketService)
.WithDaprSidecar();
builder.AddProject<Projects.HelloShop_WebApp>("webapp")
.WithReference(apiservice)
.WithReference(apiservice).WaitFor(apiservice)
.WithDaprSidecar();
builder.Build().Run();

View File

@ -5,5 +5,9 @@
"Microsoft.AspNetCore": "Warning",
"Aspire.Hosting.Dcp": "Warning"
}
},
"Parameters": {
"rabbitmqUser": "guest",
"rabbitmqPassword": "guest"
}
}