Exercise — Service Discovery: register a service and resolve a healthy instance.

Topic: microservices, Infrastructure. Level: Intermediate.

Answer

On startup the instance registers (name, host, port, health URL) with the registry and sends heartbeats; the registry evicts instances that stop reporting healthy. Clients resolve the name to the current healthy set and load-balance. Below registers with Eureka and calls another service by name with client-side load balancing. ```yaml # application.yml — self-register + heartbeat eureka: client: { service-url: { defaultZone: http://eureka:8761/eureka } } instance: { lease-renewal-interval-in-seconds: 10 } # heartbeat ``` ```java @Bean @LoadBalanced // client-side LB over discovered instances RestClient.Builder rc() { return RestClient.builder(); } // resolves "inventory-service" to a healthy instance each call: rc().build().get().uri("http://inventory-service/stock/{sku}", sku).retrieve(); ```