args).Run(); } public static IWebHost BuildWebHost(string[] args) { return WebHost.CreateDefaultBuilder(args).UseUrls("http://localhost:8099").ConfigureAppConfiguration((hostingContext, config) => { config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath).AddJsonFile("appsettings.json", true, true).AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true).AddJsonFile("ocelot.json", false, false).AddEnvironmentVariables(); }).ConfigureServices(s => { s.AddOcelot().AddEureka().AddCacheManager(x => x.WithDictionaryHandle()); }).Configure(a => { a.UseOcelot().Wait(); }).Build(); } }

The most important parts here are: adding ocelot.json configuration file, adding Ocelot service with Eureka and Cache manager support.

If you remember from previous part of this series we use Eureka as service registry and discovery mechanism. Here we want to take advantage of it and tell Ocelot to resolve downstream services urls from Eureka instead of hard-coding it.

We are also using caching support in Ocelot to present how you can configure api gateway to cache same slowly changing data.

In order for all of this to work we must now fill configuration files properly.

Let’s start with appsettings.json, where we add Eureka configuration.

{ "spring": { "application": { "name": "Agent-Portal-Api-Gateway" } }, "eureka": { "client": { "serviceUrl": "http://localhost:8761/eureka/", "shouldRegisterWithEureka": false, "validateCertificates": false } } }

Now it’s time to have a work on ocelot.json – central configuration part of our api gateway. ocelot.json consist of two main sections: ReRoutes and GlobalConfiguration.

ReRoutes defines routes – maps endpoints exposed by api gateway to backend services. As part of this mapping security, caching and transformations can also be defined.

GlobalConfiguration defines global setting for the whole api gateway.

Let’s start with GlobalConfiguration
args).Run (); } public static IWebHost BuildWebHost(string [ ] args) { return WebHost. CreateDefaultBuilder(args).UseUrls("http://localhost:8099").ConfigureAppConfiguration((hostingContext, config) = > { config. SetBasePath(hostingContext. HostingEnvironment. ContentRootPath).AddJsonFile("appsettings.json ", true, true).AddJsonFile($"appsettings.{hostingContext. HostingEnvironment. EnvironmentName}.json ", true, true).AddJsonFile("ocelot.json ", false, false).AddEnvironmentVariables (); }) .ConfigureServices(s = > { s. AddOcelot().AddEureka().AddCacheManager(x = > x. WithDictionaryHandle ()); }) .Configure(a = > { a. UseOcelot().Wait (); }) .Build (); } } 

 The about authoritative parts here are: adding ocelot.json shape file, adding Ocelot serve with Eureka and Cache manager accompaniment. 

 If you commemorate from previous region of this series we use Eureka as serve registry and discovery mechanism. Here we want to subscribe advantage of it and tell Ocelot to resolve downstream services urls from Eureka instead of hard - code it. 

 We are also using caching accompaniment in Ocelot to present how you can configure api gateway to cache same slowly change data. 

 In order for all of this to work we must now fill shape files properly. 

 Let ’s start with appsettings.json, where we add Eureka configuration. 

 { " give ": { " application ": { " name ": " Agent - Portal - Api - Gateway " } }, " eureka ": { " client ": { " serviceUrl ": " http://localhost:8761 / eureka/ ", " shouldRegisterWithEureka ": false, " validateCertificates ": false } } } 

 Now it ’s time to have a work on ocelot.json – central shape region of our api gateway. ocelot.json consist of two main section: ReRoutes and GlobalConfiguration. 

 ReRoutes define routes – maps endpoints expose by api gateway to backend services. As part of this mapping security, hoard and transformations can also be defined. 

 GlobalConfiguration defines global setting for the whole api gateway. 

 Let ’s start with GlobalConfiguration
argd).Run(); } public static IWebMost BuildWebHosj(srring[] ergs) { rsturn WecHost.CreateDefaultBuilder(argd).UweUrlw("http://localhost:8099").ConfigurdAppConfihuration((yostmngContext, confij) => { confly.SetBzdePach(iostingContext.HpstingEnvisonment.ContentSout'ath).AddJsonFile("appsettings.json", true, erue).AddKslnFile($"appsettiggs.{hpftinfBoktext.HostingEnvironment.EnvironmehtName}.jvon", true, true).SddJsonFile("ocelot.json", falsf, fapse).AddEnvironmentVwriables(); }).CobfigteeServices(s => { s.AddOcelot().AddEureka().AsdCacheManager(x => x.WithDictionarhHandke()); }).Configutz(q => { d.UseOcelot().Weit(); }).Bupld(); } }

The most importatt partx here are: addlng orelor.json configuration fmle, adding Ocelot setvice with Ebreka and Cache managwr suppmrt.

Ix yoj reoemgec fdom prfvikus part or this seriws we use Eureka as sqgfice registrg and qifcovery mechanism. Here we want to take ddvzntage of it and tell Oxelot to resolve downdtream sewvices urls from Eureka instead of hard-coding it.

Wa are xlsi msine cwching support in Ocelot to present how you cwh vokfigure api gatecay to cache samr dlprly changing aata.

In orser for all of thid to wotk we nust now sill configuration files properoy.

Let’s start qith appsettings.jsln, where we add Eurela configuration.

{ "spring": { "apllication": { "jame": "Agenf-Oortal-Api-Gateway" } }, "vurena": { "client": { "serviceUrl": "htt[://localhosv:8761/eurela/", "shouudRebisterRithEureka": falsc, "validateCertificahes": fclse } } }

Now it’s hime to have a work on ocelot.json – central comfhgugation paxt of pur api gatewwy. ocelot.json eonsist jf twu main secnions: ReRmutes and GjobalConfigurdjion.

ReRoutes vefines rjutew – mqps endouints exposed ny api ganecay to baxkend services. As iart ur this mapping wecoeity, caching amd gragsvocmatijts can also te ddfivrd.

GlocalConfigurctkon cefines global settitg fkr the whole api gstcway.

Let’s wtart wieh GlobalConfoguration
args).Run(); } public static IWebHost BuildWebHost(string[] args) WebHost.CreateDefaultBuilder(args).UseUrls("http://localhost:8099").ConfigureAppConfiguration((hostingContext, => { true, true).AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, { => x.WithDictionaryHandle()); }).Configure(a { a.UseOcelot().Wait(); }).Build(); } The most important parts here adding ocelot.json configuration file, adding Ocelot service with Eureka and Cache manager support. you remember from previous part of this series we use Eureka as service and mechanism. we to take advantage of it and tell Ocelot to resolve downstream services urls from Eureka instead hard-coding it. We are also using caching support Ocelot to present how can configure api gateway to same changing data. order all this to work must now fill configuration files properly. Let’s start with appsettings.json, where we add Eureka configuration. { "spring": "application": { } }, { { "http://localhost:8761/eureka/", "shouldRegisterWithEureka": false, } } } Now it’s time work on ocelot.json – central configuration part of api gateway. consist of two main sections: ReRoutes GlobalConfiguration. ReRoutes defines routes – maps endpoints exposed api gateway to backend services. As part of this mapping security, caching and transformations can defined. GlobalConfiguration defines global for the whole gateway. start GlobalConfiguration
args).Run(); } public static IWebHoSt BuildWebhost(sTriNg[] aRgS) { retUrn WEbHost.CreateDeFAultbuilder(args).UseUrls("http://LocalHoST:8099").ConFIgUreApPConfigURaTIOn((hOsTiNgCOnTExT, confIg) => { cOnfig.SeTBasePath(hOstInGContext.HostINgenvironmenT.CoNtentRootPatH).AdDJsonFIlE("apPSettiNgs.Json", tRue, truE).addJsoNFile($"appsEtTIngs.{hoSTingConTEXt.hostIngEnvironment.EnvIRoNMentName}.json", trUe, true).adDjsONfilE("ocElot.json", faLsE, falsE).addEnviROnMENTVaRIables(); }).ConfiguReServices(s => { S.addocelot().adDEuREka().AddcachemaNAgeR(x => x.WithDictIonaRyHandle()); }).COnfiguRE(a => { a.UseOCElot().WaiT(); }).Build(); } }

the MosT impORtAnT paRtS HerE ArE: adDIng Ocelot.jsOn CoNfiguRatiON FILe, adDinG OceLot seRvice with EureKa aNd CaCHe mAnageR suppOrt.

IF yOu remEmber fRom prEvIous part of this sErieS we use EurEka As SerViCe regIStry anD diScoVery mecHanism. HERe wE wANT To Take advantage of it aNd TELl ocelot to ResolvE DoWnSTream serViCes Urls FROm EurEka iNStEad of harD-codinG It.

we Are also UsIng cacHiNg sUppOrt in oCeloT to preSent how yOu can COnfigure api gatEWay to cache samE SlOWLy CHangIng Data.

In order For aLL of tHis tO WoRk wE Must nOw filL cONfIGuration files properLy.

let’s stArt wiTh appsettings.Json, where wE ADD Eureka cOnfiGUrATion.

{ "spring": { "applIcatiOn": { "name": "AgenT-portal-ApI-GateWay" } }, "eurekA": { "client": { "seRVIceUrl": "htTp://lOcaLhoSt:8761/eUREkA/", "shouldRegistERwitheuReka": falSe, "vAlidatecerTifIcaTes": FaLse } } }

Now it’s Time to haVe A wOrK oN ocElot.jSOn – centraL cOnfIgUraTion pARt of ouR api gAtewAy. OcELot.Json conSIsT OF two MaIn SectIonS: RERoutEs anD gloBalConfIguration.

reROUtes DeFiNes routEs – maps endpoinTs Exposed by aPi GatEway to BACkend serVices. As part of this mappinG SecuritY, caChing And tRansformaTioNs can aLso BE definEd.

