Update codes

This commit is contained in:
zedy 2021-06-25 15:09:00 +08:00
parent 0709a3ce5b
commit e7ea0109e4
13 changed files with 71 additions and 68 deletions

View File

@ -19,7 +19,5 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions
void Unsubscribe<T, TH>() void Unsubscribe<T, TH>()
where TH : IIntegrationEventHandler<T> where TH : IIntegrationEventHandler<T>
where T : IntegrationEvent; where T : IntegrationEvent;
void Dispose();
} }
} }

View File

@ -4,7 +4,6 @@ using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Extensions; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Extensions;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Azure.Messaging.ServiceBus;
using Polly; using Polly;
using Polly.Retry; using Polly.Retry;
using RabbitMQ.Client; using RabbitMQ.Client;
@ -28,7 +27,6 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
private readonly IEventBusSubscriptionsManager _subsManager; private readonly IEventBusSubscriptionsManager _subsManager;
private readonly ILifetimeScope _autofac; private readonly ILifetimeScope _autofac;
private readonly int _retryCount; private readonly int _retryCount;
private readonly ServiceBusProcessor _processor;
private IModel _consumerChannel; private IModel _consumerChannel;
private string _queueName; private string _queueName;
@ -44,7 +42,6 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
_autofac = autofac; _autofac = autofac;
_retryCount = retryCount; _retryCount = retryCount;
_subsManager.OnEventRemoved += SubsManager_OnEventRemoved; _subsManager.OnEventRemoved += SubsManager_OnEventRemoved;
this._processor = EventBusServiceBus.EventBusServiceBus.DeliverProcessor();
} }
private void SubsManager_OnEventRemoved(object sender, string eventName) private void SubsManager_OnEventRemoved(object sender, string eventName)
@ -181,7 +178,6 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
} }
_subsManager.Clear(); _subsManager.Clear();
_processor.CloseAsync().GetAwaiter().GetResult();
} }
private void StartBasicConsume() private void StartBasicConsume()

View File

@ -7,7 +7,6 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Autofac" Version="6.1.0" /> <PackageReference Include="Autofac" Version="6.1.0" />
<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.2.0" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" /> <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" /> <PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="Polly" Version="7.2.1" /> <PackageReference Include="Polly" Version="7.2.1" />
@ -15,7 +14,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\EventBusServiceBus\EventBusServiceBus.csproj" />
<ProjectReference Include="..\EventBus\EventBus.csproj" /> <ProjectReference Include="..\EventBus\EventBus.csproj" />
</ItemGroup> </ItemGroup>

View File

@ -55,6 +55,7 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus
if (_disposed) return; if (_disposed) return;
_disposed = true; _disposed = true;
_topicClient.DisposeAsync().GetAwaiter().GetResult();
} }
} }
} }

View File

@ -11,7 +11,7 @@ using System.Text.Json;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus
{ {
public class EventBusServiceBus : IEventBus public class EventBusServiceBus : IEventBus, IDisposable
{ {
private readonly IServiceBusPersisterConnection _serviceBusPersisterConnection; private readonly IServiceBusPersisterConnection _serviceBusPersisterConnection;
private readonly ILogger<EventBusServiceBus> _logger; private readonly ILogger<EventBusServiceBus> _logger;
@ -20,7 +20,7 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus
private readonly string _topicName; private readonly string _topicName;
private readonly string _subscriptionName; private readonly string _subscriptionName;
private ServiceBusSender _sender; private ServiceBusSender _sender;
private static ServiceBusProcessor _processor; private ServiceBusProcessor _processor;
private readonly string AUTOFAC_SCOPE_NAME = "eshop_event_bus"; private readonly string AUTOFAC_SCOPE_NAME = "eshop_event_bus";
private const string INTEGRATION_EVENT_SUFFIX = "IntegrationEvent"; private const string INTEGRATION_EVENT_SUFFIX = "IntegrationEvent";
@ -131,11 +131,6 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus
_processor.CloseAsync().GetAwaiter().GetResult(); _processor.CloseAsync().GetAwaiter().GetResult();
} }
public static ServiceBusProcessor DeliverProcessor()
{
return _processor;
}
private async Task RegisterSubscriptionClientMessageHandlerAsync() private async Task RegisterSubscriptionClientMessageHandlerAsync()
{ {
_processor.ProcessMessageAsync += _processor.ProcessMessageAsync +=

View File

@ -1,5 +1,6 @@
using Autofac; using Autofac;
using Autofac.Extensions.DependencyInjection; using Autofac.Extensions.DependencyInjection;
using Azure.Messaging.ServiceBus;
using Basket.API.Infrastructure.Filters; using Basket.API.Infrastructure.Filters;
using Basket.API.IntegrationEvents.EventHandling; using Basket.API.IntegrationEvents.EventHandling;
using Basket.API.IntegrationEvents.Events; using Basket.API.IntegrationEvents.Events;
@ -122,7 +123,6 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API
{ {
var serviceBusConnectionString = Configuration["EventBusConnection"]; var serviceBusConnectionString = Configuration["EventBusConnection"];
var subscriptionClientName = Configuration["SubscriptionClientName"];
return new DefaultServiceBusPersisterConnection(serviceBusConnectionString); return new DefaultServiceBusPersisterConnection(serviceBusConnectionString);
}); });
} }
@ -283,8 +283,9 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>(); var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>(); var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>();
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>(); var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
string topicName = "topicName"; var serviceBusConnectionString = Configuration["EventBusConnection"];
string subscriptionName = "subscriptionName"; string topicName = ServiceBusConnectionStringProperties.Parse(serviceBusConnectionString).EntityPath;
string subscriptionName = Configuration["SubscriptionClientName"];
return new EventBusServiceBus(serviceBusPersisterConnection, logger, return new EventBusServiceBus(serviceBusPersisterConnection, logger,
eventBusSubcriptionsManager, iLifetimeScope, topicName, subscriptionName); eventBusSubcriptionsManager, iLifetimeScope, topicName, subscriptionName);

