Spring Cloud and Spring Boot. Part 1: using Eureka Server

Future students of the course “Spring Framework Developer” and we invite everyone to an open online lesson on the topic Introducing the Cloud, Creating a Cluster in Mongo DB Atlas018… Participants, together with an expert teacher, will talk about cloud types and set up a free Mongo DB cluster for their projects.

And now we are sharing with you the traditional translation of the article.


In this article, we will talk about how to install and configure service discovery for Java microservices.

What is Eureka Server?

Eureka Server is service discovery for your microservices. Client applications can self-register with it, and other microservices can contact Eureka Server to find the microservices they need.

Eureka Server is also known as Discovery Server and contains information such as the IP address and port of the microservice.

To create an application with Eureka Server, you need to pom.xml add the below dependency.

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

Launching Eureka Server

Go to https://start.spring.io and create a project template. Provide metadata like Group, Artifact and add below dependencies / modules. Click “Generate Project” and download the project in a zip file. Next, unzip it and import it into the IDE as a Maven project.

  • Eureka Server

  • Web

  • Actuator

Check that pom.xml as follows:

<?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>
   <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>2.0.7.RELEASE</version>
       <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.example.eureka.server</groupId>
   <artifactId>eureka-server</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>eureka-server</name>
   <description>Demo project for Spring Boot</description>

   <properties>
       <java.version>1.8</java.version>
       <spring-cloud.version>Finchley.SR2</spring-cloud.version>
   </properties>

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

Now open the file EurekaServerApplication.java and add annotation for the class @EnableEurekaServeras shown below.

package com.example.eureka.server.eurekaserver;

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

Add to application.propertieslocated in src/main/resources, the following parameters.

spring.application.name=eureka-server
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
  • spring.application.name Is a unique name for your application.

  • server.port – the port on which your application will run, we will use the default port (8761).

  • eureka.client.register-with-eureka – determines if the service is registered as a client on the Eureka Server.

  • eureka.client.fetch-registry – whether or not to receive information about registered customers.

Run Eureka Server as Java Application and go to http: // localhost: 8761 /

You will see that Eureka Server is up and running, but there are no registered applications yet.

Registering a client application in Eureka Server

Go to https://start.spring.io and create a project template. Provide metadata like Group, Artifact and add the below dependencies / modules. Click “Generate Project” and download the project in a zip file. Next, unzip it and import it into the IDE as a Maven project.

  • DevTools

  • Actuator

  • Discovery Client

Check that your pom.xml as follows:

<?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>
  <parent>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId>
     <version>2.0.7.RELEASE</version>
     <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.example.eureka.client</groupId>
  <artifactId>EurekaClientApplication</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>EurekaClientApplication</name>
  <description>Demo project for Spring Boot</description>
 
  <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>Finchley.SR2</spring-cloud.version>
  </properties>
 
  <dependencies>
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
     </dependency>
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
     </dependency>
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
     </dependency>
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
     </dependency>
     <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
     </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>

Open the file EurekaClientApplication.java and add annotation for the class @EnableDiscoveryClientas shown below.

package com.example.eureka.client.application;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {

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

Adding a REST controller

Create a class HelloWorldController in the package com.example.eureka.client.application and add a GET method in this class.

package com.example.eureka.client.application;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWorldController {

   @GetMapping("/hello-worlds/{name}")
   public String getHelloWorld(@PathVariable String name) {
       return "Hello World " + name;
   }
}

IN application.propertieslocated in src / main / resources add the following parameters.

spring.application.name=eureka-client-service
server.port=8081
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

Parameter eureka.client.service-url.defaultZone defines the address of the Eureka Server so that the client application can register there.

Launching the client application

Before starting the application, you need to make sure that the Eureka Server is up and running. Run our client as Java application and go to Eureka Server at http: // localhost: 8761 /… This time you should see that our client application has registered with the Eureka Server.

Now you know how to use Eureka Server for your microservices. In the next article, we’ll take a look at Distributed Tracing for Spring Boot microservices.


Learn more about the course “Spring Framework Developer”

Sign up for an open online lesson on the topic Introducing the Cloud, Creating a Cluster in Mongo DB Atlas018

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *