ABPvNext-微服务框架基础入门

科技资讯 投稿 7500 0 评论

ABPvNext-微服务框架基础入门

ABPvNext-微服务框架基础入门


准备工作:

    1.登录ABPvNext官网 网址 http://abp.io

    • 适用于 Windows 的Visual Studio 2022 (v17.3+ /适用于 Mac 的 Visual Studio。1个
    • .NET 7.0+
    • 节点 v16 或 v18
    • Yarn v1.20+(非 v2) 2或 npm v6+(已与 Node 一起安装)
    • Redis(作为分布式缓存)。
  • 4.安装ABP CLI工具,安装方式是使用dotnet命令行工具,作为.NET开发者,这个本机一定是有的了

    //安装或者更新构建工具,如果想与我这篇文章一样,请指定安装版本号
    dotnet tool install -g Volo.Abp.Cli
    
    //登录ABP官网账号,既然使用商业版,肯定是已经有相关账号了
    abp login <username>
    
    //安装 ABP 套件(主要功能是后续创建解决方案以及代码生成工具等)
    abp suite install
    
    
    

创建项目并启动:

    1.通过ABP提供的商业套件 新建一个项目,本次使用图形化方式创建

//执行完成后会拉起一个控制台,和网页,不要关闭控制台
abp suite
  • 2.在打开的页面中,点击 Create a New solution(创建一个新的解决方案)

  • <# Check development certificates #>
    <# 本地的证书,由于项目分层,之间调用都是使用https方式,所以本地调试必须有证书 #>
    if (! (  Test-Path ".\etc\dev-cert\localhost.pfx" -PathType Leaf  {
       Write-Information "Creating dev certificates..."
       cd ".\etc\dev-cert"
       .\create-certificate.ps1
       cd ../..  
    }
    
    <# 这个是官方提供的运行环境,这个建议大家单独安装,否则开发机器会很卡,所以直接删除掉 #>
    
    $requiredServices = @(
      'sql-server-db',
      'grafana',
      'prometheus',
      'kibana',
      'rabbitmq',
      'elasticsearch',
      'redis'
    
      
    foreach ($requiredService in $requiredServices {  
    
        $nameParam = -join("name=", $requiredService
      $serviceRunningStatus = docker ps --filter $nameParam
      $isDockerImageUp = $serviceRunningStatus -split " " -contains $requiredService
      
      if( $isDockerImageUp 
      {
        Write-Host ($requiredService + " [up]"
      }
      else
      {
          cd "./etc/docker/"
        docker network create abpmicservie-network
        docker-compose -f docker-compose.infrastructure.yml -f docker-compose.infrastructure.override.yml up -d
        cd ../..
        break;
      }
    }
    <# 这个后面会手动执行,所以也删除掉 #>
    cd "./shared/ABPMicServie.DbMigrator"
    dotnet run
    cd ../..
     
    
    <# Run all services #>
    
    tye run --watch
    
  • 我们将文件重命名为 运行.ps1,内容如下

    if (! ( Test-Path ".\etc\dev-cert\localhost.pfx" -PathType Leaf {

    cd ".\etc\dev-cert"

    cd ../..

    <# Run all services #>

  • 7.先不要运行项目,在项目的根目录打开PowerShell,运行dotnet build命令,整体还原项目包

    • ABPMicServie.AuthServer
    • ABPMicServie.AdministrationService.HttpApi.Host
    • ABPMicServie.IdentityService.HttpApi.Host
    • ABPMicServie.ProductService.HttpApi.Host
    • ABPMicServie.SaasService.HttpApi.Host
  • 12.接下来要修改各个项目的中间件配置,包括 RabbitMQ,Redis,ElasticSearch等,由于修改基本涵盖了,apps,gateways,services三个目录下的所有项目,就不一一列举了

  • dotnet tool install -g Microsoft.Tye --version "0.11.0-alpha.22111.1"
    
  • 14.安装完成后,直接在项目目录的运行.ps1文件单击右键→使用powershell运行,出现下面窗口

  • 向现有解决方案中新增一个微服务

      //OrderService 是你新创建服务的名称,建议试用Serviec结尾,保证和其他服务的统一性
      abp new OrderService -t microservice-service-pro
      
      
      
    • 2.执行完成后,services文件夹下会多出一个order目录,这就是新增的微服务项目

    • 4.接下来的操作就是将新建的服务新增到解决方案中,可以使用vs界面操作,我这里就介绍另外一种添加的方法,使用dotnet命令新增一个项目

      dotnet sln add services/order/src/ABPMicServie.OrderService.HttpApi.Host/ABPMicServie.OrderService.HttpApi.Host.csproj --solution-folder services
      
      
    • 接下来配置新增微服务的授权,新增授权主要是更改OpenIddictDataSeeder.cs文件,项目中有两个地方需要修改,分别是:

        Acme.BookStore.DbMigrator\OpenIddictDataSeeder.cs
    • Acme.BookStore.IdentityService.HttpApi.Host\DbMigrations\OpenIddictDataSeeder.cs.
    • await CreateScopesAsync("OrderService";
      
    • "SwaggerClientId": "WebGateway_Swagger"

    • ProductService添加

    • ProductService,添加OrderService,这是为了增加swagger的客户端

    • CreateSwaggerClientAsync方法中需要修改两处,一个是通过配置文件获取到的RootUrl,然后把这个RootUrl拼接完整后,添加到Swagger Client客户端中,这个是授权服务中需要使用的

      • ABPMicServie.OrderService.HttpApi.Host,
      • ABPMicServie.DbMigrator
    • 在ABPMicServie.DbMigrator项目中添加OrderService的引用

      <ProjectReference Include="..\..\services\order\src\ABPMicServie.OrderService.Application.Contracts\ABPMicServie.OrderService.Application.Contracts.csproj"/>
         
      <ProjectReference Include="..\..\services\order\src\ABPMicServie.OrderService.EntityFrameworkCore\ABPMicServie.OrderService.EntityFrameworkCore.csproj"/>
      
      
    • 添加完成后的项目引用应该是这样的

    • typeof(OrderServiceApplicationContractsModule,
      typeof(OrderServiceEntityFrameworkCoreModule
      
      
    • 为了在web项目中使用orderService微服务中的页面,我们需要讲引用添加到web项目中

          <ProjectReference Include="..\..\..\..\services\order\src\ABPMicServie.OrderService.HttpApi.Client\ABPMicServie.OrderService.HttpApi.Client.csproj" />
          <ProjectReference Include="..\..\..\..\services\order\src\ABPMicServie.OrderService.Web\ABPMicServie.OrderService.Web.csproj" />
      
    • 然后在ABPMicServie.Web/ABPMicServieWebModule.cs中添加依赖

      typeof(OrderServiceWebModule,
      typeof(OrderServiceHttpApiClientModule
      
      
    • 在ABPMicServieWebModule.cs的AddAbpOpenIdConnect方法中,添加OpenID 连接范围

    • - name: order-service
        project: services/order/src/Acme.BookStore.OrderService.HttpApi.Host/Acme.BookStore.OrderService.HttpApi.Host.csproj
        bindings:
          - protocol: https
            port: 45247 ***** do not forget to replace this port with yours *****
        env:
          - Kestrel__Certificates__Default__Path=../../../../etc/dev-cert/localhost.pfx
          - Kestrel__Certificates__Default__Password=e8202f07-66e5-4619-be07-72ba76fde97f
      
      
    • 接下来添加新服务的迁移配置,更改ABPMicServie.DbMigrator/ABPMicServieDbMigrationService.cs,添加

    • 重新使用运行.ps1运行所有项目后,如果登录报错,大概率是犹豫新增加的种子数据没有初始化到表中,可以使用各种方式修改,我这里使用简单粗暴的方式,删除所有

    • 微服务间的通信(同步-HTTP代理模式)

      ProductService的相关函数

        OrderService.Application.Contracts中添加ProductService.Application.Contracts

      <ProjectReference Include="..\..\..\product\src\ABPMicServie.ProductService.Application.Contracts\ABPMicServie.ProductService.Application.Contracts.csproj" />
      
      
    • 2.在OrderServiceApplicationContractsModule.cs中添加依赖

    • .ProductService.HttpApi.Client

    • typeof(ProductServiceHttpApiClientModule
      
      
    • 接下来做一个测试,在默认项目中的新增这段代码

      using System.Threading.Tasks;
      using ABPMicServie.ProductService.Products;//引用命名空间
      using Microsoft.AspNetCore.Authorization;
      
      namespace ABPMicServie.OrderService.Samples;
      
      public class SampleAppService : OrderServiceAppService, ISampleAppService
      {
          IProductAppService _productAppService;//构造函数注入
      
          public SampleAppService(IProductAppService productAppService
          {
              _productAppService = productAppService;
          }
          public async Task<SampleDto> GetAsync(
          {
          //调用另外一个服务的方法
            await _productAppService.CreateAsync(new ProductCreateDto( { Name="zyzy", Price=100 };
              return
                  new SampleDto
                  {
                      Value = 43
                  };
              
          }
      
          [Authorize]
          public Task<SampleDto> GetAuthorizedAsync(
          {
              return Task.FromResult(
                  new SampleDto
                  {
                      Value = 42
                  }
              ;
          }
      }
      
    • 微服务间的通信(异步-RabbitMQ模式)

        1 .在ABPMicServie.OrderService.HttpApi.Host/appsettings.json中更改RabbitMQ的链接字符配置,更改成你要使用的服务

      编程笔记 » ABPvNext-微服务框架基础入门

      赞同 (41) or 分享 (0)
      游客 发表我的评论   换个身份
      取消评论

      表情
      (0)个小伙伴在吐槽