Microservice Architecture-Service Gateway (Gateway)-Detailed Routing Function

Detailed routing function

In this section, let's take a look at how the routing in Gateway works; the routing function of the Gateway gateway is not a simple "forwarding" request. There are many things that happen before the request arrives at the gateway and flows to the specified service. It Not only can the request be rejected, but even the parameters of the request can be "tampered with". Let's take a look at the doorway in routing next.

1. Routing Triple Door

Many Routes can be defined in the Gateway, and a Route is a set of routes containing complete forwarding rules, which mainly consists of three parts:
insert image description here

  • Assertion collection: Assertion is the first link in routing processing. It is a routing matching rule, which determines whether a network request can be matched to the current routing for processing. The reason why it is a collection is that we can add multiple assertions to a route, and the first pass of the route is considered when each assertion is matched successfully.
  • Filter set: If the request passes the previous assertion match, it means that it is officially taken over by the current route, and then the request will go through a series of filter sets. The function of the filter is that the Eight Immortals cross the sea to show their magical powers. They can perform a series of operations on the current request, such as permission verification, or mention other non-business verification rules to the gateway filter layer. At the filter level, the interruption effect can still be achieved by modifying the Status Code in the Response, such as setting the Status Code to 403 for an access request that fails authentication and then interrupting the operation;
  • URI: If the request is successfully processed by the filter, the next step is to forward the request. URL is a uniform resource identifier, which can be a specific URL, a combination of IP+port, or a service name registered in Eureka.

2. About load balancing

For the last step of addressing, if the service discovery mechanism based on Eureka is used, then the service registration name can be used to call during the forwarding process of the Gateway, and the background will use the Ribbon to achieve load balancing (you can specify a specific service for a service) Load balancing strategy), the configuration method is as follows: lb://FEIGN-SERVICE-PROVIDER/, the previous lb refers to Ribbon as LoadBalancer.

3. Routing workflow

From the moment a request arrives at the gateway, look at the process of the internal flow of the Gateway.

insert image description here
Predicate Handler: The specific inheritance class is RoutePredicateHandlerMapping. First, it obtains all routes (the complete set of configured routes), and then loops through each Route in turn to match the application request with all the assertions configured in the Route. If all the assertions of the current Route pass the verification , the Predict Handler selects the current route. This mode is a typical chain of responsibility.

Filter Handler: After the route is selected in the previous step, the FilteringWebHandler will pass the request to the filter. In the specific processing process, not only the filter defined in the current Route will take effect, but also the Global Filter we added in the project Get involved. You can see that there are Pre Filter and Post Filter in the figure, which refers to the stage of the filter, and we will learn more about it in a later chapter.

Addressing: This step will forward the request to the address specified by the URL. Before sending the request, all Pre type filters will be executed, and the Post filter will take effect after the call request returns. The details of the filter will be Will be covered in a later chapter.

Guess you like

Origin blog.csdn.net/jianghao233/article/details/130055605