Wanzi spring cloud gateway 2.0, a technology for the future, let’s learn about it?

Time:2020-2-24

Your praise is my greatest support.

Original: the taste of little sister (WeChat official account number ID:xjjdog), welcome to share, reprint please keep the source.

This article will talk about the function of API gateway and the usage of spring cloud gateway from the perspective of knowledge topology. The article is very long. You can go through the table of contents first.

1、 Knowledge Topology (usage and principles)
2、 Role of gateway
3、 Predict, route matching
4、 Filter, filter writing
5、 Custom filter
6、 FAQs

Why do many people find spring cloud gateway hard to use? Because it useswebflux, involving reactive programming rather than traditional procedural programming.

It’s not hard to find out that the source of this obscurity comes from the project reactor“Future oriented” responsive programming framework, which is in line with spring project.

The final code is the same as lambda.The idea behind it is the product of observer mode and non blocking hybridization, and the learning curve is relatively steep

Wanzi spring cloud gateway 2.0, a technology for the future, let's learn about it?

1、 Knowledge Topology

Spring cloud gateway involves a lot of relatively new knowledge and ideas, but the slope is not very big only for use.

Wanzi spring cloud gateway 2.0, a technology for the future, let's learn about it?

1.1 use related

We can imagine the necessary elements of a route:Web requests, through some matching conditions, locate to the real service node. And before and after the forwarding process, some fine control is carried out

Wanzi spring cloud gateway 2.0, a technology for the future, let's learn about it?

Among them, predicate is our matching condition, while filter can be understood as an omnipotent interceptor. With these two elements and the target URI, a specific route can be realized.

Since spring cloud gateway is based on spring boot, YML is used for routing configuration. The level of YML is usually deep, which makes the configuration file look very messy. It can also use java code (or kotlin) to write routing, and the style is functional programming, so you need to understand the writing method of lambda expression first.

Spring cloud gateway is the gateway of HTTP service most of the time. It can control HTTP messages in some fine-grained way, so it needs more understanding of HTTP protocol to be able to use it easily.

1.2 principle related

But in principle, it is much more complicated. Due to the lag in practice, most of the existing components haven’t caught up with the concept of “responsive” which leads to a lot of obscure components (mainly too many special functions)。 Fortunately, using spring cloud gateway does not require direct access to these APIs.

The most important thing is to encapsulate the web plus framework. Webplus is a set of solutions that can replace spring MVC, and can write responsive applications. The relationship between them can be seen in the following figure. Its underlying layer uses netty, so the operation is asynchronous and non blocking.

Wanzi spring cloud gateway 2.0, a technology for the future, let's learn about it?

Further down, webplus is an encapsulation running on top of the project reactor, whose fundamental features are provided by the latter. Like vert. X, this one feels very strange when used for the first time.

Reactor is the promotion of observer mode, so it has the concept of publisher. The main implementation is flux and mono. This is the name of the so-called weblux.

Reactor reference: https://url.cn/5b7f5iy

There is a certain cost in the transition from traditional development mode to reactor development mode. If you have time to understand the principle behind it, it is still good for the use of spring cloud gateway.

2、 Role of gateway

From the name, it can be seen that it is a network level, no matter how complex the back end, the external level performance is consistent.

More importantly, some common transactions hidden behind the level can be abstracted for processing. You can think of the gateway as something similar to the customs. Your visa information preparation, security inspection, scheduling, etc. can be processed in a unified way.

API gateway is an architecture pattern with the rise of microservice concept, which is not limited to microservice. From the figure we can see the location of the gateway.

Wanzi spring cloud gateway 2.0, a technology for the future, let's learn about it?
See the specific function of the gateway below.

2.1 reverse agency

This is the basic function of all gateways, including nginx. In addition to the ability to reshape services, a very important additional benefit of gateways is to shield the details of back-end services.

The reverse agent also has the function of load balancing, including weighted traffic distribution.

2.2 authentication

It is the authority authentication, which is often called the authority system. Due to the high similarity of authentication services, they can be abstracted and placed in the gateway layer.

For example, unified access of HTTPS protocol, processing of distributed session, access of new login authentication channel, etc.

2.3 flow control

If the flow control is distributed to each service, it is a disaster, and the gateway is the most suitable place.

Traffic control usually has many strategies to shield back-end services. Abnormal requests and requests beyond the load capacity will be intercepted quickly, which provides essential support for the stability of the system.

