1. Learning Outcomes#
इस chapter के बाद आप:
- समझ पाएँगे कि microservices में hard-coded hostname और port क्यों problem बनते हैं।
Service RegistryऔरService Discoveryका basic pattern समझ पाएँगे।Eureka ServerऔरEureka Clientकी अलग-अलग responsibilities समझ पाएँगे।- Spring Boot application में Eureka Server configure कर पाएँगे।
- microservices को Eureka में register कर पाएँगे।
spring.application.nameका service identity में role समझ पाएँगे।- service instance metadata का basic meaning समझ पाएँगे।
- logical service name से registered instances discover कर पाएँगे।
- client registration और registry fetching के बीच difference समझ पाएँगे।
- Eureka Dashboard पर registered services verify कर पाएँगे।
- service-to-service communication में hard-coded network locations avoid करने का reason समझ पाएँगे।
2. एक simple problem से शुरुआत करते हैं#
मान लीजिए हमारे पास दो microservices हैं:
order-serviceproduct-service
order-service को product की information चाहिए।
सबसे simple approach क्या हो सकती है?
order-service में हम सीधे product-service का URL लिख दें:
http://localhost:8082
Local development में यह काम कर सकता है।
लेकिन microservices environment static नहीं होता।
आज product-service port 8082 पर चल रहा है।
कल वही service restart होकर किसी दूसरे port पर चल सकती है।
Production में उसके multiple instances भी हो सकते हैं।
Container या cloud environment में hostname भी change हो सकता है।
इसलिए network location को code या configuration में permanently hard-code करना fragile approach है।
Rendering diagram…
Diagram could not be rendered.
यहाँ actual problem यह नहीं है कि HTTP गलत है।
Problem यह है कि consumer service को दूसरी service की current network location manually पता होनी चाहिए।
Dynamic environment में यही assumption टूट जाती है।
Microservices में service का नाम comparatively stable होता है, लेकिन उसका hostname, IP address और port बदल सकता है।
3. Definition#
Service discovery is a pattern in which service instances register their network locations with a service registry, allowing other services to locate available instances by using a logical service name.
सरल भाषा में:
Service Discovery का मतलब है कि किसी service को call करने के लिए हमें उसका exact IP address और port पहले से याद रखने की जरूरत न पड़े।
इसके बजाय service अपनी location एक central registry में register करती है।
दूसरी service उस registry से पूछ सकती है:
product-service कहाँ चल रही है?
Registry available instance की information देती है।
4. Service Registry क्या है?#
Service Registry को एक directory की तरह सोचिए।
लेकिन यह लोगों की directory नहीं है।
यह running microservice instances की directory है।
उदाहरण के लिए registry के पास ऐसी information हो सकती है:
| Logical Service | Host | Port | Status |
|---|---|---|---|
| product-service | host-a | 8081 | UP |
| product-service | host-b | 8082 | UP |
| order-service | host-c | 8090 | UP |
यहाँ ध्यान दीजिए:
product-service एक logical service name है।
लेकिन उसी service के multiple running instances हो सकते हैं।
यही distinction microservices में बहुत important है।
Service और Service Instance में difference#
product-service service का logical identity है।
लेकिन:
host-a:8081host-b:8082
दो अलग running instances हो सकते हैं।
इसका मतलब:
One Service → Multiple Instances
Scaling के समय यही सामान्य situation होती है।
Rendering diagram…
Diagram could not be rendered.
Registry logical service name और उसके available instances के बीच mapping रखती है।
5. Eureka क्या है?#
Eureka Netflix द्वारा बनाया गया Service Discovery system है।
Spring Cloud Netflix हमें Spring Boot applications के साथ Eureka use करने के लिए integration देता है।
हमारे setup में दो मुख्य roles होंगे:
Eureka ServerEureka Client
Eureka Server#
Eureka Server registry maintain करता है।
Microservices यहाँ register होती हैं।
Server registered services और उनके instances की information रखता है।
Eureka Client#
वह Spring Boot microservice जो Eureka के साथ communicate करती है, Eureka Client की तरह काम कर सकती है।
Client सामान्यतः दो काम कर सकता है:
- खुद को Eureka Server पर register करना।
- registry से दूसरी services की information fetch करना।
Current Spring Cloud Netflix configuration में client starter classpath पर होने पर Spring Boot application Eureka client के रूप में auto-configure हो सकती है; client application पर अलग @EnableEurekaClient annotation लगाना आवश्यक नहीं है.
Eureka Server registry है। Eureka Client वह service है जो registry के साथ register और/or discovery communication करती है।
6. Eureka का पूरा mental model#
मान लीजिए:
product-servicestart हुई।- उसने Eureka Server को अपनी information दी।
order-serviceभी Eureka से connected है।order-serviceको product service चाहिए।
Flow broadly ऐसा होगा:
Rendering diagram…
Diagram could not be rendered.
यहाँ एक important बात है।
Consumer हर business request पर Eureka Server को call करके address लेना जरूरी नहीं करता।
Eureka clients registry information locally cache कर सकते हैं और उसे periodically refresh करते हैं.
इसलिए Eureka केवल "एक URL lookup API" नहीं है।
यह service registration, heartbeat और registry synchronization का पूरा mechanism provide करता है।
7. Eureka Server बनाना#
अब एक dedicated Spring Boot application बनाएँगे जो Service Registry का काम करेगी।
मान लेते हैं project का नाम है:
eureka-server
Dependency#
Eureka Server के लिए Maven dependency:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>Spring Cloud और Spring Boot versions compatible होने चाहिए।
इसलिए random Spring Cloud version manually चुनने के बजाय project के compatible Spring Cloud release train/BOM या Spring Initializr configuration का उपयोग करना बेहतर है।
Current Spring Cloud Netflix documentation Eureka Server के लिए यही starter artifact use करती है.
8. Eureka Server enable करना#
Main application class में @EnableEurekaServer add करें।
package com.example.eurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}यहाँ दो annotations हैं।
@SpringBootApplication#
यह normal Spring Boot application configuration enable करता है।
@EnableEurekaServer#
यह application को Eureka Service Registry की भूमिका देता है।
यानी अब application केवल सामान्य Spring Boot web application नहीं है।
यह registered Eureka clients को track कर सकती है।
9. Eureka Server configuration#
Standalone development setup में application.yml इस तरह configure किया जा सकता है:
server:
port: 8761
spring:
application:
name: eureka-server
eureka:
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://localhost:8761/eureka/अब एक-एक property समझते हैं।
server.port#
server:
port: 8761हम Eureka Server को port 8761 पर चला रहे हैं।
8761 Eureka Server के लिए commonly used port है।
spring.application.name#
spring:
application:
name: eureka-serverयह application की logical identity define करता है।
register-with-eureka: false#
Standalone Eureka Server को खुद अपने ही registry में सामान्य client की तरह register करने की आवश्यकता नहीं है।
इसलिए:
register-with-eureka: falsefetch-registry: false#
Standalone registry server को client की तरह registry fetch करने की आवश्यकता नहीं है।
इसलिए:
fetch-registry: falseSpring Cloud documentation भी standalone Eureka Server setup में client-side registration और registry fetching disable करने का उदाहरण देती है.
Eureka Server और normal business microservice की client settings को बिना समझे copy-paste मत कीजिए। Standalone development server और Eureka client की responsibilities अलग होती हैं।
10. Eureka Dashboard#
Eureka Server start करने के बाद browser में खोलें:
http://localhost:8761
आपको Eureka Dashboard दिखाई देगा।
शुरुआत में business services registered नहीं हैं, इसलिए applications list empty हो सकती है।
जैसे-जैसे Eureka clients register होंगी, dashboard पर उनके service names और instances दिखाई देंगे।
Current Spring Cloud Netflix configuration में Eureka dashboard default रूप से enabled है और default dashboard path / है.
11. अब एक microservice को Eureka Client बनाएँ#
मान लेते हैं हमारे पास:
product-service
है।
इसे Eureka Registry में register करना है।
Client dependency#
Maven dependency:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>Current Spring Cloud Netflix documentation Eureka Client के लिए spring-cloud-starter-netflix-eureka-client starter use करती है.
12. Client में annotation लगाना जरूरी है क्या?#
पुराने tutorials में आपको यह दिखाई दे सकता है:
@EnableEurekaClient
लेकिन current Spring Cloud approach में client starter classpath पर होने पर application auto-configure हो सकती है।
Normal main class पर्याप्त हो सकती है:
package com.example.productservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ProductServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ProductServiceApplication.class, args);
}
}Actual registration के लिए हमें Eureka Server location और application identity configure करनी होगी।
13. Client configuration#
product-service में:
server:
port: 8081
spring:
application:
name: product-service
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/यह छोटा configuration तीन important बातें define करता है।
Service कहाँ चल रही है?#
server:
port: 8081Current instance port 8081 पर चलेगा।
Service कौन है?#
spring:
application:
name: product-serviceयह Eureka के लिए बहुत important है।
यह service का logical identity बनता है।
Registry कहाँ है?#
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/यह client को बताता है:
Eureka Server कहाँ उपलब्ध है?
Current Spring Cloud Netflix documentation में defaultZone case-sensitive map key है, इसलिए इसे ठीक इसी camel-case form में रखना महत्वपूर्ण है.
14. spring.application.name इतना important क्यों है?#
मान लीजिए हमारे पास दो instances हैं:
Instance 1:
server:
port: 8081
spring:
application:
name: product-serviceInstance 2:
server:
port: 8082
spring:
application:
name: product-servicePorts अलग हैं।
Running processes अलग हैं।
लेकिन दोनों का:
spring.application.name
same है।
इसलिए दोनों एक ही logical service के instances माने जा सकते हैं:
product-service
Mental model:
Rendering diagram…
Diagram could not be rendered.
यही service identity और instance identity का basic difference है।
spring.application.name बताता है कि application कौन-सी logical service represent करती है। Port केवल current running instance की network location का एक हिस्सा है।
15. Service registration के समय क्या information जाती है?#
जब Eureka client register होती है, तो registry को केवल service name ही नहीं मिलता।
Instance से संबंधित information भी register होती है।
इसमें सामान्यतः information जैसे:
- hostname
- port
- service identity
- status
- home page information
- health-related URL information
- instance metadata
हो सकती है।
Spring Cloud Netflix documentation भी registration metadata में host, port, health indicator URL, home page और अन्य instance details का उल्लेख करती है.
इसलिए registry केवल यह नहीं जानती:
product-service exists
बल्कि वह running service instance के बारे में network-level information भी रखती है।
16. Custom metadata क्या है?#
कभी-कभी instance के बारे में extra descriptive information भी useful हो सकती है।
उदाहरण:
eureka:
instance:
metadata-map:
team: catalog
version: v1अब instance के साथ custom metadata भी register किया जा सकता है।
लेकिन ध्यान रखें:
Metadata service identity का replacement नहीं है।
Logical service identity के लिए अभी भी:
spring:
application:
name: product-serviceमुख्य configuration है।
17. Registration और Registry Fetching अलग concepts हैं#
Beginners अक्सर इन दोनों को same समझ लेते हैं।
वे अलग हैं।
| Behavior | Meaning |
|---|---|
| Register | अपनी instance information Eureka को देना |
| Fetch Registry | Eureka से दूसरी registered services की information लेना |
Normal Eureka client दोनों कर सकती है।
Registration#
Conceptually:
product-service → Eureka
मतलब:
"मैं product-service का एक available instance हूँ।"
Registry Fetch#
Conceptually:
order-service ← Eureka
मतलब:
"मुझे available services और instances की registry information चाहिए।"
Current Eureka client configuration में register-with-eureka और fetch-registry दोनों properties available हैं; current documented defaults true हैं.
18. Heartbeat क्यों जरूरी है?#
Registration एक बार हो गई तो क्या Eureka हमेशा मान ले कि service alive है?
नहीं।
मान लीजिए service crash हो गई।
अगर registry उसे हमेशा available मानती रहे, तो consumers dead instance को call करते रहेंगे।
इसलिए Eureka instances heartbeat भेजती हैं।
Simple meaning:
Service periodically signal देती है:
"मैं अभी available हूँ।"
अगर expected heartbeat लंबे समय तक नहीं मिलता, registry eventually उस instance को unavailable/remove कर सकती है।
Registration बताता है कि instance मौजूद है। Heartbeat registry को यह समझने में मदद करता है कि instance अभी भी alive है।
19. Client registry fetch क्यों करती है?#
अब order-service को product-service locate करनी है।
इसके लिए उसे registry information चाहिए।
Eureka client registry fetch करके available services की local information maintain कर सकती है।
Current documented default registry fetch interval 30 seconds है, इसलिए local testing में registration या discovery changes हमेशा उसी millisecond पर दिखाई देना जरूरी नहीं है.
इसलिए अगर आपने service start की और तुरंत dashboard या discovery result में expected state नहीं देखी, तो configuration check करने के साथ थोड़ी synchronization delay की possibility भी समझें।
20. Logical Service Name से discovery#
अब सबसे important फायदा देखते हैं।
पहले consumer को ऐसा URL पता होना चाहिए था:
http://localhost:8081
Discovery के बाद consumer service conceptually कह सकती है:
product-service
यानी consumer की सोच बदलती है:
Old thinking
"Product service port 8081 पर है।"
Discovery thinking
"मुझे product-service चाहिए। उपलब्ध instance कौन-सा है, discovery infrastructure बताएगा।"
यही hard-coded location dependency को कम करता है।
21. Spring DiscoveryClient से registered instances देखना#
Learning के लिए हम Spring Cloud का DiscoveryClient use करके registered service instances inspect कर सकते हैं।
package com.example.orderservice.controller;
import java.util.List;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DiscoveryController {
private final DiscoveryClient discoveryClient;
public DiscoveryController(DiscoveryClient discoveryClient) {
this.discoveryClient = discoveryClient;
}
@GetMapping("/discovery/product-service")
public List<String> discoverProductService() {
List<ServiceInstance> instances =
discoveryClient.getInstances("product-service");
return instances.stream()
.map(instance -> instance.getUri().toString())
.toList();
}
}इस example का उद्देश्य business API call करना नहीं है।
हम केवल यह देख रहे हैं कि:
product-service
logical name से available instances मिल सकती हैं।
अगर दो instances registered हैं, तो returned list में multiple instance URIs हो सकती हैं।
Code में क्या हुआ?#
पहले:
discoveryClient.getInstances("product-service");से हमने registry-backed discovery system से पूछा:
product-service के available instances कौन-से हैं?
फिर:
instance.getUri()से हर discovered instance की URI देखी।
Important point:
हमने code में यह नहीं लिखा:
localhost:8081
और यह भी नहीं लिखा:
localhost:8082
यही discovery का core benefit है।
22. पूरा registration-to-discovery flow#
अब पूरी कहानी एक साथ समझते हैं।
Rendering diagram…
Diagram could not be rendered.
Observe करें:
दो product instances के ports अलग हैं।
लेकिन दोनों की logical identity:
product-service
है।
Consumer का code physical location के बजाय logical service identity पर depend कर सकता है।
23. Eureka Dashboard से registration verify करना#
अब practical verification करते हैं।
Step 1: Eureka Server start करें#
Server:
http://localhost:8761
पर चलना चाहिए।
Step 2: Dashboard खोलें#
Browser में:
http://localhost:8761
open करें।
Step 3: product-service start करें#
Client configuration में:
spring:
application:
name: product-serviceऔर:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/configured होना चाहिए।
Step 4: Dashboard refresh करें#
Registered applications section में product-service का instance दिखाई देना चाहिए।
Step 5: दूसरा instance start करें#
अगर application दूसरे port पर start की जाए लेकिन same:
spring.application.name=product-service
रखा जाए, तो registry में उसी logical service के multiple instances दिखाई दे सकते हैं।
यह scaling का basic mental model समझने में मदद करता है।
24. अगर service dashboard पर नहीं दिख रही तो क्या check करें?#
सबसे पहले panic नहीं करना है।
Systematically check करें।
1. Eureka Server running है?#
Browser से:
http://localhost:8761
open करके verify करें।
2. Client starter dependency मौजूद है?#
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>3. Eureka URL सही है?#
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/4. defaultZone spelling सही है?#
defaultZone
camel case में रखें।
5. spring.application.name set है?#
spring:
application:
name: product-service6. Application startup logs में connection error है?#
अगर client Eureka Server तक पहुँच ही नहीं पा रही, registration नहीं होगी।
7. थोड़ी synchronization delay तो नहीं?#
Registry update और dashboard visibility हमेशा instantaneous नहीं होती।
Configuration correct होने के बाद short delay normal हो सकती है।
25. Hard-coded URL बनाम Service Discovery#
अब दोनों approaches compare करते हैं।
| Hard-Coded Location | Service Discovery |
|---|---|
| Consumer exact host जानता है | Consumer logical service name जानता है |
| Consumer exact port जानता है | Registry instances track करती है |
| Location change से config/code टूट सकता है | Instance location dynamically discover हो सकती है |
| Scaling manage करना कठिन | Multiple instances register हो सकती हैं |
| Environment coupling अधिक | Network-location coupling कम |
| Dynamic infrastructure के लिए fragile | Dynamic microservice environments के लिए suitable |
26. एक practical example#
मान लीजिए आज:
product-service
यहाँ चल रही है:
server-a:8081
कल deployment system पुराने instance को remove करके नया instance start करता है:
server-b:9127
Hard-coded consumer के पास अभी भी old address हो सकता है।
लेकिन discovery-based architecture में नया instance Eureka में register हो सकता है।
Consumer logical service identity के आधार पर updated registry information use कर सकता है।
यही reason है कि Service Discovery cloud और dynamically scaled microservice environments में valuable pattern है।
27. Common Mistakes#
Mistake 1: Service name और port को same identity समझना#
गलत mental model:
"product-service मतलब port 8081।"
सही mental model:
product-service logical identity है।
Port current instance की network information है।
Mistake 2: Client पर पुराने tutorial देखकर unnecessary annotation add करना#
Current Spring Cloud setup में client starter auto-configuration provide करता है।
Client के लिए अलग @EnableEurekaClient annotation सामान्यतः आवश्यक नहीं है.
Mistake 3: defaultZone गलत लिखना#
यह map key case-sensitive है।
सही:
defaultZone: http://localhost:8761/eureka/Mistake 4: Eureka Server पर client behavior unnecessarily enabled रखना#
Standalone development registry में:
register-with-eureka: false
fetch-registry: falseरखना सामान्य configuration है।
Mistake 5: spring.application.name भूल जाना#
Service logical identity meaningful नहीं होगी तो discovery setup confusing हो जाएगा।
हर microservice को clear logical name दें।
Mistake 6: Discovery use करके भी host और port hard-code करना#
अगर consumer code में फिर भी:
http://localhost:8081
लिखा हुआ है, तो discovery architecture का मुख्य benefit खो जाता है।
28. Interview Preparation#
Service Discovery क्या problem solve करता है?Show Answer
Professional answer:
Service Discovery allows microservices to locate available service instances dynamically using logical service identities instead of relying on hard-coded hostnames and ports.
सरल meaning:
Consumer को यह permanently जानने की जरूरत नहीं रहती कि दूसरी service किस machine और port पर चल रही है।
Eureka Server और Eureka Client में क्या difference है?Show Answer
Eureka Server service registry maintain करता है।
Eureka Client registry में register हो सकती है और service information fetch कर सकती है।
spring.application.name का Eureka में क्या role है?Show Answer
यह application की logical service identity के रूप में use होता है।
एक ही logical service के multiple instances same application name के साथ अलग hosts या ports पर run कर सकते हैं।
Registration और Registry Fetching में difference क्या है?Show Answer
Registration:
अपनी information registry को देना।
Registry Fetching:
registry से available services की information लेना।
क्या Eureka client को हर request के लिए Eureka Server call करना पड़ता है?Show Answer
नहीं।
Eureka clients registry information local cache में रख सकती हैं और periodically refresh कर सकती हैं.
29. Chapter Recap#
इस chapter से आपको ये बातें साफ याद रहनी चाहिए:
- Microservices की physical network location dynamic हो सकती है।
- इसलिए host और port hard-code करना fragile approach है।
- Eureka Server Service Registry का काम करता है।
- Business microservices Eureka Clients बन सकती हैं।
- Client अपनी instance information Eureka में register कर सकती है।
- Client registry information fetch भी कर सकती है।
spring.application.namelogical service identity define करता है।- एक logical service के multiple instances हो सकते हैं।
- Instance metadata में host, port और दूसरी instance-related information हो सकती है।
- Heartbeat registry को instance availability track करने में मदद करता है।
- Consumer logical service name से available instances discover कर सकता है।
- Eureka Dashboard registration verify करने का useful development tool है।
- Service Discovery का main goal physical location dependency को कम करना है।
Microservices architecture में consumer को यह नहीं पूछना चाहिए, "Service कौन-से fixed port पर है?" बेहतर सवाल है, "मुझे कौन-सी logical service चाहिए?" Eureka उस logical service को available running instances से connect करने में मदद करता है।