Spring Cloud: Creating Student Service With Eureka

In this post, we will be creating a eureka client application. We will be creating a sample Student service. This service will have two functionalities viz. to retrieve information about all students or you can pass a specific student id and retrieve information of that particular student.

We will be also making some changes in the application.properties file to spin multiple instances of this application on same/one machine.

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

Before we begin, you will need to set up the eureka server. In case if you don’t know how to setup eureka server then please checkout Setting Up Eureka Server Using Spring Cloud first.

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

Our project structure is given below, you may refer to it. We will be creating three files which are as follows:

  1. AsmProducerApplication.java – Spring boot runable file
  2. ProducerController.java – Rest controller
  3. Student.java – our model/pojo class
  4. 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 Project 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. You can create this file in any package you want. 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);


As you saw, we have used two annotations on this class.

  1. @EnableDiscoveryClient – This will make our application as Eureka client and will register itself to Eureka server.
  2. @SpringBootApplication – The famous spring boot annotation.

Create class Student.java. This will be our POJO/model class. It has only 3 attributes viz, id, name and enrolled course.

package com.example.asmproducer;

import java.io.Serializable;

public class Student implements Serializable {

	private int id;

	private String name;

	private String enrolledCourse;

	public Student() {

	public Student(int id, String name, String enrolledCourse) {
		this.id = id;
		this.name = name;
		this.enrolledCourse = enrolledCourse;

	public int getId() {
		return id;

	public void setId(int id) {
		this.id = id;

	public String getName() {
		return name;

	public void setName(String name) {
		this.name = name;

	public String getEnrolledCourse() {
		return enrolledCourse;

	public void setEnrolledCourse(String enrolledCourse) {
		this.enrolledCourse = enrolledCourse;


Create file ProducerController.java and add the following content. This will be our rest controller. You can see we have created two endpoints. “/student/{studentId}” endpoint will return student information for given student id and “/student/all” will return information about all students.

package com.example.asmproducer;

import java.util.List;
import java.util.Optional;

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

import com.google.common.collect.Lists;

public class ProducerController {

	public Student getStudent(@PathVariable int studentId) {
		Optional<Student> student = getStudentRepository().stream().filter(std -> std.getId() == studentId).findAny();
		if (student.isPresent()) {
			return student.get();
		} else {
			return null;

	public List<Student> getAllStudents() {
		return getStudentRepository();

	private List<Student> getStudentRepository() {
		List<Student> students = Lists.newArrayList();
		students.add(new Student(1, "Ankur", "Spring cloud"));
		students.add(new Student(2, "Tushar", "Spring boot"));
		students.add(new Student(3, "Akshay", "Spring cloud"));
		students.add(new Student(4, "Pratik", "Spring data"));
		return students;


To keep things simple, we have added getStudentRepository() function which will serve as the repository for time being.

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


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

  1. spring.application.name – Our application name. You will find this in the eureka dashboard.
  2. eureka.client.serviceUrl.defaultZone – Url of eureka server
  3. server.port – In this case, the value is ‘0’. This means whenever you run this application, it will assign a random port to itself.
  4. eureka.instance.instanceId – We are leaving instance id to eureka server.

There is only one thing you need to take care of i.e. server.port=0. If you specify a server port, the application will use that port and you will not able to launch that application multiple times on the same machine. Keeping this value as 0 will initialize application to a random port on each launch.

Now you can run the application. Once the application is running you should see an instance registered to Eureka server i.e. in the eureka dashboard.


Leave a Reply

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