There are two ways of flow control: single machine current limiting and distributed current limiting. The latter is more precise, which is provided by spring cloud gateway.

2.4 fuses

The main difference between fuse and flow control is that the former service is “unavailable” for a period of time, while the latter only fails in probability.

In addition to the call between services involved in fusing, fusing at the gateway layer will have a larger scope of action, which requires accurate classification of services.

2.5 gray control

One of the ultimate functions of the gateway is to realize the gray-scale publishing of services. For example, AB test is a gray-scale publishing method.

The gray level will be fine controlled, such as for a class of users, a certain physical area, a specific request path, a specific module, random percentage and other aspects of the gray level control.

Gray level is the result of an overall architecture coordination, but the gateway is the gateway. By adding some specific flags to the request header or parameters, each request can be divided to determine whether it falls into gray level.

2.6 log monitoring

Gateway is the most suitable place for log monitoring. Through the detailed analysis of access log, we can get a lot of valuable data, and then provide decision-making basis for the optimization of back-end services.

For example, the access trend, operation data, QPS peak value, year-on-year and month on month of a certain “business”.

3、 Predict, route matching

Spring cloud gateway can be configured in two ways: fluent API and YML, which are very annoying.

Predicate means assertion in English. Here we can regard it as a conditional match, which can be matched according to the HTTP header or HTTP parameters.

3.1 time matching

A match before or after a certain point in time. For example, let the route take effect in a certain period of time.

A profile is similar to:

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - After=2020-10-20T17:42:47.789-07:00[America/Denver]

Among them.idIt is the only non repeatable name of this route. URI specifies the route address after matching, and after of predictions is our time matcher.

After 1.

Or translate it into code mode.

builder.routes().route(
r -> r.after(LocalDateTime.of(2020, 10, 17, 42, 47).atZone(ZoneId.of("America/Denver")))
    .uri("https://example.org")
);

Since most of the code is similar, in the following section, we only capture the most important fragments.

2. before

The above is the previous writing method after a certain time point, as follows:

Before=2017-01-20T17:42:47.789-07:00[America/Denver]

r.before(LocalDateTime.of(2020, 10, 17, 42, 47).atZone(ZoneId.of("America/Denver")))

Between 3.
And in a certain period of time

Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]

r.between(
LocalDateTime.of(2020, 10, 17, 42, 47).atZone(ZoneId.of("America/Denver")),
LocalDateTime.of(2027, 10, 17, 42, 47).atZone(ZoneId.of("America/Denver"))
)

3.2 HTTP information

Let’s take a brief look at the information of an HTTP request. The information in the general and request headers can be matched and controlled. For cookies, hosts and other commonly used information, special optimization is also carried out. Among them, the most commonly used ones are path, cookie, host, query, etc.

Wanzi spring cloud gateway 2.0, a technology for the future, let's learn about it?
Path

Path is the most important matching method. Multiple paths can be used,Separate.

Path=/foo/{segment},/bar/{segment}
r.path("/foo/{segment}","/bar/{segment}")

Note that we enclose {segment} in curly braces. This value can be extracted by code.

Map<String, String> uriVariables = ServerWebExchangeUtils.getPathPredicateVariables(exchange);

String segment = uriVariables.get("segment");

Header header information

Header=X-Request-Id, \d+
r.header("Header=X-Request-Id", "\\d+")

Similar to cookies, this refers to HTTP header matching. Many grayscale information, or trace information, like to put here.

Cookie [header]

Cookie=chocolate, ch.p
r.cookie("chocolate","ch.p")

Is there a name in the HTTP message calledchocolateCookie of, whether regularch.pMatch.

Host information [header]
Although the host information is also in the header information, because it is too common, there is a special matcher.

Host=**.somehost.org,**.anotherhost.org
r.host("Host=**.somehost.org","**.anotherhost.org")

Note that the matching string here is ant style, more concise, and not a regular expression in Java. Multiple hosts using,To separate.

Request Method

Method=GET
r.method("GET")

Note that I didn’t find the case conversion code in the source code, so remember to keep it uppercase in the route. All except connect.

Wanzi spring cloud gateway 2.0, a technology for the future, let's learn about it?
Query

This refers to a string of parameters after the URL question mark.

Query=baz
r.query("baz")

Query=foo, ba.
r.query("foo","ba.")

It’s too simple for me to introduce too much.

RemoteAddr

 RemoteAddr=192.168.1.1/24
 r->r.remoteAddr("192.168.1.1/24")

3.3 weight

