DEV Community

Cover image for Microservices Part 01: Creating Service Registry Application
Shivam Yadav
Shivam Yadav

Posted on

Microservices Part 01: Creating Service Registry Application

In order to create a Microservices Application we will first require a Service Registry which is nothing but a special kind of microservice that will have a list of microservices registered inside it.

There are total three steps to create a Service Registry.

Step1: We are going to write our Service Registry microservice application using the spring-cloud-starter-netflix-eureka-server dependency.

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.4.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.sky</groupId>
    <artifactId>service-registry</artifactId>
    <version>1.0</version>
    <name>service-registry</name>
    <description>Registry for Job Portal Application</description>
    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>21</java.version>
        <spring-cloud.version>2024.0.0</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
Enter fullscreen mode Exit fullscreen mode
service-registry/pom.xml

Step 2: Now, we need to also include the annotation @EnableEurekaServer to notify that this particular application will act as a Registry Server.

package com.sky.service_registry;

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

@SpringBootApplication
@EnableEurekaServer
public class ServiceRegistryApplication {

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

}
Enter fullscreen mode Exit fullscreen mode
service_registry/ServiceRegistryApplication.java

Step 3: We need to also specify the below details. To tell Spring to not register this application as a microservice. As all the other microservices will be registered inside this special microservice.

spring.application.name=service-registry
server.port=8761

eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
Enter fullscreen mode Exit fullscreen mode
/service-registry/src/main/resources/application.properties

Step 4: We will create a new microservice and try to register it in the Service Registry that was created in the previous steps.

We need to add the spring-cloud-starter-netflix-eureka-client dependency in our pom.xml of the new microservice in order to make it available/discoverable for registeration in the Service Registry.

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
Enter fullscreen mode Exit fullscreen mode

Step 5: Also, we have to add below configuration for newly created microservice to specify the url for eureka server i.e. our Service Registry.

spring.application.name=job-search-service
server.port=8762

eureka.client.service-url.defaultZone=http://localhost:8761/eureka
Enter fullscreen mode Exit fullscreen mode

Step 6: Now, make sure the Service Registry and the newly created microservice is running. To validate if the registration is success we can go to the Eureka Server URL(http://localhost:8761/) and locate the new microservice name there as shown in below snapshot.

Eureka Server Snapshot

Stay tuned for the next part of the Microservices Blog.
Thanks for reading!

Top comments (0)