ABP微服务系列学习-搭建自己的微服务结构(二)
在解决方案根目录添加common.props,这个文件的作用是可以配置项目文件全局的一些属性,如忽略警告,全局PackageReference,语言版本等。
<Project>
  <PropertyGroup>
    <LangVersion>latest</LangVersion>
    <Version>1.0.0</Version>
    <NoWarn>$(NoWarn);CS1591</NoWarn>
	  <AbpProjectType>ms</AbpProjectType>
  </PropertyGroup>
  <Target Name="NoWarnOnRazorViewImportedTypeConflicts" BeforeTargets="RazorCoreCompile">
    <PropertyGroup>
      <NoWarn>$(NoWarn);0436</NoWarn>
    </PropertyGroup>
  </Target>
  <ItemGroup>
    <PackageReference Include="ConfigureAwait.Fody" Version="3.3.1" PrivateAssets="All" />
    <PackageReference Include="Fody" Version="6.5.3">
      <PrivateAssets>All</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
    </PackageReference>
  </ItemGroup>
</Project>
搭建服务
这里我们用ABP Cli来创建module模板,为什么用module模板呢,因为module模板最干净,没有别的依赖,app模板默认会添加ABP的基础模块,需要做删减,而module模板只需要添加需要的模块即可。
abp new FunShow.AdministrationService -t module --no-ui
abp new FunShow.IdentityService -t module --no-ui
创建完后删除多余的项目,authserver,DbMigrator, installer, mongoDb等,以及多余的文件,dockerfile,database目录,nuget.config,common.props等。
然后再把HttpApi.Host移到src目录。
最终结构如下

AdministrationService
首先我们先确定一下AdministrationService需要哪些ABP基础模块功能。
一个是Permission,一个是Feature,一个是Setting,还有Tenant。这里没包含审计日志是因为后续计划把日志模块独立一个服务。
我们按照模块依赖关系安装对应的模块包,以及在Module中DependsOn对应的模块
FunShow.AdministrationService.Domain.Shared
编辑项目文件,添加依赖
<Project Sdk="Microsoft.NET.Sdk">
	<Import Project="..\..\..\..\common.props" />
	<PropertyGroup>
		<TargetFramework>netstandard2.0</TargetFramework>
		<RootNamespace>FunShow.AdministrationService</RootNamespace>
		<GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest>
	</PropertyGroup>
	<ItemGroup>
		<PackageReference Include="Volo.Abp.Validation" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.PermissionManagement.Domain.Shared" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.FeatureManagement.Domain.Shared" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.SettingManagement.Domain.Shared" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.TenantManagement.Domain.Shared" Version="7.0.0" />
	</ItemGroup>
	<ItemGroup>
		<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="7.0.1" />
	</ItemGroup>
	<ItemGroup>
		<EmbeddedResource Include="Localization\AdministrationService\*.json" />
		<Content Remove="Localization\AdministrationService\*.json" />
	</ItemGroup>
</Project>
在Module添加模块依赖
using Volo.Abp.PermissionManagement;
using Volo.Abp.FeatureManagement;
using Volo.Abp.SettingManagement;
[DependsOn(
    typeof(AbpPermissionManagementDomainSharedModule),
    typeof(AbpFeatureManagementDomainSharedModule),
    typeof(AbpSettingManagementDomainSharedModule),
    typeof(AbpValidationModule)
)]
public class AdministrationServiceDomainSharedModule : AbpModule
FunShow.AdministrationService.Domain
编辑项目文件,添加依赖
<Project Sdk="Microsoft.NET.Sdk">
	<Import Project="..\..\..\..\common.props" />
	<PropertyGroup>
		<TargetFramework>net7.0</TargetFramework>
		<RootNamespace>FunShow.AdministrationService</RootNamespace>
	</PropertyGroup>
	<ItemGroup>
		<PackageReference Include="Volo.Abp.Ddd.Domain" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.Identity.Domain" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.PermissionManagement.Domain" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.FeatureManagement.Domain" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.SettingManagement.Domain" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.PermissionManagement.Domain.OpenIddict" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.PermissionManagement.Domain.Identity" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.TenantManagement.Domain" Version="7.0.0" />
		<ProjectReference Include="..\FunShow.AdministrationService.Domain.Shared\FunShow.AdministrationService.Domain.Shared.csproj" />
	</ItemGroup>
