This is the 21st day of my participation in Gwen Challenge
Following up on our first introduction to Spring Cloud — The Hystrix principle — today we will talk about the Spring Cloud Gateway associated with Hystrix
Before we begin, what is a gateway?
Refer to the website
The premise
Let’s say your name is Spot, you live in a big yard, your neighbors have a lot of small friends, and your parents are your gateways.
When parents are not at home
When you want to play with someone in the yard, just shout his name in the yard and he will hear you and run out to play with you.
When your parents are at home
You can not just walk out the door, you want to communicate with the outside world, all must be by the parents (gateway) telephone help you contact.
If you want to chat with your classmate Xiao Ming, but you don’t know xiao Ming’s phone number, but your homeroom teacher has a list of all the students in your class and their phone numbers cross reference table, your teacher is your DNS server.
So you have the following conversation with your parents at home:
- Little Spot: Mom (or Dad), may I ask the head teacher for Xiaoming’s phone number?
- Parent: Ok, you wait. (Then your parents make a call to your head teacher and find out Xiao Ming’s number.) Yes, his home number is 211.99.99.99
- Tiny Spot: That’s great! Mom (or dad), I want to find Xiaoming, please help me to contact Xiaoming.
- Parent: No problem. (Then the parents sent a request to the telephone office to connect xiaoming’s phone number, the last pass of course is transferred to Xiaoming’s parents, and then his parents transferred the phone to Xiaoming).
So you got in touch with Xiao Ming.
What causes the gateway?
- The client requests different microservices multiple times, increasing the complexity of the client.
For example, One-inch has a lot of classmates, but it would be hard for one-inch to remember their contact information.
-
Cross-domain requests exist and are complicated to process in certain scenarios.
-
Authentication is complex and each service requires independent authentication.
The yard outside is very dangerous, One-inch should contact friends in other yards, parents and class director need to confirm one-inch’s classmate identity, or in case one-inch turns into a big bad Wolf!
-
It is difficult to refactor, and as the project iterations, microservices may need to be reclassified. For example, you might combine multiple services into one or split a service into multiple services. Refactoring can be difficult to implement if the client communicates directly with the microservice.
-
Some microservices may use firewall/browser-unfriendly protocols, making direct access difficult.
What is the SpringCloud Gateway?
The SpringCloud Gateway provides an API Gateway built on top of the Spring ecosystem, including Spring 5, Spring Boot 2, and the Project Reactor. The Spring Cloud Gateway aims to provide a simple and efficient way to route apis and provide cross-concerns for them, such as security, monitoring/metrics, and resiliency.
Why SpringCloud Gateway?
- Prior to the official release of SpringCloud Finchley, the recommended gateway for SpringCloud was Zuul provided by Netflix, and Gateway was a replacement for the original Zuul1.x release
- SpringCloud Gateway is implemented based on the WebFlux framework, and the bottom layer of the WebFlux framework uses the high performance Reactor pattern communication framework Netty
- The Spring Cloud Gateway also supports WebSocket and is tightly integrated with Spring for a better development experience
What are the features of SpringCloud Gateway?
- It is constructed based on Spring Framework 5, Project Reactor and Spring Boot 2.0.
- Dynamic routing: can match any request attribute;
- You can specify Predicate and Filter for a route;
- Integrated Circuit breaker function of Hystrix;
- Integrate Spring Cloud service discovery;
- Easy to write Predicate and Filter;
- Request traffic limiting function;
- Path rewriting is supported.
The core of the SpringCloud Gateway?
The Route (routing)
Routing is the basic building block of a gateway, consisting of an ID, a destination URL, and a set of filters, which are then forwarded according to the judgment conditions of the assertion
Predicate (claim)
At present, SpringCloud Gateway supports a variety of judgment conditions for routing and forwarding, such as Path, Query, Method and Header.
The Filter (Filter)
GatewayFilyter refers to an instance of the Spring framework GatewayFilyter, which can be modified before or after a request is routed using a filter
Hystrix is added to the Filter of Spring Cloud Gateway, so that the Gateway has Hystrixfusingfunction
Implementation process
- The client makes a request to the Spring Cloud Gateway.
- If the gateway handler map determines that the request matches the route, it is sent to the gateway Web handler. This handler runs the request through a chain of request-specific filters.
- The reason the filter is separated by a dotted line is that the filter can run logic before and after the broker request is sent.
- Performs all “pre-” filter logic. The proxy request is then made.
- After the broker request is issued, the “POST” filter logic runs.
Today’s summary
Today, mainly about the origin of Gateway, Gateway as a micro service Gateway for the reason and Gateway contains features and the implementation process!