Service Discovery works - rahul7838/quora.clone GitHub Wiki

Let me help you understand how service discovery works with Netflix Eureka in your setup. Let's break this down step by step, using your specific case of an API Gateway and an Auth Service.

Think of Eureka as a phone directory for your microservices. Just like you'd look up someone's number in a phone book, your API Gateway uses Eureka to look up where your Auth Service is running.

Here's what happens when your services start up:

First, when your Auth Service starts, it reaches out to the Eureka Server and essentially says "Hello! I'm Auth Service, and I'm running at http://localhost:port". This process is called service registration. The Auth Service will keep sending heartbeat signals to Eureka every 30 seconds (by default) to say "I'm still here and healthy!"

Meanwhile, your API Gateway, which is a Eureka Client, also connects to the Eureka Server. It asks "Can you give me a list of all available services?" Eureka responds with information about where to find the Auth Service. This process is called service discovery.

Let's see this in action with some code examples. Here's what your typical configuration might look like:

For your Auth Service (application.yml):

spring:
  application:
    name: auth-service
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    preferIpAddress: true
server:
  port: 8081

For your API Gateway (application.yml):

spring:
  application:
    name: api-gateway
  cloud:
    gateway:
      routes:
        - id: auth-service
          uri: lb://auth-service
          predicates:
            - Path=/auth/**
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8080

Now, let's say a request comes to your API Gateway at http://localhost:8080/auth/login. Here's what happens:

  1. The Gateway receives the request and sees it matches the /auth/** pattern
  2. The Gateway sees the uri is "lb://auth-service" (lb stands for load balancer)
  3. The Gateway asks Eureka "Where can I find auth-service?"
  4. Eureka responds with "auth-service is at http://localhost:8081"
  5. The Gateway forwards the request to http://localhost:8081/auth/login

The really powerful part is that if you were to move your Auth Service to a different port or machine, the Gateway would automatically get the new location from Eureka. You wouldn't need to change any configuration!

Would you like me to explain more about how the load balancing works, or would you like to see how to monitor these service registrations through Eureka's dashboard? Understanding either of these aspects would give you a fuller picture of the service discovery mechanism.

How Eureka Server works

Let me explain how Eureka Server fits into your microservices architecture. Eureka Server needs to be a separate, standalone service - it's not part of your gateway service. Think of it as the central phone directory that all your other services (including the gateway) need to communicate with.

Let me walk you through creating and running a Eureka Server:

First, create a new Spring Boot project with Eureka Server dependency. Here's how your pom.xml should look:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

In your main application class, you'll need to enable the Eureka Server:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer    // This annotation tells Spring that this is a Eureka Server
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

Then, configure your application.yml file:

server:
  port: 8761    # This is the default port for Eureka Server

spring:
  application:
    name: eureka-server

eureka:
  client:
    registerWithEureka: false    # Don't register itself with itself
    fetchRegistry: false         # Don't fetch registry info from other servers
  server:
    waitTimeInMsWhenSyncEmpty: 0 # Don't wait for client registrations

So now your architecture will look like this:

                          registers/fetches
Auth Service (8081) <-----------------------> Eureka Server (8761)
                                                    ^
                                                    |
                                            registers/fetches
                                                    |
API Gateway (8080) <--------------------------------

When you run everything locally, the startup order matters:

  1. Start Eureka Server first (it needs to be available for others to register)
  2. Start your Auth Service and Gateway Service (they can start in any order after Eureka)

You can verify everything is working by:

  1. Opening http://localhost:8761 in your browser to see the Eureka dashboard
  2. Looking for your registered services in the "Instances currently registered with Eureka" section

Now, your other services (Auth Service and Gateway) need to know where to find the Eureka Server. In their application.yml files, you'll have:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

This setup gives you a complete service discovery system where:

  • Eureka Server runs independently at port 8761
  • Auth Service registers itself with Eureka
  • Gateway Service registers itself and uses Eureka to find Auth Service
  • All services maintain their registrations through regular heartbeats
⚠️ **GitHub.com Fallback** ⚠️