Connect to the rabbitmq container using settings.

This commit is contained in:
dsanz 2017-03-13 11:03:35 +01:00
parent 4d1269b8f2
commit 172367d9d1
7 changed files with 22 additions and 19 deletions

View File

@ -8,6 +8,7 @@ services:
- ASPNETCORE_URLS=http://0.0.0.0:5103
- ConnectionString=basket.data
- identityUrl=http://identity.api:5105 #Local: You need to open your local dev-machine firewall at range 5100-5105. at range 5100-5105.
- EventBusConnection=rabbitmq
ports:
- "5103:5103"
@ -17,6 +18,7 @@ services:
- ASPNETCORE_URLS=http://0.0.0.0:5101
- ConnectionString=Server=sql.data;Database=Microsoft.eShopOnContainers.Services.CatalogDb;User Id=sa;Password=Pass@word
- ExternalCatalogBaseUrl=http://localhost:5101 #Local: You need to open your local dev-machine firewall at range 5100-5105. at range 5100-5105.
- EventBusConnection=rabbitmq
ports:
- "5101:5101"
@ -36,6 +38,7 @@ services:
- ASPNETCORE_URLS=http://0.0.0.0:5102
- ConnectionString=Server=sql.data;Database=Microsoft.eShopOnContainers.Services.OrderingDb;User Id=sa;Password=Pass@word
- identityUrl=http://identity.api:5105 #Local: You need to open your local dev-machine firewall at range 5100-5105. at range 5100-5105.
- EventBusConnection=rabbitmq
ports:
- "5102:5102"

View File

@ -8,5 +8,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API
public class BasketSettings
{
public string ConnectionString { get; set; }
public string EventBusConnection { get; set; }
}
}

View File

@ -28,8 +28,7 @@ namespace Basket.API.Events
private async Task UpdateBasket(int itemId, decimal newPrice, CustomerBasket basket)
{
//TODO here seems to be a problem with the format
var itemsToUpdate = basket?.Items?.Where(x => int.Parse(x.Id) == itemId).ToList();
var itemsToUpdate = basket?.Items?.Where(x => int.Parse(x.ProductId) == itemId).ToList();
if (itemsToUpdate != null)
{
foreach (var item in itemsToUpdate)
@ -39,7 +38,7 @@ namespace Basket.API.Events
item.OldUnitPrice = originalPrice;
}
await _repository.UpdateBasket(basket);
}
}
}
}
}

View File

@ -81,22 +81,15 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API
services.AddTransient<IBasketRepository, RedisBasketRepository>();
services.AddTransient<IIntegrationEventHandler<CatalogPriceChanged>, CatalogPriceChangedHandler>();
var eventBus = new EventBus();
var serviceProvider = services.BuildServiceProvider();
var configuration = serviceProvider.GetRequiredService<IOptionsSnapshot<BasketSettings>>().Value;
var eventBus = new EventBus(configuration.EventBusConnection);
services.AddSingleton<IEventBus>(eventBus);
var serviceProvider = services.BuildServiceProvider();
var catalogPriceHandler = serviceProvider.GetService<IIntegrationEventHandler<CatalogPriceChanged>>();
eventBus.Subscribe<CatalogPriceChanged>(catalogPriceHandler);
//var container = new ContainerBuilder();
//container.Populate(services);
//container.RegisterModule(new ApplicationModule());
//return new AutofacServiceProvider(container.Build());
//var eventBus = new EventBus();
//services.AddSingleton<IEventBus>(eventBus);
//eventBus.Subscribe<CatalogPriceChanged>(new CatalogPriceChangedHandler(new RedisBasketRepository());
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

View File

@ -11,6 +11,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.IO;
using System.Reflection;
@ -74,7 +75,9 @@
.AllowCredentials());
});
services.AddSingleton<IEventBus, EventBus>();
var serviceProvider = services.BuildServiceProvider();
var configuration = serviceProvider.GetRequiredService<IOptionsSnapshot<Settings>>().Value;
services.AddSingleton<IEventBus>(new EventBus(configuration.EventBusConnection));
services.AddMvc();
}

View File

@ -9,5 +9,6 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API
public class Settings
{
public string ExternalCatalogBaseUrl {get;set;}
public string EventBusConnection { get; set; }
}
}

View File

@ -16,6 +16,7 @@ namespace Microsoft.eShopOnContainers.Services.Common.Infrastructure
public class EventBus : IEventBus
{
private readonly string _brokerName = "event_bus";
private readonly string _connectionString;
private readonly Dictionary<string, List<IIntegrationEventHandler>> _handlers;
private readonly List<Type> _eventTypes;
@ -23,15 +24,16 @@ namespace Microsoft.eShopOnContainers.Services.Common.Infrastructure
private string _queueName;
public EventBus()
public EventBus(string connectionString)
{
_connectionString = connectionString;
_handlers = new Dictionary<string, List<IIntegrationEventHandler>>();
_eventTypes = new List<Type>();
}
public void Publish(IIntegrationEvent @event)
{
var eventName = @event.GetType().Name;
var factory = new ConnectionFactory() { HostName = "172.20.0.1" };
var factory = new ConnectionFactory() { HostName = _connectionString };
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
@ -105,7 +107,7 @@ namespace Microsoft.eShopOnContainers.Services.Common.Infrastructure
}
else
{
var factory = new ConnectionFactory() { HostName = "172.20.0.1" };
var factory = new ConnectionFactory() { HostName = _connectionString };
var connection = factory.CreateConnection();
var channel = connection.CreateModel();