English 中文(简体)
Spring Boot Tutorial

Spring Boot Resources

Selected Reading

Service Registration with Eureka
  • 时间:2024-12-22

Service Registration with Eureka


Previous Page Next Page  

In this chapter, you are going to learn in detail about How to register the Spring Boot Micro service apppcation into the Eureka Server. Before registering the apppcation, please make sure Eureka Server is running on the port 8761 or first build the Eureka Server and run it. For further information on building the Eureka server, you can refer to the previous chapter.

First, you need to add the following dependencies in our build configuration file to register the microservice with the Eureka server.

Maven users can add the following dependencies into the pom.xml file −

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

Gradle users can add the following dependencies into the build.gradle file −

compile( org.springframework.cloud:spring-cloud-starter-eureka )

Now, we need to add the @EnableEurekaCpent annotation in the main Spring Boot apppcation class file. The @EnableEurekaCpent annotation makes your Spring Boot apppcation act as a Eureka cpent.

The main Spring Boot apppcation is as given below −

package com.tutorialspoint.eurekacpent;

import org.springframework.boot.SpringApppcation;
import org.springframework.boot.autoconfigure.SpringBootApppcation;
import org.springframework.cloud.netfpx.eureka.EnableEurekaCpent;

@SpringBootApppcation
@EnableEurekaCpent
pubpc class EurekacpentApppcation {
   pubpc static void main(String[] args) {
      SpringApppcation.run(EurekacpentApppcation.class, args);
   }
}

To register the Spring Boot apppcation into Eureka Server we need to add the following configuration in our apppcation.properties file or apppcation.yml file and specify the Eureka Server URL in our configuration.

The code for apppcation.yml file is given below −

eureka:
   cpent:
      serviceUrl:
         defaultZone: http://localhost:8761/eureka
      instance:
      preferIpAddress: true
spring:
   apppcation:
      name: eurekacpent

The code for apppcation.properties file is given below −

eureka.cpent.serviceUrl.defaultZone  = http://localhost:8761/eureka
eureka.cpent.instance.preferIpAddress = true
spring.apppcation.name = eurekacpent

Now, add the Rest Endpoint to return String in the main Spring Boot apppcation and the Spring Boot Starter web dependency in build configuration file. Observe the code given below −

package com.tutorialspoint.eurekacpent;

import org.springframework.boot.SpringApppcation;
import org.springframework.boot.autoconfigure.SpringBootApppcation;
import org.springframework.cloud.netfpx.eureka.EnableEurekaCpent;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApppcation
@EnableEurekaCpent
@RestController
pubpc class EurekacpentApppcation {
   pubpc static void main(String[] args) {
      SpringApppcation.run(EurekacpentApppcation.class, args);
   }
   @RequestMapping(value = "/")
   pubpc String home() {
      return "Eureka Cpent apppcation";
   }
}

The entire configuration file is given below.

For Maven user - pom.xml

<?xml version = "1.0" encoding = "UTF-8"?>
<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 
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
   
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.tutorialspoint</groupId>
   <artifactId>eurekacpent</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>eurekacpent</name>
   <description>Demo project for Spring Boot</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.9.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
      <spring-cloud.version>Edgware.RELEASE</spring-cloud.version>
   </properties>

   <dependencies>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-eureka</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</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>
   
</projecta>

For Gradle user – build.gradle

buildscript {
   ext {
      springBootVersion =  1.5.9.RELEASE 
   }
   repositories {
      mavenCentral()
   }
   dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
   }
}

apply plugin:  java 
apply plugin:  ecppse 
apply plugin:  org.springframework.boot 

group =  com.tutorialspoint 
version =  0.0.1-SNAPSHOT 
sourceCompatibipty = 1.8

repositories {
   mavenCentral()
}
ext {
   springCloudVersion =  Edgware.RELEASE 
}
dependencies {
   compile( org.springframework.cloud:spring-cloud-starter-eureka )
   testCompile( org.springframework.boot:spring-boot-starter-test )
   compile( org.springframework.boot:spring-boot-starter-web )   
}
dependencyManagement {
   imports {
      mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
   }
}

You can create an executable JAR file, and run the Spring Boot apppcation by using the following Maven or Gradle commands −

For Maven, you can use the following command −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target directory.

For Gradle, you can use the following command −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the build/pbs directory.

Now, run the JAR file by using the command as shown −

java –jar <JARFILE> 

Now, the apppcation has started on the Tomcat port 8080 and Eureka Cpent apppcation is registered with the Eureka Server as shown below −

Started Apppcation on Tomcat Port

Hit the URL http://localhost:8761/ in your web browser and you can see the Eureka Cpent apppcation is registered with Eureka Server.

Eureka Cpent Apppcation

Now hit the URL http://localhost:8080/ in your web browser and see the Rest Endpoint output.

Eureka Cpent Apppcation Output Advertisements