Service Discovery In Microservices | Netflix Eureka
29:53

Service Discovery In Microservices | Netflix Eureka

CodeSnippet 10.05.2026 568 просмотров 40 лайков

Machine-readable: Markdown · JSON API · Site index

Поделиться Telegram VK Бот
Транскрипт Скачать .md
Анализ с AI
Описание видео
In this video, we learn how Service Discovery solves one of the biggest problems in microservices — hardcoded service URLs 🚀 Instead of calling services using fixed URLs like localhost:8081, we’ll use Netflix Eureka to dynamically register and discover microservices in Spring Boot. We’ll build a real example with: Eureka Server Order Service Inventory Service and understand how services can communicate dynamically using Feign Client and DiscoveryClient. 📚 What You’ll Learn • The problem with hardcoded URLs in microservices • What is Service Discovery? • Setting up Eureka Server • Registering microservices on Eureka • Service registration & discovery • Using Feign with Eureka • Using DiscoveryClient manually • Live demo of dynamic service discovery ⏱️ Chapters 0:00 - Introduction And Agenda 1:20 - The Problem 5:16 - Service Discovery 10:45 - Setup Eureka Server 16:58 - Register Clients On Eureka 22:22 - Feign 25:20 - DiscoveryClient 29:30 - Outro 🧩 Project Overview Order Service ↓ Eureka Server ↓ Inventory Service LINKS: Git Repos - https://github.com/codesnippetjava/ecom-inventory-service/tree/eurekap1https://github.com/codesnippetjava/ecom-order-service.git https://github.com/codesnippetjava/eureka-server.git Service Discovery Spring Docs -https://spring.io/guides/gs/service-registration-and-discovery Microservices Playlist - https://www.youtube.com/playlist?list=PL-bgVzzRdaPgSkWO70qrskTKZCHA5SCai RestTemplate - https://youtu.be/bTywhSbbgok 🔥 Why Service Discovery Matters In real-world distributed systems: Services scale dynamically Containers restart frequently IP addresses change constantly Service Discovery helps microservices communicate without hardcoded locations. 👨‍💻 Who This Video Is For Java Developers Spring Boot Developers Backend Engineers Microservices Learners System Design enthusiasts 🔜 Next Video In the next video, we’ll go deeper into: 👉 Eureka Architecture & Internal Working 👉 Heartbeats & Registry Updates 👉 Client-Side Discovery 👉 Load Balancing 🏷️ Tags service discovery spring boot, eureka server spring boot, netflix eureka tutorial, spring cloud eureka, microservices service discovery, spring boot microservices tutorial, feign client with eureka, discoveryclient spring boot, dynamic service discovery java, distributed systems spring boot #️⃣ Hashtags #Microservices #SpringBoot #Java #BackendDevelopment #SystemDesign #SoftwareEngineering #DistributedSystems

Оглавление (8 сегментов)

Introduction And Agenda

Welcome to another video of code snippet and in this video we are going to look into another important topic inside microservices that is service discovery. It is very simple yet very powerful concept inside microservices. So let's get started without wasting any time. This is going to be another fun video. So sit back, relax and enjoy the show. All right. So let's quickly jump into the agenda of this particular video. So service discovery in microservices. So very simple agenda. So first what we'll do we will look into the problem that we face with the micros service communication. So if you remember in the last video we have seen fain client before that we have seen rest templates. So this is basically a way of doing synchronous communication between microservices. So if you are yet to look into those video I will recommend you go ahead and check them out because they will help you to understand the synchronous communication between microservices and how two microservices communicate between each other. So first we will look into the problem that our friend service discovery is trying to solve. After that we will jump into what exactly is service discovery. After that once we understand the problem and how service discovery will help us. We will go ahead and jump into setting up Ureka servers. After that we'll register and discover microservices on Ureka server and we will have a quick demo. So that is basically the simple agenda of this particular video. So let's get started.

The Problem

