diff --git a/services/basket/src/EShopOnAbp.BasketService.Application/BasketProductService.cs b/services/basket/src/EShopOnAbp.BasketService.Application/BasketProductService.cs index 176f039b..d764b479 100644 --- a/services/basket/src/EShopOnAbp.BasketService.Application/BasketProductService.cs +++ b/services/basket/src/EShopOnAbp.BasketService.Application/BasketProductService.cs @@ -1,7 +1,10 @@ using System; using System.Threading.Tasks; +using EShopOnAbp.CatalogService.Grpc; using EShopOnAbp.CatalogService.Products; +using Microsoft.Extensions.Logging; using Volo.Abp; +using Volo.Abp.ObjectMapping; using Volo.Abp.Caching; using Volo.Abp.DependencyInjection; @@ -9,17 +12,23 @@ namespace EShopOnAbp.BasketService; public class BasketProductService : IBasketProductService, ITransientDependency { - private readonly IPublicProductAppService _productAppService; private readonly IDistributedCache _cache; + private readonly ILogger _logger; + private readonly IObjectMapper _mapper; + private readonly ProductPublic.ProductPublicClient _productPublicGrpcClient; public BasketProductService( - IPublicProductAppService productAppService, - IDistributedCache cache) + IDistributedCache cache, + ILogger logger, + IObjectMapper mapper, + ProductPublic.ProductPublicClient productPublicGrpcClient) { - _productAppService = productAppService; _cache = cache; + _logger = logger; + _mapper = mapper; + _productPublicGrpcClient = productPublicGrpcClient; } - + public async Task GetAsync(Guid productId) { return await _cache.GetOrAddAsync( @@ -28,9 +37,13 @@ public class BasketProductService : IBasketProductService, ITransientDependency ); } - private Task GetProductAsync(Guid productId) + private async Task GetProductAsync(Guid productId) { - return _productAppService.GetAsync(productId) ?? + var request = new ProductRequest { Id = productId.ToString() }; + _logger.LogInformation("=== GRPC request {@request}", request); + var response = await _productPublicGrpcClient.GetByIdAsync(request); + _logger.LogInformation("=== GRPC response {@response}", response); + return _mapper.Map(response) ?? throw new UserFriendlyException(BasketServiceDomainErrorCodes.ProductNotFound); } } \ No newline at end of file diff --git a/services/basket/src/EShopOnAbp.BasketService.Application/BasketServiceApplicationAutoMapperProfile.cs b/services/basket/src/EShopOnAbp.BasketService.Application/BasketServiceApplicationAutoMapperProfile.cs index 7d781826..781a2073 100644 --- a/services/basket/src/EShopOnAbp.BasketService.Application/BasketServiceApplicationAutoMapperProfile.cs +++ b/services/basket/src/EShopOnAbp.BasketService.Application/BasketServiceApplicationAutoMapperProfile.cs @@ -1,4 +1,5 @@ using AutoMapper; +using EShopOnAbp.CatalogService.Grpc; using EShopOnAbp.CatalogService.Products; namespace EShopOnAbp.BasketService @@ -8,6 +9,7 @@ namespace EShopOnAbp.BasketService public BasketServiceApplicationAutoMapperProfile() { CreateMap(); + CreateMap(); } } } diff --git a/services/basket/src/EShopOnAbp.BasketService.Application/BasketServiceApplicationModule.cs b/services/basket/src/EShopOnAbp.BasketService.Application/BasketServiceApplicationModule.cs index 72eda9ce..74a9e542 100644 --- a/services/basket/src/EShopOnAbp.BasketService.Application/BasketServiceApplicationModule.cs +++ b/services/basket/src/EShopOnAbp.BasketService.Application/BasketServiceApplicationModule.cs @@ -1,6 +1,12 @@ -using EShopOnAbp.CatalogService; +using System; +using System.Collections.Generic; +using EShopOnAbp.CatalogService; +using EShopOnAbp.CatalogService.Grpc; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; using Volo.Abp.Application; using Volo.Abp.AutoMapper; +using Volo.Abp.Http.Client; using Volo.Abp.Modularity; namespace EShopOnAbp.BasketService @@ -16,10 +22,20 @@ namespace EShopOnAbp.BasketService { public override void ConfigureServices(ServiceConfigurationContext context) { + var configuration = context.Services.GetConfiguration(); Configure(options => { options.AddMaps(); }); + + context.Services.AddGrpcClient((services, options) => + { + var remoteServiceOptions = services.GetRequiredService>().CurrentValue; + var catalogServiceConfiguration = remoteServiceOptions.RemoteServices.GetConfigurationOrDefault("Catalog"); + var catalogGrpcUrl = catalogServiceConfiguration.GetOrDefault("GrpcUrl"); + + options.Address = new Uri(catalogGrpcUrl); + }); } } } diff --git a/services/basket/src/EShopOnAbp.BasketService.Application/EShopOnAbp.BasketService.Application.csproj b/services/basket/src/EShopOnAbp.BasketService.Application/EShopOnAbp.BasketService.Application.csproj index abe34e89..bbcbc805 100644 --- a/services/basket/src/EShopOnAbp.BasketService.Application/EShopOnAbp.BasketService.Application.csproj +++ b/services/basket/src/EShopOnAbp.BasketService.Application/EShopOnAbp.BasketService.Application.csproj @@ -18,4 +18,14 @@ + + + + + + + + + + diff --git a/services/basket/src/EShopOnAbp.BasketService.HttpApi.Host/appsettings.json b/services/basket/src/EShopOnAbp.BasketService.HttpApi.Host/appsettings.json index 2e9949c5..93adbecc 100644 --- a/services/basket/src/EShopOnAbp.BasketService.HttpApi.Host/appsettings.json +++ b/services/basket/src/EShopOnAbp.BasketService.HttpApi.Host/appsettings.json @@ -11,7 +11,8 @@ }, "RemoteServices": { "Catalog": { - "BaseUrl": "https://localhost:44354/" + "BaseUrl": "https://localhost:44354/", + "GrpcUrl": "http://localhost:81" } }, "Logging": { diff --git a/services/catalog/src/EShopOnAbp.CatalogService.Application.Contracts/EShopOnAbp.CatalogService.Application.Contracts.csproj b/services/catalog/src/EShopOnAbp.CatalogService.Application.Contracts/EShopOnAbp.CatalogService.Application.Contracts.csproj index 12e67a4c..e1b43742 100644 --- a/services/catalog/src/EShopOnAbp.CatalogService.Application.Contracts/EShopOnAbp.CatalogService.Application.Contracts.csproj +++ b/services/catalog/src/EShopOnAbp.CatalogService.Application.Contracts/EShopOnAbp.CatalogService.Application.Contracts.csproj @@ -16,4 +16,16 @@ + + + + + + + + + + + + diff --git a/services/catalog/src/EShopOnAbp.CatalogService.Application.Contracts/Protos/product.proto b/services/catalog/src/EShopOnAbp.CatalogService.Application.Contracts/Protos/product.proto new file mode 100644 index 00000000..fbf6afd2 --- /dev/null +++ b/services/catalog/src/EShopOnAbp.CatalogService.Application.Contracts/Protos/product.proto @@ -0,0 +1,32 @@ +syntax = "proto3"; + +/* >> +import "google/api/annotations.proto"; +<< */ + +option csharp_namespace = "EShopOnAbp.CatalogService.Grpc"; + +package CatalogApi; + +service ProductPublic { + rpc GetById(ProductRequest) returns (ProductResponse) { + /* >> + option (google.api.http) = { + get: "/api/catalog/product/{id}" + }; +<< */ + } +} + +message ProductRequest { + string id = 1; +} + +message ProductResponse { + string id = 1; + string code = 2; + string name = 3; + string imageName = 4; + float price = 5; + int32 stockCount = 6; +} diff --git a/services/catalog/src/EShopOnAbp.CatalogService.Application/CatalogServiceApplicationAutoMapperProfile.cs b/services/catalog/src/EShopOnAbp.CatalogService.Application/CatalogServiceApplicationAutoMapperProfile.cs index 5bd4edaf..899297aa 100644 --- a/services/catalog/src/EShopOnAbp.CatalogService.Application/CatalogServiceApplicationAutoMapperProfile.cs +++ b/services/catalog/src/EShopOnAbp.CatalogService.Application/CatalogServiceApplicationAutoMapperProfile.cs @@ -1,4 +1,5 @@ using AutoMapper; +using EShopOnAbp.CatalogService.Grpc; using EShopOnAbp.CatalogService.Products; namespace EShopOnAbp.CatalogService @@ -8,6 +9,7 @@ namespace EShopOnAbp.CatalogService public CatalogServiceApplicationAutoMapperProfile() { CreateMap(); + CreateMap(); } } } diff --git a/services/catalog/src/EShopOnAbp.CatalogService.Application/Grpc/PublicProductGrpService.cs b/services/catalog/src/EShopOnAbp.CatalogService.Application/Grpc/PublicProductGrpService.cs new file mode 100644 index 00000000..ccd34229 --- /dev/null +++ b/services/catalog/src/EShopOnAbp.CatalogService.Application/Grpc/PublicProductGrpService.cs @@ -0,0 +1,26 @@ +using System; +using System.Threading.Tasks; +using EShopOnAbp.CatalogService.Products; +using Grpc.Core; +using Volo.Abp.Domain.Repositories; +using Volo.Abp.ObjectMapping; + +namespace EShopOnAbp.CatalogService.Grpc; + +public class PublicProductGrpService : ProductPublic.ProductPublicBase +{ + private readonly IRepository _productRepository; + private readonly IObjectMapper _objectMapper; + + public PublicProductGrpService(IRepository productRepository, IObjectMapper objectMapper) + { + _productRepository = productRepository; + _objectMapper = objectMapper; + } + + public override async Task GetById(ProductRequest request, ServerCallContext context) + { + var product = await _productRepository.GetAsync(Guid.Parse(request.Id)); + return _objectMapper.Map(product); + } +} \ No newline at end of file diff --git a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/CatalogServiceHttpApiHostModule.cs b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/CatalogServiceHttpApiHostModule.cs index 2b0a897b..ae8547e6 100644 --- a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/CatalogServiceHttpApiHostModule.cs +++ b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/CatalogServiceHttpApiHostModule.cs @@ -7,9 +7,12 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Threading.Tasks; +using EShopOnAbp.CatalogService.Grpc; using EShopOnAbp.CatalogService.MongoDB; +using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Configuration; using Volo.Abp; using Volo.Abp.AspNetCore.Mvc; @@ -80,6 +83,10 @@ public class CatalogServiceHttpApiHostModule : AbpModule }); Configure(options => { options.AutoValidate = false; }); + context.Services.AddGrpc(options => + { + options.EnableDetailedErrors = true; + }); } public override void OnApplicationInitialization(ApplicationInitializationContext context) @@ -111,7 +118,10 @@ public class CatalogServiceHttpApiHostModule : AbpModule app.UseAbpSerilogEnrichers(); app.UseAuditing(); app.UseUnitOfWork(); - app.UseConfiguredEndpoints(); + app.UseConfiguredEndpoints(endpoints => + { + endpoints.MapGrpcService(); + }); } public override async Task OnPostApplicationInitializationAsync(ApplicationInitializationContext context) diff --git a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/EShopOnAbp.CatalogService.HttpApi.Host.csproj b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/EShopOnAbp.CatalogService.HttpApi.Host.csproj index b7e561d5..6a0af54a 100644 --- a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/EShopOnAbp.CatalogService.HttpApi.Host.csproj +++ b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/EShopOnAbp.CatalogService.HttpApi.Host.csproj @@ -18,6 +18,10 @@ + + + + diff --git a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/Program.cs b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/Program.cs index 74728f1c..3ae318fc 100644 --- a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/Program.cs +++ b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/Program.cs @@ -17,8 +17,8 @@ public class Program try { Log.Information($"Starting {assemblyName}."); - var app = await ApplicationBuilderHelper - .BuildApplicationAsync(args); + + var app = await ApplicationBuilderHelper.BuildApplicationAsync(args); await app.InitializeApplicationAsync(); await app.RunAsync(); diff --git a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/appsettings.json b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/appsettings.json index 7b5799db..5d2eda53 100644 --- a/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/appsettings.json +++ b/services/catalog/src/EShopOnAbp.CatalogService.HttpApi.Host/appsettings.json @@ -40,5 +40,17 @@ }, "ElasticSearch": { "Url": "http://localhost:9200" + }, + "Kestrel": { + "Endpoints": { + "Http": { + "Url": "https://localhost:44354", + "Protocols": "Http1AndHttp2" + }, + "gRPC": { + "Url": "http://localhost:81", + "Protocols": "Http2" + } + } } } diff --git a/shared/EShopOnAbp.Shared.Hosting.AspNetCore/ApplicationBuilderHelper.cs b/shared/EShopOnAbp.Shared.Hosting.AspNetCore/ApplicationBuilderHelper.cs index 90faea26..15a9bd5e 100644 --- a/shared/EShopOnAbp.Shared.Hosting.AspNetCore/ApplicationBuilderHelper.cs +++ b/shared/EShopOnAbp.Shared.Hosting.AspNetCore/ApplicationBuilderHelper.cs @@ -1,5 +1,7 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Serilog;