From bb1de47e6bfced27bfc1410667205c7b6c4cc265 Mon Sep 17 00:00:00 2001 From: wangjunzzz <> Date: Fri, 16 Jan 2026 15:22:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=9B=86=E6=88=90nacos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aspnet-core/Lion.AbpPro.slnx | 1 + .../Lion.AbpPro.Nacos.csproj | 14 ++ .../Hosting/HostBuilderExtensions.cs | 38 ++++ .../Data/AbpProDbMigrationService.cs | 213 ------------------ .../Data/IAbpProDbSchemaMigrator.cs | 7 - .../Data/NullAbpProDbSchemaMigrator.cs | 15 -- .../Data/FileManagementDbMigrationService.cs | 158 ------------- .../Data/IFileManagementDbSchemaMigrator.cs | 6 - .../NullFileManagementDbSchemaMigrator.cs | 9 - .../Demo/DemoAppService.cs | 3 +- 10 files changed, 55 insertions(+), 409 deletions(-) create mode 100644 aspnet-core/frameworks/src/Lion.AbpPro.Nacos/Lion.AbpPro.Nacos.csproj create mode 100644 aspnet-core/frameworks/src/Lion.AbpPro.Nacos/Microsoft/Extensions/Hosting/HostBuilderExtensions.cs delete mode 100644 aspnet-core/modules/BasicManagement/src/Lion.AbpPro.BasicManagement.Domain/Data/AbpProDbMigrationService.cs delete mode 100644 aspnet-core/modules/BasicManagement/src/Lion.AbpPro.BasicManagement.Domain/Data/IAbpProDbSchemaMigrator.cs delete mode 100644 aspnet-core/modules/BasicManagement/src/Lion.AbpPro.BasicManagement.Domain/Data/NullAbpProDbSchemaMigrator.cs delete mode 100644 aspnet-core/modules/FileManagement/src/Lion.AbpPro.FileManagement.Domain/Data/FileManagementDbMigrationService.cs delete mode 100644 aspnet-core/modules/FileManagement/src/Lion.AbpPro.FileManagement.Domain/Data/IFileManagementDbSchemaMigrator.cs delete mode 100644 aspnet-core/modules/FileManagement/src/Lion.AbpPro.FileManagement.Domain/Data/NullFileManagementDbSchemaMigrator.cs diff --git a/aspnet-core/Lion.AbpPro.slnx b/aspnet-core/Lion.AbpPro.slnx index b85fd600..7463084d 100644 --- a/aspnet-core/Lion.AbpPro.slnx +++ b/aspnet-core/Lion.AbpPro.slnx @@ -25,6 +25,7 @@ + diff --git a/aspnet-core/frameworks/src/Lion.AbpPro.Nacos/Lion.AbpPro.Nacos.csproj b/aspnet-core/frameworks/src/Lion.AbpPro.Nacos/Lion.AbpPro.Nacos.csproj new file mode 100644 index 00000000..b1984361 --- /dev/null +++ b/aspnet-core/frameworks/src/Lion.AbpPro.Nacos/Lion.AbpPro.Nacos.csproj @@ -0,0 +1,14 @@ + + + + net10.0 + Lion.AbpPro.Nacos + + + + + + + + + diff --git a/aspnet-core/frameworks/src/Lion.AbpPro.Nacos/Microsoft/Extensions/Hosting/HostBuilderExtensions.cs b/aspnet-core/frameworks/src/Lion.AbpPro.Nacos/Microsoft/Extensions/Hosting/HostBuilderExtensions.cs new file mode 100644 index 00000000..39879d4b --- /dev/null +++ b/aspnet-core/frameworks/src/Lion.AbpPro.Nacos/Microsoft/Extensions/Hosting/HostBuilderExtensions.cs @@ -0,0 +1,38 @@ +using Microsoft.Extensions.Configuration; + +namespace Microsoft.Extensions.Hosting; + +public static class HostBuilderExtensions +{ + /// + /// 添加Nacos配置支持 + /// + /// 主机构建器 + /// 主机构建器 + public static IHostBuilder UseNacos(this IHostBuilder hostBuilder) + { + return hostBuilder.ConfigureAppConfiguration((context, builder) => + { + var configuration = builder.Build(); + // 从配置文件读取Nacos相关配置 + // 默认会使用JSON解析器来解析存在Nacos Server的配置 + builder.AddNacosV2Configuration(configuration.GetSection("NacosConfig")); + }); + } + + /// + /// 添加Nacos配置支持 + /// + /// 主机构建器 + /// Nacos配置节名称,默认为"NacosConfig" + /// 主机构建器 + public static IHostBuilder UseNacos(this IHostBuilder hostBuilder, string sectionName = "NacosConfig") + { + return hostBuilder.ConfigureAppConfiguration((context, builder) => + { + var configuration = builder.Build(); + // 从配置文件读取Nacos相关配置 + builder.AddNacosV2Configuration(configuration.GetSection(sectionName)); + }); + } +} \ No newline at end of file diff --git a/aspnet-core/modules/BasicManagement/src/Lion.AbpPro.BasicManagement.Domain/Data/AbpProDbMigrationService.cs b/aspnet-core/modules/BasicManagement/src/Lion.AbpPro.BasicManagement.Domain/Data/AbpProDbMigrationService.cs deleted file mode 100644 index d7c9a81d..00000000 --- a/aspnet-core/modules/BasicManagement/src/Lion.AbpPro.BasicManagement.Domain/Data/AbpProDbMigrationService.cs +++ /dev/null @@ -1,213 +0,0 @@ -using System.Diagnostics; -using System.Runtime.InteropServices; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Logging.Abstractions; -using Volo.Abp.Data; -using Volo.Abp.DependencyInjection; -using Volo.Abp.MultiTenancy; - -namespace Lion.AbpPro.BasicManagement.Data -{ - public class AbpProDbMigrationService : ITransientDependency - { - private ILogger Logger { get; set; } - - private readonly IDataSeeder _dataSeeder; - private readonly IEnumerable _dbSchemaMigrators; - private readonly ITenantRepository _tenantRepository; - private readonly ICurrentTenant _currentTenant; - - public AbpProDbMigrationService( - IDataSeeder dataSeeder, - IEnumerable dbSchemaMigrators, - ITenantRepository tenantRepository, - ICurrentTenant currentTenant) - { - _dataSeeder = dataSeeder; - _dbSchemaMigrators = dbSchemaMigrators; - _tenantRepository = tenantRepository; - _currentTenant = currentTenant; - - Logger = NullLogger.Instance; - } - - public async Task MigrateAsync() - { - var initialMigrationAdded = AddInitialMigrationIfNotExist(); - - if (initialMigrationAdded) - { - return; - } - - Logger.LogInformation("Started database migrations..."); - - await MigrateDatabaseSchemaAsync(); - await SeedDataAsync(); - - Logger.LogInformation($"Successfully completed host database migrations."); - - var tenants = await _tenantRepository.GetListAsync(includeDetails: true); - - var migratedDatabaseSchemas = new HashSet(); - foreach (var tenant in tenants) - { - using (_currentTenant.Change(tenant.Id)) - { - if (tenant.ConnectionStrings.Any()) - { - var tenantConnectionStrings = tenant.ConnectionStrings - .Select(x => x.Value) - .ToList(); - - if (!migratedDatabaseSchemas.IsSupersetOf(tenantConnectionStrings)) - { - await MigrateDatabaseSchemaAsync(tenant); - - migratedDatabaseSchemas.AddIfNotContains(tenantConnectionStrings); - } - } - - await SeedDataAsync(tenant); - } - - Logger.LogInformation($"Successfully completed {tenant.Name} tenant database migrations."); - } - - Logger.LogInformation("Successfully completed all database migrations."); - Logger.LogInformation("You can safely end this process..."); - } - - private async Task MigrateDatabaseSchemaAsync(Tenant tenant = null) - { - Logger.LogInformation( - $"Migrating schema for {(tenant == null ? "host" : tenant.Name + " tenant")} database..."); - - foreach (var migrator in _dbSchemaMigrators) - { - await migrator.MigrateAsync(); - } - } - - private async Task SeedDataAsync(Tenant tenant = null) - { - Logger.LogInformation($"Executing {(tenant == null ? "host" : tenant.Name + " tenant")} database seed..."); - - await _dataSeeder.SeedAsync(new DataSeedContext(tenant?.Id) - .WithProperty(IdentityDataSeedContributor.AdminEmailPropertyName, IdentityDataSeedContributor.AdminEmailDefaultValue) - .WithProperty(IdentityDataSeedContributor.AdminPasswordPropertyName, IdentityDataSeedContributor.AdminPasswordDefaultValue) - ); - } - - private bool AddInitialMigrationIfNotExist() - { - try - { - if (!DbMigrationsProjectExists()) - { - return false; - } - } - catch (Exception) - { - return false; - } - - try - { - if (!MigrationsFolderExists()) - { - AddInitialMigration(); - return true; - } - else - { - return false; - } - } - catch (Exception e) - { - Logger.LogWarning("Couldn't determinate if any migrations exist : " + e.Message); - return false; - } - } - - private bool DbMigrationsProjectExists() - { - var dbMigrationsProjectFolder = GetDbMigrationsProjectFolderPath(); - - return dbMigrationsProjectFolder != null; - } - - private bool MigrationsFolderExists() - { - var dbMigrationsProjectFolder = GetDbMigrationsProjectFolderPath(); - - return Directory.Exists(Path.Combine(dbMigrationsProjectFolder, "EntityFrameworkCore")); - } - - private void AddInitialMigration() - { - Logger.LogInformation("Creating initial migration..."); - - string argumentPrefix; - string fileName; - - if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - { - argumentPrefix = "-c"; - fileName = "/bin/bash"; - } - else - { - argumentPrefix = "/C"; - fileName = "cmd.exe"; - } - - var procStartInfo = new ProcessStartInfo(fileName, - $"{argumentPrefix} \"abp create-migration-and-run-migrator \"{GetDbMigrationsProjectFolderPath()}\"\"" - ); - - try - { - Process.Start(procStartInfo); - } - catch (Exception) - { - throw new Exception("Couldn't run ABP CLI..."); - } - } - - private string GetDbMigrationsProjectFolderPath() - { - var slnDirectoryPath = GetSolutionDirectoryPath(); - - if (slnDirectoryPath == null) - { - throw new Exception("Solution folder not found!"); - } - - var srcDirectoryPath = Path.Combine(slnDirectoryPath, "src"); - - return Directory.GetDirectories(srcDirectoryPath) - .FirstOrDefault(d => d.EndsWith(".DbMigrations")); - } - - private string GetSolutionDirectoryPath() - { - var currentDirectory = new DirectoryInfo(Directory.GetCurrentDirectory()); - - while (Directory.GetParent(currentDirectory.FullName) != null) - { - currentDirectory = Directory.GetParent(currentDirectory.FullName); - - if (Directory.GetFiles(currentDirectory.FullName).FirstOrDefault(f => f.EndsWith(".sln")) != null) - { - return currentDirectory.FullName; - } - } - - return null; - } - } -} \ No newline at end of file diff --git a/aspnet-core/modules/BasicManagement/src/Lion.AbpPro.BasicManagement.Domain/Data/IAbpProDbSchemaMigrator.cs b/aspnet-core/modules/BasicManagement/src/Lion.AbpPro.BasicManagement.Domain/Data/IAbpProDbSchemaMigrator.cs deleted file mode 100644 index f8a038ea..00000000 --- a/aspnet-core/modules/BasicManagement/src/Lion.AbpPro.BasicManagement.Domain/Data/IAbpProDbSchemaMigrator.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Lion.AbpPro.BasicManagement.Data -{ - public interface IAbpProDbSchemaMigrator - { - Task MigrateAsync(); - } -} diff --git a/aspnet-core/modules/BasicManagement/src/Lion.AbpPro.BasicManagement.Domain/Data/NullAbpProDbSchemaMigrator.cs b/aspnet-core/modules/BasicManagement/src/Lion.AbpPro.BasicManagement.Domain/Data/NullAbpProDbSchemaMigrator.cs deleted file mode 100644 index 45984b83..00000000 --- a/aspnet-core/modules/BasicManagement/src/Lion.AbpPro.BasicManagement.Domain/Data/NullAbpProDbSchemaMigrator.cs +++ /dev/null @@ -1,15 +0,0 @@ -using Volo.Abp.DependencyInjection; - -namespace Lion.AbpPro.BasicManagement.Data -{ - /* This is used if database provider does't define - * IAbpProDbSchemaMigrator implementation. - */ - public class NullAbpProDbSchemaMigrator : IAbpProDbSchemaMigrator, ITransientDependency - { - public Task MigrateAsync() - { - return Task.CompletedTask; - } - } -} \ No newline at end of file diff --git a/aspnet-core/modules/FileManagement/src/Lion.AbpPro.FileManagement.Domain/Data/FileManagementDbMigrationService.cs b/aspnet-core/modules/FileManagement/src/Lion.AbpPro.FileManagement.Domain/Data/FileManagementDbMigrationService.cs deleted file mode 100644 index 5ec9821f..00000000 --- a/aspnet-core/modules/FileManagement/src/Lion.AbpPro.FileManagement.Domain/Data/FileManagementDbMigrationService.cs +++ /dev/null @@ -1,158 +0,0 @@ -namespace Lion.AbpPro.FileManagement.Data; - -public class FileManagementDbMigrationService : ITransientDependency -{ - private readonly IDataSeeder _dataSeeder; - private readonly IEnumerable _dbSchemaMigrators; - - public FileManagementDbMigrationService( - IDataSeeder dataSeeder, - IEnumerable dbSchemaMigrators) - { - _dataSeeder = dataSeeder; - _dbSchemaMigrators = dbSchemaMigrators; - Logger = NullLogger.Instance; - } - - public ILogger Logger { get; set; } - - public async Task MigrateAsync() - { - var initialMigrationAdded = AddInitialMigrationIfNotExist(); - - if (initialMigrationAdded) - { - return; - } - - Logger.LogInformation("Started database migrations..."); - await MigrateDatabaseSchemaAsync(); - await SeedDataAsync(); - Logger.LogInformation("Successfully completed host database migrations."); - Logger.LogInformation("You can safely end this process..."); - } - - private async Task MigrateDatabaseSchemaAsync() - { - foreach (var migrator in _dbSchemaMigrators) - { - await migrator.MigrateAsync(); - } - } - - private async Task SeedDataAsync() - { - await _dataSeeder.SeedAsync(new DataSeedContext()); - } - - private bool AddInitialMigrationIfNotExist() - { - try - { - if (!DbMigrationsProjectExists()) - { - return false; - } - } - catch (Exception) - { - return false; - } - - try - { - if (!MigrationsFolderExists()) - { - AddInitialMigration(); - return true; - } - - return false; - } - catch (Exception e) - { - Logger.LogWarning("Couldn't determinate if any migrations exist : " + e.Message); - return false; - } - } - - private bool DbMigrationsProjectExists() - { - var dbMigrationsProjectFolder = GetEntityFrameworkCoreProjectFolderPath(); - - return dbMigrationsProjectFolder != null; - } - - private bool MigrationsFolderExists() - { - var dbMigrationsProjectFolder = GetEntityFrameworkCoreProjectFolderPath(); - - return Directory.Exists(Path.Combine(dbMigrationsProjectFolder, "Migrations")); - } - - private void AddInitialMigration() - { - Logger.LogInformation("Creating initial migration..."); - - string argumentPrefix; - string fileName; - - if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || - RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - { - argumentPrefix = "-c"; - fileName = "/bin/bash"; - } - else - { - argumentPrefix = "/C"; - fileName = "cmd.exe"; - } - - var procStartInfo = new ProcessStartInfo(fileName, - $"{argumentPrefix} \"abp create-migration-and-run-migrator \"{GetEntityFrameworkCoreProjectFolderPath()}\"\"" - ); - - try - { - Process.Start(procStartInfo); - } - catch (Exception) - { - throw new Exception("Couldn't run ABP CLI..."); - } - } - - private string GetEntityFrameworkCoreProjectFolderPath() - { - var slnDirectoryPath = GetSolutionDirectoryPath(); - - if (slnDirectoryPath == null) - { - throw new Exception("Solution folder not found!"); - } - - var srcDirectoryPath = Path.Combine(slnDirectoryPath, "src"); - - return Directory.GetDirectories(srcDirectoryPath) - .FirstOrDefault(d => d.EndsWith(".EntityFrameworkCore")); - } - - private string GetSolutionDirectoryPath() - { - var currentDirectory = new DirectoryInfo(Directory.GetCurrentDirectory()); - - while (Directory.GetParent(currentDirectory.FullName) != null) - { - currentDirectory = Directory.GetParent(currentDirectory.FullName); - - if (Directory.GetFiles(currentDirectory.FullName) - .FirstOrDefault(f => f.EndsWith(".sln")) != null) - { - return currentDirectory.FullName; - } - } - - return null; - } -} \ No newline at end of file diff --git a/aspnet-core/modules/FileManagement/src/Lion.AbpPro.FileManagement.Domain/Data/IFileManagementDbSchemaMigrator.cs b/aspnet-core/modules/FileManagement/src/Lion.AbpPro.FileManagement.Domain/Data/IFileManagementDbSchemaMigrator.cs deleted file mode 100644 index d01e38af..00000000 --- a/aspnet-core/modules/FileManagement/src/Lion.AbpPro.FileManagement.Domain/Data/IFileManagementDbSchemaMigrator.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Lion.AbpPro.FileManagement.Data; - -public interface IFileManagementDbSchemaMigrator -{ - Task MigrateAsync(); -} \ No newline at end of file diff --git a/aspnet-core/modules/FileManagement/src/Lion.AbpPro.FileManagement.Domain/Data/NullFileManagementDbSchemaMigrator.cs b/aspnet-core/modules/FileManagement/src/Lion.AbpPro.FileManagement.Domain/Data/NullFileManagementDbSchemaMigrator.cs deleted file mode 100644 index 9f46bc5c..00000000 --- a/aspnet-core/modules/FileManagement/src/Lion.AbpPro.FileManagement.Domain/Data/NullFileManagementDbSchemaMigrator.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace Lion.AbpPro.FileManagement.Data; - -public class NullFileManagementDbSchemaMigrator : IFileManagementDbSchemaMigrator, ITransientDependency -{ - public Task MigrateAsync() - { - return Task.CompletedTask; - } -} \ No newline at end of file diff --git a/aspnet-core/services/src/Lion.AbpPro.Application/Demo/DemoAppService.cs b/aspnet-core/services/src/Lion.AbpPro.Application/Demo/DemoAppService.cs index 489e4ef8..17f77f07 100644 --- a/aspnet-core/services/src/Lion.AbpPro.Application/Demo/DemoAppService.cs +++ b/aspnet-core/services/src/Lion.AbpPro.Application/Demo/DemoAppService.cs @@ -1,8 +1,9 @@ using Lion.AbpPro.Ddd.Application; -using Microsoft.AspNetCore.Mvc; +using Volo.Abp.Application.Dtos; using Volo.Abp.Domain.Repositories; namespace Lion.AbpPro.Demo; + //[Route("Demo")] public class DemoAppService : AbpProCrudAppService< DemoAggregate,