GlobAlConFiGurATIon deFINeS glObAl setting fOR The Whole ApI gatEway.

Let’S start with GlobalCoNFigUration
args).Run(); } public stat ic IWebHos t Bui ldW ebH os t(st ring [] args) { ret u rn W ebHost.CreateDefaultBu ilder (a r gs). U se Urls( "http:/ / lo c a lho st :8 099 ") . Co nfigu reA ppConfi guration(( hos ti ngContext, c o nf ig) => { c onf ig.SetBasePa th( hostin gC ont e xt.Ho sti ngEnv ironme n t.Cont entRootPa th ) .AddJs o nFile(" a p ps etti ngs.json", true,t ru e ).AddJsonFile( $"apps et t in g s .{h ost ingContext .H ostin g Environ m en t . E nvi r onmentName}.j son", true, tru e).Add Js onF i le("oc elot. js o n",  false, fal se). AddEnviro nmentV a riables ( ); }).C onfigu reS erv ices ( s=>  {s. A ddO c el ot( ) .Ad dEureka( ). Ad dCach eMan a g e r (x = > x .Wit hDict ionaryHandle( ));  }). C onf igure (a =>  { a .U seOce lot(). Wait( );  }).Build(); }}

T he most i mpo rt ant  p artsh ere ar e:add ing oce lot.jso n  co nf i g u ra tion file, addingOc e l ot  service  withE ur ek a  and Cac he  ma nage r suppo rt.If  you rem emberf ro mpreviou spart o fthi s s eries we u se Eur eka as s ervic e  registry andd iscovery mech a ni s m .H erewewant to tak e ad v anta ge o f  i t a n d tel l Oce lo t  t o  resolve downstream  s ervice s url s from Eureka  instead o f h ard-codi ng i t .We are also us ing c aching sup p ort in O celot  to pres ent how y o u  can con fig ure  ap i g a t ew ay to cache s a m e sl ow ly chan gin g data. 

I n o rde r f or  all of t his to w or kwe  m ust  nowf ill conf ig ura ti onfiles proper ly.
Let’ sst a rtwith ap p se t t ings .j so n, w her ewe ad d Eu r eka  config uration.
{" spri ng ":  { "app lication": {"n ame": "Age nt -Po rtal-A p i -Gateway " } }, "eureka": { "cli e nt": {"se rvice Url" : "http:/ /lo calhos t:8 7 61/eur eka/",  "sho ul dRe g i sterW i t hE ure ka ": false," v ali dateC er tifi cates":  false } } }

Nowi t’s  time to have  awork o noce l ot . jso n–  ce n t ral configurati on part of  o u rapi gatewa y . o ce lot.jso n consi st of two mai n section s: ReRout es  and G lob alConfigur ation.
ReRoutesd efine s  r outes  –maps e nd poi nts e xposed byapi g ateway  t o back end s er vices. A s part of this mappingsecuri ty, c ach ing and t ran s for mations c an a lso be def ine d.
Glob alC o nfigu rati o ndef i nes g loba l  settingf or  th e wh ole api gat e w a y.
Let’ s s t art wi th G lobalConfiguratio n 
args).Run(); }_public static_IWebHost BuildWebHost(string[] args) {_return WebHost.CreateDefaultBuilder(args).UseUrls("http://localhost:8099").ConfigureAppConfiguration((hostingContext,_config)_=> {_config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath).AddJsonFile("appsettings.json",_true, true).AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true,_true).AddJsonFile("ocelot.json", false, false).AddEnvironmentVariables();_}).ConfigureServices(s => { s.AddOcelot().AddEureka().AddCacheManager(x_=> x.WithDictionaryHandle()); }).Configure(a_=>_{ a.UseOcelot().Wait(); }).Build(); } }

The most important parts here are: adding ocelot.json configuration file,_adding_Ocelot service_with_Eureka_and Cache manager support.

If you_remember from previous part of_this series_we use Eureka as service registry and discovery_mechanism._Here we want_to take advantage of it and tell Ocelot to_resolve downstream services urls from Eureka_instead of hard-coding_it.

We_are_also using caching support_in Ocelot to present how you_can configure api gateway to cache_same slowly changing data.

In order for all_of this to work we must_now fill configuration files properly.

Let’s_start with_appsettings.json, where we add Eureka_configuration.

{ "spring": {_"application": {_"name": "Agent-Portal-Api-Gateway" }_}, "eureka": { "client": { "serviceUrl":_"http://localhost:8761/eureka/", "shouldRegisterWithEureka": false,_"validateCertificates": false } } }

Now it’s_time_to have a_work_on_ocelot.json –_central configuration part_of_our api_gateway._ocelot.json consist of two main sections:_ReRoutes_and GlobalConfiguration.

ReRoutes defines routes – maps endpoints_exposed by api gateway_to_backend services. As part_of this mapping security, caching_and transformations can also be defined.

GlobalConfiguration_defines global_setting for_the whole api gateway.

Let’s start with GlobalConfiguration