DEV Community

Cover image for Creating an API Gateway with Spring Boot
William
William

Posted on

Creating an API Gateway with Spring Boot

An API Gateway acts as a single entry point for client requests, routing them to appropriate backend services. It simplifies client interactions, enhances security, and provides features like load balancing, authentication, and monitoring.

What is an API Gateway and Why Use It?

An API Gateway(design pattern) is a server that handles incoming API requests and directs them to the relevant microservices. It serves as a reverse proxy, abstracting the complexity of a microservices architecture. Common use cases include:

  • Routing: Directing requests to the correct service.
  • Authentication/Authorization: Centralizing security checks.
  • Rate Limiting: Controlling request volumes.
  • Monitoring: Tracking API usage and performance.

Pros of Using an API Gateway

  • Simplifies client-side code by providing a unified interface.
  • Centralizes cross-cutting concerns like security and logging.
  • Improves scalability by distributing requests efficiently.
  • Enhances maintainability by decoupling clients from services.

API Gateway with Spring Boot

Spring Boot, combined with Spring Cloud Gateway, is a powerful choice for building an API Gateway due to its simplicity and robust ecosystem. Below, we’ll walk through the setup, including Maven dependencies and basic configuration.
The version of spring boot will be 3.4.3 and spring cloud 2024.0.1.

Step 1: Maven Dependencies
To get started, create a Spring Boot project and include the necessary dependencies in your pom.xml.

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> 
Enter fullscreen mode Exit fullscreen mode

Step 2: Basic Configuration

Configure the API Gateway in the application.yml file and Eureka Server.

Information about the port, application name and Eureka server.

server: port: 8080 spring: application: name: api-gateway eureka: instance: preferIpAddress: true client: service-url: defaultZone: http://localhost:8761/eureka fetch-registry: true register-with-eureka: true 
Enter fullscreen mode Exit fullscreen mode

To exposes actuator endpoints for monitoring:

management: endpoints: web: exposure: include: health,info,gateway 
Enter fullscreen mode Exit fullscreen mode

Step 3: Route

Create a Bean RouterLocator to configure the route from Api Gateway to microservice, is possible to configure your circuit break and Header here.

@Bean public RouteLocator routeConfig(RouteLocatorBuilder routeLocatorBuilder) { return routeLocatorBuilder.routes() .route(p -> p .path("/<PATH_FROM_CLIENT>/**") .filters( f -> f.rewritePath("/<PATH_FROM_CLIENT>/(?<segment>.*)","/${segment}") .addResponseHeader("X-Response-Time", LocalDateTime.now().toString()) .uri("lb://<MICROSERVICE_APP_NAME>")) .build(); } 
Enter fullscreen mode Exit fullscreen mode
  • PATH_FROM_CLIENT: Inform the path that clients will call
  • MICROSERVICE_APP_NAME: Inform spring.application.name of the microservice of destination. With the help of Eureka Server, API Gateway will find the given microservice.

In this post the microservice name will be client-service and path client-service, but could be any value you desire.

Step 4: Test

With the Bean RouteLocator now all request send to /client-service/** will be redirect to microservice called client-service.

Postman Request Api-Gateway

If you test request direct to client-service, not using API Gateway, will have the same result.

Postman Request Client-Service

In the production application, the client service will be exposed only in the private network, and only through the Api Gateway will it be exposed.

Conclusion

An API Gateway is a vital component in a microservices architecture, simplifying client interactions and centralizing concerns like routing and security. With Spring Boot and Spring Cloud Gateway, setting up a basic API Gateway is straightforward, requiring minimal dependencies and configuration.
By following the steps above, you can quickly create a gateway to route requests to your services, paving the way for advanced features like authentication and rate limiting in future iterations.

Top comments (0)