#
Spring Cloud Gateway
This tutorial explains how to build a very simple Spring Cloud Gateway. This tutorial contains an example as well.
A gateway
is a computer (software) that sits between different networks or applications.
The gateway converts information, data or other communications from one protocol or format to another.
Practically the gateway, receive a request and can transform it in any manner (for redirecting, security reason,
adding/removing information from the body or from the header).
With Spring Boot, a simple Spring Cloud Gateway application is done by downloading a simple application from
spring initializr. We need to add Gateway
dependency as in the image below:
After that we need to add the routing information. This can be done in a Java class or in the config file.
In my example I will add the following into the application.yaml file:
server:
port: 8080
logging:
level:
root: info
no.embriq.api.gateway: debug
spring:
cloud:
gateway:
routes:
- id: "route name #1"
uri: "http://www.hotmail.com"
predicates:
- Path=/hotmail
That's all. When a request hits the predicate (http://localhost/hotmail
), the request is forwarded to uri (http://www.hotmail.com
).
The same thing can be done using RouteLocator
Bean.
You can see some example here as well.
With Spring Cloud Gateway you can modify a request as you wish. This could be done in a classical manner, but
spring-cloud-starter-gateway
dependency helps you a lot on this.