Pedro-Matos / spring-zuul

Spring boot microservices with zuul and eureka

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Pre-Interview Exercise for RHO

Project developed under Java 8 and Spring Boot 2.1.6

In this exercise I was asked to build 4 microservices that would work with Zuul Proxy and Service Discovery.

The microservices were the following:

  • Service Registry (Opted for Eureka)
  • Zuul Proxy that would handle the requests and deal with the routing of each microservice
  • A service that would give a list of anything
  • Another service that would give another list

To resolve this exercise I created four directories, where each one contains one of the microservices. These microservice's skeletons were created with the help of Spring Initialz. Used some dependencies like Spring Web Starter, Eureka Server and Eureka Discovery Client, Zuul and Thymeleaf.

Decided to use Eureka because it seemed to have a simpler implementation and bigger focus on service discovery as seen here.

I also decided to add Thymeleaf to create templates to show the list of data in a tabular way, instead of showing just a json-esque document. In one of services I present a table with information about books and in the other I present information about netflix shows.

Service Discovery

When using Zuul Proxy I found that is usual to register the routes in the configuration files like application.properties, where it's possible to declare the routes like this:

zuul.routes.book.url=http://localhost:8081/

In this case, this line would allow the zuul proxy to forward any /book request to localhost:8081

Yet, to any other service added would be necessary to declare the route and the port to where forward the request. When using service discovery it's not necessary to register the routes in the zuul proxy configurations.

The server(eureka) can be configured and deployed to be highly available @EnableEurekaServer, with each client registering in the server @EnableEurekaClient and providing meta-data, like the service name and port, about itself for the zuul proxy be able to correctly route the available services.

Routes

Service Service Name Port
Eureka Registry eureka-service 8080
Zuul Proxy zuul-server 8100
Service 1 book-service 8200
Service 2 netflixseries-service 8300

Building the Project

To do the full build, do: mvn clean install in root dir of the project or do one build for each module directory

Running a Spring Boot module

To run a Spring Boot module run the command: mvn spring-boot:run in the module directory

Usage Example

After building the project, use the run.shscript to start the project and use the kill.sh to stop the project and kill all the PIDs

The zuul proxy can take a couple seconds to discover the routes for the services.

To test the endpoints:

  • localhost:8200/ - list in tabular format from book service
  • localhost:8300/ - list in tabular format from tv show service
  • localhost:8100/ - welcome message
  • localhost:8100/book-service – list in tabular format from book service
  • localhost:8100/netflixseries-service – list in tabular format from tv show service

Problems Found

During the execution of this exercise I run into the following problem:

Caused by: java.net.UnknownHostException: nodename nor servname provided, or not known

My hostname was not being recognized and the services were having problems when the zuul proxy tried to route the requests and couldn't recognize it. The solution is quite simple, it's necessary to add the hostname to the host files:

echo "127.0.0.1 $HOSTNAME" | sudo tee -a /etc/hosts

About

Spring boot microservices with zuul and eureka


Languages

Language:Java 74.5%Language:HTML 20.0%Language:Shell 5.5%