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.

This commit is contained in:
Espen Tønnessen Nordli 2020-02-03 18:57:52 +01:00
parent bbc84c6b1b
commit 1f040cb19a
12 changed files with 123 additions and 1 deletions

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);
}
}

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);
}
}
}

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);
@ -180,6 +197,38 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API
}
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>();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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