</Project>
在Module添加模块依赖
using Volo.Abp.Domain;
using Volo.Abp.Modularity;
using Volo.Abp.FeatureManagement;
using Volo.Abp.PermissionManagement;
using Volo.Abp.PermissionManagement.Identity;
using Volo.Abp.PermissionManagement.OpenIddict;
using Volo.Abp.SettingManagement;
using Volo.Abp.TenantManagement;
using Volo.Abp.Identity;
[DependsOn(
    typeof(AbpDddDomainModule),
    typeof(AbpIdentityDomainModule),
    typeof(AdministrationServiceDomainSharedModule),
    typeof(AbpPermissionManagementDomainModule),
    typeof(AbpTenantManagementDomainModule),
    typeof(AbpFeatureManagementDomainModule),
    typeof(AbpSettingManagementDomainModule),
    typeof(AbpPermissionManagementDomainOpenIddictModule),
    typeof(AbpPermissionManagementDomainIdentityModule)
)]
public class AdministrationServiceDomainModule : AbpModule
{
}
FunShow.AdministrationService.EntityFrameworkCore
编辑项目文件,添加依赖
<Project Sdk="Microsoft.NET.Sdk">
	<Import Project="..\..\..\..\common.props" />
	<PropertyGroup>
		<TargetFramework>net7.0</TargetFramework>
		<RootNamespace>FunShow.AdministrationService</RootNamespace>
	</PropertyGroup>
	<ItemGroup>
		<PackageReference Include="Volo.Abp.EntityFrameworkCore.PostgreSql" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.EntityFrameworkCore" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.Identity.EntityFrameworkCore" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.PermissionManagement.EntityFrameworkCore" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.FeatureManagement.EntityFrameworkCore" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.SettingManagement.EntityFrameworkCore" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.TenantManagement.EntityFrameworkCore" Version="7.0.0" />
		<ProjectReference Include="..\FunShow.AdministrationService.Domain\FunShow.AdministrationService.Domain.csproj" />
	</ItemGroup>
</Project>
在Module添加模块依赖
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.PostgreSql;
using Volo.Abp.FeatureManagement.EntityFrameworkCore;
using Volo.Abp.Modularity;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
using Volo.Abp.SettingManagement.EntityFrameworkCore;
using Volo.Abp.TenantManagement.EntityFrameworkCore;
[DependsOn(
    typeof(AbpEntityFrameworkCorePostgreSqlModule),
    typeof(AbpEntityFrameworkCoreModule),
    typeof(AdministrationServiceDomainModule),
    typeof(AbpPermissionManagementEntityFrameworkCoreModule),
    typeof(AbpTenantManagementEntityFrameworkCoreModule),
    typeof(AbpFeatureManagementEntityFrameworkCoreModule),
    typeof(AbpSettingManagementEntityFrameworkCoreModule),
    typeof(AbpIdentityEntityFrameworkCoreModule)
)]
public class AdministrationServiceEntityFrameworkCoreModule : AbpModule
在module中替换DbContext
context.Services.AddAbpDbContext<AdministrationServiceDbContext>(options =>
        {
            /* Add custom repositories here. Example:
             * options.AddRepository<Question, EfCoreQuestionRepository>();
             */
            options.ReplaceDbContext<IPermissionManagementDbContext>();
            options.ReplaceDbContext<ISettingManagementDbContext>();
            options.ReplaceDbContext<IFeatureManagementDbContext>();
            options.ReplaceDbContext<ITenantManagementDbContext>();
            /* Remove "includeAllEntities: true" to create
             * default repositories only for aggregate roots */
            options.AddDefaultRepositories(includeAllEntities: true);
        });
        Configure<AbpDbContextOptions>(options =>
        {
            options.Configure<AdministrationServiceDbContext>(c =>
            {
                c.UseNpgsql(b =>
                {
                    b.MigrationsHistoryTable("__AdministrationService_Migrations");
                });
            });
        });
