Spring Cloud Tutorial - Spring Cloud Gateway Filters Example
Using Predicates Spring Cloud Gateway determines which route should get called. Once decided the request is the routed to the intended microservice. Before routing this request we can apply some filters to the request. These filters are known as pre filters. After applying the filters the intended micoservice call is made and the response is returned back to the Spring Cloud Gateway which returns this response back to the caller. Before returning the response we can again apply some filters to this response. Such filters are called post filters.
As specified in the Spring Cloud Gateway Documentation, Spring Cloud provides a number of built in filters. Also we can create our own custom filter to suit our business requirement. In this tutorial we will be looking at the various Filters that can be used with Spring Cloud Gateway.
Spring Cloud - Table Of Contents
Microservice Registration and Discovery with Spring cloud using Netflix Eureka- Part 1. Microservice Registration and Discovery with Spring cloud using Netflix Eureka - Part 2. Microservice Registration and Discovery with Spring cloud using Netflix Eureka - Part 3. Microservice Registration and Discovery with Spring cloud using Netflix Eureka - Part 4. Spring Cloud- Netflix Eureka + Ribbon Simple Example Spring Cloud- Netflix Eureka + Ribbon + Hystrix Fallback Simple Example Spring Cloud- Netflix Hystrix Circuit Breaker Simple Example Spring Cloud- Netflix Feign REST Client Simple Example Spring Cloud- Netflix Zuul +Eureka Simple Example Spring Cloud Config Server using Native Mode Simple Example Spring Cloud Config Server Using Git Simple Example Spring Boot Admin Simple Example Spring Cloud Stream Tutorial - Publish Message to RabbitMQ Simple Example Spring Cloud Stream Tutorial - Consume Message from RabbitMQ Simple Example Spring Cloud Tutorial - Publish Events Using Spring Cloud Bus Spring Cloud Tutorial - Stream Processing Using Spring Cloud Data Flow Spring Cloud Tutorial - Distributed Log Tracing using Sleuth and Zipkin Example Spring Cloud Tutorial - Spring Cloud Gateway Hello World Example Spring Cloud Tutorial - Spring Cloud Gateway Filters Example Spring Cloud Tutorial - Spring Cloud Gateway + Netflix Eureka Example Spring Cloud Tutorial - Spring Cloud Gateway + Netflix Eureka + Netflix Hystrix Example Spring Cloud Tutorial - Secure Secrets using Spring Cloud Config + Vault Example
Video
This tutorial is explained in the below Youtube Video.Implementing Spring Cloud Gateway Filters
Spring Cloud Gateway filters can be classified as- Spring Cloud Gateway Pre Filters
- Spring Cloud Gateway Post Filters
Spring Cloud Filters can be implemented in following two ways-
- Spring Cloud Gateway Filters using Java Configuration
- Spring Cloud Gateway Filters using Property Configuration
Implementing Spring Cloud Gateway Filters using Java Configuration
We will be modifying the code we had created in the previous tutorial. We will be adding the pre and post filter configuration as follows-package com.javainuse.config; import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class SpringCloudConfig { @Bean public RouteLocator gatewayRoutes(RouteLocatorBuilder builder) { return builder.routes() .route(r -> r.path("/employee/**") //Pre and Post Filters provided by Spring Cloud Gateway .filters(f -> f.addRequestHeader("first-request", "first-request-header") .addResponseHeader("first-response", "first-response-header")) .uri("http://localhost:8081/") .id("employeeModule")) .route(r -> r.path("/consumer/**") //Pre and Post Filters provided by Spring Cloud Gateway .filters(f -> f.addRequestHeader("second-request", "second-request-header") .addResponseHeader("second-response", "second-response-header")) .uri("http://localhost:8082/") .id("consumerModule")) .build(); } }