View File

@ -50,6 +50,7 @@
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="7.1.0" /> <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="7.1.0" />
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.2.1" /> <PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.2.1" />
<PackageReference Include="Azure.Identity" Version="1.4.0" /> <PackageReference Include="Azure.Identity" Version="1.4.0" />
<PackageReference Include="Azure.Messaging.ServiceBus" Version="7.2.0" />
<PackageReference Include="Google.Protobuf" Version="3.14.0" /> <PackageReference Include="Google.Protobuf" Version="3.14.0" />
<PackageReference Include="Grpc.AspNetCore.Server" Version="2.34.0" /> <PackageReference Include="Grpc.AspNetCore.Server" Version="2.34.0" />
<PackageReference Include="Grpc.Tools" Version="2.34.0" PrivateAssets="All" /> <PackageReference Include="Grpc.Tools" Version="2.34.0" PrivateAssets="All" />

View File

@ -1,5 +1,6 @@
using Autofac; using Autofac;
using Autofac.Extensions.DependencyInjection; using Autofac.Extensions.DependencyInjection;
using Azure.Messaging.ServiceBus;
using Catalog.API.Grpc; using Catalog.API.Grpc;
using global::Catalog.API.Infrastructure.Filters; using global::Catalog.API.Infrastructure.Filters;
using global::Catalog.API.IntegrationEvents; using global::Catalog.API.IntegrationEvents;
@ -331,8 +332,9 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>(); var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>(); var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>();
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>(); var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
string topicName = "topicName"; var serviceBusConnectionString = configuration["EventBusConnection"];
string subscriptionName = "subscriptionName"; string topicName = ServiceBusConnectionStringProperties.Parse(serviceBusConnectionString).EntityPath;
string subscriptionName = configuration["SubscriptionClientName"];
return new EventBusServiceBus(serviceBusPersisterConnection, logger, return new EventBusServiceBus(serviceBusPersisterConnection, logger,
eventBusSubcriptionsManager, iLifetimeScope, topicName, subscriptionName); eventBusSubcriptionsManager, iLifetimeScope, topicName, subscriptionName);

View File

