From ade2f40524ba6a0bd53b3057199591b733ccc1c4 Mon Sep 17 00:00:00 2001 From: hello Date: Tue, 18 Mar 2025 22:45:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=B3=E4=BA=8E=20Aspire=20=E5=BA=94?= =?UTF-8?q?=E7=94=A8=E5=AE=BF=E4=B8=BB=E7=9A=84=E7=BC=96=E6=8E=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DaprSidecarResourceBuilderExtensions.cs | 5 ++-- src/HelloShop.AppHost/Program.cs | 30 ++++++++++--------- src/HelloShop.AppHost/appsettings.json | 4 +++ 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/HelloShop.AppHost/Extensions/DaprSidecarResourceBuilderExtensions.cs b/src/HelloShop.AppHost/Extensions/DaprSidecarResourceBuilderExtensions.cs index 781efe9..1d54b9c 100644 --- a/src/HelloShop.AppHost/Extensions/DaprSidecarResourceBuilderExtensions.cs +++ b/src/HelloShop.AppHost/Extensions/DaprSidecarResourceBuilderExtensions.cs @@ -8,13 +8,12 @@ namespace HelloShop.AppHost.Extensions { public static class DaprSidecarResourceBuilderExtensions { - public static IResourceBuilder WithReference(this IResourceBuilder builder, IResourceBuilder resourceBuilder, int waitInSeconds = 10) + public static IResourceBuilder WithReferenceAndWaitFor(this IResourceBuilder builder, IResourceBuilder resourceBuilder) { builder.WithAnnotation(new EnvironmentCallbackAnnotation(async context => { var notificationService = context.ExecutionContext.ServiceProvider.GetRequiredService(); - 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); })); diff --git a/src/HelloShop.AppHost/Program.cs b/src/HelloShop.AppHost/Program.cs index f0a878b..4ca7d13 100644 --- a/src/HelloShop.AppHost/Program.cs +++ b/src/HelloShop.AppHost/Program.cs @@ -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("identityservice") .WithDaprSidecar(); @@ -19,33 +21,33 @@ var orderingService = builder.AddProject("or .WithReference(identityService) .WithDaprSidecar(options => { - options.WithOptions(daprSidecarOptions).WithReference(rabbitmq).WithReference(cache); + options.WithOptions(daprSidecarOptions).WithReferenceAndWaitFor(rabbitmq).WithReferenceAndWaitFor(cache); }); var productService = builder.AddProject("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("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("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("webapp") - .WithReference(apiservice) + .WithReference(apiservice).WaitFor(apiservice) .WithDaprSidecar(); builder.Build().Run(); diff --git a/src/HelloShop.AppHost/appsettings.json b/src/HelloShop.AppHost/appsettings.json index 31c092a..dba66c0 100644 --- a/src/HelloShop.AppHost/appsettings.json +++ b/src/HelloShop.AppHost/appsettings.json @@ -5,5 +5,9 @@ "Microsoft.AspNetCore": "Warning", "Aspire.Hosting.Dcp": "Warning" } + }, + "Parameters": { + "rabbitmqUser": "guest", + "rabbitmqPassword": "guest" } }