在DbContext中继承基础模块的IDbContext接口并实现
[ConnectionStringName(AdministrationServiceDbProperties.ConnectionStringName)]
public class AdministrationServiceDbContext : AbpDbContext<AdministrationServiceDbContext>, IAdministrationServiceDbContext,
    IPermissionManagementDbContext,
    ISettingManagementDbContext,
    IFeatureManagementDbContext,
    ITenantManagementDbContext
在DbContext中的OnModelCreating中添加模块的数据库初始化方法
protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        builder.ConfigureAdministrationService();
        builder.ConfigurePermissionManagement();
        builder.ConfigureSettingManagement();
        builder.ConfigureFeatureManagement();
        builder.ConfigureTenantManagement();
    }
FunShow.AdministrationService.Application.Contracts
编辑项目文件,添加nuget依赖
<Project Sdk="Microsoft.NET.Sdk">
	<Import Project="..\..\..\..\common.props" />
	<PropertyGroup>
		<TargetFramework>netstandard2.0</TargetFramework>
		<RootNamespace>FunShow.AdministrationService</RootNamespace>
	</PropertyGroup>
	<ItemGroup>
		<PackageReference Include="Volo.Abp.Ddd.Application.Contracts" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.Authorization" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.PermissionManagement.Application.Contracts" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.Identity.Application.Contracts" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.FeatureManagement.Application.Contracts" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.SettingManagement.Application.Contracts" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.TenantManagement.Application.Contracts" Version="7.0.0" />
		<ProjectReference Include="..\FunShow.AdministrationService.Domain.Shared\FunShow.AdministrationService.Domain.Shared.csproj" />
	</ItemGroup>
</Project>
在Module添加模块依赖
using Volo.Abp.Application;
using Volo.Abp.Authorization;
using Volo.Abp.Modularity;
using Volo.Abp.FeatureManagement;
using Volo.Abp.PermissionManagement;
using Volo.Abp.SettingManagement;
using Volo.Abp.TenantManagement;
[DependsOn(
    typeof(AbpPermissionManagementApplicationContractsModule),
    typeof(AbpFeatureManagementApplicationContractsModule),
    typeof(AbpSettingManagementApplicationContractsModule),
    typeof(AbpTenantManagementApplicationContractsModule),
    typeof(AdministrationServiceDomainSharedModule),
    typeof(AbpDddApplicationContractsModule),
    typeof(AbpAuthorizationModule)
    )]
public class AdministrationServiceApplicationContractsModule : AbpModule
FunShow.AdministrationService.Application
编辑项目文件,添加依赖
<Project Sdk="Microsoft.NET.Sdk">
	<Import Project="..\..\..\..\common.props" />
	<PropertyGroup>
		<TargetFramework>net7.0</TargetFramework>
		<RootNamespace>FunShow.AdministrationService</RootNamespace>
	</PropertyGroup>
	<ItemGroup>
		<PackageReference Include="Volo.Abp.AutoMapper" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.Ddd.Application" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.PermissionManagement.Application" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.FeatureManagement.Application" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.SettingManagement.Application" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.TenantManagement.Application" Version="7.0.0" />
		<ProjectReference Include="..\FunShow.AdministrationService.Application.Contracts\FunShow.AdministrationService.Application.Contracts.csproj" />
		<ProjectReference Include="..\FunShow.AdministrationService.Domain\FunShow.AdministrationService.Domain.csproj" />
	</ItemGroup>
</Project>
在Module添加模块依赖
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Application;
using Volo.Abp.AutoMapper;
using Volo.Abp.Modularity;
using Volo.Abp.FeatureManagement;
using Volo.Abp.PermissionManagement;
using Volo.Abp.SettingManagement;
using Volo.Abp.TenantManagement;
[DependsOn(
    typeof(AbpPermissionManagementApplicationModule),
    typeof(AbpFeatureManagementApplicationModule),
    typeof(AbpSettingManagementApplicationModule),
    typeof(AbpTenantManagementApplicationModule),
    typeof(AdministrationServiceDomainModule),
    typeof(AdministrationServiceApplicationContractsModule),
    typeof(AbpDddApplicationModule),
    typeof(AbpAutoMapperModule)
    )]