Now let's first look into the problem. So if I go to canvas over here then we have been looking into two services right. So first one is basically our order service and second one is inventory service. So order service is sending a call to inventory service in order to check if the inventory is available for that particular product. Inventory service will send the response to order service and return if the inventory is available. Let's say very simple flow and this is basically the path of that particular API to check inventory. So this is basically the path. How about the URL? The URL of inventory service is we are maintaining it inside this particular order service. So, so inside the configuration of this order service, we have the URL of inventory service. Let's say localhost whatever, right? 8081, let's say. Now, this is the URL of inventory service. Now, we have been maintaining it inside the order service. We have hardcoded it, right? It is basically a hard coding of this particular URL. So what order service will do it will just take this particular URL up in this guy and call this inventory service. Pretty simple stuff right? That is something we have seen. Now if you remember if we go inside code now this is basically my inventory fain client and here if you see this is basically the URL of this particular inventory service and our fain client is basically making use of this URL. So now this URL is hardcoded inside ecom order service. Now what is the problem that this particular hard coding is going to create? Let's see tomorrow if the port of this particular inventory service changes. Now this particular service is now listening to 80082. But now order service is not aware of this particular change inside inventory service. It will still try to go ahead and hit this 8081 and it will just go ahead and fail because this particular inventory service is not available on 80081 port. That is basically the problem. Now, now if you change the port over here or any URL over here, you need to go ahead and make the change inside this particular configuration in order service as well. Now, this is basically a very tight coupling. So that is basically the problem because your order service must know the exact location of your inventory service in order to call it. So that is basically one problem we face because we hardcoded this particular URL. After that, let's say there are multiple instances available of this inventory service. Now let's say I have another instance over here and one more instance over here. So we have in total three instances of this particular inventory service. Now which URL this guy is going to call? There are multiple instances then we need to register all three and choose one between them. Right? So that is basically a overhead. What if you have scaling implemented over here? So let's say this is a dynamic scaling based on a load on this inventory service. It is going to scale. Now let's say you have one instance now the demand is going high so it scaled another instance but your order service do not really know what is the URL of the new instance that we have on which port it came up order service do not know that and dynamically we cannot really figure out we cannot really change it dynamically because it is happening real time so when we implement scaling this hard coding of URL again becomes a problem apart from that let's say you have your application present in different environments ments. Now let's say this is basically dev, this is basically your stage and prod environment. So we have different environments and each environment will have its own URL. Now you'll need to maintain the URL of each environment. Again within that if something happens and it moves to other port or again scaling related problem happens in respective environment you need to handle that. So the problem is the URL present inside order service which will help order service to call inventory service is hardcoded making this configuration very tightly coupled

Service Discovery

inside order service. Now if you think logically how we can solve this problem right what we can do we can simply go ahead and add a simple let's say another application let's say spring boot application. So this is basically my spring boot application. Now what this spring boot application will do, it will go ahead and register the URLs of your services. For example, let's say order service. We are going to go ahead and add the URL of order service over here. So it is going to be let's say a simple mapping. Let's say it is kind of a map, very simple map, right? very simple map which is maintaining your order service and its URL. Again, it will just go ahead and maintain the URL of your inventory service. Now inventory services again will go ahead and register themselves. So the URL is available inside this application. Now let's say this particular service is being scaled. Now new service is being created. What this service will do? This service will just go ahead and make an kind of API call to our new spring boot application. And what it will do? It will just go ahead and tell the spring boot that dude I am the new scaled service of inventory service. Just go ahead and add my address over here. So what it will do it will add another URL for inventory service. So it will just maintain a list of URLs now and it will just go ahead and add this particular service inside this particular URL. Now let's say this particular service is being descaled. So now it will just get removed. This particular connection will also get removed. Now before going down this particular service what it will do? It will call this particular spring boot application and it will tell this spring boot application that dude I'm just going down. just remove my URL from your list and that's it. Now whenever your order service wants to call inventory service, it won't directly go ahead and call on this URL. Now this URL we do not really have. Instead what order service will do? It will just go ahead and call this particular spring boot application and say dude give me all the URLs that you have related to inventory service and it will just take this particular URLs and choose one of the URL between them and call that particular service. So very simple use case right we have created a very simple spring boot application to handle all this we have a few APIs and stuff but instead of we doing this thing spring boot already did this thing for us spring boot have this particular concept in place and they call it as service discovery this particular concept of adding this particular application is called as service discovery very simple stuff right and we have tools available to create such servers And to handle all these things to register these inventory service, order service, exposing these URLs when people can go ahead and register and dregister themselves is already in place. You just need to use that particular tools which are available and in order to do this service discovery tools such as Netflix ureka is available. So Netflix ureka is basically a tool which we can use inside our spring boot application to manage all this that we have just seen. So you don't really need to go ahead and add respective controllers and manage this everything over here. Everything will be done by Netflix ureka. You just need to add the dependency inside this new spring boot application and you are done. Now this concept is called as service discovery. This is basically your server. So this is basically service discovery server and these are basically the clients. That means the applications that are going to register the microservices on this particular server are basically your clients. How simple is that? It is very simple concept right very simple concept we have just discussed the problem and we have solved it as well with the simple spring boot application and as we have solved it spring already have tools to support this particular solution and that is something we are going to look into very simple stuff nothing complex very simple stuff basically and similar thing is basically given on this particular document so if I go on top of this document then we have service registration and discovery so this guide will walk through the process of starting and using the Netflix Ureka service registry. So this is basically now service registry. Netflix ureka is basically a service registry. This particular application is going to be our server and these applications are client. Very simple stuff. So if I go back over here then we have discussed problems such as port mapping changes. If there are scaling then there will be issues type coupling hardcoded URLs all these are problems and which will be easily resolved by using service discovery. So service discovery allows services to dynamically register themselves and discover other services. That means here all these particular services inventory service can register themselves and order service can discover the URL of these services at runtime. How cool is that? Very simple stuff. And that can be done by our friend Ureka. So we can just go ahead and create Ureka server. Set up Ureka servers over here. What is ureka server? It is going to be another spring boot application. Very simple spring boot application and all the other services are going to register on that. And inside the spring boot application instead of we handling all these APIs and communication and saving of things. Netflix ureka will help us to do that. Now let's try to set up this

