日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[Abp vNext微服务实践] - 搭建租户管理服务

發(fā)布時間:2023/12/4 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Abp vNext微服务实践] - 搭建租户管理服务 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、簡介

ABP模板項目中已經(jīng)提供了租戶登錄和管理功能,但是模板項目是單體應(yīng)用結(jié)構(gòu),無法單獨部署租戶服務(wù),所以難以滿足微服務(wù)的需求。本篇文章將會介紹搭建ABP租戶管理服務(wù),并單獨部署應(yīng)用。

二、創(chuàng)建工程

2.1 創(chuàng)建TenantService.Host服務(wù)

微服務(wù)的開發(fā)應(yīng)該是模塊的,所以TenantService.Host服務(wù)名沒有放在在ABP微服務(wù)解決方案中,選擇獨立搭建、獨立部署的方式。

三、引用模塊

在大型團(tuán)隊中或者小型精英團(tuán)隊中建議拉取將ABP源碼發(fā)布本地Nuget包,所有服務(wù)引用本地包進(jìn)行開發(fā)。因為無論ABP如何通用也是無法滿足企業(yè)級后臺業(yè)務(wù)的發(fā)展和版本迭代的,有老司機(jī)的團(tuán)隊?wèi)?yīng)該要掌握修改ABP底層應(yīng)用甚至底層框架的能力。

3.1 引用ids4、.net core、ABP等必須組件

  • Serilog.Extensions.Hosting Version="3.0.0"

  • Serilog.Sinks.File Version="4.0.0"

  • Serilog.Sinks.Elasticsearch Version="6.5.0"

  • Swashbuckle.AspNetCore Version="5.0.0-rc4"

  • IdentityServer4.AccessTokenValidation Version="3.0.0"

  • Microsoft.Extensions.Caching.StackExchangeRedis Version="3.1.0"

  • Microsoft.AspNetCore.DataProtection.StackExchangeRedis Version="3.1.0"

  • Volo.Abp.AspNetCore.MultiTenancy Version="2.0.1"

  • Volo.Abp.AuditLogging.EntityFrameworkCore Version="2.0.1"

  • Volo.Abp.Autofac Version="2.0.1"

  • Volo.Abp.EntityFrameworkCore.SqlServer Version="2.0.1"

  • Volo.Abp.EventBus.RabbitMQ Version="2.0.1"

  • Volo.Abp.PermissionManagement.EntityFrameworkCore Version="2.0.1"

  • Volo.Abp.SettingManagement.EntityFrameworkCore Version="2.0.1"

  • Volo.Abp.TenantManagement.Application Version="2.0.1"

  • Volo.Abp.TenantManagement.EntityFrameworkCore Version="2.0.1"

  • Volo.Abp.TenantManagement.HttpApi Version="2.0.1"

  • Volo.Abp.Identity.EntityFrameworkCore Version="2.0.1"

  • volo.abp.identityserver.entityframeworkcore Version="2.0.1"

四、配置Module

ABP不僅基于DDD設(shè)計,更是基于模塊化實現(xiàn)的。

4.1 添加TenantServiceHostModule

在TenantService.Host根目錄中添加TenantServiceHostModule類

4.2 引用依賴

租戶服務(wù)關(guān)系的ABP應(yīng)用模塊比較多,所以依賴也比其他服務(wù)多一些。ABP模塊引用會在服務(wù)模塊啟動時載入IOC容器,這樣的好處是無需多次引用,只要在Mudule中引用一次就可以。
引用的依賴順序如下:
AbpAutofacModule
AbpEventBusRabbitMqModule
AbpAspNetCoreMultiTenancyModule
AbpEntityFrameworkCoreSqlServerModule
AbpAuditLoggingEntityFrameworkCoreModule
AbpPermissionManagementEntityFrameworkCoreModule
AbpSettingManagementEntityFrameworkCoreModule
AbpTenantManagementHttpApiModule
AbpTenantManagementEntityFrameworkCoreModule
AbpTenantManagementApplicationModule
AbpIdentityEntityFrameworkCoreModule
AbpIdentityServerEntityFrameworkCoreModule

4.3 注冊服務(wù)

與其他服務(wù)一樣需要注冊認(rèn)證、Swagger、默認(rèn)語言、數(shù)據(jù)庫服務(wù)、redis等,這里就不過多贅述了,ABP文檔有詳細(xì)的解釋。

4.4 配置初始化模塊

這里也跟其他服務(wù)一樣,沒有特殊的模塊需要初始化。

4.5 TenantServiceHostModule完整代碼如下:

