GithubHelp home page GithubHelp logo

spring-cloud-gateway's Introduction

API Gateway using Spring-Cloud-Gateway

Why do Microservices need API Gateway

  • An API gateway is a software application between a client and a set of backend microservices.
  • The API Gateway serves as a reverse proxy to accept API calls from the client application, forwarding this traffic to the appropriate service.
  • The gateway provides a single endpoint or URL for the client apps and then internally maps the requests to a group of internal microservices.

img.png

Netflix ZUUL vs Spring-Cloud-Gateway

  • Zuul is a blocking API. - A blocking gateway api makes use of as many threads as the number of incoming requests. So this approach is more resource intensive. If no threads are available to process incoming request then the request has to wait in queue.
  • Spring Cloud Gateway is a non blocking API. - When using non blocking API, a thread is always available to process the incoming request.
    These request are then processed asynchronously in the background and once completed the response is returned. So no incoming request never gets blocked when using Spring Cloud Gateway.

Spring-cloud-gateway

  • An API Gateway acts as a single entry point for a collection of microservices.
  • Any external client cannot access the microservices directly but can access them only through the application gateway
  • In a real world scenario an external client can be any one of the three-
    • Mobile Application
    • Desktop Application
    • External Services or third party Apps

Note:

  • To include Spring Cloud Gateway in your project, use the starter with a group ID of org.springframework.cloud and an artifact ID of spring-cloud-starter-gateway.
  • If you include the starter, but you do not want the gateway to be enabled, set spring.cloud.gateway.enabled=false.
  • Spring Cloud Gateway is built on Spring Boot 2.x, Spring WebFlux, and Project Reactor.
  • Spring Cloud Gateway requires the Netty runtime provided by Spring Boot and Spring Webflux. It does not work in a traditional Servlet Container or when built as a WAR.

image

Spring Cloud Gateway Architecture

Spring Cloud Gateway is API Gateway implementation by Spring Cloud team on top of Spring reactive ecosystem.

  • It consists of the following building blocks::
    -Route: Route the basic building block of the gateway. It consists of :
    * ID
    * Destination URI
    * Collection of predicates and a collection of filters
  • A route is matched if aggregate predicate is true.
  • Predicate: This is similar to Java 8 Function Predicate. Using this functionality we can match HTTP request, such as headers , url, cookies or parameters.
  • Filter: These are instances Spring Framework GatewayFilter. Using this we can modify the request or response as per the requirement.

image

A simpified diagram provided by Spring Cloud Gateway official website :

img.png

When the client makes a request to the Spring Cloud Gateway, the Gateway Handler Mapping first checks if the request matches a route. This matching is done using the predicates. If it matches the predicate then the request is sent to the filters.

  • PreFilter = Decides which downstream microservice needs to be called (i.e Service 1 or Service 2), it also modifies the request if required.
  • PostFilter = Post filter will be called once response needs to be send to a calling consumer, it also modifies the response if required.

Sample API Gateway Routing application.yml

img.png

References:

spring-cloud-gateway's People

Contributors

gauravshekhar13 avatar zanyg avatar

Stargazers

Erik Allais avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.