Volo.Abp升级小记(二)创建全新微服务模块

news/2024/5/19 20:57:27 标签: 微服务, 架构, asp.net, .netcore

文章目录

  • 创建模块
    • 领域层
    • 应用层
    • 数据库和仓储
    • 控制器
    • 配置微服务
  • 测试微服务
  • 微服务注册
    • 添加资源配置
    • 配置网关
  • 运行项目

假设有一个按照 官方sample搭建的微服务项目,并安装好了abp-cli。
需要创建一个名为GDMK.CAH.Common的模块,并在模块中创建标签管理功能

因为大部分的代码是自动生成的,此示例仅描述需要手动更改的内容。我们以创建一个全新的最小化的微服务模块为例,需要做以下几步:

创建模块

用abp-cli创建一个新的模块
在项目根目录下执行以下命令:

abp new GDMK.CAH.Common --template module --no-ui --ui='none' -d='ef' --output-folder .\modules\GDMK.CAH.Common --local-framework-ref --abp-path ..\..\..\..\abp\

–template module: 模块/服务模板
–no-ui: 不创建前端项目
-d: 指定数据库提供程序为ef.
–output-folder: 指定输出目录
–local-framework-ref: 使用本地abp框架

你可以自由设定模板选项,详情请参考abp-cli文档

我们只需要保留应用层,领域层,数据库,Api访问层以及各抽象层。将这些项目放在模块目录下,即当前位置(.\modules\GDMK.CAH.Common)

Common.Host项目作为微服务单独放置在服务目录中(一般为microservices)

看起来模块的目录结构如下

在这里插入图片描述

领域层

领域层中我们创建一个Tag实体、领域层服务和模型

Tag实体

public class Tag : AuditedEntity<long>, IMultiTenant
{
    ...
}

Tag领域层服务

public class TagManager<T> : DomainService
{
    ...
}

页面结构看起来像这样

在这里插入图片描述

应用层

在应用层抽象层中创建ITagAppService接口,以及各Dto类。

ITagAppService接口继承ICurdAppService,实现标签管理的增删改查功能

public interface ITagAppService : ICurdAppService<TagDto, TagDto, long, GetAllTagInput, GetAllTagInput, CreateTagInput, CreateTagInput>, IApplicationService
{
    ...
}

页面结构看起来像这样

在这里插入图片描述

应用层中创建标签管理的应用层服务TagAppService

TagAppService继承CurdAppServiceBase,实现ITagAppService接口

public class TagAppService : CurdAppServiceBase<Tag, TagDto, TagDto, long, GetAllTagInput, GetAllTagInput, CreateTagInput, CreateTagInput>, ITagAppService
{
    ...
}

在这里插入图片描述

配置AutoMapper

在CommonApplicationAutoMapperProfile中添加Tag的映射配置

public class CommonApplicationAutoMapperProfile : Profile
{
    public CommonApplicationAutoMapperProfile()
    {
        /* You can configure your AutoMapper mapping configuration here.
         * Alternatively, you can split your mapping configurations
         * into multiple profile classes for a better organization. */
        CreateMap<Tag.Tag, TagDto>();

        CreateMap<TagDto, Tag.Tag>().Ignore(c => c.TenantId);
        CreateMap<CreateTagInput, Tag.Tag>().IgnoreAuditedObjectProperties()
                .Ignore(c => c.TenantId);

    }
}

数据库和仓储

在CommonDbContext中添加Tag的DbSet

public class CommonDbContext : AbpDbContext<CommonDbContext>, ICommonDbContext
{
    /* Add DbSet for each Aggregate Root here. Example:
     * public DbSet<Question> Questions { get; set; }
     */
    public DbSet<Tag.Tag> Tag { get; set; }

    ...
}

在CommonDbContextModelCreatingExtensions中添加对Tag实体的配置

ConfigureByConvention会根据DataAnnotationAttributes为实体配置一些默认的属性,如Id为主键,Name为索引等,详情请参考Abp文档和EF文档

