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"
	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 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;

@EnableDiscoveryClient
@SpringBootApplication
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;

@RestController
public class ProducerController {

	@GetMapping("/hello/{name}")
	public String sayHello(@PathVariable String name) {
		return "Hello " + name;
	}

}

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

spring.application.name=ASMProducer
eureka.client.serviceUrl.defaultZone=http://localhost:11800/eureka
server.port=11801

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.


Downloads:

Leave a Reply

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