Understanding Spring Cloud Eureka Server with Example

View more Tutorials:

Websites to learn foreign languages for free:
Follow us on our fanpages to receive notifications every time there are new articles. Facebook Twitter

1- Eureka Server - Why?

OK, I'm sure that you've ever used a Chat application to talk to your friends on the Internet. When you log in the Chat application on your computer, what will happen?
  1. You log in the Chat , which means that you inform with Chat Server that you are online.
  2. The Chat Server will provide you with a list of other people. They are online or offline status.
  3. And thus, you have discovered the list of online people and certainly, others also find out you.
In fact, when the number of Chat users increases, the number of Chat Servers increases, and they have a way to share the status of the user.
You build a distributed system that consists of a lot of services (applications) running independently on different servers. For these services are able to talk to each other, they need to "discover" each other. When discovering each other, they can call each other's services. Therefore, there needs to be something like the Chat Server to help services (applications) register its existence.
It is "Service Registration". All services (applications) of the distributed system have to be registered with the "Service Registration". It is like the fact that all people in a country must register for birth certificate.
There are many technologies for you to obtain a "Service Registration". The Spring Cloud provides you with the following technology solutions:
  1. Eureka
  2. Zookeeper
  3. Cloud Foundry
  4. Consul

2- The objective of the lesson

In this post, I am going to guide you for creating one Service Registration using Spring Cloud Eureka Server.
It will be better if you learn about more information on the Netflix and Eureka introduced in the following posts:
  • TODO Link?
The issues to be discussed in this lesson:
Step 1:
  • Create one Service Registration using Spring Cloud Eureka Server.
  • Run this application directly on the Eclipse and view Eureka Monitor.
Step 2:
  • Create replicas for application to deploy on different servers. Each replica will run a different domain name. Herein, we assume that the system has a lot of users therefore, many replicas are required to run on different servers to reduce overload 
  • Explain the status sharing mechanism between Eureka Servers.

3- Create Spring Boot project

On the Eclipse, create a Spring Boot project:
  • Name: SpringCloudServiceRegistrationEurekaServer
  • Group: org.o7planning
  • Artifact: SpringCloudServiceRegistrationEurekaServer
  • Description: Spring Cloud Service Registration (Eureka Server)
  • Package: org.o7planning.eurekaserver

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"


    <description>Spring Cloud Service Registration (Eureka Server)</description>

        <relativePath/> <!-- lookup parent from repository -->







4- @EnableEurekaServer

For this application becomes a Service Registration, you need to use @EnableEurekaServer. This Annotation informs to the Spring that you should run a registration service by Netflix technology (Netflix Eureka).

package org.o7planning.eurekaserver;

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

public class SpringCloudServiceRegistrationEurekaServerApplication {

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

# This default profile is used when running a single instance completely standalone:
  profiles: default
  port: 9000  
    hostname: my-eureka-server.com
    registerWithEureka: false
    fetchRegistry: false
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/    

# united-states, france, and vietnam illustrate running 3 intercommunicating instances.  
# This example has them running side-by-side on localhost
# -- which is unrealistic in production
# -- but does illustrate how multiple instances collaborate.
# Run by opening 3 separate command prompts:
# java -jar -Dspring.profiles.active=united-states SpringCloudServiceRegistrationEurekaServer.jar
# java -jar -Dspring.profiles.active=france SpringCloudServiceRegistrationEurekaServer.jar
# java -jar -Dspring.profiles.active=vietnam SpringCloudServiceRegistrationEurekaServer.jar

  profiles: united-states
    name: eureka-server-clustered   # ==> This is Service-Id
  port: 9001  
    hostname: my-eureka-server-us.com    
    registerWithEureka: true
    fetchRegistry: true        
      defaultZone: http://my-eureka-server-fr.com:9002/eureka/,http://my-eureka-server-vn.com:9003/eureka/

  profiles: france
    name: eureka-server-clustered   # ==> This is Service-Id   
  port: 9002
    hostname: my-eureka-server-fr.com      
    registerWithEureka: true
    fetchRegistry: true        
      defaultZone: http://my-eureka-server-us.com:9001/eureka/,http://my-eureka-server-vn.com:9003/eureka/

  profiles: vietnam
    name: eureka-server-clustered    # ==> This is Service-Id  
  port: 9003
    hostname: my-eureka-server-vn.com    
    registerWithEureka: true
    fetchRegistry: true    
      defaultZone: http://my-eureka-server-us.com:9001/eureka/,http://my-eureka-server-fr.com:9002/eureka/   
In this application, I configure on the application.yml file instead of application.properties. You can learn more about difference between *.yml file and *.properties file under the following:
  • TODO Link?

5- Run the application on Eclipse

When you run this application directly on Eclipse, the default profile will be used to configure this application.
Eureka Monitor allows you to see the list of active and registered services (applications) with this Eureka Server, and see the replicas of this application running on the distributed system. You can access the Eureka Monitor by the URL below,
"Default" profile is configured to run on my-eureka-server.com domain name. By configuring the domain name on the "hosts" file, you can obtain such domain name.
On the Windows, open the file:
  • C:\Windows\System32\drivers\etc\hosts
Add the following configuration snippet:
C:\Windows\System32\drivers\etc\hosts       my-eureka-server.com       my-eureka-server-us.com       my-eureka-server-fr.com       my-eureka-server-vn.com
To edit the "hosts" file in other operating systems, you can see the following instruction:
And you have obtained my-eureka.com domain name. It is noted that this domain name works only on your computer.

6- Run many replicas

First of all, you need to use the "Maven Install" function to create the jar file. Right click on the project and select:
  • Run As/Maven Install
After running "Maven Install" successfully, you get a jar file located in the target directory of the project.
Copy the jar file just created to a directory and create 3 CMD files:
  • eureka-server-us.bat
  • eureka-server-fr.bat
  • eureka-server-vn.bat

java -jar -Dspring.profiles.active=united-states SpringCloudServiceRegistrationEurekaServer-0.0.1-SNAPSHOT.jar

java -jar -Dspring.profiles.active=france SpringCloudServiceRegistrationEurekaServer-0.0.1-SNAPSHOT.jar

java -jar -Dspring.profiles.active=vietnam SpringCloudServiceRegistrationEurekaServer-0.0.1-SNAPSHOT.jar
We are simulating the creation of three replicas and deployment of them on three different servers, with three different domain names.
Run the 3 above BAT files.
OK, now, you can access the following links and view the results:

7- Eureka Discovery (Eureka Client)

OK, Next Lesson:

View more Tutorials: