Browse Source

Refactor Locations Api eventbus using CAP

pull/970/head
Savorboard 6 years ago
parent
commit
628d34659c
4 changed files with 51 additions and 101 deletions
  1. +7
    -6
      src/Services/Location/Locations.API/Infrastructure/Services/LocationsService.cs
  2. +2
    -2
      src/Services/Location/Locations.API/IntegrationEvents/Events/UserLocationUpdatedIntegrationEvent.cs
  3. +9
    -11
      src/Services/Location/Locations.API/Locations.API.csproj
  4. +33
    -82
      src/Services/Location/Locations.API/Startup.cs

+ 7
- 6
src/Services/Location/Locations.API/Infrastructure/Services/LocationsService.cs View File

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


+ 2
- 2
src/Services/Location/Locations.API/IntegrationEvents/Events/UserLocationUpdatedIntegrationEvent.cs View File

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


+ 9
- 11
src/Services/Location/Locations.API/Locations.API.csproj View File

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

+ 33
- 82
src/Services/Location/Locations.API/Startup.cs View File

@ -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 =>
{
var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>();
var factory = new ConnectionFactory()
{
HostName = Configuration["EventBusConnection"]
};
if (!string.IsNullOrEmpty(Configuration["EventBusUserName"]))
{
factory.UserName = Configuration["EventBusUserName"];
}
if (!string.IsNullOrEmpty(Configuration["EventBusPassword"]))
{
factory.Password = Configuration["EventBusPassword"];
}
var retryCount = 5;
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))
if (Configuration.GetValue<bool>("AzureServiceBusEnabled"))
{
options.UseAzureServiceBus(Configuration["EventBusConnection"]);
}
else
{
options.UseRabbitMQ(conf =>
{
retryCount = int.Parse(Configuration["EventBusRetryCount"]);
}
return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount);
});
}
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["EventBusRetryCount"]))
{
options.FailedRetryCount = int.Parse(Configuration["EventBusRetryCount"]);
}
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…
Cancel
Save