public static class CommonDbContextModelCreatingExtensions
{
    public static void ConfigureCommon(
        this ModelBuilder builder)
    {
        Check.NotNull(builder, nameof(builder));

        builder.Entity<Tag.Tag>(b =>
        {
            b.ToTable(CommonDbProperties.DbTablePrefix + nameof(Tag.Tag), CommonDbProperties.DbSchema);

            b.ConfigureByConvention();

        });

        ...
    }
}

在CommonEntityFrameworkCoreModule的ConfigureServices方法中,为Tag添加默认仓储

public class CommonEntityFrameworkCoreModule : AbpModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        context.Services.AddAbpDbContext<CommonDbContext>(options =>
        {
            /* Add custom repositories here. Example:
             * options.AddRepository<Question, EfCoreQuestionRepository>();
             */
            options.AddDefaultRepositories(includeAllEntities: true);

        });


    }
}

控制器

添加控制器,配置路由

在这里插入图片描述

[Area(CommonRemoteServiceConsts.ModuleName)]
[RemoteService(Name = CommonRemoteServiceConsts.RemoteServiceName)]
[Route("api/Common/tag")]
public class TagController : CommonController<ITagAppService, TagDto, TagDto, long, GetAllTagInput, GetAllTagInput, CreateTagInput, CreateTagInput>, ITagAppService
{
    private readonly ITagAppService _tagAppService;

    public TagController(ITagAppService tagAppService) : base(tagAppService)
    {
        _tagAppService = tagAppService;
    }


}

配置微服务

在服务目录中打开Common.Host项目,将CommonHttpApi,CommonApplication以及CommonEntityFrameworkCore模块添加到项目引用,并建立Abp模块的依赖关系

    [DependsOn(
        typeof(AbpAutofacModule),
        typeof(AbpAspNetCoreMvcModule),
        typeof(AbpEntityFrameworkCoreSqlServerModule),
   
        typeof(CommonHttpApiModule),
        typeof(CommonApplicationModule),
        typeof(CommonEntityFrameworkCoreModule),
        ...
        )]
    public class CommonServiceHostModule : AbpModule

在launchSettings.json中指定端口号,此端口号不要跟其他服务的端口号冲突

"profiles": {  
    "CommonService.Host": {
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "http://localhost:44363",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
    ...
  }

准备一些种子数据

public static List<Tag> tags = new List<Tag>()
{
    new Tag() { Title = "医术高明" },
    new Tag() { Title = "救死扶伤" },
    new Tag() { Title = "心地仁慈" },
    new Tag() { Title = "百治百效" },
    new Tag() { Title = "白衣天使" },
    new Tag() { Title = "手到病除" },
    new Tag() { Title = "妙手回春" },
};

在CommonServiceDataSeeder创建种子数据

public class CommonServiceDataSeeder : IDataSeedContributor, ITransientDependency
{
    private readonly IRepository<Tag.Tag, long> _tagRepository;

    public CommonServiceDataSeeder(
        IRepository<Tag.Tag, long> tagRepository)
    {
        _tagRepository = tagRepository;

    }

    [UnitOfWork]
    public virtual async Task SeedAsync(DataSeedContext context)
    {
        await _tagRepository.InsertManyAsync(StaticMember.tags);
    }

}

创建迁移

将Common.Host设置为启动项目,打开程序包管理器控制台选择Common.Host默认项目。
执行Add-Migration init命令和Update-Database命令

在这里插入图片描述

测试微服务

启动Common.Host,打开浏览器,输入http://localhost:44363/swagger/index.html

在这里插入图片描述

微服务注册

添加资源配置

AuthServerDataSeeder中添加identityServer4资源配置

添加Scopes

private async Task CreateApiScopesAsync()
{  
    ...
    await CreateApiScopeAsync("CommonService");
}

添加Resource

private async Task CreateApiResourcesAsync()
{
    ...
    await CreateApiResourceAsync("CommonService", commonApiUserClaims);
    
}

添加Client

private async Task CreateClientsAsync()
{
    ...
    await CreateClientAsync(
            "common-service-client",
            commonScopes.Union(new[] { "InternalGateway", "IdentityService" }),
            new[] { "client_credentials" },
            commonSecret
        );
}

配置网关

在内外网关的appsettings.json中添加Ocelot对微服务的路由转发

 {
      "DownstreamPathTemplate": "/api/common/{everything}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 44363
        }
      ],
      "UpstreamPathTemplate": "/api/common/{everything}",
      "UpstreamHttpMethod": [ "Put", "Delete", "Get", "Post" ]
    }

