DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • How to Create Microservices Using Spring
  • Full-Duplex Scalable Client-Server Communication with WebSockets and Spring Boot (Part I)
  • A New Era Of Spring Cloud

Trending

  • The Future of Java and AI: Coding in 2025
  • Tired of Spring Overhead? Try Dropwizard for Your Next Java Microservice
  • Monolith: The Good, The Bad and The Ugly
  • How to Create a Successful API Ecosystem
  1. DZone
  2. Coding
  3. Frameworks
  4. Spring Cloud and Spring Boot, Part 1: Implementing Eureka Server

Spring Cloud and Spring Boot, Part 1: Implementing Eureka Server

Take a look at how to install and configure this discovery service for your Java microservices.

By 
Jitendra Bafna user avatar
Jitendra Bafna
DZone Core CORE ·
Jan. 09, 19 · Tutorial
Likes (10)
Comment
Save
Tweet
Share
76.7K Views

Join the DZone community and get the full member experience.

Join For Free

What is Eureka Server?

Eureka Server is service discovery for your microservices, where all client applications can register by themselves and other microservices look up the Eureka Server to get independent microservices to get the job complete.

Eureka Server is also known as Discovery Server and it contains all the information about client microservices running on which IP address and port.

To achieve this you need to create a Eureka Server application and add the below dependency in POM.xml.

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

Implementing Eureka Server

Creating Eureka Server Spring Application

Navigate to https://start.spring.io/ and create a project template. Provide project metadata like Group, Artifact, and add the below dependencies/modules. Click Generate Project and it will download .zip Spring-based project and you can unzip the downloaded project and import in Eclipse as a Maven project.

  • Eureka Server

  • Web

  • Actuator

Image title

POM.xml

Verify the POM.xml and looks like it does below:

<?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>

Spring Boot Starter Application

Now you need to open EurekaServerApplication.javaand add the annotation @EnableEurekaServer   on the top of the class as 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);
}
}

Application Properties File

You need to add the below list of properties in application.properties located at src/main/resources on your application.

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 in which your application will be bound and wewill use default port 8761 for eureka server.

  • eureka.client.fetch-registry doesn't register itself in eureka server.

  • eureka.client.register-with-eureka is determines if service register itself as a client in eureka server.

Running the Eureka Server

Run the Eureka server as Java application and go the URL: http://localhost:8761/

Image title

You can see the Eureka server is up and running but no application is registered with it yet. 

Registering Client Application With Eureka Server

Creating Spring Boot Client Application

Navigate to https://start.spring.io/ and create a project template. Provide project metadata like Group, Artifact and add below dependencies/modules. Click Generate Project and it will download a .zip Spring-based project and you can unzip the downloaded project and import it to Eclipse as Maven project.

  • DevTools

  • Actuator

  • Discovery Client

Image title


POM.xml

Verify the POM.xml looks as it does below:

<?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>

Java Spring Server Application

Now you need to open the SpringBootApplication EurekaClientApplication.java and add the annotation @EnableDiscoveryClient   on the top of the class as 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 REST Controller

You need to add the class HelloWorldController  under package com.example.eureka.client.application and implement GET method in the 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;
}
}

Application Properties File

You need to add the below list of properties in application.properties located at src/main/resources.

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

eureka.client.service-url.defaultZone determines the address where the Eureka Server is running so the client application can register itself in Eureka Server.

Running Client Application

Before running the application, you need to make sure the Eureka Server is up and running. Run the client application as a Java application and navigate to the Eureka Server at http://localhost:8761/. This time you should see that client application registered in Eureka Server.

Image title

This is how you can implement Eureka Server for your microservices. Next, we are going to learn about Distributed Tracing for Spring Boot microservices in next article.

Spring Framework Spring Boot application Spring Cloud microservice

Opinions expressed by DZone contributors are their own.

Related

  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • How to Create Microservices Using Spring
  • Full-Duplex Scalable Client-Server Communication with WebSockets and Spring Boot (Part I)
  • A New Era Of Spring Cloud

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!