--- url: "http://microservices.io/patterns/client-side-discovery.html" titulek: "Pattern: Client-side service discovery" h1: "Pattern: Client-side service discovery" popis: "" jazyk: "" kanonicka: "" drobecky: [] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 21:11:18" znaku: 8741 slov: 1055 kodovani: "utf-8" sha256: "490cf5d0b24a1f72d5932ed5f359803a6d533508e7a8bea7c76a2d8c20f1a23d" --- #### [Microservice Architecture](/index.html) **Supported by [Kong](https://konghq.com/)** # Pattern: Client-side service discovery [pattern](/tags/pattern) [inter-service communication](/tags/inter-service communication) [service discovery](/tags/service discovery) --- ## Context Services typically need to call one another. In a monolithic application, services invoke one another through language-level method or procedure calls. In a traditional distributed system deployment, services run at fixed, well known locations (hosts and ports) and so can easily call one another using HTTP/REST or some RPC mechanism. However, a modern [microservice-based](microservices.html) application typically runs in a virtualized or containerized environments where the number of instances of a service and their locations changes dynamically. Consequently, you must implement a mechanism for that enables the clients of service to make requests to a dynamically changing set of ephemeral service instances. ## Problem How does the client of a service - the API gateway or another service - discover the location of a service instance? ## Forces - Each instance of a service exposes a remote API such as HTTP/REST, or Thrift etc. at a particular location (host and port) - The number of services instances and their locations changes dynamically. - Virtual machines and containers are usually assigned dynamic IP addresses. - The number of services instances might vary dynamically. For example, an EC2 Autoscaling Group adjusts the number of instances based on load. ## Solution When making a request to a service, the client obtains the location of a service instance by querying a [Service Registry](service-registry.html), which knows the locations of all service instances. The following diagram shows the structure of this pattern. This is typically handled by a [Microservice chassis framework](microservice-chassis.html) ## Examples The [Microservices Example application](https://github.com/cer/microservices-examples) is an example of an application that uses client-side service discovery. It is written in Scala and uses Spring Boot and Spring Cloud as the [Microservice chassis](microservice-chassis.html). They provide various capabilities including client-side discovery. `RegistrationServiceProxy` is a component of that application. In order to register a user, it invokes another service using the Spring Framework’s `RestTemplate`: ``` @Component class RegistrationServiceProxy @Autowired()(restTemplate: RestTemplate) extends RegistrationService { @Value("${user_registration_url}") var userRegistrationUrl: String = _ override def registerUser(emailAddress: String, password: String): Either[RegistrationError, String] = { val response = restTemplate.postForEntity(userRegistrationUrl, RegistrationBackendRequest(emailAddress, password), classOf[RegistrationBackendResponse]) ... } ``` It is injected with the RestTemplate and the `user_registration_url`, which specifies the REST endpoint. When the application is deployed `user_registration_url` is set to this URL `http://REGISTRATION-SERVICE/user` - see the `docker-compose.yml` file. `REGISTRATION-SERVICE` is the logical service name that is resolved to a network location using client-side service discovery. The service discovery is implemented using [Netflix OSS](http://netflix.github.io/) components. It provides [Eureka](https://github.com/Netflix/eureka/wiki/Eureka-at-a-glance), which is a [Service Registry](service-registry.html), and [Ribbon](https://github.com/Netflix/ribbon), which is an HTTP client that queries Eureka in order to route HTTP requests to an available service instance. Client-side service discovery is configured using various Spring Cloud annotations: ``` @Configuration @EnableEurekaClient @Profile(Array("enableEureka")) class EurekaClientConfiguration { @Bean @LoadBalanced def restTemplate(scalaObjectMapper : ScalaObjectMapper) : RestTemplate = { val restTemplate = new RestTemplate() restTemplate.getMessageConverters foreach { case mc: MappingJackson2HttpMessageConverter => mc.setObjectMapper(scalaObjectMapper) case _ => } restTemplate } ``` The `@EnableEurekaClient` annotation enables the Eureka client. The `@LoadBalanced` annotation configures the `RestTemplate` to use Ribbon, which has been configured to use the Eureka client to do service discovery. As a result, the `RestTemplate` will handle requests to the `http://REGISTRATION-SERVICE/user` endpoint by querying Eureka to find the network locations of available service instances. ## Resulting context Client-side discovery has the following benefits: - Fewer moving parts and network hops compared to [Server-side Discovery](server-side-discovery.html) Client-side discovery also has the following drawbacks: - This pattern couples the client to the [Service Registry](service-registry.html) - You need to implement client-side service discovery logic for each programming language/framework used by your application, e.g Java/Scala, JavaScript/NodeJS. For example, [Netflix Prana](https://github.com/Netflix/Prana) provides an HTTP proxy-based approach to service discovery for non-JVM clients. - [Service Registry](service-registry.html) - an essential part of service discovery - [Microservice chassis](microservice-chassis.html) - Client-side service discovery is the responsibility the microservice chassis framework - [Server Side Discovery](server-side-discovery.html) is an alternative solution to this problem. --- [pattern](/tags/pattern) [inter-service communication](/tags/inter-service communication) [service discovery](/tags/service discovery) --- --- [Modernization Help](https://chrisrichardson.net/) Copyright © 2026 Chris Richardson • All rights reserved • Supported by [Kong](https://konghq.com/) . #### About Microservices.io Microservices.io is created by [Chris Richardson](/about.html), software architect, creator of the original CloudFoundry.com, and author of *Microservices Patterns*. Chris helps organizations modernize their architecture to enable fast flow and GenAI-powered software delivery. #### Need help modernizing your architecture? Avoid the trap of creating a modern legacy system — a new architecture with the same old problems. Contact me to discuss your modernization goals. [Get Help](https://www.linkedin.com/in/pojos/) #### Microservices Patterns, 2nd edition I am very excited to announce that the MEAP for the second edition of my book, Microservices Patterns is now available! [Learn more](/post/architecture/2025/06/26/announcing-meap-microservices-patterns-2nd-edition.html) #### ASK CHRIS ? Got a question about microservices? Fill in [this form](https://forms.gle/ppYDAF1JxHGec8Kn9). If I can, I'll write a blog post that answers your question. #### LEARN about microservices Chris offers numerous other resources for learning the microservice architecture. #### Get the book: Microservices Patterns Read Chris Richardson's book: --- #### Example microservices applications Want to see an example? Check out Chris Richardson's example applications. [See code](http://eventuate.io/exampleapps.html) #### Virtual bootcamp: Distributed data patterns in a microservice architecture My virtual bootcamp, distributed data patterns in a microservice architecture, is now open for enrollment! It covers the key distributed data management patterns including Saga, API Composition, and CQRS. It consists of video lectures, code labs, and a weekly ask-me-anything video conference repeated in multiple timezones. The regular price is $395/person but use coupon OFFEFKCW to sign up for $95 (valid until Sept 30th, 2025). There are deeper discounts for buying multiple seats. [Learn more](https://chrisrichardson.net/virtual-bootcamp-distributed-data-management.html) #### Learn how to create a service template and microservice chassis Take a look at my [Manning LiveProject](/post/patterns/2022/03/15/service-template-chassis-live-project.html) that teaches you how to develop a service template and microservice chassis. [Signup for the newsletter](http://visitor.r20.constantcontact.com/d.jsp?llr=ula8akwab&p=oi&m=1123470377332&sit=l6ktajjkb&f=15d9bba9-b33d-491f-b874-73a41bba8a76) #### BUILD microservices Ready to start using the microservice architecture? #### Consulting services [Engage Chris](http://www.chrisrichardson.net/consulting.html) to create a microservices adoption roadmap and help you define your microservice architecture, --- #### The Eventuate platform Use the [Eventuate.io platform](https://eventuate.io) to tackle distributed data management challenges in your microservices architecture. Eventuate is Chris's latest startup. It makes it easy to use the Saga pattern to manage transactions and the CQRS pattern to implement queries. --- Join the [microservices google group](https://groups.google.com/forum/#!forum/microservices)