Copyusing Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.DataProtection; using Microsoft.Extensions.DependencyInjection; using Microsoft.OpenApi.Models; using StackExchange.Redis; using System.Collections.Generic; using System.Linq; using System.Security.Claims; using Volo.Abp; using Volo.Abp.AspNetCore.MultiTenancy; using Volo.Abp.Auditing; using Volo.Abp.AuditLogging.EntityFrameworkCore; using Volo.Abp.Autofac; using Volo.Abp.Data; using Volo.Abp.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore.SqlServer; using Volo.Abp.EventBus.RabbitMq; using Volo.Abp.Identity; using Volo.Abp.Identity.EntityFrameworkCore; using Volo.Abp.IdentityServer.EntityFrameworkCore; using Volo.Abp.Localization; using Volo.Abp.Modularity; using Volo.Abp.PermissionManagement.EntityFrameworkCore; using Volo.Abp.Security.Claims; using Volo.Abp.SettingManagement.EntityFrameworkCore; using Volo.Abp.TenantManagement; using Volo.Abp.TenantManagement.EntityFrameworkCore; using Volo.Abp.Threading;namespace TenantService.Host {[DependsOn(typeof(AbpAutofacModule),typeof(AbpEventBusRabbitMqModule),typeof(AbpAspNetCoreMultiTenancyModule),typeof(AbpEntityFrameworkCoreSqlServerModule),typeof(AbpAuditLoggingEntityFrameworkCoreModule),typeof(AbpPermissionManagementEntityFrameworkCoreModule),typeof(AbpSettingManagementEntityFrameworkCoreModule),typeof(AbpTenantManagementHttpApiModule),typeof(AbpTenantManagementEntityFrameworkCoreModule),typeof(AbpTenantManagementApplicationModule),typeof(AbpIdentityEntityFrameworkCoreModule),typeof(AbpIdentityServerEntityFrameworkCoreModule))]public class TenantServiceHostModule: AbpModule{public override void ConfigureServices(ServiceConfigurationContext context){var configuration = context.Services.GetConfiguration();context.Services.AddAuthentication("Bearer").AddIdentityServerAuthentication(options =>{options.Authority = configuration["AuthServer:Authority"];options.ApiName = configuration["AuthServer:ApiName"];options.RequireHttpsMetadata = false;});context.Services.AddSwaggerGen(options =>{options.SwaggerDoc("v1", new OpenApiInfo { Title = "Tenant Service API", Version = "v1" });options.DocInclusionPredicate((docName, description) => true);options.CustomSchemaIds(type => type.FullName);});Configure<AbpLocalizationOptions>(options =>{options.Languages.Add(new LanguageInfo("en", "en", "English"));});Configure<AbpDbContextOptions>(options =>{options.UseSqlServer();});context.Services.AddStackExchangeRedisCache(options =>{options.Configuration = configuration["Redis:Configuration"];});Configure<AbpAuditingOptions>(options =>{options.IsEnabledForGetRequests = true;options.ApplicationName = "TenantService";});var redis = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]);context.Services.AddDataProtection().PersistKeysToStackExchangeRedis(redis, "DataProtection-Keys");}public override void OnApplicationInitialization(ApplicationInitializationContext context){var app = context.GetApplicationBuilder();app.UseCorrelationId();app.UseVirtualFiles();app.UseRouting();app.UseAuthentication();app.Use(async (ctx, next) =>{var currentPrincipalAccessor = ctx.RequestServices.GetRequiredService<ICurrentPrincipalAccessor>();var map = new Dictionary<string, string>(){{ "sub", AbpClaimTypes.UserId },{ "role", AbpClaimTypes.Role },{ "email", AbpClaimTypes.Email },//any other map};var mapClaims = currentPrincipalAccessor.Principal.Claims.Where(p => map.Keys.Contains(p.Type)).ToList();currentPrincipalAccessor.Principal.AddIdentity(new ClaimsIdentity(mapClaims.Select(p => new Claim(map[p.Type], p.Value, p.ValueType, p.Issuer))));await next();});app.UseAbpRequestLocalization(); //TODO: localization?app.UseSwagger();app.UseSwaggerUI(options =>{options.SwaggerEndpoint("/swagger/v1/swagger.json", "Tenant Service API");});app.UseAuditing();app.UseMvcWithDefaultRouteAndArea();//TODO: Problem on a clustered environmentAsyncHelper.RunSync(async () =>{using (var scope = context.ServiceProvider.CreateScope()){await scope.ServiceProvider.GetRequiredService<IDataSeeder>().SeedAsync();}});}} }

五、管理租戶

5.1 啟動TenantService.Host服務(wù)

Ctrl+F5啟動即可,啟動成功后可以看到Swagger跳轉(zhuǎn)。

此時ABP已經(jīng)將租戶的權(quán)限種子數(shù)據(jù)添加到數(shù)據(jù)庫。

5.2 添加租戶

使用POST請求添加Default租戶

請求成功并返回租戶信息:

此時ABP已經(jīng)自動添加Dfault租戶的admin用戶、角色和權(quán)限。

5.3 使用租戶登錄

在AuthServer中使用Default獲取token

可以看出租戶已經(jīng)啟用并成功登錄,而且token中已經(jīng)包含tenant

六、總結(jié)

ABP社區(qū)版中的租戶功能有點弱,應(yīng)付中小型項目是綽綽有余的,對于大型SaaS系統(tǒng)筆者還是建議購買商業(yè)版以獲得更大的支持。ABP的租戶還有更多的配置,如租戶獨立數(shù)據(jù)庫等,在搭建成功租戶服務(wù)后可以一步步嘗試更多的ABP特性。

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的[Abp vNext微服务实践] - 搭建租户管理服务的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。