Spring Cloud: Creating Student Service With Eureka

In this post we will be creating an 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 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 setup 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 we 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 required dependencies. Replace content in pom.xml file with following content. Don’t forget to update the project using Maven > Update Project option. That will download all necessary dependencies.

<?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 https://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.3.1.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>asm-producer</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>asm-producer</name>
	<description>Producer</description>

	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Hoxton.SR6</spring-cloud.version>
		<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</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>

Create file AsmProducerApplication.java and add 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;

@EnableDiscoveryClient
@SpringBootApplication
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() {
		super();
	}

	public Student(int id, String name, String enrolledCourse) {
		super();
		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 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;

@RestController
public class ProducerController {

	@GetMapping("/student/{studentId}")
	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;
		}
	}

	@GetMapping("/student/all")
	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 repository for time being.


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

spring.application.name=StudentProducer
eureka.client.serviceUrl.defaultZone=http://localhost:11800/eureka
eureka.instance.instanceId=:
server.port=0

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

  1. spring.application.name – Our application name. You will find this in 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, application will use that port and you will not able to launch that application multiple times on 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 eureka dashboard.

Leave a Reply

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