Implementing Discovery Server in SpringBoot | Service Discovery | Microservices
We have 3 microservices for our Hotel Rating Application →
UserService - http://localhost:8081/users
HotelService - http://localhost:8082/hotels
RatingService - http://localhost:8083/ratings
Here we will try to implement Service Discovery mechanism for out Hotel Rating application by creating a Discovery Server.
Step 1 → Create a springboot microservice named DiscoveryServer with following dependencies -
Cloud Bootstrap | SPRING CLOUD
Eureka Server | SPRING CLOUD DISCOVERY (Netflix Eureka Server)
<!-- Spring cloud version needs to be provided inside the <properties> -->
<properties>
<java.version>17</java.version>
<spring-cloud.version>2023.0.3</spring-cloud.version>
</properties>
<!-- Dependencies -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- Spring Cloud will also require this <dependencyManagement> -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Step 2 → Enable Eureka server by adding @EnableEurekaServer
package com.movie_rating.ServiceRegistry;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class ServiceRegistryApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRegistryApplication.class, args);
}
}
Step 3 → Adding configuration in properties file. There are 2 important configuration properties here -
eureka.instance.hostname=localhost → to configure hostname
eureka.client.register-with-eureka=false → StartFragmentIndicates whether or not this instance should register its information with eureka server for discoveryEndFragment
eureka.client.fetch-registry=false → StartFragmentIndicates whether this client should fetch eureka registry information from eureka server.EndFragment
spring.application.name=ServiceRegistry
server.port=8090
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
We have now setup our Discovery Server. We can run our Discovery Server to get this interface -
But since we haven’t registered any Client microservice yet, we aren’t getting any applications displayed.
Let’s discuss how to setup and register our Client microservices in our Discovery Server in the next article - Implementing Service Discovery Client in SpringBoot | Microservices
Subscribe to my newsletter
Read articles from Prashant Katare directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Prashant Katare
Prashant Katare
Experienced Spring Boot Developer with over 3+ years of expertise in developing scalable and high-performance web applications and microservices. Proficient in Java and Spring Boot frameworks, with hands- on experience in RESTful APIs and Microservices architecture. Adept at building secure, database-driven applications and integrating various third- party services. Strong problem-solving skills with a focus on delivering clean, maintainable, and efficient code.