﻿
{"id":16427,"date":"2020-07-06T00:02:23","date_gmt":"2020-07-06T00:02:23","guid":{"rendered":"https:\/\/www.gencayyildiz.com\/blog\/?p=16427"},"modified":"2020-07-06T00:13:32","modified_gmt":"2020-07-06T00:13:32","slug":"net-core-microservices-ocelot-api-gateway","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/net-core-microservices-ocelot-api-gateway\/","title":{"rendered":".NET Core Microservices &#8211; Ocelot API Gateway"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Bu i\u00e7eri\u011fimizde microservice yakla\u015f\u0131m\u0131n\u0131 benimseyen bir uygulamada, client taraf\u0131ndan gelen istekleri ilgili servislere y\u00f6nlendirme sorumlulu\u011funu \u00fcstlenen API Gateway yap\u0131lanmalar\u0131ndan a\u00e7\u0131k kaynak olan Ocelot k\u00fct\u00fcphanesiyle tan\u0131\u015facak ve nas\u0131l kullan\u0131ld\u0131\u011f\u0131n\u0131 inceleyece\u011fiz.<\/p>\n<div style=\"background-color:#F8DAD3;font-size:11px;color:green;text-align:center;\">\n<strong>\u00d6ncelikle <em>API Gateway Nedir?<\/em> sorusuna kar\u015f\u0131l\u0131k klavyeye alm\u0131\u015f oldu\u011fumuz <a target=\"_blank\" href=\"https:\/\/www.gencayyildiz.com\/blog\/microservice-mimarisinde-api-gateway-nedir\/\" rel=\"noopener noreferrer\">Microservice Mimarisinde API Gateway Nedir?<\/a> ba\u015fl\u0131kl\u0131 makalemizi incelemenizi, ard\u0131ndan bu i\u00e7eri\u011fi okuman\u0131z\u0131 tavsiye ederim.<\/strong>\n<\/div>\n<h3 style=\"color:#e83e8c;\">Ocelot Nedir?<\/h3>\n<p><a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway-254x300.png\" alt=\".NET Core Microservices - Ocelot API Gateway\" width=\"254\" height=\"300\" class=\"aligncenter size-medium wp-image-16497\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway-254x300.png 254w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway.png 301w\" sizes=\"auto, (max-width: 254px) 100vw, 254px\" \/><\/a>Microservice yap\u0131lanmas\u0131n\u0131 benimseyen uygulamalarda clientlardan gelen istekleri i\u015flevsel a\u00e7\u0131dan uygun servislere y\u00f6nlendirmekten sorumlu bir ge\u00e7i\u015f g\u00f6revi g\u00f6ren API Gateway k\u00fct\u00fcphanesidir. Ocelot, clienttan gelen istek neticesinde olu\u015fturulan HttpRequest nesnesini arkaplandaki servislere iletebilmek i\u00e7in HttpRequestMessage nesnesi olu\u015fturmakta ve bu \u015fekilde ula\u015ft\u0131rmakla g\u00f6revli middleware&#8217;dir. Ocelot, uygulaman\u0131n pipeline&#8217;\u0131nda ki son ara katmand\u0131r diyebiliriz ve bu y\u00fczden bir sonraki middleware \u00e7a\u011fr\u0131lmamaktad\u0131r.<\/p>\n<h3 style=\"color:#e83e8c;\">Ocelot&#8217;\u0131n Kullan\u0131ld\u0131\u011f\u0131 Deploy Tasar\u0131mlar\u0131<\/h3>\n<p>Ocelot a\u015fa\u011f\u0131daki gibi da\u011f\u0131t\u0131mlarda kullan\u0131lmaktad\u0131r.<\/p>\n<table>\n<thead>\n<tr>\n<th>Basit<br \/>\nUygulamalarda<\/th>\n<th>IdentityServer<br \/>\nKullan\u0131ld\u0131\u011f\u0131nda<\/th>\n<th>\u00c7oklu Instance<br \/>\nDurumlar\u0131mda<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway-150x150.jpg\" alt=\".NET Core Microservices - Ocelot API Gateway\" width=\"150\" height=\"150\" class=\"aligncenter size-thumbnail wp-image-16443\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway-150x150.jpg 150w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway-160x160.jpg 160w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway-320x320.jpg 320w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway-1.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway-1-150x150.jpg\" alt=\".NET Core Microservices - Ocelot API Gateway\" width=\"150\" height=\"150\" class=\"aligncenter size-thumbnail wp-image-16444\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway-1-150x150.jpg 150w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway-1-160x160.jpg 160w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway-1-320x320.jpg 320w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway-2.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway-2-150x150.jpg\" alt=\".NET Core Microservices - Ocelot API Gateway\" width=\"150\" height=\"150\" class=\"aligncenter size-thumbnail wp-image-16445\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway-2-150x150.jpg 150w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway-2-160x160.jpg 160w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway-2-320x320.jpg 320w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td>Birden fazla service&#8217;in kullan\u0131ld\u0131\u011f\u0131 durumlarda client taraf\u0131ndan gelen istekleri ilgili servislere y\u00f6nlendirmektedir.<\/td>\n<td>IdentityServer kullan\u0131lan bir uygulamada, Ocelot token talebi i\u00e7in gerekli y\u00f6nlendirmeyi yapmakta ve al\u0131nan token ile birlikte yap\u0131lan istekleri ba\u015far\u0131yla ilgili microservislere y\u00f6nlendirmektedir.<\/td>\n<td>Load Balancer arac\u0131l\u0131\u011f\u0131yla birden fazla instance \u00fczerinden istekleri kar\u015f\u0131layabilmekte ve ilgili servislere da\u011f\u0131tabilmektedir.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table>\n<thead>\n<tr>\n<th>Consul Kullan\u0131ld\u0131\u011f\u0131nda<\/th>\n<th>Service Fabric Kullan\u0131ld\u0131\u011f\u0131nda<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway-3.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway-3-150x150.jpg\" alt=\".NET Core Microservices - Ocelot API Gateway\" width=\"150\" height=\"150\" class=\"aligncenter size-thumbnail wp-image-16455\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway-3-150x150.jpg 150w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway-3-160x160.jpg 160w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway-3-320x320.jpg 320w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><\/a><\/td>\n<td><a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway-4.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway-4-150x150.jpg\" alt=\".NET Core Microservices - Ocelot API Gateway\" width=\"150\" height=\"150\" class=\"aligncenter size-thumbnail wp-image-16456\" srcset=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway-4-150x150.jpg 150w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway-4-160x160.jpg 160w, https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway-4-320x320.jpg 320w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3 style=\"color:#e83e8c;\">Peki Ocelot Nerelerde Kullan\u0131lmaktad\u0131r?<\/h3>\n<p>Ocelot a\u015fa\u011f\u0131daki gibi bir\u00e7ok yetene\u011fe sahip olan bir k\u00fct\u00fcphanedir.<\/p>\n<ul style=\"font-size:10px;line-height:13px;\">\n<li>Routing<\/li>\n<li>Request Aggregation<\/li>\n<li>Service Discovery with Consul &#038; Eureka<\/li>\n<li>Service Fabric<\/li>\n<li>Kubernetes<\/li>\n<li>WebSockets<\/li>\n<li>Authentication<\/li>\n<li>Authorisation<\/li>\n<li>Rate Limiting<\/li>\n<li>Caching<\/li>\n<li>Retry policies \/ QoS<\/li>\n<li>Load Balancing<\/li>\n<li>Logging \/ Tracing \/ Correlation<\/li>\n<li>Headers \/ Method \/ Query String \/ Claims Transformation<\/li>\n<li>Custom Middleware \/ Delegating Handlers<\/li>\n<li>Configuration \/ Administration REST API<\/li>\n<li>Platform \/ Cloud Agnostic<\/li>\n<\/ul>\n<h3 style=\"color:#e83e8c;\">Ocelot Nas\u0131l Kullan\u0131l\u0131r?<\/h3>\n<p>Ocelot k\u00fct\u00fcphanesinin kullan\u0131m\u0131n\u0131 \u00f6rneklendirebilmek i\u00e7in \u00f6ncelikle basit seviyede bir microservice \u00f6rneklendirmesinde bulunmam\u0131z gerekmektedir.<\/p>\n<h4 style=\"color:blue;\">\u00d6rnek Senaryo Haz\u0131rl\u0131\u011f\u0131<\/h4>\n<p>Bunun i\u00e7in a\u015fa\u011f\u0131daki gibi &#8216;ProductAPI&#8217; ve &#8216;CostumerAPI&#8217; olmak \u00fczere iki proje olu\u015ftural\u0131m.<br \/>\n<code style=\"color:red;\">dotnet new webapi --name productAPI<\/code><br \/>\n<code style=\"color:red;\">dotnet new webapi --name customerAPI<\/code><\/p>\n<p>\u015eimdi bu projelere i\u015flevsel \u00f6zellik kazand\u0131rabilmek i\u00e7in s\u0131ras\u0131yla &#8216;ProductController&#8217; ve &#8216;CustomerController&#8217; isimli controller s\u0131n\u0131flar\u0131 ekleyelim ve i\u00e7eriklerini a\u015fa\u011f\u0131daki gibi in\u015fa edelim.<\/p>\n<p>ProductAPI;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n   &#x5B;ApiController]\r\n   &#x5B;Route(&quot;api\/&#x5B;controller]&quot;)]\r\n   public class ProductController : ControllerBase\r\n   {\r\n      public IActionResult Get()\r\n      {\r\n         return Ok(new List&lt;string&gt; { &quot;Kalem&quot;, &quot;Kitap&quot;, &quot;Silgi&quot;, &quot;Defter&quot; });\r\n      }\r\n   }\r\n<\/pre>\n<p>CustomerAPI;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n   &#x5B;ApiController]\r\n   &#x5B;Route(&quot;api\/&#x5B;controller]&quot;)]\r\n   public class CustomerController : ControllerBase\r\n   {\r\n      public IActionResult Get()\r\n      {\r\n         return Ok(new List&lt;string&gt; { &quot;Hilmi Celayir&quot;, &quot;Saniye Y\u0131ld\u0131z&quot;, &quot;Nevin Y\u0131ld\u0131z&quot;, &quot;Fatih Y\u0131lmaz&quot; });\r\n      }\r\n   }\r\n<\/pre>\n<p>Ard\u0131ndan her iki projeye localde \u00e7al\u0131\u015f\u0131laca\u011f\u0131ndan dolay\u0131 uygun bir port tan\u0131mlayal\u0131m. Tabi bu i\u015flem i\u00e7in ister &#8216;Program.cs&#8217; dosyas\u0131n\u0131 istersenizde &#8216;launchSettings.json&#8217; dosyas\u0131ndaki profilleri kullanabilirsiniz. Ben \u00f6rneklendirme olarak &#8216;launchSettings.json&#8217; dosyas\u0131n\u0131 se\u00e7iyorum.<\/p>\n<p>ProductAPI;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    &quot;productAPI&quot;: {\r\n      &quot;commandName&quot;: &quot;Project&quot;,\r\n      &quot;launchBrowser&quot;: true,\r\n      &quot;launchUrl&quot;: &quot;weatherforecast&quot;,\r\n      &quot;applicationUrl&quot;: &quot;https:\/\/localhost:5003;http:\/\/localhost:5002&quot;,\r\n      &quot;environmentVariables&quot;: {\r\n        &quot;ASPNETCORE_ENVIRONMENT&quot;: &quot;Development&quot;\r\n      }\r\n    }\r\n<\/pre>\n<p>CustomerAPI;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    &quot;customerAPI&quot;: {\r\n      &quot;commandName&quot;: &quot;Project&quot;,\r\n      &quot;launchBrowser&quot;: true,\r\n      &quot;launchUrl&quot;: &quot;weatherforecast&quot;,\r\n      &quot;applicationUrl&quot;: &quot;https:\/\/localhost:5001;http:\/\/localhost:5000&quot;,\r\n      &quot;environmentVariables&quot;: {\r\n        &quot;ASPNETCORE_ENVIRONMENT&quot;: &quot;Development&quot;\r\n      }\r\n    }\r\n<\/pre>\n<p>Evet&#8230; Art\u0131k microservicelerimiz haz\u0131r. Bundan sonra clientlardan gelecek olan istekleri tek elden kar\u015f\u0131layabilecek ve iste\u011fe uygun ilgili servise y\u00f6nlendirecek olan api gateway projesini tasarlayal\u0131m.<\/p>\n<h4 style=\"color:blue;\">Ocelot Entegrasyonu ve Konfig\u00fcrasyonu<\/h4>\n<p>\u0130lk olarak &#8216;GatewayAPI&#8217; isimli projemizi olu\u015ftural\u0131m.<br \/>\n<code style=\"color:red;\">dotnet new webapi --name getwayAPI<\/code><\/p>\n<p>Bu projemizde localde \u00e7al\u0131\u015faca\u011f\u0131ndan dolay\u0131 portuna el atal\u0131m;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    &quot;getwayAPI&quot;: {\r\n      &quot;commandName&quot;: &quot;Project&quot;,\r\n      &quot;launchBrowser&quot;: true,\r\n      &quot;launchUrl&quot;: &quot;weatherforecast&quot;,\r\n      &quot;applicationUrl&quot;: &quot;https:\/\/localhost:5005;http:\/\/localhost:5004&quot;,\r\n      &quot;environmentVariables&quot;: {\r\n        &quot;ASPNETCORE_ENVIRONMENT&quot;: &quot;Development&quot;\r\n      }\r\n    }\r\n<\/pre>\n<p>Ard\u0131ndan Ocelot k\u00fct\u00fcphanesini a\u015fa\u011f\u0131daki kodu terminalde \u00e7al\u0131\u015ft\u0131rarak ilgili projeye dahil edelim. (<a href=\"https:\/\/www.nuget.org\/packages\/Ocelot\/\" rel=\"noopener noreferrer\" target=\"_blank\">Nuget Ocelot<\/a>)<br \/>\n<code style=\"color:red;\">dotnet add package Ocelot<\/code><\/p>\n<p>Bu i\u015flemden sonra uygulaman\u0131n herhangi bir dizininde(genellikle root tercih edilir) yap\u0131land\u0131rmay\u0131 konfig\u00fcre edebilmek i\u00e7in &#8216;ocelot.json&#8217; isminde bir dosya olu\u015ftural\u0131m ve i\u00e7eri\u011fini a\u015fa\u011f\u0131daki gibi temel ve en sade bi\u00e7imde olacak \u015fekilde in\u015fa edelim. Bu kullan\u0131m Ocelot&#8217;a dair herhangi bir aktivite sergilemese de ilgili k\u00fct\u00fcphaneyi aktifle\u015ftirmek i\u00e7in yeterli olacakt\u0131r.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n{\r\n   &quot;Routes&quot;: &#x5B;],\r\n   &quot;GlobalConfiguration&quot;: {\r\n       &quot;BaseUrl&quot;: &quot;https:\/\/localhost:5005&quot;\r\n   }\r\n}\r\n<\/pre>\n<p>Burada &#8216;GlobalConfiguration&#8217; i\u00e7erisindeki &#8216;BaseUrl&#8217; api gateway uygulamas\u0131 i\u00e7in esas url&#8217;i bildirmektedir. Bu projenin Ocelot k\u00fct\u00fcphanesi arac\u0131l\u0131\u011f\u0131yla microservislere eri\u015fimi sa\u011flayabilmesi i\u00e7in a\u015fa\u011f\u0131daki gibi konfig\u00fcrasyonda bulunulmas\u0131 gerekmektedir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n{\r\n   &quot;Routes&quot;: &#x5B;\r\n      {\r\n         &quot;DownstreamPathTemplate&quot;: &quot;\/api\/customer&quot;,\r\n         &quot;DownstreamScheme&quot;: &quot;https&quot;,\r\n         &quot;DownstreamHostAndPorts&quot;: &#x5B;\r\n             {\r\n                 &quot;Host&quot;: &quot;localhost&quot;,\r\n                 &quot;Port&quot;: 5001\r\n             }\r\n         ],\r\n         &quot;UpstreamPathTemplate&quot;: &quot;\/api\/gateway\/customer&quot;,\r\n         &quot;UpstreamHttpMethod&quot;: &#x5B; &quot;Get&quot; ]\r\n      },\r\n      {\r\n         &quot;DownstreamPathTemplate&quot;: &quot;\/api\/product&quot;,\r\n         &quot;DownstreamScheme&quot;: &quot;https&quot;,\r\n         &quot;DownstreamHostAndPorts&quot;: &#x5B;\r\n             {\r\n                 &quot;Host&quot;: &quot;localhost&quot;,\r\n                 &quot;Port&quot;: 5003\r\n             }\r\n         ],\r\n         &quot;UpstreamPathTemplate&quot;: &quot;\/api\/gateway\/product&quot;,\r\n         &quot;UpstreamHttpMethod&quot;: &#x5B; &quot;Get&quot; ]\r\n      }\r\n   ],\r\n   &quot;GlobalConfiguration&quot;: {\r\n       &quot;BaseUrl&quot;: &quot;https:\/\/localhost:5005&quot;\r\n   }\r\n}\r\n<\/pre>\n<p>Yukar\u0131daki konfig\u00fcrasyonu izah etmemiz gerekirse e\u011fer;<\/p>\n<ul>\n<li><strong>DownstreamPathTemplate<\/strong><br \/>\nY\u00f6nlendirme yap\u0131lacak microservice&#8217;in route&#8217;unu tutmaktad\u0131r.\n<\/li>\n<li><strong>DownstreamScheme<\/strong><br \/>\n\u0130lgili microservice&#8217;e yap\u0131lacak iste\u011fin hangi protokol \u00fczerinden ger\u00e7ekle\u015ftirilece\u011fini bildirmektedir.\n<\/li>\n<li><strong>DownstreamHostAndPorts<\/strong><br \/>\nMicroservice&#8217;in &#8216;Host&#8217; ve &#8216;Port&#8217; bilgilerini tutmaktad\u0131r.\n<\/li>\n<li><strong>UpstreamPathTemplate<\/strong><br \/>\nAPI Gateway \u00fczerinden microservice&#8217;e yap\u0131lacak y\u00f6nlendirmenin route&#8217;unu tutmaktad\u0131r.\n<\/li>\n<li><strong>UpstreamHttpMethod<\/strong><br \/>\nHangi isteklerin yap\u0131labilece\u011fi bildirilmektedir.\n<\/li>\n<\/ul>\n<p>\u015feklinde tan\u0131mlama yapabiliriz.<\/p>\n<p>Tabi i\u00e7erisini bu \u015fekilde konfig\u00fcre etti\u011fimiz &#8216;ocelot.json&#8217; dosyas\u0131n\u0131 &#8216;Program.cs&#8217; dosyas\u0131 \u00fczerinden uygulamaya a\u015fa\u011f\u0131daki gibi dahil etmemiz gerekmektedir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n   public class Program\r\n   {\r\n      public static void Main(string&#x5B;] args)\r\n      {\r\n         CreateHostBuilder(args).Build().Run();\r\n      }\r\n\r\n      public static IHostBuilder CreateHostBuilder(string&#x5B;] args) =&gt;\r\n          Host.CreateDefaultBuilder(args)\r\n              .ConfigureAppConfiguration((hosting, config) =&gt;\r\n              {\r\n                 config.AddJsonFile(&quot;ocelot.json&quot;, false, true);\r\n              })\r\n              .ConfigureWebHostDefaults(webBuilder =&gt;\r\n              {\r\n                 webBuilder.UseStartup&lt;Startup&gt;();\r\n              });\r\n   }\r\n<\/pre>\n<p>Ayriyetten &#8216;Startup.cs&#8217; dosyas\u0131nda da programatik olarak servis entegrasyonunu ve middleware \u00e7a\u011fr\u0131s\u0131n\u0131 ger\u00e7ekle\u015ftirelim.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n   public class Startup\r\n   {\r\n      public void ConfigureServices(IServiceCollection services)\r\n      {\r\n         .\r\n         .\r\n         .\r\n         services.AddOcelot();\r\n         .\r\n         .\r\n         .\r\n      }\r\n      async public void Configure(IApplicationBuilder app, IWebHostEnvironment env)\r\n      {\r\n         .\r\n         .\r\n         .\r\n         await app.UseOcelot();\r\n         .\r\n         .\r\n         .\r\n      }\r\n   }\r\n<\/pre>\n<h4 style=\"color:blue;\">Test Edelim<\/h4>\n<p>\u015eu ana kadar yapt\u0131\u011f\u0131m\u0131z \u00e7al\u0131\u015fmay\u0131 test edersek e\u011fer;<br \/>\n<a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway.gif\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/NET-Core-Microservices-Ocelot-API-Gateway.gif\" alt=\".NET Core Microservices - Ocelot API Gateway\" width=\"800\" height=\"533\" class=\"aligncenter size-full wp-image-16486\" \/><\/a><br \/>\n\u015feklinde bir sonu\u00e7la kar\u015f\u0131la\u015faca\u011f\u0131z.<\/p>\n<h4 style=\"color:blue;\">Parametreli Y\u00f6nlendirme<\/h4>\n<p>Microservice rotalar\u0131n\u0131n parametre ald\u0131\u011f\u0131 durumlarda ilgili parametre de\u011feriyle birlikte y\u00f6nlendirmeyi ger\u00e7ekle\u015ftirebilmek i\u00e7in a\u015fa\u011f\u0131daki gibi route tan\u0131mlanmas\u0131 gerekmektedir.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n      {\r\n         &quot;DownstreamPathTemplate&quot;: &quot;\/api\/product\/{id}&quot;,\r\n         &quot;DownstreamScheme&quot;: &quot;https&quot;,\r\n         &quot;DownstreamHostAndPorts&quot;: &#x5B;\r\n             {\r\n                 &quot;Host&quot;: &quot;localhost&quot;,\r\n                 &quot;Port&quot;: 5003\r\n             }\r\n         ],\r\n         &quot;UpstreamPathTemplate&quot;: &quot;\/api\/gateway\/product\/{id}&quot;,\r\n         &quot;UpstreamHttpMethod&quot;: &#x5B; &quot;Get&quot; ]\r\n      }\r\n<\/pre>\n<p>Bu yap\u0131lanma \u00fczerinden a\u015fa\u011f\u0131daki gibi bir url \u00fczerinden istek g\u00f6nderildi\u011fi taktirde hedef servis tetiklenmektedir.<br \/>\n<code style=\"color:red;\"><strong>https:\/\/localhost:5005\/api\/gateway\/product\/1<\/strong><\/code><\/p>\n<h4 style=\"color:blue;\">T\u00fcm \u0130steklerin Yakalanmas\u0131<\/h4>\n<p>A\u015fa\u011f\u0131daki gibi bir tasar\u0131m, yap\u0131lan t\u00fcm isteklerin do\u011frudan proxy \u00fczerinden y\u00f6nlendirilmesini sa\u011flayacakt\u0131r.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    {\r\n        &quot;DownstreamPathTemplate&quot;: &quot;\/{url}&quot;,\r\n        &quot;DownstreamScheme&quot;: &quot;https&quot;,\r\n        &quot;DownstreamHostAndPorts&quot;: &#x5B;\r\n                {\r\n                    &quot;Host&quot;: &quot;localhost&quot;,\r\n                    &quot;Port&quot;: 5003\r\n                }\r\n            ],\r\n        &quot;UpstreamPathTemplate&quot;: &quot;\/{url}&quot;,\r\n        &quot;UpstreamHttpMethod&quot;: &#x5B; &quot;Get&quot; ]\r\n    }\r\n<\/pre>\n<p>\u00d6rne\u011fin;<br \/>\n<code style=\"color:red;\"><strong>https:\/\/localhost:5005\/api\/product<\/strong><\/code> adresine yap\u0131lan iste\u011fi <code style=\"color:red;\"><strong>https:\/\/localhost:5003\/api\/product<\/strong><\/code> adresine y\u00f6nlendirecektir.<\/p>\n<p>Benzer mant\u0131kla;<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    {\r\n        &quot;DownstreamPathTemplate&quot;: &quot;\/{url}&quot;,\r\n        &quot;DownstreamScheme&quot;: &quot;https&quot;,\r\n        &quot;DownstreamHostAndPorts&quot;: &#x5B;\r\n                {\r\n                    &quot;Host&quot;: &quot;localhost&quot;,\r\n                    &quot;Port&quot;: 5003\r\n                }\r\n            ],\r\n        &quot;UpstreamPathTemplate&quot;: &quot;\/p\/{url}&quot;,\r\n        &quot;UpstreamHttpMethod&quot;: &#x5B; &quot;Get&quot; ]\r\n    },\r\n    {\r\n        &quot;DownstreamPathTemplate&quot;: &quot;\/{url}&quot;,\r\n        &quot;DownstreamScheme&quot;: &quot;https&quot;,\r\n        &quot;DownstreamHostAndPorts&quot;: &#x5B;\r\n                {\r\n                    &quot;Host&quot;: &quot;localhost&quot;,\r\n                    &quot;Port&quot;: 5001\r\n                }\r\n            ],\r\n        &quot;UpstreamPathTemplate&quot;: &quot;\/c\/{url}&quot;,\r\n        &quot;UpstreamHttpMethod&quot;: &#x5B; &quot;Get&quot; ]\r\n    }\r\n<\/pre>\n<p><code style=\"color:red;\"><strong>https:\/\/localhost:5005\/p\/api\/product<\/strong><\/code> adresine yap\u0131lan iste\u011fi <code style=\"color:red;\"><strong>https:\/\/localhost:5003\/api\/product<\/strong><\/code> adresine, <code style=\"color:red;\"><strong>https:\/\/localhost:5005\/c\/api\/customer<\/strong><\/code> adresine yap\u0131lan iste\u011fi <code style=\"color:red;\"><strong>https:\/\/localhost:5001\/api\/customer<\/strong><\/code> adresine y\u00f6nlendirilecektir.<\/p>\n<p>\u0130lgilenenlerin faydalanmas\u0131 dile\u011fiyle&#8230;<br \/>\nSonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar&#8230;<\/p>\n<p>Not : \u00d6rnek dosyay\u0131 indirmek i\u00e7in <a href=\"https:\/\/www.gencayyildiz.com\/blog\/wp-content\/uploads\/2020\/07\/ocelot.zip\">buraya<\/a> t\u0131klay\u0131n\u0131z.<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Merhaba, Bu i\u00e7eri\u011fimizde microservice yakla\u015f\u0131m\u0131n\u0131 benimseyen bir uygulamada, client taraf\u0131ndan gelen istekleri ilgili servislere y\u00f6nlendirme sorumlulu\u011funu \u00fcstlenen API Gateway yap\u0131lanmalar\u0131ndan a\u00e7\u0131k kaynak olan Ocelot k\u00fct\u00fcphanesiyle tan\u0131\u015facak ve nas\u0131l kullan\u0131ld\u0131\u011f\u0131n\u0131 inceleyece\u011fiz. \u00d6ncelikle API Gateway Nedir?&#46;&#46;&#46;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":16497,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3573],"tags":[3587,3579,3289,3588,3586,3585],"class_list":["post-16427","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-microservices","tag-net-core-microservices","tag-api-gateway","tag-microservices","tag-ocelot-api-gateway","tag-ocelot-microservices","tag-ocelot-nedir"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/16427","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=16427"}],"version-history":[{"count":65,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/16427\/revisions"}],"predecessor-version":[{"id":16500,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/16427\/revisions\/16500"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/16497"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=16427"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=16427"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=16427"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}