网关中添加对CommonHttpApi项目的引用,并配置Abp模块依赖

namespace BackendAdminAppGateway.Host
{
    [DependsOn(
        ...
        typeof(CommonHttpApiModule)
    )]
    public class BackendAdminAppGatewayHostModule : AbpModule

    ...

选择启动项目,将Common.Host微服务设置为启动

在这里插入图片描述

到此完成了新模块的配置工作

运行项目

可以通过网关访问Tag接口了

在这里插入图片描述


http://www.niftyadmin.cn/n/414992.html

相关文章

国产文档控件Spire.Doc for.NET ,增强从 Word 到 PDF 和 HTML 的转换

Spire.Doc pack &#xff08;hotfix&#xff09; 11.5.12 增强了从 Word 到 PDF 和 HTML 的转换 很高兴地宣布发布 Spire.Doc 11.5.12。此版本增强了从 Word 到 PDF 和 HTML 的转换。此外&#xff0c;此版本还修复了一些已知问题&#xff0c;例如应用程序在加载文档时抛出“Sy…

SQLite数据库--事务创建,提交及异常回滚

这段代码是在使用 SQLite 数据库时开启一个事务。事务是一组数据库操作&#xff0c;它们被视为一个单独的工作单元&#xff0c;要么全部成功执行&#xff0c;要么全部回滚。在事务中&#xff0c;可以执行多个 SQL 语句&#xff0c;这些语句可以是插入、更新或删除数据的操作。如…

Python+appium自动化测试-调用服务器接口

当前很多APP登录都需要绑定手机号&#xff0c;但当我们需要足够多的模拟新用户的注册登录时&#xff0c;无法提供大量的手机号来测试&#xff0c;所以可以让服务器给出一个清除账号的接口&#xff0c;在写自动化脚本的时候可以调用这个接口&#xff0c;保证能够使用一个账号进行…

【CANoe】CAPL如何检测周期性报文

文章目录 一、实例1、新建工程2、增加两个 Module3、选择在线模式(Online Mode)仿真总线(Simulated Bus)4、配置CAPL Test Moduletest.can代码如下&#xff1a; 5、配置CAN IG(发送周期报文0x101)6、测试结果 二、核心代码三、CAPL函数详解截图参考&#xff1a; 一、实例 以下…

既然jmeter也能做接口自动化,为什么还需要pytest自己搭框架?

今天这篇文章呢&#xff0c;我会从以下几个方面来介绍&#xff1a; 1、首先介绍一下pytest框架 2、带大家安装Pytest框架 3、使用pytest框架时需要注意的点 4、pytest的运行方式 5、pytest框架中常用的插件 一、pytest框架介绍 pytest 是 python 的第三方单元测试框架&a…

蓝桥杯2018年第九届真题-版本分支

题目描述 小明负责维护公司一个奇怪的项目。这个项目的代码一直在不断分支(branch)但是从未发生过合并(merge)。 现在这个项目的代码一共有N个版本&#xff0c;编号1~N&#xff0c;其中1号版本是最初的版本。 除了1号版本之外&#xff0c;其他版本的代码都恰好有一个直接的父版…

node 常用命令

1、设置npm路径 #全局安装路径 npm config set prefix "D:\Program Files\nodejs\node_global" #缓存路径 npm config set cache "D:\Program Files\nodejs\node_cache" 2、设置镜像 #1,淘宝镜像源 npm config set registry https://registry.npmmirror…

stable diffusion其他微调方法

textual inversion 发布时间&#xff1a;2022 目标&#xff1a;与DreamBooth一样&#xff0c;都是想要微调模型生成特定目标或风格的图像 方法&#xff1a;通过在vocabulary中添加一个额外的embedding来学习输入的新特征。预训练模型的所有参数都锁住&#xff0c;只有新的emb…