Refactor Marketing Api eventbus using CAP
This commit is contained in:
parent
628d34659c
commit
244d46bd4a
@ -1,10 +1,9 @@
|
|||||||
namespace Microsoft.eShopOnContainers.Services.Marketing.API.IntegrationEvents.Events
|
namespace Microsoft.eShopOnContainers.Services.Marketing.API.IntegrationEvents.Events
|
||||||
{
|
{
|
||||||
using Marketing.API.Model;
|
using Marketing.API.Model;
|
||||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
public class UserLocationUpdatedIntegrationEvent : IntegrationEvent
|
public class UserLocationUpdatedIntegrationEvent
|
||||||
{
|
{
|
||||||
public string UserId { get; set; }
|
public string UserId { get; set; }
|
||||||
|
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
namespace Microsoft.eShopOnContainers.Services.Marketing.API.IntegrationEvents.Handlers
|
using DotNetCore.CAP;
|
||||||
|
|
||||||
|
namespace Microsoft.eShopOnContainers.Services.Marketing.API.IntegrationEvents.Handlers
|
||||||
{
|
{
|
||||||
using Marketing.API.IntegrationEvents.Events;
|
using Marketing.API.IntegrationEvents.Events;
|
||||||
using Marketing.API.Model;
|
using Marketing.API.Model;
|
||||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
|
||||||
using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Repositories;
|
using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Repositories;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using Serilog.Context;
|
using Serilog.Context;
|
||||||
@ -10,8 +11,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
public class UserLocationUpdatedIntegrationEventHandler
|
public class UserLocationUpdatedIntegrationEventHandler : ICapSubscribe
|
||||||
: IIntegrationEventHandler<UserLocationUpdatedIntegrationEvent>
|
|
||||||
{
|
{
|
||||||
private readonly IMarketingDataRepository _marketingDataRepository;
|
private readonly IMarketingDataRepository _marketingDataRepository;
|
||||||
private readonly ILogger<UserLocationUpdatedIntegrationEventHandler> _logger;
|
private readonly ILogger<UserLocationUpdatedIntegrationEventHandler> _logger;
|
||||||
@ -24,11 +24,12 @@
|
|||||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO: [CapSubscribe(nameof(UserLocationUpdatedIntegrationEvent))]
|
||||||
public async Task Handle(UserLocationUpdatedIntegrationEvent @event)
|
public async Task Handle(UserLocationUpdatedIntegrationEvent @event)
|
||||||
{
|
{
|
||||||
using (LogContext.PushProperty("IntegrationEventContext", $"{@event.Id}-{Program.AppName}"))
|
using (LogContext.PushProperty("IntegrationEventContext", $"{Program.AppName}"))
|
||||||
{
|
{
|
||||||
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppName} - ({@IntegrationEvent})", @event.Id, Program.AppName, @event);
|
_logger.LogInformation("----- Handling integration event: {AppName} - ({@IntegrationEvent})", Program.AppName, @event);
|
||||||
|
|
||||||
var userMarketingData = await _marketingDataRepository.GetAsync(@event.UserId);
|
var userMarketingData = await _marketingDataRepository.GetAsync(@event.UserId);
|
||||||
userMarketingData = userMarketingData ??
|
userMarketingData = userMarketingData ??
|
||||||
|
@ -27,6 +27,10 @@
|
|||||||
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="2.2.0" />
|
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="2.2.0" />
|
||||||
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="2.2.2" />
|
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="2.2.2" />
|
||||||
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.2.1" />
|
<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.AspNetCore" Version="2.2.1" />
|
||||||
<PackageReference Include="Microsoft.ApplicationInsights.DependencyCollector" Version="2.6.1" />
|
<PackageReference Include="Microsoft.ApplicationInsights.DependencyCollector" Version="2.6.1" />
|
||||||
<PackageReference Include="Microsoft.ApplicationInsights.Kubernetes" Version="1.0.2" />
|
<PackageReference Include="Microsoft.ApplicationInsights.Kubernetes" Version="1.0.2" />
|
||||||
@ -36,10 +40,10 @@
|
|||||||
<PackageReference Include="Microsoft.AspNetCore.HealthChecks" Version="1.0.0" />
|
<PackageReference Include="Microsoft.AspNetCore.HealthChecks" Version="1.0.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.AzureKeyVault" Version="2.2.0" />
|
<PackageReference Include="Microsoft.Extensions.Configuration.AzureKeyVault" Version="2.2.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging.AzureAppServices" Version="2.2.0" />
|
<PackageReference Include="Microsoft.Extensions.Logging.AzureAppServices" Version="2.2.0" />
|
||||||
<PackageReference Include="mongocsharpdriver" Version="2.5.0" />
|
<PackageReference Include="mongocsharpdriver" Version="2.7.2" />
|
||||||
<PackageReference Include="MongoDB.Bson" Version="2.5.0" />
|
<PackageReference Include="MongoDB.Bson" Version="2.7.2" />
|
||||||
<PackageReference Include="MongoDB.Driver" Version="2.5.0" />
|
<PackageReference Include="MongoDB.Driver" Version="2.7.2" />
|
||||||
<PackageReference Include="MongoDB.Driver.Core" Version="2.5.0" />
|
<PackageReference Include="MongoDB.Driver.Core" Version="2.7.2" />
|
||||||
<PackageReference Include="Serilog.AspNetCore" Version="2.1.1" />
|
<PackageReference Include="Serilog.AspNetCore" Version="2.1.1" />
|
||||||
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.1.2" />
|
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.1.2" />
|
||||||
<PackageReference Include="Serilog.Settings.Configuration" Version="3.0.1" />
|
<PackageReference Include="Serilog.Settings.Configuration" Version="3.0.1" />
|
||||||
@ -48,8 +52,6 @@
|
|||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="3.0.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="3.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusRabbitMQ\EventBusRabbitMQ.csproj" />
|
|
||||||
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusServiceBus\EventBusServiceBus.csproj" />
|
|
||||||
<ProjectReference Include="..\..\..\BuildingBlocks\WebHostCustomization\WebHost.Customization\WebHost.Customization.csproj" />
|
<ProjectReference Include="..\..\..\BuildingBlocks\WebHostCustomization\WebHost.Customization\WebHost.Customization.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
@ -5,11 +5,6 @@
|
|||||||
using AspNetCore.Http;
|
using AspNetCore.Http;
|
||||||
using Autofac;
|
using Autofac;
|
||||||
using Autofac.Extensions.DependencyInjection;
|
using Autofac.Extensions.DependencyInjection;
|
||||||
using Azure.ServiceBus;
|
|
||||||
using BuildingBlocks.EventBus;
|
|
||||||
using BuildingBlocks.EventBus.Abstractions;
|
|
||||||
using BuildingBlocks.EventBusRabbitMQ;
|
|
||||||
using BuildingBlocks.EventBusServiceBus;
|
|
||||||
using EntityFrameworkCore;
|
using EntityFrameworkCore;
|
||||||
using Extensions.Configuration;
|
using Extensions.Configuration;
|
||||||
using Extensions.DependencyInjection;
|
using Extensions.DependencyInjection;
|
||||||
@ -19,7 +14,6 @@
|
|||||||
using Infrastructure.Filters;
|
using Infrastructure.Filters;
|
||||||
using Infrastructure.Repositories;
|
using Infrastructure.Repositories;
|
||||||
using Infrastructure.Services;
|
using Infrastructure.Services;
|
||||||
using IntegrationEvents.Events;
|
|
||||||
using Marketing.API.IntegrationEvents.Handlers;
|
using Marketing.API.IntegrationEvents.Handlers;
|
||||||
using Microsoft.ApplicationInsights.Extensibility;
|
using Microsoft.ApplicationInsights.Extensibility;
|
||||||
using Microsoft.ApplicationInsights.ServiceFabric;
|
using Microsoft.ApplicationInsights.ServiceFabric;
|
||||||
@ -81,48 +75,47 @@
|
|||||||
//Check Client vs. Server evaluation: https://docs.microsoft.com/en-us/ef/core/querying/client-eval
|
//Check Client vs. Server evaluation: https://docs.microsoft.com/en-us/ef/core/querying/client-eval
|
||||||
});
|
});
|
||||||
|
|
||||||
if (Configuration.GetValue<bool>("AzureServiceBusEnabled"))
|
services.AddTransient<UserLocationUpdatedIntegrationEventHandler>();
|
||||||
|
|
||||||
|
services.AddCap(options =>
|
||||||
{
|
{
|
||||||
services.AddSingleton<IServiceBusPersisterConnection>(sp =>
|
// using MongoDB as the event storage
|
||||||
|
options.UseMongoDB(configure =>
|
||||||
{
|
{
|
||||||
var logger = sp.GetRequiredService<ILogger<DefaultServiceBusPersisterConnection>>();
|
configure.DatabaseConnection = Configuration["MongoConnectionString"];
|
||||||
|
configure.DatabaseName = Configuration["MongoDatabase"];
|
||||||
var serviceBusConnectionString = Configuration["EventBusConnection"];
|
|
||||||
var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString);
|
|
||||||
|
|
||||||
return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger);
|
|
||||||
});
|
});
|
||||||
}
|
|
||||||
else
|
if (Configuration.GetValue<bool>("AzureServiceBusEnabled"))
|
||||||
{
|
|
||||||
services.AddSingleton<IRabbitMQPersistentConnection>(sp =>
|
|
||||||
{
|
{
|
||||||
var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>();
|
options.UseAzureServiceBus(Configuration["EventBusConnection"]);
|
||||||
|
}
|
||||||
var factory = new ConnectionFactory()
|
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"]))
|
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))
|
||||||
{
|
{
|
||||||
factory.UserName = Configuration["EventBusUserName"];
|
options.FailedRetryCount = int.Parse(Configuration["EventBusRetryCount"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(Configuration["EventBusPassword"]))
|
if (!string.IsNullOrEmpty(Configuration["SubscriptionClientName"]))
|
||||||
{
|
{
|
||||||
factory.Password = Configuration["EventBusPassword"];
|
options.DefaultGroup = Configuration["SubscriptionClientName"];
|
||||||
}
|
}
|
||||||
|
});
|
||||||
var retryCount = 5;
|
|
||||||
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))
|
|
||||||
{
|
|
||||||
retryCount = int.Parse(Configuration["EventBusRetryCount"]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add framework services.
|
// Add framework services.
|
||||||
services.AddSwaggerGen(options =>
|
services.AddSwaggerGen(options =>
|
||||||
@ -161,8 +154,6 @@
|
|||||||
.AllowCredentials());
|
.AllowCredentials());
|
||||||
});
|
});
|
||||||
|
|
||||||
RegisterEventBus(services);
|
|
||||||
|
|
||||||
services.AddTransient<IMarketingDataRepository, MarketingDataRepository>();
|
services.AddTransient<IMarketingDataRepository, MarketingDataRepository>();
|
||||||
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
||||||
services.AddTransient<IIdentityService, IdentityService>();
|
services.AddTransient<IIdentityService, IdentityService>();
|
||||||
@ -213,8 +204,6 @@
|
|||||||
c.OAuthClientId("marketingswaggerui");
|
c.OAuthClientId("marketingswaggerui");
|
||||||
c.OAuthAppName("Marketing Swagger UI");
|
c.OAuthAppName("Marketing Swagger UI");
|
||||||
});
|
});
|
||||||
|
|
||||||
ConfigureEventBus(app);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void RegisterAppInsights(IServiceCollection services)
|
private void RegisterAppInsights(IServiceCollection services)
|
||||||
@ -253,52 +242,6 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
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>();
|
|
||||||
services.AddTransient<UserLocationUpdatedIntegrationEventHandler>();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ConfigureEventBus(IApplicationBuilder app)
|
|
||||||
{
|
|
||||||
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
|
|
||||||
eventBus.Subscribe<UserLocationUpdatedIntegrationEvent, UserLocationUpdatedIntegrationEventHandler>();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected virtual void ConfigureAuth(IApplicationBuilder app)
|
protected virtual void ConfigureAuth(IApplicationBuilder app)
|
||||||
{
|
{
|
||||||
if (Configuration.GetValue<bool>("UseLoadTest"))
|
if (Configuration.GetValue<bool>("UseLoadTest"))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user