CodeGym /Courses /Module 5. Spring /Lecture 245: Introduction to Service Discovery (Eureka)

Lecture 245: Introduction to Service Discovery (Eureka)

Module 5. Spring
Level 24 , Lesson 4
Available

Before diving into the weeds of Eureka, let's try to answer the question: what's so bad about just manually specifying microservice addresses?

Imagine microservices aren't static buildings with fixed addresses, but a taxi fleet. Cars (services) constantly go on routes, break down, get replaced by new ones, and change their location. Now try to serve clients without a dispatcher — you'd have to manually find and record new coordinates of cars every time.

In the microservices world, that "dispatcher" is Service Discovery. It automatically tracks which services are available, where they are, and where to send requests. Eureka is one of the popular implementations of this system.

Drawbacks of static configuration:

  1. Higher risk of errors. If you have to manually update addresses of all microservices, there's a high chance you'll accidentally break something. Especially when you're dealing with dozens (or hundreds) of microservices.
  2. More complicated scaling. When you add a new microservice instance, you need to update the configurations of other microservices that interact with it.
  3. Slow reaction to changes. If a service address changes, manual edits are required. That slows down the delivery of new versions.

Service Discovery solves these problems by allowing microservices to automatically find each other without manual setup. The main idea is centralized management of service addresses and metadata.


How does Service Discovery work?

Service Discovery components:

  • Service Registry (service registry): the central database where addresses (and other metadata) of all available microservices are stored.
  • Service Provider: a microservice that registers itself in the Service Registry.
  • Service Consumer: a microservice that requests addresses of other services from the Service Registry to interact with them.

Example lifecycle of Service Discovery:

  1. Registration: When a microservice starts, it registers in the Service Registry, providing its address (usually IP and port) and other parameters.
  2. Heartbeat: The microservice periodically sends heartbeat messages to the registry to let it know it's still alive.
  3. Discovery: Another microservice (the consumer) gets the address of the registered service by querying the registry.
  4. Deregistration: If a microservice dies (or its instance shuts down), its entry is removed from the registry.

Eureka

Now that we understand the concept of Service Discovery, let's move to one of the most popular and easy-to-integrate tools — Eureka. It's a Netflix library that became Open Source and is part of Spring Cloud.

Benefits of Eureka:

  1. Easy integration with Spring Boot. You can easily add Eureka to your project by including the appropriate dependencies.
  2. Supports dynamic registration and discovery of services. Eureka makes scalability and fault tolerance easier.
  3. Supports the CAP model (Consistency, Availability, Partition-tolerance). Eureka can be configured to focus more on Availability, which makes it a good fit for microservices.
  4. Monitoring and management via Dashboard. You can see registered services, their current status, and metrics through a web UI.

Main components of Eureka

  1. Eureka Server:
    • The central component in the Service Discovery system that stores information about all registered services.
    • Acts as the centralized database for microservice addresses.
    • Handles registrations and heartbeat messages.
  2. Eureka Client:
    • The library that you add to your microservices so they can register with the Eureka Server.
    • Also responsible for fetching addresses of other services from the Eureka Server.
  3. Dashboard:
    • A web UI for monitoring the Eureka Server. Shows registered services, their status (UP/DOWN), logs, and addresses.

Component interaction


+------------------+        Registration       +------------------+
|  Service A       |------------------------->|  Eureka Server   |
| (Eureka Client)  |     Service Discovery    |                  |
+------------------+<-------------------------+------------------+
+------------------+
|  Service B       |
| (Eureka Client)  |
+------------------+

Example: Starting a Eureka Server

Before using Service Discovery, we need to start a Eureka Server. Do the following steps:

  1. Add the dependency to pom.xml
    
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    

  2. Annotation @EnableEurekaServer

    In the Application class add the annotation to enable the Eureka Server:

    @SpringBootApplication
    @EnableEurekaServer
    public class EurekaServerApplication {
        public static void main(String[] args) {
            SpringApplication.run(EurekaServerApplication.class, args);
        }
    }
    
  1. Configure application.yml

    Create the application.yml file in the resources folder:

    
    server:
      port: 8761 # Eureka Server port
    eureka:
      client:
        register-with-eureka: false # Eureka Server doesn't register itself
        fetch-registry: false       # Eureka Server doesn't fetch the registry
      instance:
        hostname: localhost         # Primary host
    
  1. Start the application

    Now you can run the app, and the Eureka server will be available at http://localhost:8761.

Example: Registering a microservice with the Eureka Server

After the server is up, configure a client to register with it.

  1. Add the dependency
    
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    

  2. Configure the client

    Add the connection settings to application.yml to point to the Eureka Server:

    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka/ # Eureka Server URL
      instance:
        prefer-ip-address: true # Register IP instead of hostname
    
  1. Annotation @EnableEurekaClient

    Add the annotation to the main class:

    
    @SpringBootApplication
    @EnableEurekaClient
    public class ServiceAApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceAApplication.class, args);
    }
    }
    

Verification

Start the Eureka Server first, then the Eureka Client (your microservice). Go to http://localhost:8761 to see that your client registered with the system. You'll see the service name, its status, and its address.


Benefits and challenges

Using Eureka significantly simplifies managing microservices in a dynamic environment, but you should consider the load on the server and the need for reliable heartbeat messages. It's a powerful tool that helps make systems more flexible and scalable.

Now that you know how Service Discovery works and even how to set up Eureka Server and Eureka Client, you're ready to build more advanced microservice applications with automatic service discovery.

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION