The main content of this paper is: Why use a gateway, integrating Gateway, Gateway Core Learning: Route, Predicate, Filter, and finally the use of polymeric microspheres Gateway service request
First we summarize the point of architecture
Worth Gateway
Gateway does not use the line thing?
- Each request direct hit in each micro service
Small scale looks okay, a lot of it if the micro-services? Hundreds of it? What problems caused by it
- Certification complicated, the interface to do the authentication process for each service in each!
- The client is difficult to reconstruct, with the architecture iterations, is likely to re-division of micro-services, due to the dynamics of the split is, if the client to communicate with the micro-services directly, then realize it is slightly reconstruction, is likely to re-split micro service has changed the domain name, the client needs to do a lot of changes
- Other micro-services protocol used browser unfriendly, such as Thrift agreement, if not the direct access to it!
To solve these problems, a common practice in the industry is to build a gateway, external requests through the gateway forwards the call to the appropriate micro-services, the following benefits
- Simplifies the login authentication without the need for each service are certified again
- Foreign exposure is always a domain name, regardless of the internal micro-split service, domain name will not change, the cost of reconstruction significantly reduced client
2 Spring Cloud Gateway Profile
◆ is Spring Cloud Gateway (second generation), the future will replace Zuul (first generation) ◆ based Netty, Reactor building and WebFlux
2.1 advantage
◆ strong performance is 1.6 times the first-generation gateway Zuul 1.x performance PK!:
- Recommended reading
error correction note: Zuul & Spring Cloud Gateway & Linkerd performance comparison
◆ Powerful
- It built a lot of useful features, such as forwarding, control, current limiting
◆ elegant design, easy to expand
2.1 shortcomings
◆ rely Netty and Webflux, not Servlet programming model, there is a certain learning costs ◆ not work in Servlet container, it can not build a WAR package does not support ◆ Spring Boot 1.x, 2.x version had at least
3 write Spring Cloud Gateway
- And start new projects
3.1 Forwarding rule
- Access to $ {GATEWAY_ URL} / {microService X} / xx will be forwarded to / xx path of micro-services X
4 core concepts
◆ Route (Route)
- Spring. Cloud. Gateway's basic elements, can be simply interpreted as a forwarding rule.
It includes: ID, target URL, Predicate Filter collections and collections.
◆ Predicate (predicate)
- That java.util.function.Predicate, Spring Cloud Gateway using
Predicate matching conditions to achieve the route.
◆ Filter (filter)
- Modification request and in response
Routing Configuration Example
However, we did not so configured routing Oh! But follows
5 architecture analysis
The corresponding source code
- Gateway Handler Mapping:
org.springframework.cloud.gateway.handler.RoutePredicateHandlerMapping - Gateway Web Handler :
org.springframework.cloud.gateway.handler.FilteringWebHandler
6 predicate routing facility (Route Predicate Factories)
Built-in 6.1
- Recommended Reading
Spring Cloud Gateway- predicate factory Detailed route (Route Predicate Factories)
6.2 custom route predicate factory
- Routing rules
- Configuration class
- Custom verbs factory
7 filter plant (GatewayFilter Factories)
Built-in 7.1
- Recommended Reading
Spring Cloud Gateway- filter factory Detailed (GatewayFilter Factories) - Add configuration, access getway / ** adds a header prefix Oh, zai'ch
- Break point
Custom filter factory 7.2
7.2.1 Life Cycle
- Before forwarding the request Gateway: pre
- post: Gateway after forwarding the request
7.2.2 Custom
Mode 1
◆ Inheritance: AbstractGatewayFilterFactory ◆ Reference Example:org.springframework.cloud.gateway.filter.factory.RequestSizeGatewayFilterFactory
- Configurations
Mode 2
◆ Inheritance: AbstractNameValueGatewayFilterFactory ◆ Reference Example:org.springframework.cloud.gateway.filter.factory.AddRequestHeaderGatewayFilterFactory
- Configurations
这种方式其实是第一种的简化版
7.2.3 Core API
◆ exchange.getRequest (). Mutate (). Xxx // Modify request ◆ exchange.mutate (). Xxx // Modify exchange ◆ chain.filter (exchange) // passed to the next filter process ◆ exchange.getResponse () // get response
7.2.4 implement a filter plant
Logging function
- Profiles, two parameters
7.3 Global Filters
Executive order Oh!
- Recommended Reading
Spring Cloud Gateway- global filter (Global Filters)
Suspense: how to integrate Sentinel as Spring Cloud Gateway? Sentinel is supported in V1.6 + gateway!
Execution order filter 7.4
- Order the smaller the forward execution
- Order filter factory configuration order is incremented by one starting from
- If the default filter configuration, the first filter performs the default of the same Order
Core code
- org.springframework.cloud.gateway.route.RouteDefinitionRouteLocator # loadGatewayFilters: Order set value of the filter, starting at 1
- org.springframework.cloud.gateway.route.RouteDefinitionRouteLocator # getFilters: load default route filters & filter, sort and filter made
- org.springframework.cloud.gateway.handler FilteringWebHandler # handle:. Construction and performs filter chain
8 Monitoring Spring Cloud Gateway
- Recommended Reading
Spring Cloud Gateway Monitoring
9 troubleshooting, debugging techniques
- Recommended Reading
Spring Cloud Gateway troubleshooting and debugging skills summary
10 limiting
- Recommended Reading
Spring Cloud Gateway limiting Comments
11 summary
◆ routing, routing predicate factories, filter factories, global filters ...
◆ master gateway
● registered to Nacos
● Integrated Ribbon
● Fault Tolerance (default Hystrix, can also be used Sentinel)
reference
- GateWay official documents
- Future Micro Service: Spring Cloud Alibaba from entry to advanced
welcome attention to all the dry technology public number: JavaEdge