public class AdministrationServiceApplicationModule : AbpModule
FunShow.AdministrationService.HttpApi
编辑项目文件,添加依赖
<Project Sdk="Microsoft.NET.Sdk">
	<Import Project="..\..\..\..\common.props" />
	<PropertyGroup>
		<TargetFramework>net7.0</TargetFramework>
		<RootNamespace>FunShow.AdministrationService</RootNamespace>
	</PropertyGroup>
	<ItemGroup>
		<PackageReference Include="Volo.Abp.AspNetCore.Mvc" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.PermissionManagement.HttpApi" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.FeatureManagement.HttpApi" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.SettingManagement.HttpApi" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.TenantManagement.HttpApi" Version="7.0.0" />
		<ProjectReference Include="..\FunShow.AdministrationService.Application.Contracts\FunShow.AdministrationService.Application.Contracts.csproj" />
	</ItemGroup>
</Project>
在Module添加模块依赖
using Localization.Resources.AbpUi;
using Microsoft.Extensions.DependencyInjection;
using FunShow.AdministrationService.Localization;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Localization;
using Volo.Abp.Modularity;
using Volo.Abp.FeatureManagement;
using Volo.Abp.PermissionManagement.HttpApi;
using Volo.Abp.SettingManagement;
using Volo.Abp.TenantManagement;
[DependsOn(
    typeof(AbpPermissionManagementHttpApiModule),
    typeof(AbpFeatureManagementHttpApiModule),
    typeof(AbpSettingManagementHttpApiModule),
    typeof(AbpTenantManagementHttpApiModule),
    typeof(AdministrationServiceApplicationContractsModule),
    typeof(AbpAspNetCoreMvcModule))]
public class AdministrationServiceHttpApiModule : AbpModule
FunShow.AdministrationService.HttpApi.Client
编辑项目文件,添加nuget依赖
<Project Sdk="Microsoft.NET.Sdk">
	<Import Project="..\..\..\..\common.props" />
	<PropertyGroup>
		<TargetFramework>netstandard2.0</TargetFramework>
		<RootNamespace>FunShow.AdministrationService</RootNamespace>
	</PropertyGroup>
	<ItemGroup>
		<PackageReference Include="Volo.Abp.Http.Client" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.PermissionManagement.HttpApi.Client" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.FeatureManagement.HttpApi.Client" Version="7.0.0" />
		<PackageReference Include="Volo.Abp.SettingManagement.HttpApi.Client" Version="7.0.0" />
		<ProjectReference Include="..\FunShow.AdministrationService.Application.Contracts\FunShow.AdministrationService.Application.Contracts.csproj" />
	</ItemGroup>
	<ItemGroup>
		<EmbeddedResource Include="**\*generate-proxy.json" />
		<Content Remove="**\*generate-proxy.json" />
	</ItemGroup>
</Project>
在Module添加模块依赖
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Http.Client;
using Volo.Abp.Modularity;
using Volo.Abp.VirtualFileSystem;
using Volo.Abp.FeatureManagement;
using Volo.Abp.PermissionManagement;
using Volo.Abp.SettingManagement;
[DependsOn(
    typeof(AbpPermissionManagementHttpApiClientModule),
    typeof(AbpFeatureManagementHttpApiClientModule),
    typeof(AbpSettingManagementHttpApiClientModule),
    typeof(AdministrationServiceApplicationContractsModule),
    typeof(AbpHttpClientModule))]
