Open In App

Getting Started with Spring Boot and Eureka Service Registry

Last Updated : 29 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Eureka is the other hand in the Service Registry supplied by Netflix. It can be designed for the resilient mid-tier for the load balancing and failover of the middle-tier servers. When used together then the spring boot and Eureka can enable the developers to build the microservices architectures.

The main concept revolves around building the microservices architecture where the multiple small and it can independent services communicate with each other over HTTP. Eureka acts as the service registry where each microservice registers itself upon the startup. It allows different services to discover and communicate dynamically.

Key Terminologies:

  • Microservices: It is an architectural style that can structure an application as a collection of small, independent services, each focused on the specific business capability and running in its process.
  • Eureka: It is the service registry and discovery server and it can provided by Netflix. It can allow the microservices to register themselves and discover the other services dynamically. It can enable easy communication between the services in the distributed environment.
  • Service Registry: The service registry is the database containing the network locations of the service instances. It can allows the services to the register themselves when they become the available and deregister when they are no longer available.
  • Service Discovery: It is the process of the dynamically finding and locating the available service instances within the distributed system. It can allowing the services to the communicate with the each other without the hardcoding their network locations.
  • Eureka Server: It is the service registry server can provided by the Netflix Eureka. It can hosts the registry of the available microservices and it can provides the interface for the service registration, discovery and health monitoring.
  • Eureka Client: It is the library that can enables the microservices to the registry itself with the Eureka server and discovery the other services registered with the server. It can provides the integration with the Spring boot applications to the enable the service registration and discovery.
  • Instance Registration: It is the process by the which the microservices registers itself with the Eureka server upon the startup. It can includes the providing the metadata such as the service name, host, port, health status etc.
  • Instance Deregistration: It is the process by the which a microservices removes itself from Eureka sever registry when it shuts down or becomes the unavailable.
  • Service Name: It is the unique identifier assigned to the microservice. It can be used by the other services to the discover and communicate with the microservice.
  • Load Balancing: It is the process of the distributing incoming requests across the multiple service instances to the ensure optimal resource utilization and high availability. Eureka can provide the client side load balancing by the providing the list of the available service instances to the client.

Step-by-step implementation to Get Started with Spring Boot and Eureka Service Registry

Below are the steps for both Eureka Server and Eureka Client to start with Spring Boot and Eureka Service Registry.

Eureka-Server

Step 1: Create a Spring project using Spring Initializr and add the following dependencies:

Dependencies:

  • Spring Web
  • Eureka Server
  • Spring Dev Tools
  • Lombok

After creating the Spring project, the file structure resembles the image below.

File Structure


Step 2: Open the application.properties file and put the below code for the server port and eureka server configuration to the project.

spring.application.name=eureka-server-config

server.port=9099
eureka.instance.prefer-ip-address=true
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=true
eureka.client.service-url.defaultZone= http://localhost:9099/eureka


Step 3: In the main class, add the annotation @EnableEurekaServer to enable Eureka server functionality.

Java
package org.example.eurekaserverconfig;

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerConfigApplication.class, args);
    }

}


Step 4: Once complete the spring project and it run as spring application once it runs successful then it starts at port 9099.

Eurekaserver Started


User-service(Eureka-Client)

Step 1: Create the spring project using spring initializer on creating the project add the below dependencies into the project.

Dependencies:

  • Spring Web
  • Eureka Server Client
  • Spring Dev Tools
  • Lombok

After creating the Spring project, the file structure resembles the image below.

File Structure


Step 2: Open the application.properties file and put the below code for the server port and eureka client configuration to the project.

spring.application.name=user-service
server.port=8086

eureka.instance.prefer-ip-address=true
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=true
eureka.client.service-url.defaultZone= http://localhost:9099/eureka


Step 3: Create the new Java class and it named as UserController.

Go to src > org.example.userservice > UserController and put the below code.

Java
package org.example.userservice;

import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController

public class UserController {

    @GetMapping("/client")
        public String check() {
        return "Welcome to client";
        }
}


Step 4: Open the main class and add the @EnableDiscoveryClient into it.

Java
package org.example.userservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class UserServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(UserServiceApplication.class, args);
    }

}


Step 5: Once complete the spring project and it run as spring application once it runs successful then it starts at port 8086.

Userlog Started


Eureka Dashboard

This dashboard provides the visibility into the microservices registered with Eureka Server and their status and the other relevant information.

Eureka Dashboard


If we follow the above steps, we can successfully implement the Eureka Service Registry of the spring boot application. By the following these steps and principles, we can effectively build and manage the Microservices Architecture using the Spring Boot and Eureka Service Registry.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads