Api Gateway Using Spring Boot Cloud Gateway + Netflix Hystrix
An API gateway is an interface between clients and backend microservices. When a gateway is used, it becomes the single point of contact for clients; it receives their API calls and routes each one to the appropriate backend.
It facilitates microservice architectures. When an API gateway is used, clients do not know (nor should they know) the structure of the backend. Modern architectures discourage the use of large monolithic services; rather, numerous small microservices are preferred. This approach provides some compelling advantages, but it does introduce significant complexity. An API gateway mitigates this for the client.
What is Spring Cloud Gateway?
Spring Cloud Gateway provides a library for building API gateways on top of Spring and Java. It provides a flexible way of routing requests based on a number of criteria, as well as focuses on cross-cutting concerns such as security, resiliency, and monitoring.
Spring Cloud allows developers to implement things such as distributed configuration, service registration, load balancing, the circuit breaker pattern, and more. It provides these tools to implement many common patterns in distributed systems.
Spring cloud gateway is working by clients make requests to Spring Cloud Gateway. If the Gateway Handler Mapping determines that a request matches a Route, it is sent to the Gateway Web Handler. This handler runs sends the request through a filter chain that is specific to the request. The reason the filters are divided by the dotted line, is that filters may execute logic before the proxy request is sent or after. All “pre” filter logic is executed, then the proxy request is made. After the proxy request is made, the “post” filter logic is executed.
Project Setup and Dependency
I’m depending Spring Initializr for this as it is much easier.
We need import spring bloot cloud gateway dependencies, such as:
pom.xml like following below.
application.properties file to setup spring cloud gateway and actuator like following below.
Gateway Filter Factories
Route filters allow the modification of the incoming HTTP request or outgoing HTTP response in some manner. Route filters are scoped to a particular route. Spring Cloud Gateway includes many built-in GatewayFilter Factories.
In this step we will rewrite path from spring cloud gateway to another service, we will use my Spring Boot Hello World project as a microservice. Clone and run the project by
mvn spring-boot:run command. Or you can run the Spring Boot Hello World Container and run with port
Add gateway filter factory with creating new file
application.yml inside main resources.
Run spring boot cloud gateway
http://localhost:8080/springboot-helloworld/. The result should be has response from
For more documentation of
GatewayFilterFactories please visit Spring Cloud Gateway.
Resilience4j: Circuit Breaker Implementation
What is circuit breaker?
The concept of a circuit breaker is to prevent calls to microservice when it’s known the call may fail or time out. This is done so that clients don’t waste their valuable resources handling requests that are likely to fail. Using this concept, you can give the server some spare time to recover.
So, how do we know if a request is likely to fail? Yeah, this can be known by recording the results of several previous requests sent to other microservices. For example, 4 out of 5 requests sent failed or timeout, then most likely the next request will also encounter the same thing.
In the circuit breaker, there are 3 states Closed, Open, and Half-Open.
- Closed: when everything is normal. Initially, the circuit breaker is in a Closed state.
- Open: when a failure occurs above predetermined criteria. In this state, requests to other microservices will not be executed and fail-fast or fallback will be performed if available. When this state has passed a certain time limit, it will automatically or according to certain criteria will be returned to the Half-Open state.
- Half-Open: several requests will be executed to find out whether the microservices that we are calling are working normally. If successful, the state will be returned to the Closed state. However, if it still fails it will be returned to the Open state.
One of the libraries that offer a circuit breaker features is Resilience4J for reactive web flux. We will adding the dependency in
In the main
Application add annotation
@EnableHystrix. And then create
ApiResponse to handle when service backend is unavailable.
So, that mean when service backend is unavailable will be redirected to
/fallback as soon as possible.
application.yml like following below.
Let’s try to shutdown the
springboot-helloworld service, then go to
Logging with Global Filter
Global filters are executed for every route defined in the API Gateway. The main difference between pre-filter and post-filter class is that the pre-filter code is executed before Spring Cloud API Gateway routes the request to a destination web service endpoint. While the post-filter code will be executed after Spring Cloud API Gateway has routed the HTTP request to a destination web service endpoint.
Execute pre filter to call
RequestBodyRewriter then can be used for logging.
Execute post filter to call
ResponseBodyRewriter and construct the log message and then can be used for logging.
In this article, we explored some of the features and components that are part of Spring Cloud Gateway. This new API provides out-of-the-box tools for gateway and proxy support.
- What is Spring Cloud Gateway?
- Resilience4J: Circuit Breaker Implementation on Spring Boot
- Spring Cloud API Gateway Global Filter Example
- Microservices using SpringBoot | Full Example
- Spring Cloud Gateway
- Spring Cloud Gateway - Proxy/Forward the entire sub part of URL
- Spring Cloud Circuit Breaker
- Writing Custom Spring Cloud Gateway Filters
- Get request body string from ServerHttpRequest / Flux
- How to get response body in Spring Gateway