Refactor Locations Api eventbus using CAP
This commit is contained in:
parent
6919decef3
commit
628d34659c
@ -1,6 +1,7 @@
|
||||
namespace Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Services
|
||||
using DotNetCore.CAP;
|
||||
|
||||
namespace Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Services
|
||||
{
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
||||
using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Exceptions;
|
||||
using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Repositories;
|
||||
using Microsoft.eShopOnContainers.Services.Locations.API.IntegrationEvents.Events;
|
||||
@ -14,12 +15,12 @@
|
||||
public class LocationsService : ILocationsService
|
||||
{
|
||||
private readonly ILocationsRepository _locationsRepository;
|
||||
private readonly IEventBus _eventBus;
|
||||
private readonly ICapPublisher _eventBus;
|
||||
private readonly ILogger<LocationsService> _logger;
|
||||
|
||||
public LocationsService(
|
||||
ILocationsRepository locationsRepository,
|
||||
IEventBus eventBus,
|
||||
ICapPublisher eventBus,
|
||||
ILogger<LocationsService> logger)
|
||||
{
|
||||
_locationsRepository = locationsRepository ?? throw new ArgumentNullException(nameof(locationsRepository));
|
||||
@ -73,9 +74,9 @@
|
||||
var newUserLocations = MapUserLocationDetails(newLocations);
|
||||
var @event = new UserLocationUpdatedIntegrationEvent(userId, newUserLocations);
|
||||
|
||||
_logger.LogInformation("----- Publishing integration event: {IntegrationEventId} from {AppName} - ({@IntegrationEvent})", @event.Id, Program.AppName, @event);
|
||||
_logger.LogInformation("----- Publishing integration event: {AppName} - ({@IntegrationEvent})", Program.AppName, @event);
|
||||
|
||||
_eventBus.Publish(@event);
|
||||
_eventBus.Publish(nameof(UserLocationUpdatedIntegrationEvent), @event);
|
||||
}
|
||||
|
||||
private List<UserLocationDetails> MapUserLocationDetails(List<Locations> newLocations)
|
||||
|
@ -1,12 +1,12 @@
|
||||
namespace Microsoft.eShopOnContainers.Services.Locations.API.IntegrationEvents.Events
|
||||
{
|
||||
using Locations.API.Model;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class UserLocationUpdatedIntegrationEvent : IntegrationEvent
|
||||
public class UserLocationUpdatedIntegrationEvent
|
||||
{
|
||||
public string UserId { get; set; }
|
||||
|
||||
public List<UserLocationDetails> LocationList { get; set; }
|
||||
|
||||
public UserLocationUpdatedIntegrationEvent(string userId, List<UserLocationDetails> locationList)
|
||||
|
@ -11,6 +11,10 @@
|
||||
<PackageReference Include="AspNetCore.HealthChecks.Rabbitmq" Version="2.2.0" />
|
||||
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="2.2.2" />
|
||||
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.2.1" />
|
||||
<PackageReference Include="DotNetCore.CAP" Version="2.5.0-preview-69210974" />
|
||||
<PackageReference Include="DotNetCore.CAP.AzureServiceBus" Version="2.5.0-preview-69210974" />
|
||||
<PackageReference Include="DotNetCore.CAP.RabbitMQ" Version="2.5.0-preview-69210974" />
|
||||
<PackageReference Include="DotNetCore.CAP.MongoDB" Version="2.5.0-preview-69210974" />
|
||||
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.2.1" />
|
||||
<PackageReference Include="Microsoft.ApplicationInsights.DependencyCollector" Version="2.6.1" />
|
||||
<PackageReference Include="Microsoft.ApplicationInsights.Kubernetes" Version="1.0.2" />
|
||||
@ -20,22 +24,16 @@
|
||||
<PackageReference Include="Microsoft.AspNetCore.HealthChecks" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.AzureKeyVault" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.AzureAppServices" Version="2.2.0" />
|
||||
<PackageReference Include="mongocsharpdriver" Version="2.5.0" />
|
||||
<PackageReference Include="MongoDB.Bson" Version="2.5.0" />
|
||||
<PackageReference Include="MongoDB.Driver" Version="2.5.0" />
|
||||
<PackageReference Include="MongoDB.Driver.Core" Version="2.5.0" />
|
||||
<PackageReference Include="mongocsharpdriver" Version="2.7.2" />
|
||||
<PackageReference Include="MongoDB.Bson" Version="2.7.2" />
|
||||
<PackageReference Include="MongoDB.Driver" Version="2.7.2" />
|
||||
<PackageReference Include="MongoDB.Driver.Core" Version="2.7.2" />
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="2.1.1" />
|
||||
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.1.2" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="3.0.1" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
|
||||
<PackageReference Include="Serilog.Sinks.Seq" Version="4.0.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="3.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusRabbitMQ\EventBusRabbitMQ.csproj" />
|
||||
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusServiceBus\EventBusServiceBus.csproj" />
|
||||
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBus\EventBus.csproj" />
|
||||
</ItemGroup>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -9,11 +9,6 @@ using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Azure.ServiceBus;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus;
|
||||
using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure;
|
||||
using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Filters;
|
||||
using Microsoft.eShopOnContainers.Services.Locations.API.Infrastructure.Middlewares;
|
||||
@ -57,50 +52,45 @@ namespace Microsoft.eShopOnContainers.Services.Locations.API
|
||||
|
||||
services.Configure<LocationSettings>(Configuration);
|
||||
|
||||
if (Configuration.GetValue<bool>("AzureServiceBusEnabled"))
|
||||
services.AddCap(options =>
|
||||
{
|
||||
services.AddSingleton<IServiceBusPersisterConnection>(sp =>
|
||||
// using MongoDB as the event storage
|
||||
options.UseMongoDB(configure =>
|
||||
{
|
||||
var logger = sp.GetRequiredService<ILogger<DefaultServiceBusPersisterConnection>>();
|
||||
|
||||
var serviceBusConnectionString = Configuration["EventBusConnection"];
|
||||
var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString);
|
||||
|
||||
return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger);
|
||||
configure.DatabaseConnection = Configuration["ConnectionString"];
|
||||
configure.DatabaseName= Configuration["Database"];
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
services.AddSingleton<IRabbitMQPersistentConnection>(sp =>
|
||||
|
||||
if (Configuration.GetValue<bool>("AzureServiceBusEnabled"))
|
||||
{
|
||||
var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>();
|
||||
|
||||
var factory = new ConnectionFactory()
|
||||
options.UseAzureServiceBus(Configuration["EventBusConnection"]);
|
||||
}
|
||||
else
|
||||
{
|
||||
options.UseRabbitMQ(conf =>
|
||||
{
|
||||
HostName = Configuration["EventBusConnection"]
|
||||
};
|
||||
conf.HostName = Configuration["EventBusConnection"];
|
||||
if (!string.IsNullOrEmpty(Configuration["EventBusUserName"]))
|
||||
{
|
||||
conf.UserName = Configuration["EventBusUserName"];
|
||||
}
|
||||
if (!string.IsNullOrEmpty(Configuration["EventBusPassword"]))
|
||||
{
|
||||
conf.Password = Configuration["EventBusPassword"];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(Configuration["EventBusUserName"]))
|
||||
{
|
||||
factory.UserName = Configuration["EventBusUserName"];
|
||||
}
|
||||
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))
|
||||
{
|
||||
options.FailedRetryCount = int.Parse(Configuration["EventBusRetryCount"]);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(Configuration["EventBusPassword"]))
|
||||
{
|
||||
factory.Password = Configuration["EventBusPassword"];
|
||||
}
|
||||
|
||||
var retryCount = 5;
|
||||
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))
|
||||
{
|
||||
retryCount = int.Parse(Configuration["EventBusRetryCount"]);
|
||||
}
|
||||
|
||||
return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount);
|
||||
});
|
||||
}
|
||||
|
||||
RegisterEventBus(services);
|
||||
if (!string.IsNullOrEmpty(Configuration["SubscriptionClientName"]))
|
||||
{
|
||||
options.DefaultGroup = Configuration["SubscriptionClientName"];
|
||||
}
|
||||
});
|
||||
|
||||
// Add framework services.
|
||||
services.AddSwaggerGen(options =>
|
||||
@ -238,45 +228,6 @@ namespace Microsoft.eShopOnContainers.Services.Locations.API
|
||||
|
||||
app.UseAuthentication();
|
||||
}
|
||||
|
||||
private void RegisterEventBus(IServiceCollection services)
|
||||
{
|
||||
var subscriptionClientName = Configuration["SubscriptionClientName"];
|
||||
|
||||
if (Configuration.GetValue<bool>("AzureServiceBusEnabled"))
|
||||
{
|
||||
services.AddSingleton<IEventBus, EventBusServiceBus>(sp =>
|
||||
{
|
||||
var serviceBusPersisterConnection = sp.GetRequiredService<IServiceBusPersisterConnection>();
|
||||
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
|
||||
var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>();
|
||||
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
|
||||
|
||||
return new EventBusServiceBus(serviceBusPersisterConnection, logger,
|
||||
eventBusSubcriptionsManager, subscriptionClientName, iLifetimeScope);
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
services.AddSingleton<IEventBus, EventBusRabbitMQ>(sp =>
|
||||
{
|
||||
var rabbitMQPersistentConnection = sp.GetRequiredService<IRabbitMQPersistentConnection>();
|
||||
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
|
||||
var logger = sp.GetRequiredService<ILogger<EventBusRabbitMQ>>();
|
||||
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
|
||||
|
||||
var retryCount = 5;
|
||||
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))
|
||||
{
|
||||
retryCount = int.Parse(Configuration["EventBusRetryCount"]);
|
||||
}
|
||||
|
||||
return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, subscriptionClientName, retryCount);
|
||||
});
|
||||
}
|
||||
|
||||
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
|
||||
}
|
||||
}
|
||||
|
||||
public static class CustomExtensionMethods
|
||||
|
Loading…
x
Reference in New Issue
Block a user