Spring Cloud: Creating first client application With eureka client (Part 2)

This is a quick tutorial for creating a eureka client application. This tutorial has the prerequisite of a running eureka server. In case if you do not have running eureka server or a newbie, check out my previous post in this series which explains how to setup eureka server yourself. The eureka client which we will be developing in this tutorial will be registered in the eureka server.

TL;DR You can download whole project by clicking following link.

We will be creating a Eureka client in this tutorial. The client will be a spring boot application and will expose one rest endpoint. That endpoint will accept one value (or String, as said in Java world) as path parameter and will prefix it with ‘Hello’. The endpoint will return its result as String again.

This post is a part of following series.

  1. Part 1 – Setting Up Eureka Server Using Spring Cloud
  2. Part 2 – Developing a Eureka client and registering it with Eureka Server.
  3. Part 3 – Developing Eureka client and consuming service exposed by another Eureka client.

For this tutorial, we will be creating a Maven Project. To keep thing more simple, you may proceed with a simple maven project.

Our project structure is given below. We will be creating three files which are as follows:

  1. AsmProducerApplication.java – Spring boot runable file
  2. ProducerController.java – Rest controller
  3. application.properties – Spring properties file
Project Structure

Let’s start by adding the required dependencies. Replace content in pom.xml file with the following content. Don’t forget to update the project using Maven > Update option. That will download all the necessary dependencies.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
		<relativePath /> <!-- lookup parent from repository -->








Create file AsmProducerApplication.java and add the following content. This file will serve as the entry point for our application.

package com.example.asmproducer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

public class AsmProducerApplication {

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


If you have noticed here, we have used two annotations on this class.

  1. @EnableDiscoveryClient – This annotation will do the magic for you. It will make your application as Eureka client and will register itself to Eureka server. Essentially it will do all the hard work for you.
  2. @SpringBootApplication – The famous spring boot annotation

Create file ProducerController.java and add the following content. This will be our rest controller and will have endpoint related stuff. Our endpoint is quite simple in this case. It just takes one argument as path parameter, prefixes it with ‘Hello’ and will return the resulting String.

package com.example.asmproducer;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

public class ProducerController {

	public String sayHello(@PathVariable String name) {
		return "Hello " + name;


Under resources folder create a file with name application.properties and add the following content


We have specified three properties over here. Those are as follows:

  1. spring.application.name – Our application name. You will find this in the eureka dashboard. We will also use it to refer to this service in our future tutorial.
  2. eureka.client.serviceUrl.defaultZone – Url of eureka server
  3. server.port – The port which our server will use.

Now you can run AsmProducerApplication.java. This will invoke spring boot and will start the server. Once the server is started it will register automatically itself to the eureka instance. You can now refresh your eureka dashboard and you should able to see this server under ‘Instances currently registered with Eureka’ section.

Eureka dashboard: Client registered with name ‘ASMPRODUCER’

That’s great. Let’s see if we can invoke our service or not. For that, you will need to navigate to ‘http://localhost:11801/hello/yourname’ URL. Just replace ‘yourname’ with your actual name and the application should greet you as shown below.

Greetings shown after navigating to url.

Cool :-). That’s all for this tutorial.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.