Session 6.1: Microservices – API Gateway
1. Clone the week7 branch of the course repository
$ git clone https://github.com/M-Gharib/ESI-W6.git
Note: if you want to create a new Spring Boot project from scratch, you need to install the following dependencies for both the Product and Inventory services:
- Spring Web
- Spring Data JPA SQL
- PostgresSQL Driver SQL
- Lombok
- Spring Reactive Web
- Validation I/O
- Eureka Discovery Client
- Cloud Loadbalancer
For the Discovery service, you need to install the following dependencies:
- Lombok
- Eureka Server
For the API Gateway, you need to install the following dependencies:
- Gateway - Spring Boot Routing
- Eureka Discovery Client
The project you cloned is composed of four different Spring Boot projects that represent two different microservices, a discovery server (the same as last week), and a service to work as our API Gateway. Without the API Gateway service, the application "architecture" is shown in the following diagram.
Implementing Spring Cloud API Gateway
1. the API Gateway has already been created as a Spring Boot project for this application, but you still need to register it with your discovery server, uncomment the @EnableDiscoveryClient
annotation above the main class of the ApiGatewayApplication
, and check the configurations for registering with the discovery server in application.properties
.
server.port=8080 spring.application.name=api-gateway spring.cloud.discovery.enabled=true eureka.client.serviceUrl.defaultZone = http://localhost:8761/eureka eureka.client.instance.preferIpAddress = true
2. Run the discovery server
. Then, run product-service
, inventory-service
. When you have services that need to register with a discovery server, they need to find it. That is why we run discovery server
first. discovery server
should run on port 8761. You can visit http://localhost:8761/ to see which services are registered with it (under "Instances currently registered with Eureka
").
3. Send the following request, in RestClientFile.rest
. It should work.
### Get All products GET http://localhost:8082/api/products
Now, we need to configure our API Gateway so requests for our services will be passed through it.
4. The routes for the discovery server
and product-service
are already defined in the application.properties
of the API Gateway, you just need to uncomment them, and your teacher will describe them for you.
... ################ Discover Server ##################### # spring.cloud.gateway.routes[0].id=discovery-server # spring.cloud.gateway.routes[0].uri=http://localhost:8761 # spring.cloud.gateway.routes[0].predicates[0]=Path=/eureka/web # spring.cloud.gateway.routes[0].filters[0]=SetPath=/ ####### product-service ############## # spring.cloud.gateway.routes[1].id=product-service # spring.cloud.gateway.routes[1].uri=http://localhost:8082/ # spring.cloud.gateway.routes[1].predicates[0]=Path=/api/products/** ...
5. After uncommenting the routes configurations, run your services again, visit http://localhost:8761/ to be sure that all your services are registered with the discovery service, as follows:
6. Send the following requests, in RestClientFile.rest
. Both should work and return the same result.
### Get All products GET http://localhost:8082/api/products ### Get All products - port 8080 GET http://localhost:8080/api/products
7. Based on what you have learned, define the routes for the inventory-service
in the application.properties
of the API Gateway service. Then, send the following requests, in RestClientFile.rest
. Both should work and return the same result.
### Get a quantity of a product based on its code - port 8083 GET http://localhost:8083/api/inventory/p-109-09 ### Get a quantity of a product based on its code- port 8080 - API GET http://localhost:8080/api/inventory/p-109-09
By now, the "architecture" of our application should be like the following diagram.