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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here