Browse Source

Work in progress to implement multiple connections to RabbitMQ with one virtual host per tenant in the application. Each tenant specific microservice would then only be given the credentials for their own channel, while the baseline product would have to create connections to all the different virtual hosts in RabbitMQ, and also have awareness of which connection should be used for publishing an event.

pull/1240/head
Espen Tønnessen Nordli 5 years ago
parent
commit
1f040cb19a
12 changed files with 123 additions and 1 deletions
  1. +12
    -0
      src/BuildingBlocks/EventBus/EventBusRabbitMQ/IMultiRabbitMQPersistentConnections.cs
  2. +30
    -0
      src/BuildingBlocks/EventBus/EventBusRabbitMQ/MultiRabbitMQPersistentConnections.cs
  3. +62
    -1
      src/Services/Basket/Basket.API/Startup.cs
  4. +2
    -0
      src/Services/Catalog/Catalog.API/Startup.cs
  5. +2
    -0
      src/Services/Location/Locations.API/Startup.cs
  6. +2
    -0
      src/Services/Marketing/Marketing.API/Startup.cs
  7. +3
    -0
      src/Services/Ordering/Ordering.API/Startup.cs
  8. +2
    -0
      src/Services/Ordering/Ordering.BackgroundTasks/Startup.cs
  9. +2
    -0
      src/Services/Ordering/Ordering.SignalrHub/Startup.cs
  10. +2
    -0
      src/Services/Payment/Payment.API/Startup.cs
  11. +2
    -0
      src/Services/TenantCustomisations/TenantACustomisations/Startup.cs
  12. +2
    -0
      src/Services/Webhooks/Webhooks.API/Startup.cs

+ 12
- 0
src/BuildingBlocks/EventBus/EventBusRabbitMQ/IMultiRabbitMQPersistentConnections.cs View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
{
public interface IMultiRabbitMQPersistentConnections
{
List<IRabbitMQPersistentConnection> GetConnections();
void AddConnection(IRabbitMQPersistentConnection connection);
void RemoveConnection(IRabbitMQPersistentConnection connection);
}
}

+ 30
- 0
src/BuildingBlocks/EventBus/EventBusRabbitMQ/MultiRabbitMQPersistentConnections.cs View File

@ -0,0 +1,30 @@
using System.Collections.Generic;
namespace Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ
{
public class MultiRabbitMQPersistentConnections : IMultiRabbitMQPersistentConnections
{
public List<IRabbitMQPersistentConnection> Connections;
public MultiRabbitMQPersistentConnections()
{
Connections = new List<IRabbitMQPersistentConnection>();
}
public List<IRabbitMQPersistentConnection> GetConnections()
{
return Connections;
}
public void AddConnection(IRabbitMQPersistentConnection connection)
{
Connections.Add((connection));
}
public void RemoveConnection(IRabbitMQPersistentConnection connection)
{
Connections.Remove(connection);
}
}
}

+ 62
- 1
src/Services/Basket/Basket.API/Startup.cs View File

