Create Eureka Server Using Spring Cloud

Overview of Eureka Server:

Eureka is a discovery server where all the micro-services of a system can register, de-register and communicate between themselves. It also works as the Load Balancer between the multiple instances of a component. In this post, you will learn how to implement an Eureka Server quickly using Maven and the Spring Cloud Libraries.

The Implementation:

Eureka Server Maven Dependency:

You will find the latest maven dependency of Spring Cloud Eureka Server here.

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
    <version>1.4.6.RELEASE</version>
</dependency>

Creating the Maven Project:

The best way to create the project is from the Spring Initializr – https://start.spring.io and follow the below steps –

  1. Select project as “Maven Project”
  2. Select Language as “Java”
  3. Select the Spring Boot Release Version (I would recommend not to use any SNAPSHOT version)
  4. Fill up the Group Name and the Artifact Name.
  5. Search Dependencies and Select “Eureka Server”
  6. Click on “Generate Project”.

The above steps will download a .zip file. Unzip it and import it in any IDE ( I use IntelliJ Free Community Edition. You can download it from IntelliJ Website )

For your convenience I am also providing the POM file below, also at the end of this article you will find the link of the full source code.

<?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.1.4.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
 </parent>
 <groupId>com.2darray</groupId>
 <artifactId>eureka-server</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>eureka-server</name>
 <description>2dArray Demo Project</description>

 <properties>
  <java.version>1.8</java.version>
  <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
 </properties>

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

Note : I am using the Spring Cloud version as “Greenwich.RELEASE”. As in this POM we have the “dependencyManagement” block, it will download the compatible version of “spring-cloud-starter-netflix-eureka-server” and I would recommend you to use it this way and let Spring handle the complex part. 😀

The Magic of the Annotation:

Now, you are almost done. Go to the Main file, i.e. the entry point of the Application, and you will find something like below :

package com.twodarray.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class EurekaServerApplication {

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

}

You have to just put one annotation (@EnableEurekaServer) just above the MainClass…and you are done.

package com.twodarray.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);
 }

}

Build and Run:

You can build and run your project from the dedicated button of the IDE, otherwise if you are comfortable with terminal, then use the below command to build your project :

 mvn clean package

..and to run –

mvn spring-boot:run

The Dashboard:

If everything is fine, I am sure it is, you need to open Google Chrome / Safari or whatever application you are using to explore internet and go to this URL – http://localhost:8080. You will find the Eureka Dashboard running.

Eureka Server Configuration:

Here are some extra configurations from “src/main/resources/application.properties”, which are good to know –

spring.application.name=eureka-server # Used as the name of the Application

spring.profiles.active=default # The current active profile

server.port=9091 # The port where you want to run this Eureka Server. By default it is 8080

Conclusion:

So, in this post I have shown how can you write an Eureka Server quickly and to put your first step to the micro-service architecture. You can find the full source code here in my GitHub page.

Feel free to ask any question below in the comment box. If you have liked this post… please share.

Parijat Mukherjee

Hi There !!! I am a Software Engineer and I am always in a process of learning something new. I love the works that are outside my comfort zone. Rather than focusing on a particular programming language, I try to focus on the design patterns and technologies because I believe, if someone understands the "What" and the "Why" part of a problem and the technology or tool, then the "How" part can be easily done by any of the programming language available in the market.

Recent Posts

Spring Security Architecture – Authentication

The Spring Security project is perhaps the biggest and one of the best projects so far by Spring. When I…

2 weeks ago

Make Use of SOLID Coding Principles

We all know what are the pros and cons of Object Oriented Programming, but what about writing better codes? What…

2 weeks ago

Free Birthday and Anniversary Mail Sender Software

Open Source Application to send Birthday and Anniversary greetings to the employees

1 month ago

How to Dockerize Spring Boot Applications

Article to demonstrate how to dockerize a spring boot application with all the required maven dependency plugins and Dockerfile.

2 months ago

Create a Spring Boot 2 Application & Register it in Eureka

Spring boot tutorial and a eureka client example including eureka client properties to guide the microservice beginners.

3 months ago

How to create MariaDB Galera Cluster in CentOS 6.x

Galera cluster tutorial and overview for the beginners followed by galera cluster setup centos 6 and galera cluster configuration.

2 years ago