public class AdministrationServiceHttpApiClientModule : AbpModule
FunShow.AdministrationService.HttpApi.Host
编辑项目文件,添加依赖
<Project Sdk="Microsoft.NET.Sdk.Web">
	<Import Project="..\..\..\..\common.props" />
	<PropertyGroup>
		<TargetFramework>net7.0</TargetFramework>
		<RootNamespace>FunShow.AdministrationService</RootNamespace>
		<PreserveCompilationReferences>true</PreserveCompilationReferences>
		<UserSecretsId>FunShow.AdministrationService-c2d31439-b723-48e2-b061-5ebd7aeb6010</UserSecretsId>
	</PropertyGroup>
	<ItemGroup>
		<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.1">
			<PrivateAssets>all</PrivateAssets>
			<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
		</PackageReference>
	</ItemGroup>
	<ItemGroup>
		<ProjectReference Include="..\..\..\..\shared\FunShow.Shared.Localization\FunShow.Shared.Localization.csproj" />
		<ProjectReference Include="..\..\..\..\shared\FunShow.Shared.Hosting.Microservices\FunShow.Shared.Hosting.Microservices.csproj" />
		<ProjectReference Include="..\FunShow.AdministrationService.Application\FunShow.AdministrationService.Application.csproj" />
		<ProjectReference Include="..\FunShow.AdministrationService.EntityFrameworkCore\FunShow.AdministrationService.EntityFrameworkCore.csproj" />
		<ProjectReference Include="..\FunShow.AdministrationService.HttpApi\FunShow.AdministrationService.HttpApi.csproj" />
	</ItemGroup>
	<ItemGroup>
		<Compile Remove="Logs\**" />
		<Content Remove="Logs\**" />
		<EmbeddedResource Remove="Logs\**" />
		<None Remove="Logs\**" />
	</ItemGroup>
</Project>
在module中添加模块依赖
[DependsOn(
    typeof(FunShowSharedLocalizationModule),
    typeof(FunShowSharedHostingMicroservicesModule),
    typeof(AdministrationServiceApplicationModule),
    typeof(AdministrationServiceHttpApiModule),
    typeof(AdministrationServiceEntityFrameworkCoreModule)
    )]
public class AdministrationServiceHttpApiHostModule : AbpModule
然后编辑Module中的ConfigureServices方法,OnApplicationInitialization方法,OnPostApplicationInitializationAsync方法
public override void ConfigureServices(ServiceConfigurationContext context)
    {
        //You can disable this setting in production to avoid any potential security risks.
        Microsoft.IdentityModel.Logging.IdentityModelEventSource.ShowPII = true;
        // Enable if you need these
        // var hostingEnvironment = context.Services.GetHostingEnvironment();
        var configuration = context.Services.GetConfiguration();
        JwtBearerConfigurationHelper.Configure(context, "AdministrationService");
        SwaggerConfigurationHelper.ConfigureWithAuth(
            context: context,
            authority: configuration["AuthServer:Authority"],
            scopes: new
                Dictionary<string, string> /* Requested scopes for authorization code request and descriptions for swagger UI only */
                {
                    {"AdministrationService", "AdministrationService API"}
                },
            apiTitle: "AdministrationService API"
        );
        context.Services.AddCors(options =>
        {
            options.AddDefaultPolicy(builder =>
            {
                builder
                    .WithOrigins(
                        configuration["App:CorsOrigins"]
                            .Split(",", StringSplitOptions.RemoveEmptyEntries)
                            .Select(o => o.Trim().RemovePostFix("/"))
                            .ToArray()
                    )
                    .WithAbpExposedHeaders()
                    .SetIsOriginAllowedToAllowWildcardSubdomains()
                    .AllowAnyHeader()
                    .AllowAnyMethod()
                    .AllowCredentials();
            });
        });
    }
    public override void OnApplicationInitialization(ApplicationInitializationContext context)
    {
        var app = context.GetApplicationBuilder();
        var env = context.GetEnvironment();
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        app.UseCorrelationId();
        app.UseAbpRequestLocalization();
        app.UseAbpSecurityHeaders();
        app.UseStaticFiles();
        app.UseRouting();
        app.UseCors();
        app.UseAuthentication();
        app.UseAbpClaimsMap();
        app.UseAuthorization();
        app.UseSwagger();
        app.UseAbpSwaggerUI(options =>
        {
            var configuration = context.ServiceProvider.GetRequiredService<IConfiguration>();
            options.SwaggerEndpoint("/swagger/v1/swagger.json", "AdministrationService API");
            options.OAuthClientId(configuration["AuthServer:SwaggerClientId"]);
        });
        app.UseAbpSerilogEnrichers();
        app.UseAuditing();
        app.UseUnitOfWork();
        app.UseConfiguredEndpoints(endpoints => endpoints.MapMetrics());
    }
    public async override Task OnPostApplicationInitializationAsync(ApplicationInitializationContext context)
    {
        using (var scope = context.ServiceProvider.CreateScope())
        {
            await scope.ServiceProvider
                .GetRequiredService<AdministrationServiceDatabaseMigrationChecker>()
                .CheckAndApplyDatabaseMigrationsAsync();
        }
    }
JwtBearerConfigurationHelper.Configure()和SwaggerConfigurationHelper.ConfigureWithAuth()就是我们在shared模块中封装的JWT和Swagger配置操作。
编辑Program
public async static Task<int> Main(string[] args)
{
    var assemblyName = typeof(Program).Assembly.GetName().Name;
    SerilogConfigurationHelper.Configure(assemblyName);
    try
    {
        Log.Information($"Starting {assemblyName}.");
        var builder = WebApplication.CreateBuilder(args);
        builder.Host
            .AddAppSettingsSecretsJson()
            .UseAutofac()
            .UseSerilog();
        await builder.AddApplicationAsync<AdministrationServiceHttpApiHostModule>();
        var app = builder.Build();
        await app.InitializeApplicationAsync();
        await app.RunAsync();
        return 0;
    }
    catch (Exception ex)
    {
        Log.Fatal(ex, $"{assemblyName} terminated unexpectedly!");
        return 1;
    }
    finally
    {
        Log.CloseAndFlush();
    }
}
SerilogConfigurationHelper.Configure就是shared项目中共用的日志配置操作
修改appsettings.json
{
  "App": {
    "SelfUrl": "https://localhost:44367",
    "CorsOrigins": "https://localhost:44325,https://localhost:44353"
  },
  "AuthServer": {
    "Authority": "https://localhost:44322",
    "RequireHttpsMetadata": "true",
    "SwaggerClientId": "WebGateway_Swagger"
  },
  "RemoteServices": {
    "AbpIdentity": {
      "BaseUrl": "https://localhost:44388/",
      "UseCurrentAccessToken": "false"
    }
  },
  "IdentityClients": {
    "Default": {
      "GrantType": "client_credentials",
      "ClientId": "FunShow_AdministrationService",
      "ClientSecret": "1q2w3e*",
      "Authority": "https://localhost:44322",
      "Scope": "IdentityService"
    }
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "AdministrationService": "Server=localhost,1434;Database=FunShow_Administration;User Id=sa;password=myPassw0rd;MultipleActiveResultSets=true"
  },
  "StringEncryption": {
    "DefaultPassPhrase": "IOiW1AE6WjSf2KIH"
  },
  "Redis": {
    "Configuration": "localhost:6379"
  },
  "RabbitMQ": {
    "Connections": {
      "Default": {
        "HostName": "localhost"
      }
    },
    "EventBus": {
      "ClientName": "FunShow_AdministrationService",
      "ExchangeName": "FunShow"
    }
  },
  "ElasticSearch": {
    "Url": "http://localhost:9200"
  }
}
修改Shared.Hosting.Microservices
因为我们AdministrationService中很多数据是共用的,所以为了方便其他服务,我们把AdministrationService的数据操作也抽出来,每个服务可以直接操作AdministrationService的数据库读取数据。
在项目文件添加AdministrationService的EfCore项目引用
<ItemGroup>
  <ProjectReference Include="..\..\services\administration\src\Walk.AdministrationService.EntityFrameworkCore\Walk.AdministrationService.EntityFrameworkCore.csproj" />