@ -120,14 +120,31 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API
factory.Password = Configuration["EventBusPassword"];
}
factory.VirtualHost = "customisation";
var retryCount = 5;
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))
{
retryCount = int.Parse(Configuration["EventBusRetryCount"]);
}
return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount);
});
services.AddSingleton<IMultiRabbitMQPersistentConnections>(sp =>
{
IMultiRabbitMQPersistentConnections connections = new MultiRabbitMQPersistentConnections();
connections.AddConnection(GenerateConnection("customisation", sp));
connections.AddConnection(GenerateConnection("/", sp));
return connections;
});
}
RegisterEventBus(services);
@ -178,7 +195,39 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API
return new AutofacServiceProvider(container.Build());
}
private IRabbitMQPersistentConnection GenerateConnection(String vHost, IServiceProvider sp)
{
var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>();
var factory = new ConnectionFactory()
{
HostName = Configuration["EventBusConnection"],
DispatchConsumersAsync = true
};
if (!string.IsNullOrEmpty(Configuration["EventBusUserName"]))
{
factory.UserName = Configuration["EventBusUserName"];
}
if (!string.IsNullOrEmpty(Configuration["EventBusPassword"]))
{
factory.Password = Configuration["EventBusPassword"];
}
factory.VirtualHost = vHost;
var retryCount = 5;
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))
{
retryCount = int.Parse(Configuration["EventBusRetryCount"]);
}
return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
@ -302,8 +351,20 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API
retryCount = int.Parse(Configuration["EventBusRetryCount"]);
}
var multiRabbitMqPersistentConnections = sp.GetRequiredService<IMultiRabbitMQPersistentConnections>();
List<IEventBus> testing = new List<IEventBus>();
multiRabbitMqPersistentConnections.GetConnections().ForEach(conn =>
{
testing.Add(new EventBusRabbitMQ(conn, logger, iLifetimeScope, eventBusSubcriptionsManager, subscriptionClientName, retryCount));
});
Console.WriteLine(testing);
return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, subscriptionClientName, retryCount);
});
}
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();


+ 2
- 0
src/Services/Catalog/Catalog.API/Startup.cs View File

@ -310,6 +310,8 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API
{
factory.Password = configuration["EventBusPassword"];
}
factory.VirtualHost = "customisation";
var retryCount = 5;
if (!string.IsNullOrEmpty(configuration["EventBusRetryCount"]))


+ 2
- 0
src/Services/Location/Locations.API/Startup.cs View File

@ -90,6 +90,8 @@ namespace Microsoft.eShopOnContainers.Services.Locations.API
{
factory.Password = Configuration["EventBusPassword"];
}
factory.VirtualHost = "customisation";
var retryCount = 5;
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))


+ 2
- 0
src/Services/Marketing/Marketing.API/Startup.cs View File

@ -115,6 +115,8 @@
factory.Password = Configuration["EventBusPassword"];
}
factory.VirtualHost = "customisation";
var retryCount = 5;
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))
{


+ 3
- 0
src/Services/Ordering/Ordering.API/Startup.cs View File

@ -318,6 +318,9 @@
{
factory.Password = configuration["EventBusPassword"];
}
factory.VirtualHost = "customisation";
var retryCount = 5;
if (!string.IsNullOrEmpty(configuration["EventBusRetryCount"]))


+ 2
- 0
src/Services/Ordering/Ordering.BackgroundTasks/Startup.cs View File

@ -81,6 +81,8 @@ namespace Ordering.BackgroundTasks
{
factory.Password = Configuration["EventBusPassword"];
}
factory.VirtualHost = "customisation";
var retryCount = 5;
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))


+ 2
- 0
src/Services/Ordering/Ordering.SignalrHub/Startup.cs View File

@ -93,6 +93,8 @@ namespace Ordering.SignalrHub
{
factory.Password = Configuration["EventBusPassword"];
}
factory.VirtualHost = "customisation";
var retryCount = 5;
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))


+ 2
- 0
src/Services/Payment/Payment.API/Startup.cs View File

@ -71,6 +71,8 @@ namespace Payment.API
{
factory.Password = Configuration["EventBusPassword"];
}
factory.VirtualHost = "customisation";
var retryCount = 5;
if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))


+ 2
- 0
src/Services/TenantCustomisations/TenantACustomisations/Startup.cs View File

@ -311,6 +311,8 @@
factory.Password = configuration["EventBusPassword"];
}
factory.VirtualHost = "customisation";
var retryCount = 5;
if (!string.IsNullOrEmpty(configuration["EventBusRetryCount"]))
{


+ 2
- 0
src/Services/Webhooks/Webhooks.API/Startup.cs View File

@ -335,6 +335,8 @@ namespace Webhooks.API
{
factory.Password = configuration["EventBusPassword"];
}
factory.VirtualHost = "customisation";
var retryCount = 5;
if (!string.IsNullOrEmpty(configuration["EventBusRetryCount"]))


Loading…
Cancel
Save