Microservices Example using Spring Cloud Eureka

In this tutorial, we will create a demo microservice using spring boot framework in java.

Tools Required

  • Java 8
  • IntelliJ IDE

We need to create three different applications as below –

  1. Eureka Service–  This Service will register every microservice and then the client microservice will look up the Eureka server to get a dependent microservice to get the job done.This Eureka Server is owned by Netflix and in this, Spring Cloud offers a declarative way to register and invoke services by using Java annotation.
  2. demo-server – This service will return a simple hello message.
  3. demo-client – It is similar to the standalone client service created in Bootiful Development with Spring Boot. It will consume the APIs provided by demo-server through Eureka Service .

1. Eureka Service

We need to create new project in intelliJ editor by selecting below dependencies –

Project Setup
Maven dependencies

You need to do below configurations to create Eureka service which allows other microservices to register with it .

package com.myjavablog;

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

public class EurekaServiceApplication {

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


You need to create application.yml to configure your EurekaService project as register service. Other services will be able to register themselves with EurekaService. So you need to do below configurations –

    registerWithEureka: false
    fetchRegistry: false
      waitTimeInMsWhenSyncEmpty: 0

Now you can run the application . Once the application is up and running you can go to below URL and see EurekaService is working . You will be able to see below home page of eureka-

As no instances are registered with EurekaService , you can see No instances available message as highlighted above . Now we will create services which will register with our Eureka Server.

2. Demo Server

This is a simple REST based webservice. This will be registering itself with the EurekaService. This service has a simple API which is just returning hello message. Create a project structure as shown below in IntelliJ IDE –

Maven Dependencies required

Project structure is as shown below –

You need to include Eureka Discovery client dependency in your pom file.

Once the application is up and running , you can see instance of demo-server registered on EurekaService as shown below –

3. Demo Client

Same way as Demo server , we need to create demo client which will consume services provided by demo-server through EurekaService.

demo-client will have autowired RestTemplate to call the services provided by demo-server.

package com.myjavablog.democlient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

public class TestController {

    public RestTemplate restTemplate;

    public String test(){
        String url = "http://demo-server/demo/hello/server";
        return restTemplate.getForObject(url, String.class);

Also you can see in the above code , we are using String url = “http://demo-server/demo/hello/server”

demo-server is the name specified in the application.yml file of demo-server project . So instead of using http://localhost:8071/demo/hello/server we are using http://demo-server/demo/hello/server .

demo-server is the name registered on Eureka server for demo-server application as shown below –

    name: demo-server

  port: 8071

    registerWithEureka: true
    fetchRegistry: true
      defaultZone: http://localhost:8070/eureka/
    hostname: localhost

Once the application is up and running , you can see the demo-client is registered on Eureka Server as below –

When you call the APIs from demo client ,it will internally call demo-server APIs through Eureka Service . Eureka server handles all routing and load balancing part. So even if you scale up your services in future by creating multiple instances, you can go on and use the services with the name which is registered on Eureka server.

Github Downlod Link:

Leave a Comment

Close Bitnami banner