Setup Eureka Server

particular server. Now if I go back to code then you might remember that we have couple of services. First is basically order service. Another is basically inventory service. So these two services we already know but these are going to be our clients right? If you go back then these are these two are basically our client. Inventory service and order service are our client. But what I want server now. So I want to set up a ureka server. So what I will do I will just create a new application. So what you can do you can just go ahead on spring initializer and let's say group name I will give code snippet and artifact I will give urea server so this is basically spring boot application that I'm creating I'm going to use java 21 I will use yl configuration and the dependency what you need to add is if I try to search ureka over here then I will just go ahead and add a ureka server so springcloud Netflix ureka server. So you can just simply go ahead and add it and generate it over here and you will get a zip file. You can just open it inside your IntelliJ. What I am going to do is I will just go ahead and create a new project from here and this is going to be my spring boot application. So you can just go ahead and create it from your intellig itself if you are making use of a ultimate edition. If not you can do it by using this particular initializer. Simple stuff. So here I will give it a name as Ureka server. After that I will choose Java and Maven project code snippet ureka server Java 21. And I'm going to make use of a jar. And let me just say YAML over here and say next. After that dependencies what did we add? Ureka server. So this is going to be my ureka server. And I will just say create and I will open it inside the same window. And that created our order service. So let me just open my order service as well. We need that one as well. So I will say new window over here. So now we have ureka server, inventory service and order service. Let me move it over here so that it's in sequence. This is our ureka server. This is our order service and this is our inventory service. Now let me just go to pom. xml over here. And what do we have? Let me zoom it a bit so that it's clearly visible. This is going to be my Ureka server. If I scroll down, we have this particular dependency. What is this dependency? Spring cloud starter Netflix ureka server. That means this guy is going to be my server. Also, if you see over here, we have this particular dependency management which is basically Spring Cloud dependencies. So, this Netflix ureka server is basically coming from Spring Cloud. It is a part of Spring Cloud. We have a simple dependency which will make this guy convert this guy to a ureka server. And here you don't need to do much. What you need to do, you just need to go inside application. yml and add few simple configurations. For example, if I go back to this particular page and if I scroll down over here, then start a ureka service registry. How you can do that? First thing you need to do is add a annotation on top of your main class that is enable ureka server. Now if I go back and let me find the main application class and let me just annotate this guy by using at enable ureka server and let me just import it. Now the moment I add it and whenever this particular application is starting what this application will do it will initiate the beans which are required for ureka server. For example, if I go back over here, where is our diagram? Huh? So, in order to do this communication, this guy needs to expose few API calls in order to save this particular URL inside this particular application, it needs some services, some beans. All those things will be automatically added inside this particular service. When we add this annotation, you don't need to do much over here. Simple stuff. After that, once we add this, what is the next step? You need to add a few configuration. This is basically the configuration you need to add. So, if I go back and uh if I go to application. yamel. So, first of all, I'm going to add a server. So, 8761 is basically the port of your Ureka server. After that, you have Ureka client. Now, this is going to be my client configuration. Now I'm saying that register with Ureka falls over here. Fetch registry falls over here. Now this is basically the configuration for your client. Now this is not our client. Ureka server application So it need not go ahead and register itself with the Ureka server or it did not go ahead and fetch any registry. So I'm making it as false over here. But when we do the configuration inside our client, this particular configuration is necessary because it needs to go ahead and register itself with the URA server. Same with your registry this particular because order service needs to fetch the registry from this particular URA server and use it. So I'm going to make it false over here. Now what I will do? Let's try to start this particular application. Let's see what happens. Now it is doing everything. And what exactly is happening? Completed initialization. Tomcat started on 8761. So what we can do? Let's try to load it on our URL. Local host col 80061. And let's try to load it. There we go. What do we have over here? We have spring ureka. What did we do? We just add one single dependency. And our ureka server is up and running. So all these things that we wanted to do to solve this problem Netflix urea already did that by using a single dependency and single annotation very simple stuff these people are making our life so easy you don't need to go ahead and do it on your own you just need to add a dependency if I go back over here and this is basically our spring ureka and it is kind of a service registry and here if you see instances currently registered

Register Clients On Eureka

with ureka no instances available What we need to do, we need to go ahead and register our instances. These clients we need to register over here. So let's try to do that now. Now what we need to do inside our inventory service, we need to add a dependency. Dependency of what? Client. Let me just copy this particular dependency. Add it over here inside our inventory service. So let me go back to dependency over here and add it. And yes, your doubt is correct. We don't need server over here. Instead we need a client and let me try to reload this dependency and let it kind of resolve it. There we go. So our dependency is basically resolved. Apart from that what we need to do we need to add few configuration inside our service. So we just need to go to application. yamel and add few configuration. What configuration we need to add? So the configuration that we just discussed when we were looking into this particular server, right? So I will just go ahead and c copy this and make it true over here inside this particular ureka. So I will just make it true. And this guy also I will make it true. It is not needed. It is by default true once you go ahead and add the dependency. But I'm just adding it over here so that we are aware. Now what I will do I will just go ahead and stop and rerun this application and let's see what happens. There we go. Our application is started and service started and discovery client. Do you see these logs? Registration status 204 that is that means it is successful. It is registering the application with Ureka with status ID up. So if I go back and try to reload this guy then now you will see that this is basically the application and the name is unknown. We don't really want unknown over here. So let's go back and let's give the name of this particular server. This server do not really have any name. So what we'll do? Let me just go to this particular service over here and spring application name we will just copy and give the name to this particular server. Do we already have spring? So I will just add it over here itself. So this particular application name I will add it over here and give it a name as ecom inventory service. And that's it. Let me just remove it. We don't really want any duplicate. This is going to be my ecom inventory service. And now let me try to stop and rerun. There we go. Now ecom inventory service is basically registered. And let me go back and let's try to restart this guy. Now you will see that a name is coming just fine. Ecom inventory service. And the URL of this guy is also coming and the status is also up. Now one service is basically registered. So our inventory service is registered on this particular URA server. Now let's go back and do the same stuff with our friend order service. So what again what we will do let's go back copy our dependency from here this particular client dependency I will just go ahead and copy and add it to our order service so where is it so this open fane is again coming from spring cloud we are adding one more which is basically Netflix ureka client and again the same configuration we can just go ahead and add it over here it's not needed but I'm just adding it over here so that you can just go ahead and play around it and I will just go ahead and add it here and that's it. Now what we will do we will just try to restart this particular application. Now here if you see discovery client ecom order service is basically registered. So if I go back and try to reload our ureka server then you will see that the two services are registered over here. Cool stuff isn't it? How cool is that? Very simple and very cool stuff. All these things will be handled by this guy. Now we have registered both of our services to service registry. Now here we are not telling our services where exactly my ureka server is. So we are not giving any URL of this particular URA server. But now still it is working. We are still looking at these two services over here. How my clients are automatically figuring out that this particular server is present. That is happening because of the default port. So this particular ureka server 8761 is basically the default configuration. But if you want to change that, if you don't want to run it on 80071, then what you can do, you can just go ahead and add a service URL over here. You can just add this particular URL. You can say service URL default zone and this particular URL. After that same configuration, I'm going to add it inside our inventory service. Now if we try to restart both these services, let me just go ahead and restart them. Let's see what happens. And now if I go back and try to reload this particular page, then you will see our services are still registered and up and running. So that is how you can make sure that you are pointing to correct service registry. This is basically the default configuration. That's why it was working before as well. But if you have a different URL for your Eureka server, you can maintain it over here. That is how you can maintain different configuration for your different environments like dev stage and prod environments. So that is basically another configuration that we can maintain. Now if you remember when our

Feign

order service is talking to our inventory service what we are making use of a ve client. So if I go inside our friend order service then what do we have over here? Let's go through it. Now here we have seen rest templates. clients and we have seen this is rest client faint client. So this inventory client is our faint client. If I go inside it uh it is basically a fain client. Where is it? This is inventory client is a fain client. This is basically interface fain client. And here we were giving a URL. What we will do? Let me try to hit a simple API. Right? We will see what happens. First I will just go ahead and add some inventory inside inventory service. We'll just add a number. After that we will try to place order. Now order placed successfully. But now it is using the same URL. It is using this particular URL. What if I go ahead and remove this URL? So I've restarted the application. Let it come back. Now does this guy have any information about inventory service API? No, it does not. It don't know where the inventory service is. Now if I send the request you will see that it will fail. Now here if you see it says no servers available for service inventory service. It is trying to call the inventory service/ inventory/1 but it is not able to because it does not find any service by using this name. And that is true right because inside our registry the name of our friend inventory service is ecom inventory service. And the name we are giving over here is just inventory service. So what I will do, I'll just say ecom inventory service. And let's start this application. Now our application is back up. Now if I reload this, let's see if both of them are up. Those are running. Now let's try to send the same request. And this time it worked. And now if we go ahead and look at logs, let's see what do we have. Let let's go from the beginning. So after initialization, what it did? It try it is trying to get the inventory and in order it is calling this API ecom inventory service ecom inventory service/ inventory / one with this correlation ID and stuff it is trying to call after that post API is also being called by using this particular URL so now what is happening over here now your f client if you don't really give any URL over here it will automatically go and try to fetch the URL from your urea server. So it is not able to find any URL. So what it is doing? It is checking if any Netflix ureka server is available. So what it will do? It will just get the respective URL from this particular ureka server and call the inventory service. So you don't need to manage anything when it comes to fain client. It will automatically go ahead and fetch the URL from your URA servers. But when it comes

DiscoveryClient

to our friend this rest template, it does not really know that. what we need to do, we need to give this particular URL. So first what we need to do, we need to get the respective URL and then send it to this particular rest template because rest template is old stuff and it will not be able to do that. But if you still want to do it, you can just go ahead and inject a bean inside your order service. For example, if I go back, then what do we have? We have this discovery client, right? So you can just go ahead and inject this guy. For example, let's try to do that. So what I will do I will just go ahead and add this particular discovery client. Let's try to import it. Add it to our constructor so that it becomes a parameterized constructor. We can just say discovery client dot get instances. And here inside instances we can just give the name of our friend ecom inventory service. That means I want the list of instances present inside ecom inventory service. Now what I will do I will just go ahead and restart our application in debug mode. Let's see what happens. And I will just go ahead and add a debug point over here. Now let our application come up. It is started. And now what I will do? I will just go ahead and send this request. Now if you see now here if I proceed and let's check what is present inside instances. So here if you see it has one element which contains the instance of this particular service and it will also contain the URL of this particular service health check URL everything is basically present over here uh address of a service everything is present now what we need to do we just need to get one URL from this particular instance so what I will do I can just say instances dot let's say I will get the first one so I will just say get zero basically and take it in a instance and after that I will just say service instance dot get URI. So there should be a method to get a URI which is basically my URI and this URI now can be used inside your rest template or rest client wherever you want. This is how you resolve this particular URI by using discovery client. But F client makes it very easy that we have seen it automatically handles stuff. You don't need to do that because here if you see if you have list then we are taking the first one right but we may need to do load balancing over here. So you manually need to handle load balancing and everything which is a problem because if you get zero then request will always go to one instance if you have multiple instances you need to handle the load balancing from here but that is something automatically will be done by our f client. So that is basically you can simply set up your Netflix urecast server and register all your services over here and use that particular services to fetch the URL of other services. So the problem that we were facing initially is basically simply solved by using this particular urecast server. Very simple stuff. So if I go back over here we have seen the problem in the beginning. After that we have seen what exactly is service discovery. After that we have successfully set up ureka server Netflix ureka server basically after that we have seen register and discover microservices on ureka and we have seen the demo as well. So very simple stuff we have seen. So in this video we have seen how we can simply solve the problem by using by introducing this Netflix ureka server and we understood how we can set up this server how they will be able to communicate between each other by using fain client or our other clients like rest template or rest clients. But that's not it. We need to look into many other aspects. We just got it working and understood the big picture but we want to jump inside the details of it. We still need to understand the internal working of it and that is something we will do in the part two of this particular video because this is already a big video and

Outro

if you don't want to miss that subscribe to code snippet right now. So that is basically it for this particular video. If you like the video hit the like button don't forget to subscribe to code snippet. Share this video with your friends so that they also have understanding of service discovery in microservices. That's it for this video. See you in the next video.

Другие видео автора — CodeSnippet

Ctrl+V

Экстракт Знаний в Telegram

Экстракты и дистилляты из лучших YouTube-каналов — сразу после публикации.

Подписаться

Дайджест Экстрактов

Лучшие методички за неделю — каждый понедельник