Understand microservices – gateway of service gateway


Hello, I’m a side dish.

A hope can becomeTalking about architecture with ox xMy man! If you want to be the person I want to be, or pay attention and be a partner, so that the dishes are no longer lonely!

This paper mainly introducesSpring cloud service gateway

If necessary, refer to

If it helps, don’t forgetgive the thumbs-up

The official account of WeChat has been opened.Little dish Liangji, students who don’t pay attention remember to pay attention!

When chatting with small partners some time ago, we talked about the current situation of their company. In the near future, the company will comprehensively transition from single service to micro service architecture. Here we hear the key words—Microservices。 At first glance, it sounds reasonable. With the continuous development of the Internet, it is not only the development of the industry, but also the development of the system architecture. The current market structure has also gone through these stages:

Understand microservices - gateway of service gateway

Is that a good thing? It’s a good thing, no doubt. Because they don’t keep up with the tide of the times, they will always be shot dead on the beach. But is it all a good thing? Not necessarily.

Let’s first understand what problems microservices solve? On the large side, it should be the application level and human level

  • Application level:The architecture of single service is very simple, and the low cost of project development and maintenance is its undisputed advantage. However, bloated coupling will bring too much burden to the infrastructure. If an application’s processing resources occupy a large amount of CPU, it will lead to the starvation of other processing resources, increase the system delay, and directly affect the availability of the system.
  • Human level:independentMultilingual ecologyIt is also a label for micro services. In a single service, the more human resources invested is not necessarily the more efficient, but the more likely it is to make mistakes. However, microservices are different. Each service is independent. Teams can more easily cooperate in development, even a set of systems, multiple services, multiple languages, without conflict.

Understand microservices - gateway of service gateway

But we can’t be blinded by benefits. The disadvantages of microservices also exist all the time:

  • Fault tolerance among services needs to be considered
  • Data consistency needs to be considered
  • Distributed transactions need to be considered

Many people believe that the core of microservices istiny。 The finer the service is, the better. It’s like a single principle that is not considered at all when writing code. Instead, it is used incisively and vividly in service splitting. Here we need to understand a core concept:The key to microservices is not microservices, but the appropriate size

This sentence seems very simple, but how old is the appropriate size? This may depend on different teams and projects. The appropriate size may depend on fewer code warehouses, fewer deployment queues, fewer languages… It’s even harder to make a decision here!

If you can’t achieve the appropriate size and brainless split services, microservices may become a burden on your project. Therefore, sometimes the overall transformation of micro services is not a good thing, do you think?

The topic is a little far away. Let’s try to bring it back. Since microservices have become the mainstream, how to design microservices is what we should do, rather than thinking about how to argue with people and how to refuse microservices when talking about microservices. So what we want to talk about in this article isSpring cloud service gateway

Spring cloud service gateway

1、 Cognitive gateway

What is a service gateway? Don’t hit yourself in the head. Let’s ask another question, why do we need a service gateway?

Service gateway provides a single unified access portal across one or more service nodes

Its role is not dispensable, but crucial. We can do it at the service gatewayRouting forwardingandfilterImplementation of. The advantages are summarized as follows:

  • Prevent internal service concerns from being exposed to external clients
  • Additional layers of security have been added to our internal services
  • Reduce the complexity of microservice access

Understand microservices - gateway of service gateway

According to the contents in the figure, we can get the following information:

  • The user accesses the portal and accesses other micro service nodes through the gateway
  • The functions of the service gateway areRouting forwardingAPI monitoringPermission controlCurrent limiting

And these areService gatewayThe meaning of existence!

1) Zuul comparison

SpringCloud GatewayIt is a new project of spring cloud, which aims to replace Netflix zuul. It is based onSpring5.0 + SpringBoot2.0 + WebFluxAnd other technologies. The performance is higher than that of zuul. According to the official information, the performance is 1.6 times higher than that of zuul. It is intended to provide a simple and effective unified API routing management method for microservice architecture.

SpringCloud GatewayIt not only provides a unified routing method (reverse proxy), but also provides the basic functions of the gateway based on the filter chain (defining filter to filter requests), such as authentication, flow control, fusing, path rewriting, log monitoring, etc.

In fact, when it comes to Netflix zuul, small partners who are using or preparing to use the micro service architecture should not be unfamiliar. After all, Netflix is an old micro service open source. The competition between rookies and veteran players, if rookies don’t have some hard power, how to make people feel at ease!

