From 81d8f1b3f95696b3df3764e0565da39d47296ab5 Mon Sep 17 00:00:00 2001 From: hello Date: Fri, 11 Oct 2024 21:30:57 +0800 Subject: [PATCH] =?UTF-8?q?Aspire=20=E9=9B=86=E6=88=90=20Dapr=20=E5=92=8C?= =?UTF-8?q?=20RabbitMQ?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DaprComponents/env-secretstore.yaml | 7 ++++ .../DaprComponents/rabbitmq-pubsub.yaml | 23 +++++++++++ .../DaprSidecarResourceBuilderExtensions.cs | 30 ++++++++++++++ .../HelloShop.AppHost.csproj | 2 + src/HelloShop.AppHost/Program.cs | 39 ++++++++++++++++--- 5 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 src/HelloShop.AppHost/DaprComponents/env-secretstore.yaml create mode 100644 src/HelloShop.AppHost/DaprComponents/rabbitmq-pubsub.yaml create mode 100644 src/HelloShop.AppHost/Extensions/DaprSidecarResourceBuilderExtensions.cs diff --git a/src/HelloShop.AppHost/DaprComponents/env-secretstore.yaml b/src/HelloShop.AppHost/DaprComponents/env-secretstore.yaml new file mode 100644 index 0000000..1724793 --- /dev/null +++ b/src/HelloShop.AppHost/DaprComponents/env-secretstore.yaml @@ -0,0 +1,7 @@ +apiVersion: dapr.io/v1alpha1 +kind: Component +metadata: + name: env-secretstore +spec: + type: secretstores.local.env + version: v1 \ No newline at end of file diff --git a/src/HelloShop.AppHost/DaprComponents/rabbitmq-pubsub.yaml b/src/HelloShop.AppHost/DaprComponents/rabbitmq-pubsub.yaml new file mode 100644 index 0000000..2a75bc3 --- /dev/null +++ b/src/HelloShop.AppHost/DaprComponents/rabbitmq-pubsub.yaml @@ -0,0 +1,23 @@ +apiVersion: dapr.io/v1alpha1 +kind: Component +metadata: + name: event-bus-pubsub +spec: + type: pubsub.rabbitmq + version: v1 + metadata: + - name: connectionString + secretKeyRef: + name: ConnectionStrings__rabbitmq + - name: durable + value: "false" + - name: deletedWhenUnused + value: "false" + - name: autoAck + value: "false" + - name: reconnectWait + value: "0" + - name: concurrency + value: parallel +auth: + secretStore: env-secretstore \ No newline at end of file diff --git a/src/HelloShop.AppHost/Extensions/DaprSidecarResourceBuilderExtensions.cs b/src/HelloShop.AppHost/Extensions/DaprSidecarResourceBuilderExtensions.cs new file mode 100644 index 0000000..1ede3b2 --- /dev/null +++ b/src/HelloShop.AppHost/Extensions/DaprSidecarResourceBuilderExtensions.cs @@ -0,0 +1,30 @@ +// Copyright (c) HelloShop Corporation. All rights reserved. +// See the license file in the project root for more information. + +using Aspire.Hosting.Dapr; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HelloShop.AppHost.Extensions +{ + public static class DaprSidecarResourceBuilderExtensions + { + public static IResourceBuilder WithReference(this IResourceBuilder builder, IResourceBuilder resourceBuilder, int waitInSeconds = 5) + { + 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)); + var connectionStringName = resourceBuilder.Resource.ConnectionStringEnvironmentVariable ?? $"ConnectionStrings__{resourceBuilder.Resource.Name}"; + context.EnvironmentVariables[connectionStringName] = new ConnectionStringReference(resourceBuilder.Resource, false); + })); + + return builder; + } + } +} diff --git a/src/HelloShop.AppHost/HelloShop.AppHost.csproj b/src/HelloShop.AppHost/HelloShop.AppHost.csproj index 8ce83a0..2498792 100644 --- a/src/HelloShop.AppHost/HelloShop.AppHost.csproj +++ b/src/HelloShop.AppHost/HelloShop.AppHost.csproj @@ -16,6 +16,8 @@ + + \ No newline at end of file diff --git a/src/HelloShop.AppHost/Program.cs b/src/HelloShop.AppHost/Program.cs index 0c0c73f..2f8b185 100644 --- a/src/HelloShop.AppHost/Program.cs +++ b/src/HelloShop.AppHost/Program.cs @@ -1,24 +1,51 @@ // Copyright (c) HelloShop Corporation. All rights reserved. // See the license file in the project root for more information. +using HelloShop.AppHost.Extensions; +using Aspire.Hosting.Dapr; + var builder = DistributedApplication.CreateBuilder(args); var cache = builder.AddRedis("cache", port: 6379); -var identityService = builder.AddProject("identityservice"); +var rabbitmq = builder.AddRabbitMQ("rabbitmq").WithManagementPlugin(); -var orderingService = builder.AddProject("orderingservice").WithReference(identityService); +var identityService = builder.AddProject("identityservice") + .WithDaprSidecar(); -var productService = builder.AddProject("productservice").WithReference(identityService); +DaprSidecarOptions daprSidecarOptions = new() { ResourcesPaths = ["DaprComponents"] }; -var basketService = builder.AddProject("basketservice").WithReference(identityService).WithReference(cache); +var orderingService = builder.AddProject("orderingservice") + .WithReference(identityService) + .WithDaprSidecar(options => + { + options.WithOptions(daprSidecarOptions).WithReference(rabbitmq); + }); + +var productService = builder.AddProject("productservice") + .WithReference(identityService) + .WithDaprSidecar(options => + { + options.WithOptions(daprSidecarOptions).WithReference(rabbitmq); + }); ; + +var basketService = builder.AddProject("basketservice") + .WithReference(identityService) + .WithReference(cache) + .WithDaprSidecar(options => + { + options.WithOptions(daprSidecarOptions).WithReference(rabbitmq); + }); var apiservice = builder.AddProject("apiservice") .WithReference(identityService) .WithReference(orderingService) .WithReference(productService) -.WithReference(basketService); +.WithReference(basketService) +.WithDaprSidecar(); -builder.AddProject("webapp").WithReference(apiservice); +builder.AddProject("webapp") + .WithReference(apiservice) + .WithDaprSidecar(); builder.Build().Run();