The configuration of weight information is a bit 2B. For example, there are two servers behind us. Spring cloud gateway has two routes for them, and the link hub is a hub calledWeightGroup.

spring:
  cloud:
    gateway:
      routes:
      - id: weight_high
        uri: https://weighthigh.org
        predicates:
        - Weight=group1, 8
      - id: weight_low
        uri: https://weightlow.org
        predicates:
        - Weight=group1, 2

The same code is as follows.

builder.routes()
.route("weight_high",r -> r.weight("group1", 8).uri("https://weighthigh.org"))
.route("weight_low",r -> r.weight("group1", 2).uri("https://weightlow.org"));

If the service has 100 nodes and a pile of filters, do you want to repeatedly configure them 100 times? I have to say very fuck.

4、 Filter, filter writing

Match, able to locate the route to be proxied. Now, it’s inside our route. Most of the functions of routing mentioned above are configured here.

It is known to all who have used zuul gateway that there will be two annotations, pre and post, to control the routing behavior before and after the proxy when customizing the route. Spring cloud Gatewa has the same effect.

4.1 information modification

Crud not only exists in SSM, but also in routing configuration. You may modify the HTTP header or other information before routing to the real back-end service, or you may make some changes after the proxy reaches the corresponding link.

According to our understanding, the so-called request corresponds to pre, while the response corresponds to post.

AddRequestHeader=X-Request-Foo, Bar
AddRequestParameter=foo, bar
AddResponseHeader=X-Response-Foo, Bar

RemoveRequestHeader=X-Request-Foo
RemoveResponseHeader=X-Response-Foo
RemoveRequestParameter=foo

SetRequestHeader=X-Request-Foo, Bar
SetResponseHeader=X-Response-Foo, Bar

SetStatus=401

4.2 request body modification

This one hurts a little. The reason is caused by weblux. It’s more personal in writing.

