Open In App

Getting Started with Spring Boot and Eureka Service Registry

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:

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:

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.

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:

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.

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.

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.

Article Tags :