关于 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 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 => builder.WithAnnotation(new EnvironmentCallbackAnnotation(async context =>
{ {
var notificationService = context.ExecutionContext.ServiceProvider.GetRequiredService<ResourceNotificationService>(); var notificationService = context.ExecutionContext.ServiceProvider.GetRequiredService<ResourceNotificationService>();
await notificationService.WaitForResourceAsync(resourceBuilder.Resource.Name, KnownResourceStates.Running); await notificationService.WaitForResourceHealthyAsync(resourceBuilder.Resource.Name);
await Task.Delay(TimeSpan.FromSeconds(waitInSeconds));
var connectionStringName = resourceBuilder.Resource.ConnectionStringEnvironmentVariable ?? $"ConnectionStrings__{resourceBuilder.Resource.Name}"; var connectionStringName = resourceBuilder.Resource.ConnectionStringEnvironmentVariable ?? $"ConnectionStrings__{resourceBuilder.Resource.Name}";
context.EnvironmentVariables[connectionStringName] = new ConnectionStringReference(resourceBuilder.Resource, false); context.EnvironmentVariables[connectionStringName] = new ConnectionStringReference(resourceBuilder.Resource, false);
})); }));

View File

@ -6,9 +6,11 @@ using HelloShop.AppHost.Extensions;
var builder = DistributedApplication.CreateBuilder(args); 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") var identityService = builder.AddProject<Projects.HelloShop_IdentityService>("identityservice")
.WithDaprSidecar(); .WithDaprSidecar();
@ -19,33 +21,33 @@ var orderingService = builder.AddProject<Projects.HelloShop_OrderingService>("or
.WithReference(identityService) .WithReference(identityService)
.WithDaprSidecar(options => .WithDaprSidecar(options =>
{ {
options.WithOptions(daprSidecarOptions).WithReference(rabbitmq).WithReference(cache); options.WithOptions(daprSidecarOptions).WithReferenceAndWaitFor(rabbitmq).WithReferenceAndWaitFor(cache);
}); });
var productService = builder.AddProject<Projects.HelloShop_ProductService>("productservice") var productService = builder.AddProject<Projects.HelloShop_ProductService>("productservice")
.WithReference(identityService) .WithReference(identityService).WaitFor(identityService)
.WithDaprSidecar(options => .WithDaprSidecar(options =>
{ {
options.WithOptions(daprSidecarOptions).WithReference(rabbitmq).WithReference(cache); options.WithOptions(daprSidecarOptions).WithReferenceAndWaitFor(rabbitmq).WithReferenceAndWaitFor(cache);
}); ; });
var basketService = builder.AddProject<Projects.HelloShop_BasketService>("basketservice") var basketService = builder.AddProject<Projects.HelloShop_BasketService>("basketservice")
.WithReference(identityService) .WithReference(identityService).WaitFor(identityService)
.WithReference(cache) .WithReference(cache).WaitFor(cache)
.WithDaprSidecar(options => .WithDaprSidecar(options =>
{ {
options.WithOptions(daprSidecarOptions).WithReference(rabbitmq).WithReference(cache); options.WithOptions(daprSidecarOptions).WithReferenceAndWaitFor(rabbitmq).WithReferenceAndWaitFor(cache);
}); });
var apiservice = builder.AddProject<Projects.HelloShop_ApiService>("apiservice") var apiservice = builder.AddProject<Projects.HelloShop_ApiService>("apiservice")
.WithReference(identityService) .WithReference(identityService).WaitFor(identityService)
.WithReference(orderingService) .WithReference(orderingService).WaitFor(orderingService)
.WithReference(productService) .WithReference(productService).WaitFor(productService)
.WithReference(basketService) .WithReference(basketService).WaitFor(basketService)
.WithDaprSidecar(); .WithDaprSidecar();
builder.AddProject<Projects.HelloShop_WebApp>("webapp") builder.AddProject<Projects.HelloShop_WebApp>("webapp")
.WithReference(apiservice) .WithReference(apiservice).WaitFor(apiservice)
.WithDaprSidecar(); .WithDaprSidecar();
builder.Build().Run(); builder.Build().Run();

View File

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