.filters(f -> f.modifyRequestBody(String.class, String.class, MediaType.APPLICATION_JSON_VALUE,
    (exchange, s) -> {
            return Mono.just(s.toUpperCase());
})

In the above code, all the contents in the requestbody are capitalized.

Similarly, the response corresponds to the modifyresponsebody, and the writing method is similar. For details, please refer to
ModifyRequestBodyGatewayFilterFactory
The code. If I haven’t touched the theoretical part mentioned above, it’s still hard to read.

4.3 redirection

RedirectTo=302, https://acme.org

.filters(f -> f.redirect(302,"https://acme.org"))

Direct redirection. This one is relatively simple. I won’t introduce it too much.

4.4 remove prefix

A key.

StripPrefix=2

.filters(f->f.stripPrefix(2))

Stripprefix can accept a non negative integer to remove the corresponding prefix. For example, the path of external access is
/a/b/c/d
Then, the path to back-end services is/c/dIt’s gone./a/bPrefix.

This is a special way of path rewriting, which is often used forlb://Protocol’s microservice path rewriting.

4.5 path rewriting

Rewritepath is very similar to the path rewriting of nginx.

RewritePath=/foo(?<segment>/?.*), $\{segment}

f.rewritePath("/foo(?<segment>/?.*)", "${segment}")

Official said it was due to the YML configuration file. Must put$finish writing sth.$\But it’s not necessary in Java code. Because the internal use is java regular, and the concept of group is used, the code is really dirty.

4.6 fuse configuration

The default integrated circuit breaker is still hystrix.

Hystrix=myCommandName

.filters(f -> f.hystrix(c->c.setName("myCommandName")))

In addition, there is another parameter called fusingfallbackUriBut unfortunately, only forward mode is supported. For example:

fallbackUri: forward:/myfallback

4.7 retry configuration

For some services with high stability requirements, one unavoidable problem is to retry. There are many parameters to retry. A typical configuration is as follows:

- name: Retry
    args:
        retries: 3
        statuses: BAD_GATEWAY
        backoff:
            firstBackoff: 10ms
            maxBackoff: 50ms
            factor: 2
            basedOnPreviousValue: false

Among them, backoff specifies the retry policy and interval, which will follow the formulafirstBackoff * (factor ^ n)Grow.

Fusing ensures the security of the service, retrying ensures the robustness of the service, and attention should be paid to screening the use scenarios.

4.8 current limiting

Built in current limiter, if triggered, will return “HTTP 429 – too many requests” error.

The parameter of current limiter is a implementation called keyresolver, in which there is the concept mentioned aboveMono。 So if you want to extend the current limiter, you need to understand the webplus package.

public interface KeyResolver {
    Mono<String> resolve(ServerWebExchange exchange);
}

At the same time, based on the token bucket principle of redis, distributed current limiting is implemented. Because the underlying layer uses “spring boot starter data redis reactive”, it has “responsive” application characteristics, and supports the backpressure of Webflux (reactor). For the configuration, there are some detours, such as the official configuration.

- name: RequestRateLimiter
    args:
        key-resolver: '#{@ipKeyResolver}'
        redis-rate-limiter.replenishRate: 10
        redis-rate-limiter.burstCapacity: 20

We need a nameipKeyResolverBean.

There are many dimensions of current limitation, so we need to develop and manage the background by ourselves. Because of the length, we will not discuss it.

5、 Custom filter

The filter of spring cloud gateway consists of global filter and local filter. The corresponding interface isGatewayFilterandGlobalFilter

If the built-in filter can not meet the requirements, it can be solved through custom filter. Through implementationGatewayFilterandOrderedInterface, can be more flexible control.

You can refer to the implementation of the built-in filter. Later, we will introduce the specific code implementation in detail.

6、 FAQs

What does LB: / /?

LB: / / servicename is the load balancing URI that spring cloud gateway automatically creates for us in microservices. In some special cases, it can be written directly. For example, if the registered name in Eureka is pay RPC, the writing method at this time is:

lb://pay-rpc

How to modify HTTP content? Like method?

Pay attention to server web exchange. Use it
The exchange. Mutate() function can enter modify mode. For example, to convert get to post:

ServerHttpRequest request = exchange.getRequest();
if (request.getMethod() == HttpMethod.GET) {
    exchange = exchange.mutate().request(request.mutate().method(HttpMethod.POST).build()).build();
}

How to update route dynamically?
It is mainly through the actor management interface to ensure that these contents are placed in the intranet.

Get / Actor / gateway / routes route list
Get / Actor / gateway / routes / {ID} get a route information
Get / Actor / gateway / globalfilters global filter
Get / Actor / gateway / routefilters filter list
Post / Actor / gateway / refresh refresh route
Post / gateway / routes / {ID  route  to  create} route
Delete / gateway / routes / {ID [route] to [delete} delete a route

How to do some data statistics

This function is very simple. We only need to implement a global filter, and we can add any statistical function. There are two common methods: log analysis and intra application aggregation.

Neither of these is very difficult, mainly because of the planning of the function rather than the code.

I have more advanced functions, such as the need to decrypt data. What should I do?

This is about to implement its own filter.

 Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain);

Through server web exchange, you can control the addition, modification, deletion, rewriting of any parameter in the whole request process. Before and after the proxy method, you can use the

exchange.getAttributes().put();
exchange.getAttribute()

These two functions are used for parameter passing

Therefore, even if the official does not write any of the above mentioned filters, we can still play with this basic interface.

End

The official account of WeChat wise remark of an experienced person is not suitable for writing some tutorial articles, so this article is still a summary experience.

With the withdrawal of zuul1 and the dystocia of zuul2, parental SCG becomes the best choice. The spring team is very interesting. They directly adopt webplus as the back-end technology , which will make many people painful: to learn new technology again.

This article does not test the performance of SCG, which has been verified by many teams, with good results.

But there are still many problems in spring cloud gateway. Fortunately, the problem is the use, not the function. It has built in a lot of predicate and filter, but many times it can’t solve the problem. Users need to create their own filters. Well, most of my filters are self created.

Another way to configure Fluent API and YML is to make complaints about B. There are also complicated Java regular things, which are maddening — please look at the deep claw marks on the wall, which are my masterpiece.

About the author:Taste of little sister(xjjdog) a official account that does not allow programmers to take a detour. Focus on infrastructure and Linux. Ten year architecture, 10 billion traffic per day, and you explore the high concurrency world, give you a different taste. My personal wechat xjjdog0, welcome to add friends, further exchanges. ​

Wanzi spring cloud gateway 2.0, a technology for the future, let's learn about it?

Recommended Today

Hot! Front and rear learning routes of GitHub target 144K

Hello, Sifu’s little friend. I’m silent Wang Er. Last week, while appreciating teacher Ruan Yifeng’s science and technology weekly, I found a powerful learning route, which has been marked with 144K on GitHub. It’s very popular. It covers not only the front-end and back-end learning routes, but also the operation and maintenance learning routes. As […]