Here we can learn about it by the wayWefluxWebfluxThe emergence of fills the gap in spring’s responsive programming.

There may be many friends who don’t know about Webflux. Next, there will be an explanation about Webflux, which is really delicious!

Webflux’s responsive programming is not only a change in programming style, but also a development package that provides responsive access to a series of famous frameworks, such asNettyRedis(if you don’t know the strength of netty, you can think about why nginx can carry so much concurrency. The bottom layer is based on netty)

So say so much, withZuulWhat does it matter? We can have a lookZuulIO model for

The zuul version integrated in spring cloud uses the Tomcat container and the traditional servlet IO processing model. Servlets are managed by the servlet container lifecycle.

Understand microservices - gateway of service gateway

But the problem isServletIs a simple network IO model when a request entersServletContainerA thread will be bound to it. In the scenario of low concurrency, this model is no problem, but once concurrency comes up, the number of threads will increase. The problem is that context switching is frequent, memory consumption is serious, and the processing time of requests will become longer. It’s the so-called pulling one hair and moving the whole body!

Spriingcloud zuul is a blocking processing model based on servlets, that is, spring implements a servlet (dispatcher servlet) that handles all request requests and is blocked by the servlet. Although spring cloud zuul 2.0 started with netty as the concurrent IO framework, spring cloud has no official plan to integrate this version!

Note: there is no meaning of advocating gateway here. The specific use depends on the specific project

3、 Master gateway

1. Gateway dependency

The most critical step is to introduce gateway dependency

<!-- Gateway -- >
2. Project structure

Understand microservices - gateway of service gateway

I have simply created a microservice project here. There is one in the projectstore-gatewayService gateway and onestore-orderOrder service. Because we only explain the role of service gateway, we don’t need too many service providers and consumers!

staystore-orderThere is only one controller in the order serviceOrderController, there is only a simple API

public class OrderController {
    public String detail(@PathVariable String id) {
        Return stringutils.join ("the product with ID is obtained");

We start two services respectively, and then access the API of the order service:

Understand microservices - gateway of service gateway

The result is certainly in line with expectations and will not overturn.8001It is the interface of order service. At this time, we can understand that each service in the original micro service architecture is started independently and can be accessed independently, which is equivalent to the traditional single service.

Let’s think about it. If we use ports to distinguish each service, can we also achieve the effect of microservice? It seems possible in theory, but what if there are hundreds of services? Port explosion, maintenance explosion, governance explosion… Nothing else, mentality directly exploded! At this time, we thought how good it would be if we only used one unified port to access all services! The ports are consistent and the routing prefixes are inconsistent. Services are distinguished by routing prefixes, which brings us to the scenario of service gateway. Yes, here is one of the functions of the service gateway—Routing forwarding

3. The gateway appears

Since the gateway is used, one of the services we created abovestore-gatewayIt comes in handy! How to use it? Let’s make a simple modification to the configuration file:

    name: store-gateway
        - id: store-order 
          uri: http://localhost:8001 
          order: 1
            - Path=/store-order/** 
            - StripPrefix=1

Not much nonsense, we directly start the gateway through accesshttp://localhost:8000/store-order/order/123See if you can get the order?

Understand microservices - gateway of service gateway

Very smoothly, we successfully got the order goods with ID 123!

Let’s look at the composition of the URL:

Understand microservices - gateway of service gateway

Being able to access our services indicates that the gateway configuration has taken effect. Let’s take a look at the configuration items again!

spring.cloud.gatewayThis is the configuration prefix of the service gateway gateway. There’s nothing to say. Just remember.

routesHere’s what we should pay attention to,routesIt is a plural form, so it is certain that this configuration item is in the form of an array, which means that we can configure multiple routes to forward to which micro service when the request meets what conditions.

  • id:Current routeonlyidentification
  • uri:The address to forward the request to
  • order: the priority of the route. The lower the number, the higher the level
  • predicates:The conditions that the route needs to meet are also an array (here isor(relationship)
  • filters:Filter, which can be modified during request delivery

After knowing the necessary parameters, we are happy to deploy and use them, but the good times are not long, and we have ushered in new problems. My order service was originally used8001The port is used by other services for some reason. At this time, the small head is big again. This will certainly not happenMarry the right man in the wrong sedan chairThe result!

Let’s think about how to solve this problem? Since microservices are adopted, can we jump to access by using the service name, so that no matter how the port changes, our normal business will not be affected! If we adopt the service method, we need aRegistration CenterIn this way, the service we started can be synchronized toRegistration CenterofregistryIn this way, the sub gateway canservice nameGo to the registry to find a service for routing jump! Then we need a registration center, which is used hereNacosAs a registry

aboutNacosYou know, you can airborneNacos, a rookie in microservice, will see it. I said it!

We have made the following configurations in the configuration files of service gateway and order service respectively:

Understand microservices - gateway of service gateway

After starting the two services, we canNacosYou can see two services in the list of console services:

Understand microservices - gateway of service gateway

You can see it at this timeOrder serviceThe service name is:store-order, we can make the following modifications in the gateway configuration file:

Understand microservices - gateway of service gateway

The configuration here is different from one of the abovehttpInsteadlblbIt refers to getting microservices by name from Nacos and following load balancing policy (Part 2)portInsteadservice name

Let’s continue to visit the above URL to see if we can successfully access the order service:

Understand microservices - gateway of service gateway

The result is still no rollover! In this way, no matter how the port of the order service changes, as long as our service name remains unchanged, we can always access our corresponding service!

Day by day, the service has increased bit by bit! Finally one day, I was bored with the small dishes, because every time I added a service, I had to add one in the configuration fileroutesConfiguration column, although it is onlyCVHowever, one day the dishes accidentally shake their hands, so ~ ~ ~ forget it, find out if there is anything you can write lazily, and finally live up to the dishes and find a simplified version! The configuration is as follows:

Understand microservices - gateway of service gateway

That’s it? Yes, that’s it! No matter how many services you have, my configuration file doesn’t need to be modified. In this way, the purpose of sub configuration is to request that the unified method becomesGateway address: gateway port / service name / interface nameAccess by. Visit the page again and it still works!

Understand microservices - gateway of service gateway

However, convenience is convenience, which also limits many extended functions, so you need to think twice! must notlazy

4、 Master the core

The above has finished the simple use of the gateway. After reading it, the little partner must be ready to start! Next, let’s continue to strike while the iron is hotGatewayThe core of the gateway. Nothing else,Routing forwardingIt must be the core of the gateway! We have learned from the above that a specific routing information carrier mainly defines the following information(Recall):

  • id:The unique identifier of the route, which is different from other routes
  • uri:The route points to the destination URI, that is, the micro service to which the client request is finally forwarded
  • order:It is used for sorting among multiple routes. The smaller the value, the higher the sorting priority and the higher the matching priority
  • predicate:It is used to judge conditions. Only when the assertions return true can the route be truly executed
  • filter:Filters are used to modify request and response information

Let’s sort out the access process:

Understand microservices - gateway of service gateway

This figure clearly describes the calling process of the service gateway(Blind confidence

  1. GatewayClienttowardsGatewayServerMake a request
  2. The request will be rejected firstHttpWebHandlerAdapterExtract group into gateway context
  3. The context of the gateway is then passed to theDispatcherHandler, which is responsible for distributing requests toRoutePredicateHandlerMapping
  4. RoutePredicateHandlerMappingIt is responsible for route finding and judging whether the route is available with more route assertions
  5. If the assertion is successful, theFilteringWebHandlerCreate a filter chain and call
  6. The request will pass at one timePreFilter —> Microservices —> PostFilterFinally, the response is returned

The process is understood. Let’s extract the key!Assertandfilter

1. Assertion

PredicateThat is, assertion is mainly applicable to conditional judgment,Only when all assertions return true will the route be truly executed

1) Assertion factory

Many assertion factories are built in the spring cloud gateway, and all these assertions match different attributes of HTTP requests, as follows:;

  • Assertion factory based on datetime type

This type of assertion factory is based on time

1、AfterRoutePredicateFactory:receiveOneDate parameter to judge whether the request date is later than the specified date

2、BeforeRoutePredicateFactory:receiveOneDate parameter to judge whether the request date is earlier than the specified date

3、BetweenRoutePredicateFactory:receiveTwoDate parameter to judge whether the request date is within the specified time period

  • Assertion factory based on remote address remoteaddroutepredicatefactory

The assertion factory of this type is receiveOne parameterIPThe address side judges whether the address of the requesting host is in the address segment. (eq:-RemoteAddr=

  • Cookie based assertion factory

This type of assertion factory receivesTwo parameters, cookie name and a regular expression to determine whether the request cookie has the given name and the value matches the regular expression. (eq:-Cookie=cbuc)

  • Header based assertion factory headerroutepredicatefactory

This type of assertion factory receivesTwo parameters, title name and regular expression. Judge whether the request header has the given name and the value matches the regular expression. (eq:-Header=X-Request)

  • Host based assertion factory hostroutepredicatefactory

This type of assertion factory receivesOne parameter, hostname mode. Determine the of the requesthostWhether matching rules are met. (eq:-Host=**.cbuc.cn)

  • Assertion factory based on method request method methodroutepredicatefactory

This type of assertion factory receivesOne parameterTo determine whether the request type matches the specified type. (eq:-Method=GET)

  • Assertion factory based on path request pathroutepredicatefactory

This type of assertion factory receivesOne parameter, judge whether the URI part of the request satisfies the path rule. (-eq:-Path=/order/)

  • Assertion factory queryroutepredicatefactory based on query request parameters

This type of assertion factory receivesTwo parameters, requestParamAnd regular expressions. Judge whether the request parameter has the given name and the value matches the regular expression. (eq:-Query=cbuc)

  • Route weight based assertion factory weightroutepredicatefactory

This type of assertion factory receivesA [group name, weight]Then, the routes in the same group are forwarded according to the weight

2) Use

There are so many assertion factories that we won’t use them one by one. Let’s demonstrate them in combination with the use of several assertion factories.

We don’t talk much nonsense as usual. We just go to the code:


Understand microservices - gateway of service gateway

configuration file

Understand microservices - gateway of service gateway

test result


Understand microservices - gateway of service gateway


Understand microservices - gateway of service gateway

ExclaimedAmazingAt the same time, don’t worry about looking down. Let’s return to the code to see why one can access successfully, but the other fails. Two aspects: 1. What are the differences between the URLs accessed by the two? 2. What part of the code handles the URL

Develop independent thinking before looking at solutions

After you think about it, some students may already have results. Let’s continue to look down! The first is aCustomRoutePredicateFactoryClass. This class acts a bit like an interceptor. It intercepts the request forwarding. We can make a breakpoint when we request:

Understand microservices - gateway of service gateway

It can be seen that it is indeed the function of the interceptor, which intercepts each request when it is initiated. The result of question 2 comes out. The original URL processing is inRoutePredicateFactoryIt is processed in the apply methodexchange.getRequest()GetServerHttpRequestObject, so as to obtain the request parameters, request mode, request header and other information.shortcutFieldOrder()Method is also one of the keys of rewriting. We need to return the attributes defined in our entity class here, and thenapply()Method can receive the attribute parameters assigned by us!

Note: if there are multiple attributes in a user-defined entity that need to be judged,shortcutFieldOrder()The order of the method should be consistent with the order of the parameters in the configuration file

So when we write the assertion factory, what if we make it take effect?@ComponentThis annotation must be essential for spring container management. So how does a registered assertion factory declare its use? Then you have to go back to the configuration file!

Understand microservices - gateway of service gateway

Let’s focus herepredicatesThere are three configurations under this configuration item. One is familiar to usPath, the other two are a little strange, but take a look hereCustomDoes it look familiar again? Yes, we seem to have defined a name on itCustomRoutePredicateThe assertion factory, the two are a little similar, and it seems almost something. Then I’ll give you another hint:

Understand microservices - gateway of service gateway

Let’s take a look at the self implemented classes of the abstract assertion factory! Are there anyPathRoutePredicateFactoryYes, that’s what you think! Is there onePull away the rain and fog and see the blue skyHow you feel! Originally, the key of our configuration file is declared with the prefix of the class name, that is, the format of asserting the factory class must be:Custom name+ RoutePredicateFactoryIs a suffix, and then declared in the configuration file. In this way, we will naturally understand - BeforeThe role of is:Limit request time before XXX

and– Custom=cbuc, this cbuc is our restriction rule. Only the user whose name is cbuc can request successfully. If there are multiple parameters, you can use ,The order needs to be separated from the assertion factoryshortcutFieldOrder()The order of returned parameters is consistent!

If you encounter any obstacles on the way to the custom assertion factory, otherwise, see how the built-in assertion factory is implemented.It's always right to look at the source code!

2. Filter

Next, go to the second core, the filter. The function of the core is also very simple, that is, it makes a series of hands and feet on the request and response in the process of request transmission. In order to avoid too much trouble when you click back to see the request process, the side dish kindly pastes the flow chart again:

Understand microservices - gateway of service gateway

stayGatewayThe filter can be divided intoLocal filterandGlobal filter。 Listen to the name to know its function,localIt is used on a route,overall situationIs used on all routes. But whether it’slocalstilloverall situation, the life cycle is divided intopreandpost

  • pre:Invoked before routing to micro service. We can use this filter to realize authentication, select the requested microservices in the cluster, and record debugging records
  • post:Acts on execution after routing to the microservice. We can use this filter to add a standard HTTP header, collect statistics and indicators, and send the response from the micro service to the client.
1) Local filter

A local filter is a filter for a single route. Similarly, the gateway has many built-in filters

Understand microservices - gateway of service gateway

We choose several common filters for Description: (the following filters omit the suffix)GaewayFilterFactory, full name is prefix + suffix)

Filter prefix effect parameter
StripPrefix The path used to truncate the original request Use a number to indicate the number of paths to truncate
AddRequestHeader Add header for original request Name and value of header
AddRequestParameter Add request parameters to the original request Parameter name and value
Retry Retry for different responses reties、statuses、methods、series
RequestSize Sets the size of the maximum request packet allowed to be received Request packet size, in bytes, default 5m
SetPath Modify the path of the original request Modified path
RewritePath Rewrite the original request path The regular expression of the original path and the rewritten path
PrefixPath Prefix the original request path prefix path
RequestRateLimiter For request current limiting, the current limiting algorithm is token bucket KeyResolver、reteLimiter、statusCode、denyEmptyKey

Built in filter, friends can try it by themselves. If you have any questions, please ask questions!

Let’s talk about how to customize the filter factory.don't say so much, let’s go over the code


Understand microservices - gateway of service gateway

configuration file

Understand microservices - gateway of service gateway

When we turn on request counting, we can see that the console counts the number of requests:

Understand microservices - gateway of service gateway

Therefore, we can easily implement local filters in this way

2) Global filter

The global filter works on all routes without configuration. The global filter can realize the unified verification of permissions, security verification and other functions

As usual, let’s have a look firstGatewayWhat global filters exist in:

Understand microservices - gateway of service gateway

Compared with the local filter, the naming of the global filter does not have too many constraints. After all, it does not need to be configured in the configuration file.

Let’s get familiar with the classic global filter

Filter name effect
ForwardPathFilter / ForwardRoutingFilter Path forwarding related filter
LoadBalanceerClientFilter Load balancing client related filters
NettyRoutingFilter / NettyWriteResponseFilter HTTP client related filters
RouteToRequestUrlFilter Routing URL related filters
WebClientHttpRoutingFilter / WebClientWriteResponseFilter Request the webclient client to forward the real URL of the request and write the response to the current request response
WebsocketRoutingFilter Websocket related filter

After understanding the built-in filter, let’s see how to define the global filter!


Understand microservices - gateway of service gateway

For the global filter, we do not need to configure it in the configuration file because it works on all routes

test result


Understand microservices - gateway of service gateway


Understand microservices - gateway of service gateway

As you can see, we use the global filter toauthentication Handle if not carriedtokenWill not be accessible!

Understand microservices - gateway of service gateway

Here we have learned that the routing, forwarding and permission verification of the service gateway can even be based onAssertions and filtersMake rough and simple API monitoring and current limiting

But actually forAPI monitoringandCurrent limiting, there are already better components in the spring cloud to do both. After all, a single principle, the more you do, the more you often make mistakes!

We will continue to organize articles on spring cloud components later. Please pay attention!

For the framework of microservices, it is up to us to decide which is better or worse. But no matter which is good or bad, in the face of the emergence of a new technology, what we need to do most is to accept it, tolerate it, and then make good use of it. It’s a mule or a horse. We’ll know by ourselves.

Don’t talk, don’t be lazy, make a meal with a small dishBlowing ox x as a frameworkProgram ape ~ point a concern and be a partner, so that the dishes are no longer lonely. See you later!

Understand microservices - gateway of service gateway

If you work harder today, you can say less begging words tomorrow!

I am a small dish, a man who grows stronger with you. 💋

The official account of WeChat has been opened.Little dish Liangji, students who don’t pay attention remember to pay attention!

Recommended Today

On the mutation mechanism of Clickhouse (with source code analysis)

Recently studied a bit of CH code.I found an interesting word, mutation.The word Google has the meaning of mutation, but more relevant articles translate this as “revision”. The previous article analyzed background_ pool_ Size parameter.This parameter is related to the background asynchronous worker pool merge.The asynchronous merge and mutation work in Clickhouse kernel is completed […]