</ItemGroup>
module添加依赖
typeof(AdministrationServiceEntityFrameworkCoreModule)
到这我们AdministrationService的配置基本就完成了。
但是现在还不能启动服务,因为我们数据库还没迁移,并且还没对接上认证服务。
接下来我们在实现IdentityService,然后搭建认证服务和网关服务即可初步完成最初的服务搭建。
IdentityService
和AdministrationService不同的是,这个服务我们只需要依赖Identity和OpenIddict的基础模块。
配置方式和AdministrationService基本一致。
这里我就不重复了,以免水文。
LoggingService
上面我们说了打算把日志抽离单独一个服务,并且其他服务写日志通过消息队列写入数据库。
处理方法参考ABP商业版的文档https://docs.abp.io/en/commercial/latest/guides/extracting-module-as-microservice
不同的是由于Audit-Logging Management是商业版模块,所以我们的服务只依赖Volo.Abp.AuditLogging的开源模块,基本也满足了。日志相关的操作我们后续再自定义一些API提供对接。
以发布事件写入审核日志
在Shared.Hosting.Microservices项目中创建Logging目录,添加类EventBasedAuditingStore
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Volo.Abp.Auditing;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.Uow;
namespace FunShow.Shared.Hosting.Microservices.Logging
{
    [Dependency(ReplaceServices = true)]
    [ExposeServices(typeof(IAuditingStore))]
    public class EventBasedAuditingStore : IAuditingStore, ITransientDependency
    {
        private readonly IDistributedEventBus _distributedEventBus;
        private readonly ILogger<EventBasedAuditingStore> _logger;
        public EventBasedAuditingStore(IDistributedEventBus distributedEventBus, ILogger<EventBasedAuditingStore> logger)
        {
            _distributedEventBus = distributedEventBus;
            _logger = logger;
        }
        [UnitOfWork]
        public async Task SaveAsync(AuditLogInfo auditInfo)
        {
            _logger.LogInformation("Publishing audit log creation...");
            // EntityEntry will break serialization so we remove it
            for (var i = 0; i < auditInfo.EntityChanges.Count; i++)
            {
                auditInfo.EntityChanges[i].EntityEntry = null;
            }
            await _distributedEventBus.PublishAsync(auditInfo);
        }
    }
}
在 LoggingService.HttpApi.Host 下命名的处理程序:AuditCreationHandler
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
using Volo.Abp.Auditing;
using Volo.Abp.AuditLogging;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus.Distributed;
using Volo.Abp.Uow;
namespace FunShow.LoggingService.HttpApi.Host.Handlers
{
    public class AuditCreationHandler : IDistributedEventHandler<AuditLogInfo>, ITransientDependency
    {
        private readonly IAuditLogRepository _auditLogRepository;
        private readonly IAuditLogInfoToAuditLogConverter _converter;
        private readonly ILogger<AuditCreationHandler> _logger;
        public AuditCreationHandler(IAuditLogRepository auditLogRepository, IAuditLogInfoToAuditLogConverter converter,
            ILogger<AuditCreationHandler> logger)
        {
            _converter = converter;
            _logger = logger;
            _auditLogRepository = auditLogRepository;
        }
        [UnitOfWork]
        public async Task HandleEventAsync(AuditLogInfo eventData)
        {
            try
            {
                _logger.LogInformation("Handling Audit Creation...");
                await _auditLogRepository.InsertAsync(await _converter.ConvertAsync(eventData));
            }
            catch (Exception ex)
            {
                _logger.LogWarning("Could not save the audit log object ...");
                _logger.LogException(ex, LogLevel.Error);
            }
        }
    }
}
这就完成了日志服务的搭建。
下一章我们来搭建认证服务和网关服务
ABP微服务系列学习-搭建自己的微服务结构(二)的更多相关文章
- ABP微服务系列学习-搭建自己的微服务结构(一)
		在原本的结构里面,由于默认服务引用的都是ABP原生的模块,所以结构目录里面没有包含modules目录,这里我们添加一个modules目录,用于存放我们的自定义模块.在shared里面,我们再抽一个Ev ... 
- ABP微服务系列学习-搭建自己的微服务结构(四)
		上篇我们实现了认证服务和网关服务,基本我们的基础服务已经完成了,接下来我们才需要做服务的数据迁移.这里我们需要使用EF的CodeFirst模式.通过DotnetCli的命令去操作: dotnet ef ... 
- ABP微服务系列学习-搭建自己的微服务结构(三)
		上一篇我们基础服务初步搭建完毕,接下来我们整一下认证和网关. 搭建认证服务 认证服务的话,ABP CLI生成的所有模板都包括了一个AuthServer.我们直接生成模板然后微调一下就可以直接用了. a ... 
- ABP微服务系列学习-使用Tye启动微服务
		Tye是微软开源的一款开发人员工具, 能够用于简化微服务以及分布式应用程序的开发.测试以及部署过程.Tye 的首要目标是简化微服务的开发,具体方式包括仅用一行命令执行多项服务.在容器中使用依赖项目,以 ... 
