ABP VNext 微服务搭建入门(4)-- 微服务

微服务

微服务是独立的HTTP API,依赖各自的模块,它们以分布式方式实现系统业务

该项目中有4个微服务,分别是 IdentityService, BloggingService,ProductServiceTenantManagementService
详情查看abp官方文档
下面以 Identity Service 举例,其他服务同理。

Identity Service (IdentityService.Host)

功能

此服务提供用户和角色管理API.

数据库

与AuthServer应用程序共享相同的数据库.

模块

该服务实际上只托管ABP身份包/模块. 不包含任何API本身. 为了托管它,添加以下依赖项:

  • AbpIdentityHttpApiModule (Volo.Abp.Identity.HttpApi 包) 提供身份API.
  • AbpIdentityApplicationModule (Volo.Abp.Identity.Application 包)承载模块的应用程序和域层的实现.
  • AbpIdentityEntityFrameworkCoreModule (Volo.Abp.Identity.EntityFrameworkCore 包) 使用EF Core作为数据库API.

微服务的调用

微服务的调用,可以利用ABP的动态api功能,只需引用对应模块的Application.Contracts,配置对应的服务地址即可。

服务端代理

[DependsOn(
        typeof(ProductManagementApplicationContractsModule),
        typeof(AbpHttpClientModule))]
    public class ProductManagementHttpApiClientModule : AbpModule
    {
        public const string RemoteServiceName = "ProductManagement";

        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            context.Services.AddHttpClientProxies(
                typeof(ProductManagementApplicationContractsModule).Assembly,
                RemoteServiceName
            );
        }
    }

客户端配置

{
  "RemoteServices": {
    "Default": {
      "BaseUrl": "http://localhost:53929/"
    },
    "ProductManagement": {
      "BaseUrl": "http://localhost:48392/"
    } 
  } 
}

客户端调用

跟本地调用一致

 public class ClientDemoService : ITransientDependency
    {
        private readonly IProductAppService _productAppService;

        public ClientDemoService(
            IProductAppService productAppService)
        {
            _productAppService = productAppService;
        }

        public async Task RunAsync()
        {
            await TestProductService();
        }

        private async Task TestProductService()
        {
            Console.WriteLine();
            Console.WriteLine("*** TestProductService ************************************");

            try
            {
                var output = await _productAppService.GetListAsync();

                Console.WriteLine("Total product count: " + output.Items.Count);

                foreach (var product in output.Items)
                {
                    Console.WriteLine($"- Code={product.Code}, Name={product.Name}, Price={product.Price}, StockCount={product.StockCount}");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }
原文地址:https://www.cnblogs.com/zk-ljc/p/14142544.html