Browse Source

Implemented Distributed Lock for Catalog Service

Implemented Distributed Lock for MongoDB on shared and has been used in CatalogService
pull/85/head
malik masis 5 years ago
parent
commit
b1924b2f55
  1. 9
      services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/CatalogServiceHttpApiHostModule.cs
  2. 19
      services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/DbMigrations/CatalogServiceDatabaseMigrationEventHandler.cs
  3. 17
      shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/DatabaseMongoDbMigrationEventHandler.cs

9
services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/CatalogServiceHttpApiHostModule.cs

@ -20,6 +20,9 @@ using Volo.Abp.AspNetCore.Mvc.AntiForgery;
using Volo.Abp.Modularity;
using Volo.Abp.Threading;
using Volo.Abp.Uow;
using Medallion.Threading.Redis;
using Medallion.Threading;
using StackExchange.Redis;
namespace EShopOnAbp.CatalogService;
@ -67,6 +70,12 @@ public class CatalogServiceHttpApiHostModule : AbpModule
});
});
context.Services.AddSingleton<IDistributedLockProvider>(sp =>
{
var connection = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]);
return new RedisDistributedSynchronizationProvider(connection.GetDatabase());
});
Configure<AbpAspNetCoreMvcOptions>(options =>
{
options.ConventionalControllers.Create(typeof(CatalogServiceApplicationModule).Assembly, opts =>

19
services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/DbMigrations/CatalogServiceDatabaseMigrationEventHandler.cs

@ -2,6 +2,8 @@
using System.Threading.Tasks;
using EShopOnAbp.CatalogService.MongoDB;
using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.MongoDb;
using Medallion.Threading;
using Microsoft.Extensions.Logging;
using Volo.Abp.Data;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.MultiTenancy;
@ -18,13 +20,16 @@ namespace EShopOnAbp.CatalogService.DbMigrations
IUnitOfWorkManager unitOfWorkManager,
ITenantStore tenantStore,
IDistributedEventBus distributedEventBus,
IServiceProvider serviceProvider
IServiceProvider serviceProvider,
IDistributedLockProvider distributedLockProvider
) : base(
currentTenant,
unitOfWorkManager,
tenantStore,
distributedEventBus,
CatalogServiceDbProperties.ConnectionStringName,serviceProvider)
CatalogServiceDbProperties.ConnectionStringName,
serviceProvider,
distributedLockProvider)
{
}
@ -42,7 +47,15 @@ namespace EShopOnAbp.CatalogService.DbMigrations
try
{
await MigrateDatabaseSchemaAsync(null);
Logger.LogInformation("CatalogService - Before Acquire ");
await using (var handle = await DistributedLockProvider.AcquireLockAsync(DatabaseName))
{
if (handle != null)
{
await MigrateDatabaseSchemaAsync(null);
}
}
}
catch (Exception ex)
{

17
shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/MongoDb/DatabaseMongoDbMigrationEventHandler.cs

@ -1,11 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Medallion.Threading;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Data;
using Volo.Abp.Domain.Entities.Events.Distributed;
@ -27,16 +27,20 @@ public abstract class DatabaseMongoDbMigrationEventHandler<TDbContext> : Databas
protected ITenantStore TenantStore { get; }
protected IDistributedEventBus DistributedEventBus { get; }
protected ILogger<DatabaseMongoDbMigrationEventHandler<TDbContext>> Logger { get; set; }
protected IServiceProvider ServiceProvider { get; }
protected string DatabaseName { get; }
protected IDistributedLockProvider DistributedLockProvider { get; }
protected DatabaseMongoDbMigrationEventHandler(
ICurrentTenant currentTenant,
IUnitOfWorkManager unitOfWorkManager,
ITenantStore tenantStore,
IDistributedEventBus distributedEventBus,
string databaseName, IServiceProvider serviceProvider)
string databaseName,
IServiceProvider serviceProvider,
IDistributedLockProvider distributedLockProvider
)
{
CurrentTenant = currentTenant;
UnitOfWorkManager = unitOfWorkManager;
@ -44,6 +48,7 @@ public abstract class DatabaseMongoDbMigrationEventHandler<TDbContext> : Databas
DatabaseName = databaseName;
ServiceProvider = serviceProvider;
DistributedEventBus = distributedEventBus;
DistributedLockProvider = distributedLockProvider;
Logger = NullLogger<DatabaseMongoDbMigrationEventHandler<TDbContext>>.Instance;
}

Loading…
Cancel
Save