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.
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.
Service | Service Name | Port |
---|---|---|
Eureka Registry | eureka-service | 8080 |
Zuul Proxy | zuul-server | 8100 |
Service 1 | book-service | 8200 |
Service 2 | netflixseries-service | 8300 |
To do the full build, do: mvn clean install
in root dir of the project or do one build for each module directory
To run a Spring Boot module run the command: mvn spring-boot:run
in the module directory
After building the project, use the run.sh
script 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
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