![]() |
VOOZH | about |
In microservices architecture, an API Gateway acts as the single entry point for client applications to interact with multiple microservices. It manages requests, routes them to the appropriate services, and can handle cross-cutting concerns like authentication, logging, and rate limiting. A well-known example of an API Gateway is Netflixβs Zuul.
Spring Cloud Gateway provides a simple, flexible way to implement API gateways in Spring Boot applications.
π API-Gateway-in-Microservices
Spring Cloud Gateway is a library built on Spring Framework 5, Project Reactor, and Spring Boot 2.x. It provides:
Note: Please refer to this article to know more about Spring Cloud Gateway.
For Maven:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
For Gradle:
implementation("org.springframework.cloud:spring-cloud-starter-gateway")
To disable the gateway, if needed:
spring.cloud.gateway.enabled=false
pom.xml Example:
server:
port: 8085
spring:
application:
name: API-GATEWAY-SERVICE
cloud:
gateway:
routes:
- id: DEMO-SERVICE
uri: http://localhost:9090
predicates:
- Path=/demo/**
Create a Spring Boot microservice with Spring Web dependency.
DemoController.java
To run the microservice at port 9090.
application.properties:
server.port=9090
Access the microservice directly:
http://localhost:9090/demo/gfg
And you are going to get a response like this
Now, you can access the microservice via the API Gateway too:
http://localhost:8085/demo/gfg
And you are going to get a response like this
Both URLs return the same response. The API Gateway routes requests without requiring the client to know microservice ports.