By Marone: June 2020

Spring Cloud Gateway

spring Cloud Gateway

Table of contents


In this article we will learn how to use Spring Cloud Gateway, especially how to route the incoming requests.

Used tools

Java 11
Maven 3.x
cURL 7.x

What is Spring Cloud Gateway

Spring Cloud Gateway is a non blocking and a open source API Gateway. It is built on top of the Spring Ecosystem, including: Spring 5, Spring Boot 2 and Project Reactor. The Gateway is about routing requests/responses between clients and API's and provide some central cross-cutting capabilities such as security, metrics, ...etc.


There are two ways to create Routing, using java DSL or by using configuration files. In this article we will use the Java Way.

Building blocks

A route is matched if aggregate predicate is true

Maven dependency

Create Routing

There are three routes defined.

Simple Route

It has an ID simple_route and a destination and one predicate /get

The send request is http://localhost:9001/get , Spring reads the request URL and checks if the path equals to /get. If it is true the request will be forward to the target destination . Just keep in mind the path part has been appended to end of the destination

The external Route

This route is almost similar to the previous one except the filter part. With stripPrefix(1) the path part will be stripped from the target url
Instead of sending the gateway sends only

The Host Route

The third route is little bit different, it has an ID and a destination BUT a predicate based on host. The request sould contain a special information to indicate the host.

In order to use host you have to map the IP address to host name. The hosts file on windows will look like:

The host information has been send as a request header


The complete code can be found in GitHub