@ -1,16 +1,15 @@
namespace Microsoft.eShopOnContainers.Services.Ordering.API using Microsoft.AspNetCore.Http;
{
using AspNetCore.Http;
using Autofac; using Autofac;
using Autofac.Extensions.DependencyInjection; using Autofac.Extensions.DependencyInjection;
using Azure.Messaging.ServiceBus;
using global::Ordering.API.Application.IntegrationEvents; using global::Ordering.API.Application.IntegrationEvents;
using global::Ordering.API.Application.IntegrationEvents.Events; using global::Ordering.API.Application.IntegrationEvents.Events;
using global::Ordering.API.Infrastructure.Filters; using global::Ordering.API.Infrastructure.Filters;
using GrpcOrdering; using GrpcOrdering;
using HealthChecks.UI.Client; using HealthChecks.UI.Client;
using Infrastructure.AutofacModules; using Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.AutofacModules;
using Infrastructure.Filters; using Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Filters;
using Infrastructure.Services; using Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Services;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
@ -36,7 +35,10 @@
using System.IdentityModel.Tokens.Jwt; using System.IdentityModel.Tokens.Jwt;
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure;
namespace Microsoft.eShopOnContainers.Services.Ordering.API
{
public class Startup public class Startup
{ {
public Startup(IConfiguration configuration) public Startup(IConfiguration configuration)
@ -373,8 +375,9 @@
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>(); var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>(); var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>();
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>(); var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
string topicName = "topicName"; var serviceBusConnectionString = configuration["EventBusConnection"];
string subscriptionName = "subscriptionName"; string topicName = ServiceBusConnectionStringProperties.Parse(serviceBusConnectionString).EntityPath;
string subscriptionName = configuration["SubscriptionClientName"];
return new EventBusServiceBus(serviceBusPersisterConnection, logger, return new EventBusServiceBus(serviceBusPersisterConnection, logger,
eventBusSubcriptionsManager, iLifetimeScope, topicName, subscriptionName); eventBusSubcriptionsManager, iLifetimeScope, topicName, subscriptionName);

View File

@ -1,4 +1,5 @@
using Autofac; using Autofac;
using Azure.Messaging.ServiceBus;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ; using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ;
@ -63,8 +64,9 @@ namespace Ordering.BackgroundTasks.Extensions
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>(); var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>(); var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>();
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>(); var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
string topicName = "topicName"; var serviceBusConnectionString = configuration["EventBusConnection"];
string subscriptionName = "subscriptionName"; string topicName = ServiceBusConnectionStringProperties.Parse(serviceBusConnectionString).EntityPath;
string subscriptionName = configuration["SubscriptionClientName"];
return new EventBusServiceBus(serviceBusPersisterConnection, logger, eventBusSubcriptionsManager, iLifetimeScope, topicName, subscriptionName); return new EventBusServiceBus(serviceBusPersisterConnection, logger, eventBusSubcriptionsManager, iLifetimeScope, topicName, subscriptionName);
}); });

View File

@ -1,5 +1,6 @@
using Autofac; using Autofac;
using Autofac.Extensions.DependencyInjection; using Autofac.Extensions.DependencyInjection;
using Azure.Messaging.ServiceBus;
using HealthChecks.UI.Client; using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
@ -211,8 +212,9 @@ namespace Ordering.SignalrHub
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>(); var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>(); var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>();
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>(); var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
string topicName = "topicName"; var serviceBusConnectionString = Configuration["EventBusConnection"];
string subscriptionName = "subscriptionName"; string topicName = ServiceBusConnectionStringProperties.Parse(serviceBusConnectionString).EntityPath;
string subscriptionName = Configuration["SubscriptionClientName"];
return new EventBusServiceBus(serviceBusPersisterConnection, logger, return new EventBusServiceBus(serviceBusPersisterConnection, logger,
eventBusSubcriptionsManager, iLifetimeScope, topicName, subscriptionName); eventBusSubcriptionsManager, iLifetimeScope, topicName, subscriptionName);

View File

@ -1,5 +1,6 @@
using Autofac; using Autofac;
using Autofac.Extensions.DependencyInjection; using Autofac.Extensions.DependencyInjection;
using Azure.Messaging.ServiceBus;
using HealthChecks.UI.Client; using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.AspNetCore.Diagnostics.HealthChecks;
@ -128,8 +129,9 @@ namespace Payment.API
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>(); var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>(); var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>();
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>(); var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
string topicName = "topicName"; var serviceBusConnectionString = Configuration["EventBusConnection"];
string subscriptionName = "subscriptionName"; string topicName = ServiceBusConnectionStringProperties.Parse(serviceBusConnectionString).EntityPath;
string subscriptionName = Configuration["SubscriptionClientName"];
return new EventBusServiceBus(serviceBusPersisterConnection, logger, return new EventBusServiceBus(serviceBusPersisterConnection, logger,
eventBusSubcriptionsManager, iLifetimeScope, topicName, subscriptionName); eventBusSubcriptionsManager, iLifetimeScope, topicName, subscriptionName);

View File

@ -1,5 +1,6 @@
using Autofac; using Autofac;
using Autofac.Extensions.DependencyInjection; using Autofac.Extensions.DependencyInjection;
using Azure.Messaging.ServiceBus;
using Devspaces.Support; using Devspaces.Support;
using HealthChecks.UI.Client; using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.JwtBearer;
@ -214,8 +215,9 @@ namespace Webhooks.API
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>(); var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>(); var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>();
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>(); var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
string topicName = "topicName"; var serviceBusConnectionString = configuration["EventBusConnection"];
string subscriptionName = "subscriptionName"; string topicName = ServiceBusConnectionStringProperties.Parse(serviceBusConnectionString).EntityPath;
string subscriptionName = configuration["SubscriptionClientName"];
return new EventBusServiceBus(serviceBusPersisterConnection, logger, return new EventBusServiceBus(serviceBusPersisterConnection, logger,
eventBusSubcriptionsManager, iLifetimeScope, topicName, subscriptionName); eventBusSubcriptionsManager, iLifetimeScope, topicName, subscriptionName);