Video in progress

Junior 🌱 Spring

Why do you need service discovery (for example, Eureka)?

Microservices run as many instances, and their IPs keep changing. With discovery, each instance registers itself in a registry, and the others ask for an address by service name.

📖 Answer

Without discovery, the address of payment-service is written by hand in the order-service configuration: http://10.0.3.17:8080. Then:

  • an instance restarts, and its IP changes;
  • traffic grows and 3 instances are added: who updates the configuration?
  • one instance dies, and requests are still going to it.

How it works with Eureka:

  1. When each service starts, it registers with the Eureka server: "I'm payment-service, here's my address".
  2. It sends heartbeats periodically. If the heartbeats stop, it's removed from the registry.
  3. The client asks by service name, and the LoadBalancer picks one of the instances.
@Bean
@LoadBalanced
RestClient.Builder restClientBuilder() {
    return RestClient.builder();
}

// then: builder.build().get().uri("http://payment-service/api/pay/42")

The URL has a service name instead of an IP, and @LoadBalanced replaces it with the real address.

In Kubernetes the Service object and DNS do this job, so Eureka is usually not installed there.