From 591086956d80c9db65fbc7b117036887a1ef8d91 Mon Sep 17 00:00:00 2001 From: eiximenis Date: Thu, 27 Jun 2019 16:16:11 +0200 Subject: [PATCH] grpc poc working on k8s, using envoy as a sidecar in catalog api --- docker-compose.override.yml | 2 + docker-compose.yml | 5 +- .../apigwws/configuration-web-shopping.json | 18 +++++- .../envoycfg/_catalog.proto-descriptor.pb | Bin 0 -> 9348 bytes k8s/helm/catalog-api/envoycfg/_envoy.yaml | 56 ++++++++++++++++++ .../catalog-api/templates/deployment.yaml | 24 +++++++- k8s/helm/catalog-api/templates/envoy-cm.yaml | 16 +++++ k8s/helm/catalog-api/templates/service.yaml | 8 +++ k8s/helm/catalog-api/values.yaml | 12 +++- .../aggregator/Config/UrlsConfig.cs | 4 +- .../aggregator/Services/CatalogService.cs | 3 +- .../Catalog.API/Grpc/CatalogService.cs | 7 ++- src/Services/Catalog/Catalog.API/Program.cs | 42 +++---------- 13 files changed, 148 insertions(+), 49 deletions(-) create mode 100644 k8s/helm/catalog-api/envoycfg/_catalog.proto-descriptor.pb create mode 100644 k8s/helm/catalog-api/envoycfg/_envoy.yaml create mode 100644 k8s/helm/catalog-api/templates/envoy-cm.yaml diff --git a/docker-compose.override.yml b/docker-compose.override.yml index c36cd2b3a..11ea6138c 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -89,6 +89,8 @@ services: - AzureStorageEnabled=False - ApplicationInsights__InstrumentationKey=${INSTRUMENTATION_KEY} - OrchestratorType=${ORCHESTRATOR_TYPE} + - GRPC_PORT=81 + - PORT=80 ports: - "5101:80" - "9101:81" diff --git a/docker-compose.yml b/docker-compose.yml index 3b2f4f77d..243fbf2c6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -240,7 +240,4 @@ services: - webhooks.api envoy: - image: envoy:v1 - build: - context: src/ApiGateways/Envoy - dockerfile: Dockerfile \ No newline at end of file + image: envoyproxy/envoy \ No newline at end of file diff --git a/k8s/helm/apigwws/configuration-web-shopping.json b/k8s/helm/apigwws/configuration-web-shopping.json index 021056f43..208406793 100644 --- a/k8s/helm/apigwws/configuration-web-shopping.json +++ b/k8s/helm/apigwws/configuration-web-shopping.json @@ -1,5 +1,17 @@ { "ReRoutes": [ + { + "DownstreamPathTemplate": "/api/{version}/{everything}", + "DownstreamScheme": "http", + "DownstreamHostAndPorts": [ + { + "Host": "catalog", + "Port": 5000 + } + ], + "UpstreamPathTemplate": "/api/{version}/c/{everything}", + "UpstreamHttpMethod": [ "GET" ] + }, { "DownstreamPathTemplate": "/api/{version}/{everything}", "DownstreamScheme": "http", @@ -9,9 +21,9 @@ "Port": 80 } ], - "UpstreamPathTemplate": "/api/{version}/c/{everything}", + "UpstreamPathTemplate": "/grpc/{version}/c/{everything}", "UpstreamHttpMethod": [ "GET" ] - }, + }, { "DownstreamPathTemplate": "/api/{version}/{everything}", "DownstreamScheme": "http", @@ -102,7 +114,7 @@ "DownstreamHostAndPorts": [ { "Host": "catalog", - "Port": 80 + "Port": 5000 } ], "UpstreamPathTemplate": "/catalog-api/{everything}", diff --git a/k8s/helm/catalog-api/envoycfg/_catalog.proto-descriptor.pb b/k8s/helm/catalog-api/envoycfg/_catalog.proto-descriptor.pb new file mode 100644 index 0000000000000000000000000000000000000000..7c94ce9f05ec461731aea1a9659446fde3810e23 GIT binary patch literal 9348 zcmc&4O>ZMvaqZZN{hTjiCJ=p z`qitdSFc`Gy{cCx-^-J8eb?>Ve8U)88z(_9tPVXla4C`CDuC5flDh>n^cu-~Bb)n` z%;mYOrP^9`j8jDf29r#pP>ozV9@+M(-s7g*Ck3C|+zea~M95q%vUp4>nd|dF@GRbXjNt%b!%#n9QlDes1#tLRC_Y1 zjHZ}DxjRM>aL<9&Lv#=-cjWd?D+|bviEmKWs?qCN0cgdr^&`vaSx(=tls-kxbWG3) z@f$_50J7qG>P@ne5(1aL3%H@~#5cl#=0qxviSNc8Qa0T|VndCh*b5p)C-YuCJodh8 z8&1FKdi{+)cZAt&$SE)yUl^3(IBsCzzWkes?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T4reD zKKR4Av9gkcD@j7J%V|AvLe#&{kwsCc;4#7b9+ovG))1n2EZ3e%-AYO9Y7_6W z&3Ty}Gf9aLCg3=C!HI?TSU93PWd4pXc;n?$uBD5)3EsE6f_d?FSKn{5z4|T-$?4-H zXB!`!PTqO~bk;)v4*2jkdDjTgqdNk1gXD$2C?TQ7l*ed+2+ef!&qGZ0XBkos zulm{IJ^gNdXz?<}ea8tKQvi504?G2Jeb zZLYSqw7pLIK-+J=s~_&awcmbwpD79U0__pHeTOL1LeUFkmRR7~t7Lh<4R<13jN10L z);#FShmUv@@bHoJvY*V6)frD<5XNP%Lsre#Q&D^dO%-#74!j#OL5jH$XvqnzV+#^` zS%k|vW_b)3MIU{MP-w?N=j$HV;qfj+#=0g4&5&@W*W`Qi$|` z4_prD1X3sQn3{!*v48+n66SxF4}H+6!;!t5y;@);%a0#6)sG-ynQSgV@lvo|$Z+AV z4Le&`Q)e?tQr!*lSxmY9=L{)CV7P8&?+@F#`9@aB$o?O~A(zAkZ4AdTo~z9b>`g*% zWH5je_GNRGEr;-C2*w^);2D-Zfh&5hOo=99DmuIe^<+Jg=j2|g0dKDNWOu|XZ{P-v7Vw& z%;Q-pM8*OAa;#nb2pVQRk%*USm52hA9DO!$o<&QE5w_FeVaJ003v2>@zG2>lUs<5z zm1A%hp%}oRS%#l_Z~|W^F7#osjSEfsaN;0>(<)gV7!M@B1p1OdUx6vXbBqr!Hqia> zxa;+xJ5&a=0`?P`1gJH#7^*@FJ!Js14px1cltLPzrWqsw)@GqC;CJmqvJjF`kCb+J zfJWCiZS}wb!-<486BfCC>7nz|6jk**fBED`*^6{t{0ijW%NwS2fej0m@Hf6`^*)S# G`SBmHwv $"/api/v1/catalog/items/{id}"; - public static string GetItemsById(IEnumerable ids) => $"/api/v1/catalog/items?ids={string.Join(',', ids)}"; + // REST call standard must go through port 5000 + public static string GetItemsById(IEnumerable ids) => $":5000/api/v1/catalog/items?ids={string.Join(',', ids)}"; } public class BasketOperations diff --git a/src/ApiGateways/Web.Bff.Shopping/aggregator/Services/CatalogService.cs b/src/ApiGateways/Web.Bff.Shopping/aggregator/Services/CatalogService.cs index 159e56d85..4c98c031e 100644 --- a/src/ApiGateways/Web.Bff.Shopping/aggregator/Services/CatalogService.cs +++ b/src/ApiGateways/Web.Bff.Shopping/aggregator/Services/CatalogService.cs @@ -24,7 +24,8 @@ namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Services public async Task GetCatalogItemAsync(int id) { - var stringContent = await _httpClient.GetStringAsync(_urls.Catalog + UrlsConfig.CatalogOperations.GetItemById(id)); + var uri=_urls.Catalog + UrlsConfig.CatalogOperations.GetItemById(id); + var stringContent = await _httpClient.GetStringAsync(uri); return JsonConvert.DeserializeObject(stringContent); } diff --git a/src/Services/Catalog/Catalog.API/Grpc/CatalogService.cs b/src/Services/Catalog/Catalog.API/Grpc/CatalogService.cs index 940a63c3b..97f40240f 100644 --- a/src/Services/Catalog/Catalog.API/Grpc/CatalogService.cs +++ b/src/Services/Catalog/Catalog.API/Grpc/CatalogService.cs @@ -8,6 +8,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.eShopOnContainers.Services.Catalog.API; using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure; using Microsoft.eShopOnContainers.Services.Catalog.API.Model; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using static CatalogApi.Catalog; @@ -17,15 +18,17 @@ namespace Catalog.API.Grpc { private readonly CatalogContext _catalogContext; private readonly CatalogSettings _settings; - public CatalogService(CatalogContext dbContext, IOptions settings) + private readonly ILogger _logger; + public CatalogService(CatalogContext dbContext, IOptions settings, ILogger logger) { _settings = settings.Value; _catalogContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext)); + _logger = logger; } public override async Task GetItemById(CatalogItemRequest request, ServerCallContext context) { - + _logger.LogInformation($"Begin grpc call CatalogService.GetItemById for product id {request.Id}"); if (request.Id <=0) { context.Status = new Status(StatusCode.FailedPrecondition, $"Id must be > 0 (received {request.Id})"); diff --git a/src/Services/Catalog/Catalog.API/Program.cs b/src/Services/Catalog/Catalog.API/Program.cs index 73d644526..39f3643d8 100644 --- a/src/Services/Catalog/Catalog.API/Program.cs +++ b/src/Services/Catalog/Catalog.API/Program.cs @@ -75,11 +75,12 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API .UseConfiguration(configuration) .ConfigureKestrel(options => { - options.Listen(IPAddress.Any, 80, listenOptions => + var ports = GetDefinedPorts(configuration); + options.Listen(IPAddress.Any, ports.httpPort, listenOptions => { - listenOptions.Protocols = HttpProtocols.Http1; + listenOptions.Protocols = HttpProtocols.Http1AndHttp2; }); - options.Listen(IPAddress.Any, 81, listenOptions => + options.Listen(IPAddress.Any, ports.grpcPort, listenOptions => { listenOptions.Protocols = HttpProtocols.Http2; }); @@ -107,38 +108,11 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API .CreateLogger(); } - private static IEnumerable<(int portNumber, bool https)> GetDefinedPorts(IConfiguration config) + private static (int httpPort, int grpcPort) GetDefinedPorts(IConfiguration config) { - const string https = "https://"; - const string http = "http://"; - var defport = config.GetValue("ASPNETCORE_HTTPS_PORT", 0); - if (defport != 0) - { - yield return (defport, true); - } - - var urls = config.GetValue("ASPNETCORE_URLS", null)?.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries); - if (urls?.Any() == true) - { - foreach (var urlString in urls) - { - var uri = urlString.ToLowerInvariant().Trim(); - var isHttps = uri.StartsWith(https); - var isHttp = uri.StartsWith(http); - if (!isHttp && !isHttps) - { - throw new ArgumentException($"Url {uri} must start with https:// or http://"); - } - - uri = uri.Substring(isHttps ? https.Length : http.Length); - var lastdots = uri.LastIndexOf(':'); - if (lastdots != -1) - { - var sport = uri.Substring(lastdots + 1); - yield return (int.TryParse(sport, out var nport) ? nport : isHttps ? 443 : 80, isHttps); - } - } - } + var grpcPort = config.GetValue("GRPC_PORT", 5001); + var port = config.GetValue("PORT", 80); + return (port, grpcPort); } private static IConfiguration GetConfiguration()