Home >Java >javaTutorial >Microservices Part Creating Service Registry Application

Microservices Part Creating Service Registry Application

Susan Sarandon
Susan SarandonOriginal
2025-01-17 04:08:09460browse

Building a Microservices Application: Creating a Service Registry

To build a microservices application, you'll first need a service registry – a specialized microservice that maintains a list of registered microservices. This process involves six steps.

Step 1: Creating the Service Registry Microservice

We'll build the service registry using the spring-cloud-starter-netflix-eureka-server dependency. The pom.xml file will look like this:

<code class="language-xml"><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> </url>
  <licenses>
    <license/>
  </licenses>
  <developers>
    <developer/>
  </developers>
  <scm>
    <connection/>
    <developerConnection/>
    <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></code>

Step 2: Enabling the Eureka Server

The ServiceRegistryApplication.java file requires the @EnableEurekaServer annotation:

<code class="language-java">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);
    }

}</code>

Step 3: Service Registry Configuration

The application.properties file needs these settings to prevent the service registry from registering itself as a microservice:

<code class="language-properties">spring.application.name=service-registry
server.port=8761

eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false</code>

Step 4 - 6: Registering a New Microservice

These steps detail adding a new microservice and registering it with the service registry. This involves adding the spring-cloud-starter-netflix-eureka-client dependency, configuring the Eureka server URL, and verifying registration via the Eureka Server URL (https://www.php.cn/link/0ae12dc3676bc09a35fe6ed96926a6b5).

Microservices Part  Creating Service Registry Application

This concludes the initial setup. Further details on microservices will be covered in subsequent posts.

The above is the detailed content of Microservices Part Creating Service Registry Application. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn