Microservices with Spring Boot & Spring Cloud | Live Example & Source Code 🔥🔥🔥

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome back to my youtube channel i'm omar kumar hope you guys are doing great in this pandemic in this video we are going to explore nothing but spring cloud micro services framework along with real time demo for each components mentioned in this diagram let me give you a quick heads up about these components we are going to write multiple micro services during this entire session and we will start with first component and that is spring cloud config server spring cloud config provides server and client-side support for externalized configuration in a distributed system with the config server you have a central place to manage external properties for applications across all environments like development qa staging production etc second important component in spring cloud framework is eureka discovery server eureka server holds the information about all client service applications every micro service will register into eureka server and urika server knows all the client applications running on each port and ip address next component is ribbon load balancer we will explore this component and understand how it can balance the load on the same micro services running on different ports later i will show you how you can achieve the same through urika server itself next is zul api gateway like any other api gateway it handles all the requests and perform the dynamic routing and delegates the request to external micro services it is also known as edge server next component is spring cloud gateway spring cloud gateway is a non-blocking gateway means when using non-blocking api gateway a trade is always available to process the incoming requests these requests are then processed asynchronously in the background and once completed the response is returned so no incoming request never gets blocked in case of an unblocking api gateway next component is zipkin which is used for distributed log tracing as you all are aware microservice architecture involves multiple services which interact with each other so a functionality may involve calling to multiple microservices it uses spring cloud sleuth to generate and attach the trace id span id to the logs this can be used by zipkin and elk for storage and analysis another related component is active mq i will show you with real use case how you can configure activemq to get streams of logs from different micro services and how zip can uses this locks for distributed tracing i would also demonstrate to integrate spring boot actuator in your micro services to monitor and manage application usages we will use open fin for any api to api communication i will cover error handling in microservices and i personally believe it is really important part of entire microservices and java ecosystem we'll also quickly show you to configure swagger for api documentation i'm sure there would be a lot of takeaways for you after watching the entire session as i have briefed you about all these components shown in the diagram let's talk about the prerequisites and environment details you need to have one java standard editor like eclipse or spring tool source or any other java supported id for writing and executing microservices regarding jdk i'm gonna use open jdk 14 version i'm going to use spring 2.3.5 release you will also need postman tool for executing apis here is the port details used by our components i am going to use port 888888 for spring cloud config server i am going to use default port 8761 for eureka discovery server for zoola api gateway service i'll use port 8762 likewise for spring cloud gateway server i'll be using port 8763 for zipkin i'm gonna use default port nine four double one i'm going to create three micro services in this session first one is property file access service this is going to use port eight one double zero second service is product stock service which will use port 8200 later we would configure multiple instances of this micro services which will use port 81018102 and so on next is product enquiry service this service will use port 8300 let's start with the first use case now and this is to use a spring cloud config server configured with git repository for accessing external property files for this i'm going to create one simple micro service named property file access service which will try to read the environment wise property files stored in github but before we create service let's create a spring cloud config server let's begin now let's create our spring cloud config server so i'm using intellich idea as editor i can select the option called spring initializer as you can see here you can select multiple jdk versions as i mentioned earlier i'm going to use jtk 14 just click on next by the way in eclipse also you have similar feature called spring initializer in this screen as you can see there's a group artifact and this language selection the package java versions and all so i usually start by putting groups com dot take effects dot micro services since we are going to create a spring cloud config server we can say take effects spring cloud config server i'm going to use a type maven project packaging is jar java version i'm going to use 14 i'm going to keep version as it is here click next in this screen as you can see you can select your own spring boot uh i'm going to use 2.3.4 because we are going to write a microservice for a spring cloud config server we just need two components one is the web component and other is spring cloud config server component just two components that's it project name is going to be the same and say finish project is getting initialized takes a little time to set up the entire project structure the framework now the editor has loaded the project so you can go to src main here you'll find one mail class the class name is take fx spring cloud config server application which contains a main method so this is going to be the default you simply need to add one more annotation and that is enable config server that's it you don't need to do anything else out here now go to resource you will find application.property you can use the same file name but i always prefer to use yaml file so i am renaming it to yaml let's give the application name and set the gate to repository detail as well as this server port information giving the spring application name in yaml is going to be like this you so let me explain you what i have done over here so i have given the spring application name which is exactly the same what you can see in this root folder name the second thing what i have done is i have mentioned the git url cloud config server git and this is the uri and the last thing is server port as i mentioned earlier i'm going to use port 8888 so let me just give you the summary we i have created a spring cloud config server and annotated with enable config server that's it and this main class then i have renamed application.yaml file where i have inserted spring application name and have also put the configuration for our git uri this git uri where i have kept my property files so this is hosted on github with the tech effects environment variable repo and if you go to clone this is what that clone uri i have mentioned over there as you can see currently this repository contains three files which are nothing but a property file one is a default property file and this is what it is and the name is take effects property file access service dot property it doesn't have any any uh suffix later so this we call it default property file this file has got two key values one is property file dot name and the value is something like github new change one more time some some values and second key is property file description which also has some values likewise we have to more property file specific to environment so this property file the suffix by hyphen qa and here is the name names are same and but the values are different so qa and given environment property likewise if you go back there one more property file with the suffix of dave so that specifies that this this belongs to development properties and here the key is going to be the same but the value is changed so now instead of qa it is showing dave so i'll exactly tell you how to access this this property files while i will write microservice for accessing this file for now let's get back to intelh so this is what we have written is this very simple cloud config server now let's execute and see if it runs properly or not as you can see target folder has been created if you look at palm dot xml you will see this the version 2.3.4 dot release and the group id and the name of this cloud config server if you remember we had selected two dependency one was the web and second was spring cloud config server as a dependency other than that the tests and the other dependent plugins are going to be the same one very important plugin is spring boot maven plugin you would require this plugin if you want to run or execute your microservices out of that id environment for example from the command prompt if you want to run you will need this springboot maven plugin as we can see the spring boot is initiated and now it's loaded as you can see the tomcat server is up on the port 88888 so this was so easy to create a spring cloud config server you must be wondering uh i have not configured anywhere about the tomcat but it says starting service tomcat this is the beauty of spring boot that it is integrated with tomcat as an application server so you don't need to configure or you don't need to separately explicitly integrate that tomcat it comes as a bundle now let's create our first microservice and that is property file access service in our previous step we had created spring cloud config server and we had configured spring cloud config server to run on port 888 let's create one new microservice which will communicate to spring cloud config server to access property files kept on github repository let's begin for this microservices we are going to have three dependencies first one is spring boot dev tools second is spring web and the last and most important is config client since this micro service is going to communicate with spring cloud config server to access property files it is necessary to have config client dependency injected in our microservices i'll show you in the form okay now project is initialized and loaded as you can see there are microservice empty project structures are created through spring initializer and this is the default project file which has a main method gets created second important thing is the application property file which is as of noise empty so let me rename this to yaml now let's come to pom.xml as you can see this artifact id and this is the version 2.3.4 dot release the name is going to be the techfx property access service it has got a first dependency called spring boot starter wave second one is spring cloud starter config this is what we had injected while we were creating a micro service and the third dependency is spring boot dev tools the other dependency will be the same another important plugin which i had mentioned during spring cloud config server as well you need to have this plug-in spring boot maven plugin if you want to run this micro service from out of your id environment for example is like through command prompt with spring hyphen run command so you will need this so now let's begin to create the micro services business logic since this micro service is going to communicate to spring cloud config server and then will access the property file we would need to create three main java classes one is the controller through that controller you will access your create and access your endpoint second thing is a very plain java beans which you can call is value object and third and most important is environment property component so let's create one by one as you can see in environment variable repo they are going to be a multiple property file each property file has a name suffix and prefix so if you'll open one property file the name is take effects property access service dot property it doesn't have any suffix so you can call it as a default property file i'll explain you the importance of this default file let me just quickly brief you about what this property file contains so as you can see this property file has got two keys values one is a name and second description followed by you know uh there's prefix of property file this prefix is very much important to know you have to define this prefix in your configurator so this name and description are the keys and the values could be anything here i've just made some some value like github new changing one more time and all you can have any any kind of a string value so we have name and description as our variable so going back to intelh we'll create two variables you so now i have created two simple java bin one is a value object which have private string name and description and i have created one constructor with the parameter of these two values along with the getter setter and the two string object i have created for this value object now i have created another bin with the same name and description with the getter setter but if you'll see here i have added couple of additional annotations one is a component annotations means there's a class as a component i have inserted here and second is configuration properties this configuration properties says that is going to check all those keys which has prefix of property file as you can see here so all the keys which will have prefix with property file will be red that's it so now from the business side is done now since we had not provided any values to application yaml so now let's let's provide the microservice name to the yaml for that spring so we have defined application name in yaml and the port on which we are expecting this microservice to run that's it so now let's run this microservice and see so now let's run this microservice and see what it does so as you can see spring boot got initiated and tomcat is initialized on port eight one double zero now he's saying starting and then then finally you see the tomcat is started on it one double zero the couple of things let me explain you like what happened if if you look at the line first line fetching config from server at 888 this is what it was expecting to connect but unfortunately it says connect time out exception on url this because if you look at in our passive step we had created but that service is no longer running that we are talking about spring cloud config server service it's no longer running so what we'll have to do we'll have to open spring cloud config server this we had created now let's run this server so as you can see no active profile set but still tomcat initialized on port 888 and now it's running by exposing two endpoints that's actuator i'll talk to you about that later now let's focus on the stormcard is running on 888 right so if you go to your property service you will have to just rerun this service and see what happens now if you look at the log file it's again saying fetching config from server and this time it says located environment take effects property access service with the profile default i'll explain you this the meaning of this but for now just check out the other things so tomcat is running on 81 double zero so now we are set we are done so now let's open postman so let's if you remember the url localhost eight one double zero slash access slash so just for cross check so just open up your okay now let's create our controller that's it now we have created two beans simple java bins that's property access value and second is property access beans and now we have created property file access control we have also created application yaml file which has spring application name and the server port on which this microservice will run earlier we had tested this micro service by running on port 8100 which was successful now since we have added all those required classes now let's restart the microservice so as you can see here is able to fetch the config server on localhost 888 with the default uh environment profile that's default and so tomcat is now running on 81 double zero now let's go to postman and say access property file right this is what actually we had created so just to ensure just copy and go to this and hit enter look what happens as you can see it is able to fetch property file and its key value name is github new changing one more time this change and the description is this now we are able to access property file so in next step we will see how we can access this property file dynamically so if runtime if any property file changes we are able to fetch that change to value how we look into our next step welcome back in our previous step we had created spring cloud config server then we created micro service which were communicating to spring cloud config server and were able to fetch property files from github to your postman as i mentioned there are three property files how did this micro service determined that out of these three file it has to access take effects property access service dot property so if you look at this is the property file it accessed we had not mentioned this regarding this property file anywhere in our microservice but still microservice was able to access this default access service dot property i'll tell you what happened inside so if you go back to intellij if you go to application.yaml i have not mentioned any default so what this service says that if you don't mention any profile active profile then it will take the default property file now this time let's put that profile active detail here what you have to do is simply you have to say profiles and active so dave so remember we had three type of profile one was the default second day when third was qa so i've just mentioned dave so now this time let's restart this micro service if you look at is fetching config server on localhost 888 is locating the environment but still if you look at the profile is default however we have mentioned profile active dave but it's not taking let's find the reason why it is happening now the tomcat is starting on portrait one double zero so let's go to postman and run this again you will be surprised to see still it is facing the default value the reason is this micro service is not reflecting the profile environment for that what you have to do you have to use actuator that actuator will refresh the value so if you look at our palm we have not used actuator over here let's insert the actuator dependency so what you have to do is just copy this and say spring boot starter and say actuator and you have to save it now let's restart this service what this actuator does if you are aware actuator can provide lot of information about the micro services for example like a health information the information about the the end points and many things and many more things one one more advantage of using actuator is that i can show you how you can utilize this actuator to refresh the end point so let's see so we have restarted our service but still if you look at profile is default so now i'll show you how you can use actuator to activate your profile which you have mentioned in your application yaml file profile active dave so if you go to this thing and say send again you'll find the same value if you want to refresh if you want to get updated value you will have to use http it's running on localhost colon 8 1 double 0 slash actuator slash refresh right and you have to use the post oops you will have to add we missed to add management endpoints include and say refresh we'll talk about more values like refresh health info and all little later for now just focus on this that include skull to refresh now restart this service so service is started i'm going back i'm accessing this again it still has not reflected with the property file so going to post http localhost 81 double zero actuator slash refresh and say send says property file dot name and property file dot description and if you go to log file now if you look at it says debut property default active set spring dev tool now fetching and now if you look at profile equal to dave located environment name take effects property access service profile equal to dave this is what actuator has done so now since the following profile are active day profile are active if you go to your get endpoint get method and say send now look at the name equal to dave and description is a dev environment property so now we have seen that now we are changing our environment variable as per the profile what we have set active profile what we have set in application.yaml file now let's quickly check one more thing now i'm going to that day profile and i'm editing this value say like dave new and i'm saying expecting changes and i'm saving it now if you go back to end point and say send you still get old value the new values are not getting reflected if you want a new values what you have to do is you have to go back to post refresh and send it means whenever you want to get new values you will have to use actuator refresh but in real life scenario this is not possible every time right so i can tell you there are many approaches by which you can get dynamic values one of the very rude way of getting dynamic value every time whenever you are accessing your property file by using a refresh command in your business logic itself so let me show you how we can do that for that you have to go to controller and let's write one refresh actuator method so public void you you so let me brief you what i have done in this refresh actuator method so i have used rest template object and i have put a base url this base url value i'm going to give exactly what i had used here next i have created http header object in that header i have set content type application json next i have created http entity object where i have inserted here is value to this entity and finally i had created a response entity with a string type where i had set rest template dot post for entity base url comma entity comma string dot class the reason is this is string.class because here we are expecting the string that's it so now you can use this to your call refresh actuator now let's restart the service now once service is started you can go to postman you don't need to run now this explicitly whenever you will try to hit the end point for accessing property file what you will do it will send request to actuator for refreshing the service and every time you will get a refresh value so now this try out so this time i'm saying editing new again and one i'm saving it now if i'll access this method you will see now it's checking updated value i have shown you how you can create a microservice which can communicate to a spring cloud config server to access the property multiple property files based on your environment setting in your application.yaml file if next time if you're changing to qa or if you're not putting any values like this then it will access a default else whatever profile you will active here so that based on this profile your environment property will be read that's it for this session in this section i am going to demonstrate you how one micro service can talk to other micro services and how netflix ribbon load balancer can be used to distribute the load between multiple instances of the same microservice running on different ports later in this section we would also understand how we can configure eureka naming and discovery service and how we can register all our services i would also demonstrate how eureka naming server can be acted like load balancer for multiple instances of the same service to demonstrate we are going to create two micro services first is product stock service this service will store product information and the second service is product inquiry service which will eventually be used for fetching product data from stock service now let me brief you about service structure as i mentioned product stock service will store productive related data like product name price stock availability discount offer if any available on this product since this service stores product data we would certainly require some database isn't it i'm using h2 database for demo purpose you can use any database like sql or nosql i am using here spring jpi repository for data persistence coming to product inquiry service this service will primarily use to get the product information from stock microservice through api call since both the micro services will be used together there are quite similarities in the field variables like product name price etc however there are couple of additional fields are in inquiry macro services like unit and total price for calculation purpose as shown in the table both the microservices will use some comma libraries like devtools web actuator eureka client etc along with couple of additional libraries like you know h2 database and ribbon load balancer it is important to know that we are going to use fan client instead of a rest template for api to api communication in inquiry microservices through proxy client interface i would demonstrate you while we will program it end of this section i will demonstrate how we can load balance multiple service instances of stock service through ripple load balancer and eureka naming service i will also show you how we can use zool api gateway proxy server and spring cloud gateway server to route the api to api call let's get started now let's create product stock service so here is the group i'm defining com.techyfx.microservices.productstockservice this is the way i define my package you can define your own similar way the artifact we had i'm defining take effects product stock service this will become the name of that micro service the description and clicking next here you have to select a spring boot version and then i'm taking i'm selecting the deep tools and the web since i'm using h2 database i'm selecting spring data gpa nh2 database i'm going to use ereka discovery client uh in future so i'm just integrating that dependency we'll have to wait for some time map and build is interview happening now if we'll go to main class so you'll find one spring boot application let it be like that i typically prefer to use yaml so i am renaming it to application.yaml you can keep as application.property as well so here i'm going to define the application name take effects product stock service and the port i'm going to define now as i mentioned earlier on that data sheet i'm going to run on port 8800 okay so now let's first create bean package as i mentioned earlier in microservices structure sheet that i'm going to create a product fields here that like a product name product price product availability product discount offer to start with is id this is going to be a initial that unique id so i'm keeping as long you can keep as a string it's up to you product price i'm keeping as a big decimal product availability as a string i'm keeping discount offer as double because in future there are possibilities like okay 20 dot 5 or something in some some decimal value and i'm also defining port number which i will take it later uh as for now just just keep it as it is remember we had mentioned that we are going to run stock services the multiple copy of stock services this is what that port this will help us to identify what on what port that particular instances are running so just for reference purpose keep as it is now creating the constructor with entire fields whatever we have defined there creating a getter setter as well and finally i'm just creating a two string which is basically the overheading of the two string in in case if we don't need to print that output now here i'm creating another package name entity so whatever field we had created uh in product stock bin we can copy the entire fields and we can replicate same here for the entity remember i am not taking a port i'm just taking a any any fields related to product not related to any environmental variable so to create entity class you have to give the annotation as a spring entity and then we are going to define all those fields so that id will be represented as a persistent id so in any spring you can define annotation a product name will be taken as a column in the product stock table and here i'm trying to map the column name with my fields this is going to be the table structure so when we will write sql statement we will ensure that the column what we are defining here should be same in that statement as well uh so we are done now we have defined mapped so we are done now we have mapped all those fields with database column now we can create a data setter for all those fields entity fields and then one more constructor that is going to be the empty constructor we are done so now let's create jpa repository file which is going to be interface extending to jpi repository this interface is going to extend jp repository i'm going to put two parameter here one is the entity class name which is product stock and its primary key if you remember we have kept id as a long so this is what we need to put here we need to define one more find by value where i am trying to little bit customized here because i just want a product name and availability search so rather putting all those fields i just need a composite kind of a field like i'm going to search with product name and availability so we have created this find by product name and product capability now i'm going to create a data sql as i told we'll have to create a insert statement because we are using h2 database so just for a demo purpose what i am doing when when we are loading the when we are sorry when we are executing the microservice at that time only we are creating in memory database so this is the table name product stock and basically a sql statement where we are saying okay map id product name product price product availability and product discount offer and just some some sample values i'm going to put so these sample values what we are saying okay id mapped to 1 0 0 1 for product name as it is string mapped to say like bat or cricket bat basically and the cost is going to be the 5000 rupees inr so this is what actually a numeric value have put again the availability yes or no so this again is a string and then finally the discount offer which is double so it's without the quote so let's create some 323 sample data so when we will run this microservice so this data will be loaded at that time so i'm just trying to copy paste the same and we will change the you know id values so i'm just trying to copy paste the same value two to three times i will change the id values along with that that product information as well so first row was for bat the price was 5000 for ball i'm just putting the price of 500 rupees it's just an identical value so you can put anything in your data the whole objective is to find when we will run this microservice we should pitch this data when we apply through the postman so i'll show you that that thing so i think you're almost done now the only thing is left is the controller so let's create a controller package so let's create a very simple a single controller that get mapping [Music] method since it's our controller we'll have to put rest controller annotation going to auto wire our product stock repository if you remember we had created later we will use this object for fetching the value from repository and pushing it to our entity so i'm going to create one method return type is product stock bin say like that check product stock method which will take two values that's path parameter the product name and product availability so this we will be passing through our query string so whoever api will be accessing product stock service we will need to pass these two parameter product name and product availability for now just complete this we are just doing a request mapping so get mapping here i'm doing check product stock product name then we'll be taking as a variable some sort of values and the product availability again that variable so now let's create a entity class entity object basically and map this entity object with a repository so i'm just passing product name and the product availability so if this matches with our database if this name and ability will be matched through the query jp query that data will be paged now since this is entity bean we'll have to map to a normal java bean or you can see the value object so this is what we are going to do and so we will map to will map entity into java bin so remember we have we had created a constructor with arguments so this is what actually we are trying to map those we are taking product stock get id product name product availability next is going to be product discount offer so get product availability product discount offer and finally and finally let's put the port detail so for putting a port detail we'll have to do uh we'll have to auto wire the environment class through that environment class we will use a property to access the port details so let's create a environment class object so here environment dot git property so here environment dot get property and inside the property we'll have to put local dot server server.port so this will give me the port information where this microservice is running so we are done so we have created been we have created entities we have created repository we have created the data and we are loading this data when we are running this microservice and now the time has come to run the microservice and let's see how we are going to paste the data so one more thing just we missed that uh so uh basically is here in the palm let's let's revise that okay this jpa then wave since uh at this time we're not using eureka client so i have disabled it later we will enable this while we will configure it with error card naming and discovery service so let's add one more plugin that's my one compiler plugin let's change the java version to 14. okay cool so we are all set now let's execute this microservice this is the beauty of spraying that tomcat comes as an embedded tool so you don't need to configure anything regarding tomcat and here you can see the tomcat is now running on port 8800 without any problem so now let's open postman and run the api so since we are using localhost 8800 uh it was lowercase so i'm just changing to you let's quickly cross check about that and just quickly check about this path product name is going to be if you remember this product name is going to be bad and the ability what we are checking is that it should be available right so now okay cool so it is fetching the data now you can see when we search for bat and the ability yes the data matched with database and now it is showing this this value so we are able to see the id the name the product price availability discount offer and along with the port detail so now we'll create another microservice name product enquiry service this product inquiry service will be accessing product stock service for inquiring about the product now let's create product inquiry service to start with let's create a group first this is the way i do create my group com.techyfx dot microservice dot my micro service name so here in this case it's a product enquiry service the artifact which is the name of the microservice i'm going to create take effects hyphen product hyphen inquiry iphone service packaging is going to be jar java version i'm going to use 14 but since it's not there i'm going to keep 11 itself later on we'll change description product inquiry service and then you can click next here very first thing i am going to select the spring boot version then dev tool web since i am not going to use any database for inquiry i'm not going to select this so eureka discovery client just for dependency per se i'll just enable this and now one more important thing is open fiend so we are going to use open fin client for api to api communication as you can see this is spring cloud routing uh product enquiry service will communicate to product stock service through open fiend and here we go so clicking on finish will take little while to bring that project up so i think this is loaded so now we can go to sarcy main my main class as you can see is spring boot application that's it let's not touch anything on this in this class now open application properties i do prefer to rename this as a yaml file but you can keep as it is now open the ml file and try to put the micro service name over here for that you can use spring.application.name so here if you remember we had named this microservice to take fx product inquiry service the same i'm copying here now let's assign the port so if you remember 8 7 double 0 was report we had specified in microservices sheet before the time we were talking about microservices structure so that's it two things spring application name and the server port is enough now let's create bean package in this package we are going to create java bean a simple you know value object [Music] we are going to create one simple java bin class product inquiry bean this bin will contain the filled descriptions like you know the product name the product price is availability discount offer very much similar what we had now defined in product stock service class sorry product stock service microservice so earlier in microservice product is earlier in product stock microservice we had defined if you remember uh id product name product price product availability discount offer since this microservice will access product stock service a couple of more attributes we will need to define like you know the unit number of unit means like a number of product and is total cost so this is what we have additionally defined here along with the port specification now let's create guitar setter for for entire field objects and create a constructor as well argument construction argument constructor and finally we will create two string method as well so we are done for java bin definition since inquiry service will access stock service we'll have to define proxy or the client interface through that the fiend will communicate all right so now let's create another package called client you can also name as a proxy client or something let's create another package called client because inquiry service will communicate to product service through fin client so fee needs a proxy or a client interface to be created where we will map the api call so product stock client interface is created this interface will have detail about product stock service api call the end point the end point of product stock service so let's copy the end point from product stock service and paste over here so let's copy this endpoint from product stock service and just paste as it is here you can again copy the function the method signature from product stock bin and copy it here only things you need to change is the return type because he's in query bin you will have to change the return type to product inquiry main since his interface you just need to create a signature out here now let's now add fin client annotation to this interface class so to enable fin client annotation you can use fin clients give the name of this fin client so to enable finn client you will have to give the annotation to enable the fin client at the red fin clients and the name so here the name of your micro service so it's a take effects product stock service remember this is the name we have given for stock service and one more entry you have to do is here you have to enable the fin client here with the package name the package name it could be the entire you can copy the entire package from from this file and you can put it here so you can copy the entire package and put it inside enable fin client as a string so we are done here so we have enabled the fin client by putting the notation in our main class now let's go back to proxy client interface again and let's define the fin client let's put in the double quote and here we go so we have set up the fin client proxy what we are saying that we are enabling through this entire package and we have created client and that client is now communicating to product stock service now finally let's create a controller package since this is separate micro service there should be there should be an entry point because it's a controller class you will have to give the annotation to rest controller you will have to give notation of rest controller then you will have to give the entry point mapping and since this controller is going to access the fiend client you will have to auto wire by giving the reference product stock client and now let's create a entry point for inquiry service so user will be accessing entry point of inquiry service and internally inquiry service will call will make a api call to stock service through fin client since its inquiry service that the return type should be inquiry bin and now let's put entry point mapping because it's the inquiry service you can have a product hyphen inquiry then the name name says about the product name and the argument again i'm using the argument as a name and then second argument is going to be the availability this is again about the product availability if you remember in our product stock service also we were having two parameters product name and availability since this inquiry service we are going to have couple of additional fields like unit the unit specifies the number of product you are looking for then the count you are looking for and that's it so uh name availability and unit we are taking as a path variable um both are as a string but if you look at the unit is going to be the integer path variable int unit now we have this three variable now let's pass this variable to fin client through client proxy so for doing that we'll have to create a proxy we'll have for doing that we'll have to create a object of product inquiry bin and then we can call through client the check product stock remember this is what we have created inside our client package so this is what we are calling this what we had created right basically this this with this signature we had copied from stock microservice to inquiry microservice so you just need to need to call a single iron statement and this is what the rest the fin client will do for you so now product inquiry bean has the data about product stock service so now let's map this value to the product inquiry bean constructor so that data gets mapped from stock service to value object foreign because we are going to calculate the price based on the number of you need of the product you will have to do couple of now you'll have to define couple of variables to get that you need multiply by the product uh know that the price so for that what i'm doing i'm just creating couple of variables a local variables like a double total price the total price will be a gate total price multiplied by the number of unit this is what actually we are trying to do so extracting get total price from product inquiry bin and then multiplying by unit the unit is the you know input passed by user similarly for calculating the calculating similarly for calculating the discounted price you will have to extract first discount offer and then you'll have to create another variable where you will be you know manipulating it know that that percentage amount to the price amount so this is what the discounted price we are saying discounted price equal to so this is what we are now calculating our discounted price the discounted price equal to total price minus total price multiplied by now i mean to get that percentage out from this so now we have total price now we have discounted price now let's set this entire value to our java bean so little tedious because we got so much values here is going to be the name name is nothing but a product name then next is product price it should be product price next is product price it should be product price not product total price after name it should be product price not a total price so now this is corrected one there's one comma is missing so yeah so comma is missing next will be the product availability which were passed as a path variable next comes product discount offer then the unit the number of unit and then discounted price so remember we had calculated this discounted price and finally the port detail about where stock service is running so now let's review the maven palm [Music] as you know that we had taken the dependency of web since we are not using netflix utica client as of now so let's disable that the open fin we are going to use this is spring cloud starter open field and then let's put one more plug-in of that compiler plug-in so this will enable you to you know compile and run anywhere not not within the id environment you can once you add this maven compiler plugin you will be able to run this spring boot application as an application anywhere so here as i had mentioned you that i'm going to use java 14 but since that 14 version was not enabled while we were creating the project so here we have to manually change now let's review all these classes and interfaces so before we run product inquiry service since enquiry service is going to communicate to product stock service please let's ensure that stock service is running so now we have run the stock stock service now let's go back to product and query service so stock service is running now let's go back to inquiry service okay so now yes we have done all the setup now let's run and see cool this is the fin client name and this is what we had copied the signature and the entry point from stock microservice and this is what we have defined in the controller so what we are saying that when the request comes to the controller through this mapping [Music] path this method is getting called this method is anyway calling to client proxy and it you know maps the product stock object to product an inquiry object oops something went wrong so it looks like okay we had missed one thing yes so we had missed one very important thing because we are not using any load balancer or not using any kind of you know naming registry service so here you will have to specify where your product stock service is running so along with the the host name and the port you have to define manually we are done here so now tomcat is getting up and cool so now it's running now let's check through postman and see whether it's we are able to access the entry point of inquiry service and see if an enquiry service is able to access data from stock service so as you know that enquiry service is running on port 8700 so this is the entry point we need to specify the name of the product the number of units and availability and here you go so cool we have got all the information the id is there product name is there product price is there availability is there discount is there unit is there but here you see the total price is zero must be something wrong like in our calculation let's go back to inquiry service and let's see where we have done something wrong please open the now we can open the controller yes this is here we had done wrong so instead of get total price we will have to call the gator of get product price get product price and then convert into a double then multiply by unit so i had wrongly given the getter name so yes now let's rerun the inquiry microservice okay again execute this url okay now here you see all the data are coming as per our expectation cool so we are done so far we have learnt to create basic spring boot micro services and how through fin client we can call other micro services entry point for understanding this we created two micro services one is stock service and other is inquiry service and we were able to make api call from inquiry service to stock service through fin client now in this section we are going to learn how we can run multiple instances of stock service on different port and how we can enable spring cloud's ribbon load balancer in inquiry service to access multiple instances of stock service for doing this let's create another instance of stock service as you can see here one instance of stock service is already running on port 8800 for creating another instance in intellij it is quite easy to run multiple instances of the service with different port for that you have to go to edit configuration option here there is one option called copy configuration once you copy configuration you will find another instance in that list so go and change it to 8801 or whatever name you want to give since i'm going to run another instance on 8801 port i'm just specifying the name and the next thing what you have to do is on vm option you have to specify the port so for express import you have to do so through hyphen d option you can specify the server dot port equal to 8801 apply the change and ok so now you will find two instances in the drop down so select the second one as you can see here an option is enable now for 8801 just run it now you can see another tomcat distance is up and running and here you can see tomcats started on port 8801 to ensure whether both the ports are working or not you can go to postman and check so i'm here in postman as you can see i'm just sending the data so yes i am getting a response from port 8800 and this is what actually port equal to 8800 is specified here now since we are adding on another instance let's change the port and hit now you can see the response has come from 8801 as well when we are changing the url so this ensures that both the ports are working now next step is let's enable spring cloud ribbon load balancer in inquiry service and for doing that go to inquiry service as you can see here earlier version through fin client we had specified the url hard coded url because we had only one instances running so now since we are going to enable ribbon load balancer we will modify this this class as well and so now step one go to pom dot xml and here specify the dependency for even load balancer so this is the display uh dependency so this is the dependency i'm talking about so uh the artifact id as you can see spring cloud starter netflix ribbon so now as you can see it's resolving the dependency yes dependency got resolved and now we are ready to execute through the ribbon the second step what you have to do is you have to go to interface the product stock client interface as i told you that earlier version we had to specify the hard coded port over here so now just copy this line and remove the url from this line so this much change you have to do and this interface now go to application yaml and in application yaml you have to specify the ports so for doing that what you have to do is you have to take the name of the service go to application yaml and annotate it so what we are saying this is what we are saying take effects product stock service this is the service name and then we are saying ribbon and then we are saying list of services this is the syntax so as you can see this is a syntax you have to put the ribbon and the list of servers equal to these are the two instances you have to specify one is 8800 another 8801 now let's see what are the changes we have done then we will run this service so first thing what we have done we have added the dependency of ribbon here step number two we had made changes uh of inside the fin client annotation we have removed the url and instead of putting the url here we are putting the url in our application yaml and this is the third change and once this change is done that's it you have to restart your service now the service is getting restarted cool so now you can see the tomcat started on port 8700 now let's go to postman and here let's try to run this option so localhost 8700 the same same url earlier we used to hit now if you'll hit again you will be able to see the result so that result has come from port 8800 next time when you are hitting this or that couple of times you will find this ribbon is balancing the load between two instances here you can see next time when we are hitting the same url the response is coming from 8801 port so this way so look so every time when you are hitting the same url in postman you are finding the responses are coming from both the instances one by one so ribbon is working this way you can run as many instances as you want you simply need to put the entry inside the application eml file and ribbon will take care for the rest but you must have observed that even this solution has got some limitation in terms of you know as and when you will create you will add or remove the extents you will have to go to the micro service and make the changes right you definitely would not like to do in real time environment kind of a production environment right so how you can get rid of it it's quite simple you can do it through eureka naming and registry server how i will demonstrate you in next section i am going to demonstrate you how you can configure eureka naming and registry server for our both the microservices and i will also demonstrate you how eureka naming server balances the load between multiple instances of stock service without enabling or configuring anything extra to the service so let's get started so you have to create eureka server first for doing that in intellige you have to go file new project and here you can select the spring initializer uh since i'm using java 14 i'm just selecting this option you have to specify the group so so in group i have specified com.techfx.microservices.takefx eureka naming server the artifact id is going to be take effects your recon naming server i'm just going to keep this language and packaging as it is later section i'm i will update the java version to 14 i'll show you how click next so even though if you don't select all this thing that's fine what you have to do is go to spring cloud discovery and just select eureka server and yeah i specify the spring boot version because i am creating all our micro services which is spring boot version 2.3.6 um just picking that version project name is going to take effects eureka naming server inside this location i'm clicking finish it will take little a while to load because in background intelligence will resolve lot of dependencies will create the project structures and all so yeah it's almost done so as you can see here this is my main class for enabling eureka naming server you simply have to go and annotate enable eureka server let's go to palm and check the entire dependencies as i had mentioned you i'm going to change this java version to 14. here you can see the dependency and last but not least because i always add this plugin to my microservice this allows me to run microservice from anywhere even out of the id environment through command prompt through command prompt you can run however this is just an optional if you don't want to specify don't do that it's not going to there is no dependency of it so second thing is go to application.property i am renaming it here specify the application name specify the port this is the default eureka port 8761 i'm going to keep the same just save it and we are done with irica naming server so now let's run this service now open any browser you can see when you type localhost colon 8761 you will find eureka is up and running and is saying currently instance register to be required there's no instance is available there are some instances detail ip address and status is up so now because so now since eureka server is up and running we need to register our micro services to eureka server so let's start with stock service first and then subsequently we'll go to enable inquiry service so go to palm first and here if you remember we had disable the eureka client dependency so now time has come to enable it so i'm just enabling it in earlier version of eureka we had to specify the eureka default url but now since we are using the newer version we don't need to specify all those however you can still specify but those are optional now so i'm not i'm not going to do any changes over here in earlier version we also had to specify discovery client annotation here as i mentioned because now it's no longer mandatory it's all optional so just let us a background noise so um yeah so the earlier version we had to specify the discovery client there was a notation we also don't need to assist we also don't need to specify any annotation over here so what we have done so far we have just added the dependency of eureka client and i'm just stopping both instances so just add the dependency and you're good to go so i'm starting my stock service one by one so this is the first instance i am restarting and this is the second instance so now if you go to eureka web interface you can see there are some entry the very first entry is eureka naming server itself um it shows is up and running on 8761 the other entry if you can see here that available zones are two and up two instances so the first instance is 8800 and the second entry is for 8801 so now stock service is registered to eureka now let's add inquiry service also to eureka so to enable inquiry service to eureka you have to go to pom.xml just enable this dependency save it as i had mentioned in earlier stock service we don't need to enable we don't need to put any entry and we are good to go now if you rerun this now i am restarting the service once it's started we'll go and check in web interface of eureka as you can see here see discovery client it's got discovered right and the services started so now go to eureka and refresh this so now you are also finding the product inquiry service is registered now as a next step because we have enabled eureka naming service earlier we were executing two instances of stock services and we had configured ribbon as a load balancer in inquiry service to fetch the data from both the services now we just need to remove all those ribbon setting so very first thing what you have to go go to service yaml and remove this entry so here i'm disabling it i'm commenting it out i'm going to do i'm going i'm going to product stock client interface here you don't need to do anything and just keep as it is this is absolutely right so what we are saying the fin client is going to talk to stock service and this is the name of the service and in from application yaml we have removed that and we are expecting now we are expecting eureka to find the instances whatever instances that stock services are running so whatever instances stock service has that eureka naming server will understand that and it will serve data for you so now we have restarted both the services so time to check through the postman you simply need to execute this and you can see here data is coming from port 8800 so next time when you're hitting it is now coming from port 8801 so cool so let me recap whatever we have covered in this section the first thing we have created eureka naming server and specified the port to run on 8761 and we registered both our services to eureka naming server and we restarted the service and when we restarted we were we are able to fetch the data from both the services let me show you another thing even if you create another instance of your stock service say like okay i'm going to now i'm going to edit configuration and i'm adding one more instance of it and this time i'm running on 8802 so here i'm spacing the port 8802 and i'm applying i'm clicking okay i'm running this so so now three instances of stock services running one is on eti0 another is on 8801 and now this is the fresh one which is running on 8802 so now if you go to postman so sorry uh first let's let's check in the eureka web interface so now you can see there are three instances of product stock service are running on different ports so now if you go to postman and now if you are trying to hit so initially it takes some seconds like 10 or 15 seconds once the server is still completely stable it will start yes so now you can see 8801 8802 and 8800 so now you can see all these three services are up when you're hitting first time it's eight eight zero zero second time it's coming eighty zero one and this time it is zero two so whatever instance you create that eureka naming server takes care of that and balances the load between that so that's it for this section so in next section i'm going to demonstrate you how to enable routing services through zul api gateway server and spring cloud gateway server which is called non-blocking api gateways in this section we are going to learn to configure zul proxy server and spring cloud gateway server to enable routing service on enquiry service to access the inquiry services endpoint to do so let's first create zool api gateway service i i've opened new project selected spring initializer let's give the group id so what i have done i have specified the group package com.techfx.microservices.cool api gateway service given the artifact id name given the artifact id name which is going to be the microservices name as well i have kept the language and packaging as it is java version 11 later on i will change to 14 as i have done in my previous services as well description i have specified and that's it here i have selected the springboot version again 2.3.6 directly go to spring cloud routing and select zool as you can see zuul is now in maintenance mode almost like you know deprecated um spring cloud is trying to recommend you to utilize spring cloud is trying to re spring cloud is recommending you to use non-blocking api gateway and that is a spring cloud api gateway which anyway i'm going to touch base in next section just enable couple of packages like dave tools and wave as part of the practice so here what we have enabled is spring dev tool spring web and zool api gateway we'll take a little while to you know enable all those dependencies so now it's ready so if you go to pom.xml here you can see if you go to pom.xml here you can see netflix zuul dependency is enabled along with the deep tools and web as usual as usual let's add the compiler plugin now go to src main java and open the main class here you have to enable zoolproxy once you enable this go to application property as i have mentioned you earlier i always recommend prefer to use i always like to use yaml file so i'm going to refactor this to the yaml and one more thing you can even change this application to bootstrap so as per the preference bootstrap property file or yaml file takes gets the first preference so i'll show you how this works so i'm changing this time bootstrap.yaml file refactor which is same like your application amalfi just matter of setting preferences so now give the application name take efx zulu api gateways server specify the port you can specify any port 8765 anything whichever you like okay and now you have to specify the routing detail so before we split the routing detail let's first run this service and see whether it's up or not so here i'm running this service while it is running you may also go back and specify the you go back and specify the netflix client so you will be able to see whether zuul is registered or not for doing that you have to enable the dependency of your client inside the palm so here i'm adding once you have the dependency you got to restart the service it takes a little while now the services started when you go to eureka refresh that now you can see take effect zool api gateway server is added so now eureka knows that we have registered will appear gateway server so if zoo api gateway is going to access any services um naming server will know that and will will help to sub the request so now let's go to yaml file now let's go to your file and as i had now let's go to yaml file and as i had mentioned you let's define the routing so for defining the routing you have to apply this command so we are done so what we are trying to specify that we are saying that this is this is routing id and we are specifying the enquiry service endpoint that the whole let me explain you the whole whole objective of enabling zul proxy is we are going to run in query service not directly with 8700 port as we have done so far right so now going forward we will be accessing inquiry service through zul proxy say like through 8 7 6 5 port number so to enable that we need to specify the routing so for product in so here i'm specifying the name the route name and says anything which starts from product inquiry if we are specifying anything let's redirect to localhost 8700 so this is what the instruction i'm given so now let's save it and restart it so now you can see service is up and running so go to postman open the postman and as we were accessing the url of product inquiry through the 8700 this will still run without any problem and it will be able to access the data now let's copy this entire thing and paste it here select a new thing so here what we are changing the port number we are going to use the port number of our zool gateway proxy so for using this pro you have to specify the for doing this you have to for accessing this we have to additionally specify the for for doing for doing this for using 8765 you have to again repeat for accessing inquiry service through 8765 you have to additionally specify the service name so in this case we are saying take effects product stock product enquiry service and so look now we are able to access the now look we are able to access the inquiry service endpoint through joule proxy so as you keep on hitting use you are seeing that it is now redirected to different distances so now 8802 is responding next time when you again hit it's 8801 and so on so this is what actually we have done so far that we have created as well api gateway server inside that zoolo api gateway server we have specified the proxy uh we have to inside the zool api gateway server we have a specified zool routing and since we have single uh end point for product inquiry this is what we are specific this is what we are specifying here in case if you got multiple you may provide the multiple routing name and url so that's it here and we also have enabled uh eureka client with zul api proxy um and that is the reason it is now registered here we can see a zool api gateway server is registered to eureka and then we were able to access the inquiry service endpoint through 8765 which is zuul api proxy port okay that's it for now i just want to highlight one thing that this is just an introductory session for zoolopia gateway there are really a lot many things which can be performed through any api gateway and this is really a very vast topic as i am going to cover almost all the spring cloud components in a single session it is very difficult to deep dive into each component right what i will do i will create component wise separate session where i will give much more insights about all those components and and upload it to my channel okay now let's come to the topic and create spring cloud api gateway service which is also called non-blocking api gateway and for doing that let's create a service now to configure spring cloud api gateway you have to create a new project specify the group specify the artifact id so so this same artifact id is going to be the service name as well specify the spring boot version select the default dev tool now go to spring cloud routing and select this gateway so we got only two dependency one is spring boot deep tools and other is the spring cloud api gateway next and what you're saying is finish so our spring cloud api gateway service project is created now i'm going to src main java open the main class and okay everything is fine let's go to application.property let's rename this application.property to yaml file let's open it provide the spring application name specify the port now let's run and check let's specify the netflix eureka client dependency we missed to add that so let's see uh spring cloud starter gateway is added spring starter netflix eureka client dependencies added devtools dependencies added one more plugin let's let's add here regarding the compiler plugin so all these dependencies are added now let's restart the service so we have restarted the service okay it's getting up the couple of things let me highlight you as you must have seen the other services we're using tomcat to start the service but here you can see spring cloud api gateway uses neti to start the service so this is the difference i wanted to highlight you so next step is go to application yaml and add the routing so to add the routing what you have to do is spring cloud you can select spring cloud gateway routes so this got added on top gateway routes and then specify the id it could be anything here since i'm going to access stock in query service so here i'm saying stock and query and now let's specify the uri remember we are running stock inquiry on port number 8700 this is what we have specified now let's now let's specify the predicates tickets specify the prescription path specify the predicates path product inquiry this is what we have to define so since we have single entry point for stock inquiry service and so this is what actually we are defining we are saying that for this route this is the id and this is the uri so if anything comes from this path product hyphen inquiry then please redirect to localhost 8700 with the this predicates that's it so you can now rerun this service once the service is up we can go to postman and try to execute the uri through eight nine double zero yes okay the service is up now here so what are you saying updated port two at nine double zero on the on the native port and one more thing let's go because we have added the dependency of eureka client so let's go to eureka and see whether this dependency is added or not so as you can see here take effects spring cloud api gateway is up with port 8900 now go to postman so here let's copy the entire url here let's go to new and paste out here instead of eight seven double zero just write eight nine double zero and hit the service now you can see that we are able to access product in query endpoint through eight nine double zero api gateways routing if you keep on hitting this two three times all these instances values will be fetched so this time this time the response is coming from 8801 again executing now response is coming from 8800 and when i click again and again now it is coming to 8802 the reason because we have run three instances of stock service so the api gateway is working with routing enablement i just want to add one more thing so far we have seen how we have enabled the spring cloud routing for inquiry service and we were able to access the inquiry service endpoint through spring cloud api gateway right so as you can see this postman we executed this postman endpoint by using api gateway url and we are able to get the data from all those instances of stock service for doing this we configured the routing service in application.yaml file additionally i will show you how we can enable routing through other way around like by using configuration file both are the same and it's just matter of choosing the option now let me demonstrate you how we can do it through config file for that let's create a new package called config so right click to that main package and now i'm creating a child package inside so now let's create a new class you can name anything so for like i'm naming it like cloud routing config since this is a configuration file uh just annotate it with the configuration and primarily this file will have one method and this method time will be a router or route locator so we will be uh writing one method with a return type of route locator and the parameter of route locator builder so i'll show you how we can do this so i have used route locator as a return type and given the parameter of router builder through this builder just add a route inside this right route inside this route use a predicate use the path so remember we had used the path exactly as an application yaml so the same path we can use so i'm copying this path from here and pasting it in a path and now let's go to application yaml copy the uri just put it inside the uri give the id id you can again copy this id from here stock inquiry and finally build this so that's it this is the way you can build it out if you don't want to use through yaml file so you you will need to create a class this class will have one method written type of route locator with the argument router get a builder through this builder you need to create a predicates exactly same what we are defined in application yaml this predicate will have a path the uri and the id that's it so now for exiting this you will have to disable you'll have to comment out just save this restart the service okay service is started go to postman and execute the same oops there's one mistake so you have to define the bin here we missed to define the bin so restart it so now service is up go back to postman and hit the same url you are getting the data right so this is the data we were expecting so from all these three instances so now let me recap so we discussed about how we can configure routing uh first way is to do it through application.yaml and second way is to write a config file with the help of route locator welcome back in this section i am going to demonstrate you how we can integrate zipkin as a distributed tracing in our microservices architecture that helps collecting timing data needed to troubleshoot latency problems in service architectures we are going to configure zipkin distributed tracing server with the help of silhouette and rabbit mq components salute is tool from spring cloud family it is used to generate the trace id and rabbit mk will be used as a data collector in this case there are a couple of prerequisites for configuring zipkin first one is rabbitmq server itself and i assume you already have downloaded it from revitamk.com website and have configured in your environment likewise you can download latest zipkin.jar executable file from zipkin.io website since i am using mac os and i have installed rabbitmq through homebrew command at user local has been location now let's follow steps to enable distributed tracing so the very first thing what you have to do is you have to open a terminal and run the rabbitmq through this command so user local has been revitamq server this will start a rabbit fq server in your machine now it shows okay starting block broker so rabbit mq server is up and running next step is you need to execute zipkin.jar file so for that open new terminal since i have downloaded my zipkin.jar and my eclipse workspace you have to apply this command um so you have to so you have to provide the rapid mq uri since i am running rabbit mq in my localhost so the url would be like amqp colon localhost if you are running on some some remote host you need to provide the ip address for the same the next section of command is java java iphone jar this you require to execute any jar file right so here i'm applying java iphone jar zipkin server and this is my zipkin version 2.23.0 hexagonal.jar so now it shows that zipkin is running and this is the url so we are almost done let's enable the zipkin in our microservices and for that open the microservices pom.xml and add these three dependencies so i'm going to add these three dependencies to my first micro service which is product stock service so here i have opened palm.xml file so down here you can integrate so i have added these three dependencies so first one is spring cloud starter sleuth second one is spring cloud sleuth zipkin and the third one is spring cloud starter bus mqp you need to add these three dependencies and you are done once you have added these dependencies you don't need to do anything we have added these three dependencies and we are done for this service now let's start the service so i have got three instances of this service first instance which is running on port number eight eight zero zero as you can see second instance i'm running on port number 8801 and third one is i'm running on port number 8802 so i will restart the service one by one so i am now starting 8801 service now i am going to 8802 and starting this service as well when this service is getting started we can go to now product enquiry service and make the changes so this is the product inquiry service here also we will have to add the dependency for zipkin and solute so i have added these three dependencies again and now let me refresh this dependency now start the service as well so now i'm starting it i'm going to add dependencies to our third microservice which is spring cloud api gateway service so so this is my spring cloud api gateway service i'm going to add these three dependencies over here as well so other than adding dependencies i have not done anything i have just added the dependencies and i'm restarting the service now let's go to our browser and see whether the service are open up or not so we can see here eureka naming server is up and running on 8761 likewise the product inquiry service which is now up on port number 8700 and there are three instances of product stock service which are running on port number 8800 8801 and 8802 so now let's go to zipkin and run the query so still there's nothing because we have not done any kind of activity or we have not accessed any endpoint so now let's go to postman and let's start executing the service endpoint one by one so in first place i'm directly executing the product stock service with this 8800 port so now i have executed this and now i can see that data is fetched that we got response so if i go to zipkin and i run the refresh as you can see here this one entry we will we will see those entry details later now let me execute the services one by one now second we are going to execute the service through our enquiry service so uh which is running on port 8700 so in so we are executing endpoint of inquiry service and internally in enquiry service executes the endpoint of stock service through fan client so now i'm just looking at this as well so as you can see two times i have executed and there are there are that i've got response from two different instances eight eight zero one and eight zero two and now i'm again if you run it you will get the response from other instance which is 8800 now i will go and execute this through our api gateway so here my api gateway is running on port number 8900 now let's execute it okay now let's go back to zebkin dashboard and run the query so look at you you will be able to see there are multiple entries now so let's check one by one so at first place we had directly executed our micro service on port 8800 so this is this so if i click on this show it's it says that we had executed a get method and this was a url check product stock product name so this is the url and if you look at the overall the response time got executed on zero ms and the entire that that from request to response it took 613 ms so if you click on this you will be able to see what exactly happened what time it got that request started and that response was serviced so go to show all and here you can see relative time which is 0ms and this the server finish time is 613. and both at both the place you can see the take efx product stock service was the server is initiator and the server respond uh that he was able to give you the response within 613 milliseconds so likewise if you go back and now let's check enquiry service remember we had executed twice twice or thrice i guess right yeah thrice so this is the first one entry the second entry is this and third entry is this so just just open anyone any one entry so i'm just trying to open the top one and now in this situation you can see there are two entries so product inquiry got initiated at zero ms and the entire entire response was served in 1.844 second as you can see here so executed initiated at 0 ms and sub served at 1.88 ms second line as you can see that that red one it shows that it's in it got initiated over here the meaning of this like the this this color combination is that somewhere very much close to around 650 seconds around 650 milliseconds at that time this service got initiated and was able to complete the journey so now to understand that completion of the journey you can click here okay just a moment okay let me expand it and let me just do it this way so now look this product stock service almost like a initiated at around 842 or 850 ms and this and it took 918.398 ms entire journey so once you click on this and go to show all annotations you will understand what what i'm trying to highlight it says that client start relative time around 8 42 ms and it was initiated by client the client name is take effects product inquiry service okay then the servicing started somewhere i mean like a 1.22 seconds and at that time or take effects product stock service was called and client and the response client has received around 1.78 seconds so it took somewhere you know client initiated the request initiated at 8 42 ms and uh this client finished that that journey around 1.761 second so we can say around it took around one second time now coming to product stock service response so product uh stock service responded around 1.764 seconds so this is what is the journey we are talking about so if you can see if you if you see this that if this is the start point where 0ms is written then the whole journey took 1.844 second so first the entire journey it took from 0 to 1.84 per second and within this time frame product stock service had responded so got initiated around somewhere 800 ms here in this point and around 1.7 second he was able to sub the request back so this is the tracing you can easily you know understand and wherever you find wherever you want to debug your micro services where in real time in real time scenarios where you got multiple micro services and there are chain of requests happens my i'm talking i'm talking about api to api call so suppose you got 50 or 30 50 micro services and in normal scenario each micro service communicates to four or five more endpoints on or the api of different microservices you will be able to trace the entire calling journey and you would be able to understand where is the bottleneck so as as i've shown you uh if you look at the turnaround of product stock service it's his turnaround was faster than the the turnaround of the product inquiry service because look when inquiry service got initiated it took almost 700 milliseconds to initiate the product stock service right so this is what actually you can understand through this zipkin tracing now i'll show you another scenario where we had initiated our request from spring cloud api gateway service so in that case go to this now you will find one three entries right so it shows that okay uh spring cloud api gateway had initiated the product inquiry and product inquiry had initiated the request to product stock so let's go one by one again so what we are saying the entire journey took three seven four dot five to one milliseconds it's entire journey within this entire journey if you look at calling the as a client as a client uh cloud api gateway as a client initiated the request somewhere around uh around 100 ms time so 0-100 ms the api gateway was busy and then api gateway work up and executed the the further as a client as a request to product inquiry service now if you look at the product inquiry service product inquiry service got a call around 200 ms so look look this time lapses so when when we hit through postman we hit at you can say that at 0 ms api gateway took little time to initiate the to to wrap the call and to to get prepared for the call and that initiation time was around 100 ms over here somewhere very much close to 124 so i am assuming is 100 ms then once the client was ready to request the product inquiry service got a call request around 200 ms and it took 66 ms to respond back so product inquiry service took 66 ms within that the inquiry service requested to product stock service at around somewhere 220 and this got respond from stock service around 11.889 milliseconds so this is the journey what exactly we are trying to highlight and if you look at down you will understand uh what what type of the gate or post method was there and what was the url what was the the um i mean that's endpoint detail and uh what all the classes were involved so that controller class was product inquiry controller and then the method was getting query of product so this is what a zip can can help you and other than that you can also put the no filters and you can also you know define the timings like okay since uh in in reproduction suppose your entire service is running since last say like one month and you would just want to do some some debugging for particular time period so you can define that time period over here by selecting the calendar date and time okay so last five minutes so i am running this okay i think it was more than five minutes so if i take last 15 minutes and you get all those responses back one more thing i want to show you like if you click on the plus button you can even filter by with the service name so what what i am saying i just want to see the tracing data only for my stock service so now i selected stock service and then span name i can select uh the method so this was the method and now you see when i run this you will see the data which are from that method okay stock and this yes and if you go and say like okay i'm not this not this so add service name and i'm saying okay inquiry service span name status so now in this case if you go and run you will find one entry within this 15 15 minutes so uh product inquiry service and this was the inquiry uh this thing so this was this is the data so this way you can filter out your uh results so i'm sure this will help you to configure distributed tracing in your micro services architecture for doing any kind of debugging or to understand the entire api to api call journey that's all for now thanks for showing patience to watch the entire session and i am sure there will be a good take away from this session we have covered almost all the major components which are used in microservices architecture however there are still many components which we would deep dive in our upcoming videos i've also bundled the entire source code which we have built during this entire session and uploaded to techyfx github account the github url is mentioned on the screen you can freely download and use it for your practice feel free to reach out to me on my email address mentioned on the screen in case you have any queries or you have any kind of suggestions last but not least please like and share my video and if you have not subscribed my channel yet requesting youtube please subscribe and this really means a lot to me thanks for watching see you until next time bye
Info
Channel: TechEFX
Views: 43,073
Rating: 4.9456244 out of 5
Keywords: what is microservices, microservices architecture, spring boot microservices, spring cloud microservices, spring cloud config server, spring cloud api gateway, netflix eurka naming and discovery server, netflix ribbon load balancer, zipkin distributed tracing, rabbitMQ, spring sleuth, spring boot fraemwork, techefx, amar kumar, actuator, github, open feign, api to api communication, learn microservice, mastering microservice, microservice architecture explained
Id: Z7A_M8HkJG0
Channel Id: undefined
Length: 188min 45sec (11325 seconds)
Published: Tue Dec 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.