Hi guys this is basan in this tutorial we are going to see what is spring cloud Eureka and how to work on it so first let's see what is Eureka Eureka is a service registry provided by the third party library that is the Netflix organization provide this Eureka concept and it integrated with spring framework and internally using cloud to deploy our micro services so and what is the use of Eureka? Normally in microservice architecture design we are developing separate services and exposing each API as service end point and whenever we need to communicate one service to another service we are internally calling by the rest API so let's see this diagram you will get some idea on it Assume this is my one micro service, this rest service one and this is the second micro service and this is the third micro service and this is my client. Suppose this client want to access this service one using this.
particular URL and for REST service 2 there is different URL and REST service 3 there is different URL. So there may be a chance we have multiple microservices and we have different different URL with the different hostname and different portname. So when REST client this is my suppose assume this is my client and it want to interact with REST service 1. So he need to use this particular URL and it is tightly coupled to this rest client and rest service one so it's not easy to remember the url basically this service one slash product we need this is the endpoint url and this local host and 8081 is the local localhost is our host and 8081 is our port so every micro service is deployed in different different ports so it's not easy to remember so for that what we should use we need to use one container and all these services we need to register there instead of hit url to this particular rest service we can talk to that service registry and we'll get the actual local host and the port to access that rest endpoint so let's go the let's take the different example let's open this image this is my rest client assume this is the shopping service amazon or flipkart anything and there is multiple micro services like product service one micro service order service payment service like this there is three micro services and this rest client want to access product service so what you need to do in our traditional approach you need to use the exact url exact host and port and the endpoint URL so instead of remember those things this Eureka Eureka server give us the flexibility no need to invoke directly the endpoint just register your micro services in my Eureka server this is the Eureka server where I register product service order service payment service so when I will give request from this client it will not directly goes to the micro service one or so initially it will go to the this Eureka server and it will find out which service you are calling based on that service it will go to that particular micro service and match with the incoming URL to the endpoint URL which one is match it will forward the request to that particular micro service so that my client is not directly coupled to this micro services so similar way we need to interact using the eureka server so let's do a small application in it i will do the same application what is i mentioned in my blog so it will easy to like catch up the things so let's do a small pfc so to access this first we need to develop this eureka server we need to develop our customized eureka server then we will deploy one micro service and that we will access from client okay so let's create one our own eureka server and we will register one micro service so let's create one spring starter project that is a spring boot i am naming it as shopping server I will give a proper name Amazon Server Eureka server okay now let's create add the required dependency I am adding dev tools and web and Eureka server we no need to add anything i guess okay fine okay application imported so I just want this as a server. I don't want to write any business here. So just annotate at the rate enable Eureka server.
I want to make this as a server where I can register my micro services. name it as Eureka server ok now this will behave like a server but to inform that Eureka server we need to provide some configuration file that we will write here yml file so let's create one yml file and there we need to mention this is not any client this is the server and this is the port and the application name okay create a yml file even you can mention same as in your application.properties file okay so let's check the configuration line.registerWithEureka registerWithEureka registerWithEureka false then we need to mention the server port it should be start server.port normally we can mention our custom any port any 4 digit integer but by default it will use 8761 so better to mention that it's not taking in ok 8761 and we need to mention our application name let's mention it as Amazon server any name you can give here ok now this register with eureka false phase register false it's just this is the root Tag here we are mentioning it's not a client it's a server and the port is this one and this is the name of this application Amazon server. Now let's start our application and we'll check the in dashboard. using port 8761 and threshold is 1 this will automatically increase so let's run and we'll check the dashboard Yes, our Eureka server is up now. You can see this is the dashboard.
Here in instance currently registered with Eureka, we didn't register any microservices here. Once we'll register, we'll see we are able to see that in this section. Okay, now let's develop this the same microservice.
That will be one easy microservice will develop with one get method. let's create one spring boot project again this is the name payment service okay this is the provider and this will consume from one client and this provider will register in our eureka server okay eureka provider add the required dependency uef here we no need to write eureka server we want to make it as a microservices and we want this to register in eureka so add this eureka discovery and add the dev tools as well let's create clear this console okay now here just annotate at the rate enable eureka client okay now we want to expose a small API so just write one controller package create here one controller payment controller okay annotate at the rate raised controller and write a get method public something return string pay now and end price will pass this price as a request URL return this is the dummy like API to explain this you rega how it works I am doing that so payment amount okay now annotate this is the gate method so i am annotating gate mapping giving the url something like pay now and as this i want to pass as request url so i am using here at the rate path variable okay and this i need to pass this expression okay and here annotate the root url like annotate something payment provider okay so now Let's register this payment service in our Eureka server. Eureka server is the Amazon server we created. Let's register.
To register it we need to write a YML file. I am directly copying and pasting this. Then we will change that. ok so i have the yml details let me i will explain here again eureka we are mentioning this is client client in the sense we are mentioning this is the micro service page register true service url service url means this is the service url of eureka server so that it will register directly in eureka and the host is localhost port you can change any name I am keeping here 9090 if you want lets write trip48 ok and mention the provider name means this service name so I mention here payment service any name you can give here with same name it will register in your ureka server now let's run it you can see the log here it's run on port and register in instance payment service so let's check in our dashboard whether the instance is register or not yes it's register this is the some warning message as I am doing this in some other host so this is the payment service the name which I mention in this ML file payment service same syntax with same service name it registered in this eureka server this eureka server we name it as amazon server okay registered replica this is this we mention in yml file and this is the most syntax we need to follow because this is 8761 is default port for eureka server so now let's develop the client and from client we will access this payment service instead directly hit this payment service we will talk with this amazon server let's see how to do that so let's create the payment service so let's create a springboard project we will name it as like assume from Amazon we are going to invoke payments gateway like payment service so I am naming it as Amazon shopping okay client client service here i am changing the package to client okay here also i am adding only web and eureka discovery we don't want to make this as a server so i am not adding eureka server only amazon server this name service i want to make it as eureka server so that's why i added this eureka server in other case we need to add eureka discovery and added dev tools so that in every change it will restart automatically now create it okay this is the amazon service shopping this is the client so annotate add the rate enable eureka client okay now write a controller through which we will invoke this payment service using the rest call okay so create a package write a controller class name it as shopping controller okay annotate at the rate rest controller now write a method public something written string like in book payment service okay so here we need to pass the price and that we want to pass from request url so annotate at the rate path variable okay and this is also get method so write get mapping mention the url what will mention the url amazon payment okay now to invoke this payment service from amazon shopping we need to use the rest template so this is the resource and this is the client so first we need to inject the rest template as a bin in our client application so create a bin here public race template template return new race template okay at the red bean This is the java based configuration approach where we need to annotate at the rate b in configuration. bin and there may be a chance multiple user can multiple resource can access our micro service so better to annotate at the rate load balance okay so it will take care the cluster things so now inject here the template another red auto add okay so now directly return normally without just forgot about the eureka things normally if i want to access this payment service from this shopping amazon shopping what we need to do we need to pass like this string url equal to http://localhost.html in which port you deployed this payment service port number 8848 then endpoint URL what what is that endpoint URL let's see this payment provider payment provider then we need to pass the controller URL method level API URL pay now then we need to pass the runtime price okay so for without using the Eureka this is the traditional approach what we previously doing local in which host it is deployed and what is the port we need all the information to access this payment service so as we deployed this payment service already in eureka server we no need to pass these two we no need to bother about the host and the port what we need to pass the same name what we deployed in eureka server this payment service okay so instead of this url we can directly pass the payment service we no need to you Recall the host and port details.
This is the endpoint URL. This must be passed. The rest, the host and port can be ignored if you are passing the service name which registered in Eureka. So this is the URL. Let's try it.
directly i am returning from resource this is the get method so get for object method is there in rest template where you need to pass the url and response type url and response type it will return plain string dot class okay so when i will from my postman when i will this shopping controller with this url initially it will goes to the as per url it will go to the eureka server and find with the service this payment service and if found this service then again it will match the incoming URL what we are sending to the register Eureka server URL if both matched then the request will forward to the corresponding micro service and from micro service using template and as the method is get using get for object method it will get the response and will and it will return to us so again we need to specify this Eureka server I am registered this Amazon shopping as one more micro service so again for that we need to mention the YML file so again I will copy paste this search main resource and we will change accordingly only you need to change the port and service name suppose i am adding the port here this is triple eight four eight nine nine nine four nine and this is the shopping service name it as okay so now from shopping service we will invoke this payment service okay using rest template but directly interact to the payment service we will access it through the eureka server now let's run this application and let's register it in eureka server so that we can access it so first run this application it will throw error i think because this is the path variable and we didn't pass the dynamic expression here this should be price okay now let's as i added dev tools it will restart automatically okay now let's check in our dashboard whether this shopping service is register or not okay shopping service also register and payment service also okay so let's open it in new browser so that i think this warning will not be there okay let it be we register our two services from shopping service we will invoke this payment service so for that we need to register both of them so let's open our postman what is the url you need to pass or 4d strip 49 and this okay so add the url http localhost the port of client is 49 okay and then the URL Amazon payment and the price of a join adding 4000 okay localhost 499 Amazon payment for them let's see payment with 4000 is successful this this payment with 4000 this business we wrote in our payment service okay here we are not writing in client we didn't write any service it's just we are invoking the client using the template invoking the resource using the template okay and whatever response we are getting from this client it's the response is coming from the resource so you can see this is the 4000 suppose i will pass and how quickly the response we are getting it's in milliseconds 45 milliseconds 36, 53 see here the milliseconds as it is balancing the load okay so this is the simple get method we can write post put delete whatever http method we have we can write all and this eureka registry will run then means dynamically like it's kind of scheduler it will run automatically and the same example what i mention in my blog that i developed live here so you can refer this video as well as this blog i will add this blog link and this video and the way we did so if anyone want to do some like use case video if anyone have just let me know i will prepare one more spring cloud eureka with some valid logic that's it about this particular video if anyone didn't subscribe go ahead and subscribe it thanks for watching this video