- springcloud微服务架构搭建
		SpringCloud微服务框架搭建 一.微服务架构 1.1什么是分布式 不同模块部署在不同服务器上 作用:分布式解决网站高并发带来问题 1.2什么是集群 多台服务器部署相同应用构成一个集群 作用:通 ... 
- SprngCloud微服务框架搭建(一)
		参照来源 :https://blog.csdn.net/forezp/article/details/70148833 1.简介 目前来说,SpringCloud是比较完整的微服务解决方案框架.不像其 ... 
- Spring Cloud 微服务中搭建 OAuth2.0 认证授权服务
		在使用 Spring Cloud 体系来构建微服务的过程中,用户请求是通过网关(ZUUL 或 Spring APIGateway)以 HTTP 协议来传输信息,API 网关将自己注册为 Eureka ... 
- 【spring colud】spring cloud微服务项目搭建【spring boot2.0】
		spring cloud微服务项目搭建 =================================== 示例版本: 1.spring boot 2.0版本 2.开发工具 IntellJ IDE ... 
- 简单Spring  Cloud 微服务框架搭建
		微服务是现在比较流行的技术,对于程序猿而言,了解并搭建一个基本的微服务框架是很有必要滴. 微服务包含的内容非常多,一般小伙伴们可以根据自己的需求不断添加各种组件.框架. 一般情况下,基本的微服务框架包 ... 
- java架构之路-(微服务专题)初步认识微服务与nacos初步搭建
		历史演变: 以前我们都是一个war包,包含了很多很多的代码,反正我开始工作的时候做的就是这样的项目,一个金融系统,代码具体多少行记不清楚了,内部功能超多,但是实际能用到的不多,代码冗余超大,每次部署大 ... 
随机推荐
- Chrome扩展开发系列开篇
			大家好,我是 dom 哥.这是我关于 Chrome 扩展开发的系列文章,感兴趣的可以 点个小星星. 浏览器现状 研究机构 Statcounter 发布了 2023 年 9 月报告,揭示了有关浏览器的最 ... 
- 组合式api的使用方式
			方式一:通过setup选项 <script> export default { setup(){ // 提供数据 // 提供函数 // 提供计算属性等等..... } } </scr ... 
- Selenium-无头模式headless
			无头模式适合的场景: 部署到没有gui界面的服务器,比如linux 开发环境测试完全没问题后可以使用无头模式,提高selenium速度. # 使用headless无界面浏览器模式 chrome_opt ... 
- Sql整理
			1:数据库 数据库是以某种有组织的方式存储的数据集合. 保存有组织数据的容器,通常是一个文件或者一组文件. SQL 是Structured Query Language (结构化查询语言)的缩写. 2 ... 
- 在macOS中搭建.NET MAUI开发环境
			@ 目录 准备 安装扩展 安装 .NET 安装工作负载 安装 Xcode 命令行工具 调试安卓应用 安装 JDK 安装 Android SDK 安装 Android 模拟器 安装模拟器 安装镜像 创建 ... 
- 某物流客户Elasticsearch集群性能优化案例
			客户背景 客户使用ES来进行数据存储.快速查询业务订单记录,但是经常会出现业务高峰期ES集群的cpu负载.内存使用均较高,查询延迟大,导致前端业务访问出现大量超时的情况,极大影响其客户使用体验. 部分 ... 
- Programming Abstractions in C阅读笔记:p246-p247
			<Programming Abstractions in C>学习第68天,p246-p247总结,总计2页. 一.技术总结 本章通过"the game of nim(尼姆游戏) ... 
- 斐波那契数Fibonacci
			509. 斐波那契数 斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列.该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和.也就是: F(0) = 0, F(1) = ... 
- Python图像处理丨图像的灰度线性变换
			摘要:本文主要讲解灰度线性变换. 本文分享自华为云社区<[Python图像处理] 十五.图像的灰度线性变换>,作者:eastmount. 一.图像灰度线性变换原理 图像的灰度线性变换是通过 ... 
- 对话 BitSail Contributor | 梁奋杰:保持耐心,享受创造
			2022 年 10 月,字节跳动 BitSail 数据引擎正式开源.同期,社区推出 Contributor 激励计划第一期,目前已有 13 位外部开发者为 BitSail 社区做出贡献,成为了首批 B ... 
