Reader small image

You're reading from  Mastering Spring Cloud

Product typeBook
Published inApr 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781788475433
Edition1st Edition
Languages
Right arrow
Author (1)
Piotr Mińkowski
Piotr Mińkowski
author image
Piotr Mińkowski

Piotr works as a Solution Architect at Red Hat. He has several years of experience in software architecture and development. During this time, he was working in large organizations, where he was responsible for IT transformation to the modern cloud-native development approach. He is interested in technologies related to programming, containerization, and microservices. He writes about it in his blog https://piotrminkowski.com.
Read more about Piotr Mińkowski

Right arrow

Chapter 8. Routing and Filtering with API Gateway

In this chapter, we will discuss the next important element of microservice-based architecture, an API gateway. It is not our first encounter with that element in practice. We have already implemented a simple gateway pattern in Chapter 4, Service Discovery, for the purpose of presenting how a zoning mechanism works for service discovery with Eureka. We used Netflix's Zuul library, which is a JVM-based router and server-side load balancer. Netflix designed Zuul to provide features such as authentication, stress and canary testing, dynamic routing, and active/active multiregional traffic management. Although this is not explicitly stated, it also acts as a gateway in microservice architecture and its main task is to hide the complexity of your system from an external client.

Until now, Zuul, in fact, didn't have any competition when it came to API gateway pattern implementation inside the Spring Cloud framework. However, the situation is changing...

Using Spring Cloud Netflix Zuul


Spring Cloud has implemented an embedded Zuul proxy to allow frontend application's proxy calls to backend services. This feature is useful for external clients, because it hides system complexity and helps to avoid the need to manage CORS and authentication concerns independently for all microservices. To enable it, you should annotate a Spring Boot main class with @EnableZuulProxy, and this forwards incoming requests to the target service. Of course, Zuul is integrated with the Ribbon load balancer, Hystrix circuit breaker, and service discovery, for example with Eureka. 

Building a gateway application

Let's go back to the example from the previous chapter to append the last element in the microservice-based architecture, API Gateway. What we haven't considered yet is how the external client would call our services. First, we would not want to expose the network addresses of all microservices running inside the system. We may also perform some operations such...

Using Spring Cloud Gateway


There are three basic concepts around Spring Cloud Gateway:

  • Route: That is the basic building block of the gateway. It consists of a unique ID for identifying a route, a destination URI, a list of predicates, and a list of filters. A route is matched only if all the predicates have been fulfilled.
  • Predicates: These are the logic that is executed before processing each request. It is responsible for detecting whether the different attributes of the HTTP request, such as headers and parameters, match the defined criteria. The implementation is based on the Java 8 interface java.util.function.Predicate<T>. The input type is in turn based on Spring's org.springframework.web.server.ServerWebExchange.
  • Filters: They allow the modification of the incoming HTTP request or outgoing HTTP response. They may be modified before or after sending the downstream request. Route filters are scoped to a particular route. They implement Spring's org.springframework.web.server.GatewayFilter...

Summary


With an API gateway, we have finished the discussion about the implementation of the core elements of a microservice-based architecture in Spring Cloud. After reading that part of the book, you should be able to customize and use tools such as Eureka, Spring Cloud Config, Ribbon, Feign, Hystrix, and finally a gateway based on Zuul and Spring Cloud Gateway together.

Treat this chapter as a comparison between two available solutions—the older Netflix Zuul and the newest one, Spring Cloud Gateway. The second of them is changing dynamically. Its current version, 2.0, may be used only with Spring 5 and is still not available in release version. The first of them, Netflix Zuul, is stable, but it does not support asynchronous, non-blocking connections. It is still based on Netflix Zuul 1.0, although there is a new version of Zuul that supports asynchronous communication. Regardless of the differences between them, I have described how to provide a simple and a more advanced configuration...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Spring Cloud
Published in: Apr 2018Publisher: PacktISBN-13: 9781788475433
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Piotr Mińkowski

Piotr works as a Solution Architect at Red Hat. He has several years of experience in software architecture and development. During this time, he was working in large organizations, where he was responsible for IT transformation to the modern cloud-native development approach. He is interested in technologies related to programming, containerization, and microservices. He writes about it in his blog https://piotrminkowski.com.
Read more about Piotr Mińkowski