1

Spring Microservices with Netflix Stack – Create the Eureka Client

[Social9_Share type="horizontal"]

This is the Part – II post of the Spring Microservices with Netflix stack series. In this post, let us create a RESTful Service and register it with the Eureka Server. This will be my Eureka Client. To learn how to create Eureka Server which is a service registry, move to Part-I.

Create a spring boot application

Create a spring boot application using spring starter project in eclipse or from http://start.spring.io/. I am giving the artifact name as hotel-service. Add Web, Eureka Discovery as dependencies. The application is created and gets downloaded. Now, import this as a maven project in eclipse.To know more on how to create a RESTful application in detail using Spring Boot in detail, click here.

Now, lets get started

restapp

pom.xml

The dependencies in pom.xml

application.yml

Just give the application name and server port number in application.yml

Model Class

Create a class Hotel with instance variables and getter and setter methods.

Controller Class

Create a class and annotate with @RestController. Add two methods and annotate with @RequestMapping.

Main Class – Eureka Client

Annotate the main class with @EnableEurekaClient. This annotation helps to register the RESTful service in the service registry(Eureka).

Run the application as Spring Boot App. Check your REST service that is running up in

http://localhost:8081/greet/Jack
http://localhost:8081/greet/all-hotels

Eureka Console

Now, lets also check whether this service is registered with the service registry. Open the eureka console at http://localhost:8761. You can see that the hotel service application is registered

eureka-restconsole1

Test the Eureka Client

Click on the RESTful Service in the Eureka Console. It gets opened in the browser. Give the proper path (/all-hotels) and check for the expected output. You can see the output now in the browser as given below.

finalrest

Thats it. In the next post Part-III, let us create a Feign Client, and access this rest service using the feign client.

Post a comment