Learning a thing first to understand what it is mainly to do, what features, write a small demo, and then to each point in the demo respectively in-depth and divergent, come up to see the source code is not realistic, this paper mainly introduces the basic characteristics of SCG, concept and SCG overall architecture.


SCG features

The following translation is from github.com/spring-clou…

  • Based on the Java8
  • Based on the Spring5
  • Based on the Spring2.0
  • Support for dynamic routing, matching routes on any request attribute
  • Support for route matching built into Spring Handler mappings
  • Supports route matching (PATH, METHOD, HEADER, HOST) based on HTTP requests (Predicate)
  • Filter applied to matched routes (Filter)
  • Filters can modify HTTP requests and HTTP responses (adding/modifying headers, adding/modifying request parameters, overwriting request PATH, and so on)
  • Support for API or configuration drivers
  • Spring Cloud DiscoveryClient is supported to configure routes and work with service discovery and registration

SCG is a Framework (not middleware) based on WebFlux and uses the Reactor pattern, while the underlying WebFlux is based on Netty.

The core concept

Using a small demo as an example (using external configuration), the configuration is as follows

GatewayProperties gateway: routes: -id: user-service #//127.0.0.1:8080 # Specifies the destination address to which the route is routedPredicates: # predicates, RouteDefinition, RouteDefinition, RouteDefinition, RouteDefinition, RouteDefinition, RouteDefinition, RouteDefinition, RouteDefinition1# say belowCopy the code

The SCG configuration corresponds toGatewayProperties, and the route array definition, can also configure our default filter.

  • From the source can configure multiple routes (a bit like nonsense ha ha), inRouteDefinitionPredicate and Filter, both of which can be configured multiple times.

Routing the Route

Route A Route is one of the most basic components of a Gateway. It consists of an ID, A URI, a set of Predicate, and a set of filters. A request matches a route if it satisfies all assertions of the route

  • Id: indicates the unique identifier of a route
  • Uri: The destination URI to which the route points and the address to which the request is finally forwarded

Assertions Predicate

Predicate: Conditions for matching a route. These conditions must be met at the same time. Gateway has multiple built-in implementations, such as Path, Method, and Host.



Filter Filter

Filter: intercepts requests and implements user-defined functions. Also, Gateway has several built-in implementations of filters, such as fuses, which we used aboveStripPrefixGatewayFilterFactoryThis filter removes the prefix of the request,1Indicates the number of prefixes to be removed from the request, such as the request addresshttp://127.0.0.1:8080/test/login, the actual address forwarded to will be changed to /testhttp://xxxx:8888/login

The overall architecture

I think Route, Predicate, and Filter are the core of SCG. SCG is built around these three components. The corresponding source code is as follows. Where the handler asserts the Predicate. In the Spring-Cloud-gateway-server project, v2.2.6.RELEASE was migrated from spring-Cloud-gateway-core.



The following figure shows the overall workflow of an SCG, somewhat similar to SpringMVC.

  • The first is the Handler part, that is, the assertion that finally matches the unique Route
  • It is filtered through a series of Filter chains
  • It is forwarded to the target URI by the Proxy Filter, and the service response is finally obtained
  • The response also passes through a series of Filter chains
  • Returns the response to the client

Configuration drivers and API drivers

Configuration drivers have been demonstrated above, so only API drivers will be shown

@Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes().route(
                route -> route.path("/test/api/hello").and().host("*.spring.io")
                        .filters(
                                filter -> filter.addRequestHeader("hello"."502819LHJ").stripPrefix(1)
                        ).uri("http://localhost:8088")
        ).build();

    }
Copy the code



Like configuration drivers, we are flexible enough to freely configure our assertions and filters.