31. Microservice Service Gateway

1. What is a service gateway

Service Gateway = Route Forwarding + Filter

1. Routing and forwarding: Receive all external requests and forward them to the back-end microservices;

2. Filter: A series of cross-cutting functions can be completed in the service gateway, such as permission verification, current limiting and monitoring, etc., which can be completed through filters (in fact, routing and forwarding are also implemented through filters).

 

2. Why do you need the service gateway The above-mentioned cross-cutting function (taking permission verification as an example) can be written in three places:

·         Implement each service by itself

Write to a common service, and then all other services depend on this service        

Write to the pre-filter of the service gateway, and all requests come over for permission verification        

The first one, the disadvantages are too obvious and basically unnecessary; the second one is much better than the first one, code development will not be redundant, but there are two disadvantages:

Since each service introduces this public service, it is equivalent to introducing the same code for permission verification in each service, which increases the size of the jar         package of each service for no reason, especially for the use of docker images. In deployment scenarios, the smaller the jar , the better;

Since each service introduces this public service, it may be difficult for us to upgrade this service in the future, and the more functions of         the public service, the more difficult it is to upgrade, and suppose we change the way of permission verification in the public service , to make all services use the new permission verification method, we need to re-package all the previous services, compile and deploy.

The service gateway can just solve such problems:

Write the logic of         permission verification in the filter of the gateway, the back-end service does not need to pay attention to the code of permission verification, so the jar package of the service will not introduce the logic of permission verification, and will not increase the size of the jar package;

·         If you want to modify the logic of permission verification, you only need to modify the permission verification filter in the gateway, instead of upgrading all existing microservices.

So, a service gateway is needed! ! !

 

3. Service Gateway Technology Selection

The microservice architecture after the introduction of the service gateway is as above, which generally consists of three parts: service gateway, open-service and service .

1. Overall process:

·         Service gateway, open-service and service are registered to the registry when they are started;

·When the         user requests, the gateway is directly requested, and the gateway performs intelligent routing and forwarding (including service discovery, load balancing) to the open-service , which includes operations such as permission verification, monitoring, and current limiting

·         open-service aggregates the internal service response, returns it to the gateway, and the gateway returns it to the user

2. Points to note when introducing gateways

·         Adding a gateway and adding a layer of forwarding (original user requests to directly access open-service ), the performance will drop a little (but not much, usually, the performance of the gateway machine will be very good, and the gateway and open-service access Usually intranet access, very fast);

·         Single point problem of gateway: In the whole network call process, there must be a single point, which may be gateway, nginx , dns server, etc. To prevent the single point of the gateway, you can hang another nginx in front of the gateway layer. The performance of nginx is extremely high, and it will not hang. After this, the gateway service can continuously add machines. But such a request is forwarded twice, so the best way is to deploy the gateway single-point service on a powerful machine (estimating the configuration of the machine through stress testing), and the performance comparison between nginx and zuul , according to foreign countries According to the experiment done by one of my buddies, there is not much difference. Zuul is an open source framework used as a gateway open sourced by netflix ;

·The         gateway should be as light as possible.

3. Basic functions of service gateway

Intelligent routing: Receive all external requests and forward them to the back-end external service open         -service ;

oNote    : We only forward external requests, and requests between services do not go through the gateway, which means that full link tracking, internal service API monitoring, fault tolerance for calls between internal services, and intelligent routing cannot be done at the gateway; of course, you can also If all service calls go through the gateway, almost all functions can be integrated into the gateway, but in this case, the gateway will be under great pressure and overwhelmed.

Permission verification: Only the user's request to the open-service service is verified ,         and the internal request of the service is not verified. Is it necessary to verify the request inside the service?

API monitoring: only monitor requests passing through the gateway, as well as some performance indicators of the gateway itself (for example, gc         , etc.);

Current limiting: cooperate with monitoring to perform current limiting operation ;        

Unified collection of API logs: similar to an aspect         , recording relevant logs when the interface enters and exits

·         . . . follow-up

The above functions are the basic functions of the gateway, and the gateway can also implement the following functions:

·         A|B test: A|B test is a relatively big thing, including background experiment configuration, data buried point (see conversion rate), and offload engine. In the service gateway, the offload engine can be implemented, but in fact the offload engine will Call internal services, so if it is based on the architecture shown above, the offloading engine is best done in the open-service , not in the service gateway.

·         . . . follow-up

 

4. Technical selection

The author is going to build a lightweight service gateway by himself. The technical selection is as follows:

·         Development language: java + groovy , the advantage of groovy is that the gateway service can dynamically add filters to achieve some functions without restarting;

Microservice         basic framework: springboot ;

·         Gateway basic components: netflix zuul ;

·         Service registry: consul ;

·         Permission verification: jwt ;

API monitoring: prometheus + grafana         ;

·         API unified log collection: logback + ELK ;

·         Stress test: Jmeter ;

 


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326007687&siteId=291194637