Aspire 集成 Dapr 和 RabbitMQ

This commit is contained in:
hello 2024-10-11 21:30:57 +08:00
parent 16386a8ea5
commit 81d8f1b3f9
5 changed files with 95 additions and 6 deletions

View File

@ -0,0 +1,7 @@
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: env-secretstore
spec:
type: secretstores.local.env
version: v1

View File

@ -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

View File

@ -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<IDaprSidecarResource> WithReference(this IResourceBuilder<IDaprSidecarResource> builder, IResourceBuilder<IResourceWithConnectionString> resourceBuilder, int waitInSeconds = 5)
{
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));
var connectionStringName = resourceBuilder.Resource.ConnectionStringEnvironmentVariable ?? $"ConnectionStrings__{resourceBuilder.Resource.Name}";
context.EnvironmentVariables[connectionStringName] = new ConnectionStringReference(resourceBuilder.Resource, false);
}));
return builder;
}
}
}

View File

@ -16,6 +16,8 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Aspire.Hosting.AppHost" Version="8.2.1" /> <PackageReference Include="Aspire.Hosting.AppHost" Version="8.2.1" />
<PackageReference Include="Aspire.Hosting.Dapr" Version="8.2.1" />
<PackageReference Include="Aspire.Hosting.RabbitMQ" Version="8.2.1" />
<PackageReference Include="Aspire.Hosting.Redis" Version="8.2.1" /> <PackageReference Include="Aspire.Hosting.Redis" Version="8.2.1" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,24 +1,51 @@
// Copyright (c) HelloShop Corporation. All rights reserved. // Copyright (c) HelloShop Corporation. All rights reserved.
// See the license file in the project root for more information. // 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 builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("cache", port: 6379); var cache = builder.AddRedis("cache", port: 6379);
var identityService = builder.AddProject<Projects.HelloShop_IdentityService>("identityservice"); var rabbitmq = builder.AddRabbitMQ("rabbitmq").WithManagementPlugin();
var orderingService = builder.AddProject<Projects.HelloShop_OrderingService>("orderingservice").WithReference(identityService); var identityService = builder.AddProject<Projects.HelloShop_IdentityService>("identityservice")
.WithDaprSidecar();
var productService = builder.AddProject<Projects.HelloShop_ProductService>("productservice").WithReference(identityService); DaprSidecarOptions daprSidecarOptions = new() { ResourcesPaths = ["DaprComponents"] };
var basketService = builder.AddProject<Projects.HelloShop_BasketService>("basketservice").WithReference(identityService).WithReference(cache); var orderingService = builder.AddProject<Projects.HelloShop_OrderingService>("orderingservice")
.WithReference(identityService)
.WithDaprSidecar(options =>
{
options.WithOptions(daprSidecarOptions).WithReference(rabbitmq);
});
var productService = builder.AddProject<Projects.HelloShop_ProductService>("productservice")
.WithReference(identityService)
.WithDaprSidecar(options =>
{
options.WithOptions(daprSidecarOptions).WithReference(rabbitmq);
}); ;
var basketService = builder.AddProject<Projects.HelloShop_BasketService>("basketservice")
.WithReference(identityService)
.WithReference(cache)
.WithDaprSidecar(options =>
{
options.WithOptions(daprSidecarOptions).WithReference(rabbitmq);
});
var apiservice = builder.AddProject<Projects.HelloShop_ApiService>("apiservice") var apiservice = builder.AddProject<Projects.HelloShop_ApiService>("apiservice")
.WithReference(identityService) .WithReference(identityService)
.WithReference(orderingService) .WithReference(orderingService)
.WithReference(productService) .WithReference(productService)
.WithReference(basketService); .WithReference(basketService)
.WithDaprSidecar();
builder.AddProject<Projects.HelloShop_WebApp>("webapp").WithReference(apiservice); builder.AddProject<Projects.HelloShop_WebApp>("webapp")
.WithReference(apiservice)
.WithDaprSidecar();
builder.Build().Run(); builder.Build().Run();