From d30f34803be905af3ee6099f6fb7800df437eb51 Mon Sep 17 00:00:00 2001 From: malik masis Date: Fri, 4 Mar 2022 17:39:31 +0300 Subject: [PATCH] Implemented DistributedLock.Redis Added into Administration Identity Ordering Payment --- .../AdministrationServiceHttpApiHostModule.cs | 9 ++++++ ...ionServiceDatabaseMigrationEventHandler.cs | 28 +++++++++++++---- ...ityServiceDatabaseMigrationEventHandler.cs | 29 +++++++++++++----- .../IdentityServiceHttpApiHostModule.cs | 18 +++++++---- ...ingServiceDatabaseMigrationEventHandler.cs | 30 ++++++++++++++----- ...pOnAbp.OrderingService.HttpApi.Host.csproj | 3 ++ .../OrderingServiceHttpApiHostModule.cs | 14 +++++++-- ...entServiceDatabaseMigrationEventHandler.cs | 20 +++++++++++-- .../PaymentServiceHttpApiHostModule.cs | 9 ++++++ .../DatabaseEfCoreMigrationEventHandler.cs | 9 +++++- ...pOnAbp.Shared.Hosting.Microservices.csproj | 1 + 11 files changed, 137 insertions(+), 33 deletions(-) diff --git a/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/AdministrationServiceHttpApiHostModule.cs b/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/AdministrationServiceHttpApiHostModule.cs index beafc3f7..44f96600 100644 --- a/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/AdministrationServiceHttpApiHostModule.cs +++ b/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/AdministrationServiceHttpApiHostModule.cs @@ -6,11 +6,14 @@ using EShopOnAbp.AdministrationService.DbMigrations; using EShopOnAbp.AdministrationService.EntityFrameworkCore; using EShopOnAbp.Shared.Hosting.AspNetCore; using EShopOnAbp.Shared.Hosting.Microservices; +using Medallion.Threading.Redis; +using Medallion.Threading; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Cors; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using StackExchange.Redis; using Volo.Abp; using Volo.Abp.AspNetCore.Mvc.UI.MultiTenancy; using Volo.Abp.Http.Client.IdentityModel.Web; @@ -66,6 +69,12 @@ public class AdministrationServiceHttpApiHostModule : AbpModule .AllowCredentials(); }); }); + + context.Services.AddSingleton(sp => + { + var connection = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]); + return new RedisDistributedSynchronizationProvider(connection.GetDatabase()); + }); } public override void OnApplicationInitialization(ApplicationInitializationContext context) diff --git a/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/DbMigrations/AdministrationServiceDatabaseMigrationEventHandler.cs b/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/DbMigrations/AdministrationServiceDatabaseMigrationEventHandler.cs index 4640dfcf..4df15ec5 100644 --- a/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/DbMigrations/AdministrationServiceDatabaseMigrationEventHandler.cs +++ b/services/administration/src/EShopOnAbp.AdministrationService.HttpApi.Host/DbMigrations/AdministrationServiceDatabaseMigrationEventHandler.cs @@ -1,8 +1,10 @@ using EShopOnAbp.AdministrationService.EntityFrameworkCore; +using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.EfCore; +using Medallion.Threading; +using Microsoft.Extensions.Logging; using System; using System.Linq; using System.Threading.Tasks; -using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.EfCore; using Volo.Abp.Authorization.Permissions; using Volo.Abp.Data; using Volo.Abp.EventBus.Distributed; @@ -26,13 +28,16 @@ namespace EShopOnAbp.AdministrationService.DbMigrations ITenantStore tenantStore, IPermissionDefinitionManager permissionDefinitionManager, IPermissionDataSeeder permissionDataSeeder, - IDistributedEventBus distributedEventBus + IDistributedEventBus distributedEventBus, + IDistributedLockProvider distributedLockProvider ) : base( currentTenant, unitOfWorkManager, tenantStore, distributedEventBus, - AdministrationServiceDbProperties.ConnectionStringName) + AdministrationServiceDbProperties.ConnectionStringName, + distributedLockProvider + ) { _permissionDefinitionManager = permissionDefinitionManager; _permissionDataSeeder = permissionDataSeeder; @@ -40,6 +45,8 @@ namespace EShopOnAbp.AdministrationService.DbMigrations public async Task HandleEventAsync(ApplyDatabaseMigrationsEto eventData) { + Logger.LogInformation("AdministrationService - HandleEventAsync started ..."); + if (eventData.DatabaseName != DatabaseName) { return; @@ -47,7 +54,7 @@ namespace EShopOnAbp.AdministrationService.DbMigrations try { - var schemaMigrated = await MigrateDatabaseSchemaAsync(eventData.TenantId); + await MigrateDatabaseSchemaAsync(eventData.TenantId); await SeedDataAsync(eventData.TenantId); } catch (Exception ex) @@ -60,8 +67,17 @@ namespace EShopOnAbp.AdministrationService.DbMigrations { try { - await MigrateDatabaseSchemaAsync(eventData.Id); - await SeedDataAsync(eventData.Id); + Logger.LogInformation("AdministrationService - Before Acquire"); + + await using (var handle = await DistributedLockProvider.AcquireLockAsync(DatabaseName)) + { + if(handle != null) + { + await MigrateDatabaseSchemaAsync(eventData.Id); + Logger.LogInformation("Starting AdministrationService DataSeeder..."); + await SeedDataAsync(eventData.Id); + } + } } catch (Exception ex) { diff --git a/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationEventHandler.cs b/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationEventHandler.cs index 17d8dd98..00cdaade 100644 --- a/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationEventHandler.cs +++ b/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/DbMigrations/IdentityServiceDatabaseMigrationEventHandler.cs @@ -10,6 +10,8 @@ using Volo.Abp.EventBus.Local; using Volo.Abp.Identity; using Volo.Abp.MultiTenancy; using Volo.Abp.Uow; +using Medallion.Threading; +using Microsoft.Extensions.Logging; namespace EShopOnAbp.IdentityService.DbMigrations { @@ -29,13 +31,15 @@ namespace EShopOnAbp.IdentityService.DbMigrations IIdentityDataSeeder identityDataSeeder, IdentityServerDataSeeder identityServerDataSeeder, IDistributedEventBus distributedEventBus, - ILocalEventBus localEventBus + ILocalEventBus localEventBus, + IDistributedLockProvider distributedLockProvider ) : base( currentTenant, unitOfWorkManager, tenantStore, distributedEventBus, - IdentityServiceDbProperties.ConnectionStringName) + IdentityServiceDbProperties.ConnectionStringName, + distributedLockProvider) { _identityDataSeeder = identityDataSeeder; _identityServerDataSeeder = identityServerDataSeeder; @@ -51,12 +55,21 @@ namespace EShopOnAbp.IdentityService.DbMigrations try { - var schemaMigrated = await MigrateDatabaseSchemaAsync(eventData.TenantId); - await SeedDataAsync( - tenantId: eventData.TenantId, - adminEmail: IdentityServiceDbProperties.DefaultAdminEmailAddress, - adminPassword: IdentityServiceDbProperties.DefaultAdminPassword - ); + Logger.LogInformation("IdentityService - Before Acquire "); + + await using (var handle = await DistributedLockProvider.AcquireLockAsync(DatabaseName)) + { + if (handle != null) + { + await MigrateDatabaseSchemaAsync(eventData.TenantId); + Logger.LogInformation("Starting IdentityService DataSeeder..."); + await SeedDataAsync( + tenantId: eventData.TenantId, + adminEmail: IdentityServiceDbProperties.DefaultAdminEmailAddress, + adminPassword: IdentityServiceDbProperties.DefaultAdminPassword + ); + } + } await _localEventBus.PublishAsync(new ApplyDatabaseSeedsEto()); } diff --git a/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/IdentityServiceHttpApiHostModule.cs b/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/IdentityServiceHttpApiHostModule.cs index 5fbed800..e6a22d55 100644 --- a/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/IdentityServiceHttpApiHostModule.cs +++ b/services/identity/src/EShopOnAbp.IdentityService.HttpApi.Host/IdentityServiceHttpApiHostModule.cs @@ -1,20 +1,22 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; using EShopOnAbp.IdentityService.DbMigrations; using EShopOnAbp.IdentityService.EntityFrameworkCore; using EShopOnAbp.Shared.Hosting.AspNetCore; using EShopOnAbp.Shared.Hosting.Gateways; using EShopOnAbp.Shared.Hosting.Microservices; +using Medallion.Threading; +using Medallion.Threading.Redis; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Cors; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using StackExchange.Redis; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; using Volo.Abp; using Volo.Abp.Modularity; -using Volo.Abp.Threading; namespace EShopOnAbp.IdentityService; @@ -63,6 +65,12 @@ public class IdentityServiceHttpApiHostModule : AbpModule .AllowCredentials(); }); }); + + context.Services.AddSingleton(sp => + { + var connection = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]); + return new RedisDistributedSynchronizationProvider(connection.GetDatabase()); + }); } public override void OnApplicationInitialization(ApplicationInitializationContext context) diff --git a/services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/DbMigrations/OrderingServiceDatabaseMigrationEventHandler.cs b/services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/DbMigrations/OrderingServiceDatabaseMigrationEventHandler.cs index 98f77546..1e32c00f 100644 --- a/services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/DbMigrations/OrderingServiceDatabaseMigrationEventHandler.cs +++ b/services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/DbMigrations/OrderingServiceDatabaseMigrationEventHandler.cs @@ -1,8 +1,9 @@ using EShopOnAbp.OrderingService.EntityFrameworkCore; -using System; -using System.Threading.Tasks; using EShopOnAbp.Shared.Hosting.Microservices.DbMigrations.EfCore; +using Medallion.Threading; using Microsoft.Extensions.Logging; +using System; +using System.Threading.Tasks; using Volo.Abp.Data; using Volo.Abp.EventBus.Distributed; using Volo.Abp.MultiTenancy; @@ -15,24 +16,29 @@ namespace EShopOnAbp.OrderingService.DbMigrations IDistributedEventHandler { private readonly IDataSeeder _dataSeeder; + public OrderingServiceDatabaseMigrationEventHandler( ICurrentTenant currentTenant, IUnitOfWorkManager unitOfWorkManager, ITenantStore tenantStore, - IDistributedEventBus distributedEventBus, - IDataSeeder dataSeeder) + IDistributedEventBus distributedEventBus, + IDataSeeder dataSeeder, + IDistributedLockProvider distributedLockProvider) : base( currentTenant, unitOfWorkManager, tenantStore, distributedEventBus, - OrderingServiceDbProperties.ConnectionStringName) + OrderingServiceDbProperties.ConnectionStringName, + distributedLockProvider) { _dataSeeder = dataSeeder; } public async Task HandleEventAsync(ApplyDatabaseMigrationsEto eventData) { + Logger.LogInformation("OrderingService - HandleEventAsync started ..."); + if (eventData.DatabaseName != DatabaseName) { return; @@ -45,9 +51,17 @@ namespace EShopOnAbp.OrderingService.DbMigrations try { - await MigrateDatabaseSchemaAsync(null); - Logger.LogInformation("Starting OrderingService DataSeeder..."); - await _dataSeeder.SeedAsync(); + Logger.LogInformation("OrderingService - Before Acquire "); + + await using (var handle = await DistributedLockProvider.AcquireLockAsync(DatabaseName)) + { + if (handle != null) + { + await MigrateDatabaseSchemaAsync(null); + Logger.LogInformation("Starting OrderingService DataSeeder..."); + await _dataSeeder.SeedAsync(); + } + } } catch (Exception ex) { diff --git a/services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/EShopOnAbp.OrderingService.HttpApi.Host.csproj b/services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/EShopOnAbp.OrderingService.HttpApi.Host.csproj index 116c3153..5c007a33 100644 --- a/services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/EShopOnAbp.OrderingService.HttpApi.Host.csproj +++ b/services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/EShopOnAbp.OrderingService.HttpApi.Host.csproj @@ -10,6 +10,9 @@ + + + diff --git a/services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/OrderingServiceHttpApiHostModule.cs b/services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/OrderingServiceHttpApiHostModule.cs index d5a9a9eb..23484319 100644 --- a/services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/OrderingServiceHttpApiHostModule.cs +++ b/services/ordering/src/EShopOnAbp.OrderingService.HttpApi.Host/OrderingServiceHttpApiHostModule.cs @@ -2,19 +2,22 @@ using EShopOnAbp.OrderingService.DbMigrations; using EShopOnAbp.OrderingService.EntityFrameworkCore; using EShopOnAbp.Shared.Hosting.AspNetCore; using EShopOnAbp.Shared.Hosting.Microservices; +using Medallion.Threading; +using Medallion.Threading.Redis; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Cors; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using StackExchange.Redis; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using Microsoft.Extensions.Configuration; using Volo.Abp; using Volo.Abp.AspNetCore.Mvc; +using Volo.Abp.DistributedLocking; using Volo.Abp.Modularity; -using Volo.Abp.Threading; namespace EShopOnAbp.OrderingService; @@ -22,6 +25,7 @@ namespace EShopOnAbp.OrderingService; typeof(OrderingServiceHttpApiModule), typeof(OrderingServiceApplicationModule), typeof(OrderingServiceEntityFrameworkCoreModule), + //typeof(MedallionAbpDistributedLock), typeof(EShopOnAbpSharedHostingMicroservicesModule) )] public class OrderingServiceHttpApiHostModule : AbpModule @@ -63,6 +67,12 @@ public class OrderingServiceHttpApiHostModule : AbpModule }); }); + context.Services.AddSingleton(sp => + { + var connection = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]); + return new RedisDistributedSynchronizationProvider(connection.GetDatabase()); + }); + // TODO: Crate controller instead of auto-controller configuration Configure(options => { diff --git a/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/DbMigrations/PaymentServiceDatabaseMigrationEventHandler.cs b/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/DbMigrations/PaymentServiceDatabaseMigrationEventHandler.cs index 7fd87b4f..19f29057 100644 --- a/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/DbMigrations/PaymentServiceDatabaseMigrationEventHandler.cs +++ b/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/DbMigrations/PaymentServiceDatabaseMigrationEventHandler.cs @@ -6,6 +6,8 @@ using Volo.Abp.Data; using Volo.Abp.EventBus.Distributed; using Volo.Abp.MultiTenancy; using Volo.Abp.Uow; +using Medallion.Threading; +using Microsoft.Extensions.Logging; namespace EShopOnAbp.PaymentService.DbMigrations { @@ -17,13 +19,15 @@ namespace EShopOnAbp.PaymentService.DbMigrations ICurrentTenant currentTenant, IUnitOfWorkManager unitOfWorkManager, ITenantStore tenantStore, - IDistributedEventBus distributedEventBus) + IDistributedEventBus distributedEventBus, + IDistributedLockProvider distributedLockProvider) : base( currentTenant, unitOfWorkManager, tenantStore, distributedEventBus, - PaymentServiceDbProperties.ConnectionStringName) + PaymentServiceDbProperties.ConnectionStringName, + distributedLockProvider) { } @@ -41,7 +45,17 @@ namespace EShopOnAbp.PaymentService.DbMigrations try { - await MigrateDatabaseSchemaAsync(null); + Logger.LogInformation("PaymentService - Before Acquire "); + + await using (var handle = await DistributedLockProvider.AcquireLockAsync(DatabaseName)) + { + if (handle != null) + { + await MigrateDatabaseSchemaAsync(null); + Logger.LogInformation("PaymentService No Seed..."); + + } + } } catch (Exception ex) { diff --git a/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/PaymentServiceHttpApiHostModule.cs b/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/PaymentServiceHttpApiHostModule.cs index e267e8d3..c0576256 100644 --- a/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/PaymentServiceHttpApiHostModule.cs +++ b/services/payment/src/EShopOnAbp.PaymentService.HttpApi.Host/PaymentServiceHttpApiHostModule.cs @@ -14,6 +14,9 @@ using Microsoft.Extensions.Configuration; using Volo.Abp; using Volo.Abp.Modularity; using Volo.Abp.Threading; +using Medallion.Threading.Redis; +using Medallion.Threading; +using StackExchange.Redis; namespace EShopOnAbp.PaymentService; @@ -63,6 +66,12 @@ public class PaymentServiceHttpApiHostModule : AbpModule .AllowCredentials(); }); }); + + context.Services.AddSingleton(sp => + { + var connection = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]); + return new RedisDistributedSynchronizationProvider(connection.GetDatabase()); + }); } public override void OnApplicationInitialization(ApplicationInitializationContext context) diff --git a/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/DatabaseEfCoreMigrationEventHandler.cs b/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/DatabaseEfCoreMigrationEventHandler.cs index 298102b5..1fd0d377 100644 --- a/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/DatabaseEfCoreMigrationEventHandler.cs +++ b/shared/EShopOnAbp.Shared.Hosting.Microservices/DbMigrations/EfCore/DatabaseEfCoreMigrationEventHandler.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Medallion.Threading; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -29,19 +30,23 @@ public abstract class DatabaseEfCoreMigrationEventHandler : Database protected IDistributedEventBus DistributedEventBus { get; } protected ILogger> Logger { get; set; } protected string DatabaseName { get; } + protected IDistributedLockProvider DistributedLockProvider { get; } protected DatabaseEfCoreMigrationEventHandler( ICurrentTenant currentTenant, IUnitOfWorkManager unitOfWorkManager, ITenantStore tenantStore, IDistributedEventBus distributedEventBus, - string databaseName) + string databaseName, + IDistributedLockProvider distributedLockProvider + ) { CurrentTenant = currentTenant; UnitOfWorkManager = unitOfWorkManager; TenantStore = tenantStore; DatabaseName = databaseName; DistributedEventBus = distributedEventBus; + DistributedLockProvider = distributedLockProvider; Logger = NullLogger>.Instance; } @@ -76,6 +81,7 @@ public abstract class DatabaseEfCoreMigrationEventHandler : Database if (tenantId == null) { //Migrating the host database + Logger.LogInformation($"{DatabaseName} MigrateDatabase in Shared - TenantId is null"); result = await MigrateDatabaseSchemaWithDbContextAsync(); } else @@ -85,6 +91,7 @@ public abstract class DatabaseEfCoreMigrationEventHandler : Database !tenantConfiguration.ConnectionStrings.GetOrDefault(DatabaseName).IsNullOrWhiteSpace()) { //Migrating the tenant database (only if tenant has a separate database) + Logger.LogInformation($"{DatabaseName} MigrateDatabase in Shared - TenantId is not null"); result = await MigrateDatabaseSchemaWithDbContextAsync(); } } diff --git a/shared/EShopOnAbp.Shared.Hosting.Microservices/EShopOnAbp.Shared.Hosting.Microservices.csproj b/shared/EShopOnAbp.Shared.Hosting.Microservices/EShopOnAbp.Shared.Hosting.Microservices.csproj index b4bd451f..948da22e 100644 --- a/shared/EShopOnAbp.Shared.Hosting.Microservices/EShopOnAbp.Shared.Hosting.Microservices.csproj +++ b/shared/EShopOnAbp.Shared.Hosting.Microservices/EShopOnAbp.Shared.Hosting.Microservices.csproj @@ -9,6 +9,7 @@ +