Understand web api gateway and webapi Gateway

Source: Internet
Author: User

Understand web api gateway and webapi Gateway
Hide details

In real life, there are many cases of hiding details. For example, when we press the power button on a computer, the computer will start automatically, for users, you only need to know how to press the button. However, the working principle inside the computer is actually a complicated process.

What if I don't hide the details?

I think the possible result is that computers can only be operated by special professionals and will never become a necessary tool for everyone. For most users, they do not know what CPUs, memory, hard disks, and graphics cards work with each other. They only care about how to use the software to complete their tasks after opening the computer.

Facade Mode

In the object-oriented design, GOF has a facade mode, which is a typical application that hides details from the client. You can also take a look at my notes a few years ago: the facade mode.

Web api Gateway

Generally, web api gateway is the only entry to the system. It encapsulates the internal architecture of the system and provides unified services for clients. Some public logics unrelated to services can be abstracted into gateways for implementation, such as client authentication, access control, monitoring, and caching.

  • Client Authentication
    Both Intranet and Internet interfaces require user identity authentication. In some large companies, user authentication has a unified single-point logon system, if every microservice system is connected to a single sign-on system, it is a waste of resources and the development efficiency is low. The authentication part can be extracted to the gateway layer, and then the microservice system only pays attention to its own business without paying attention to the authentication logic.

  • Access Control
    Set the whitelist, access count, Access frequency, and other settings for some specific interfaces. However, the configuration and changes of these non-business functions will not affect the implementation of microservices, which can be performed independently at the gateway layer.

  • Server Load balancer
    You can flexibly develop Load Balancing policies at the gateway layer.

  • Security
    An additional protection layer can be added to the gateway layer to prevent malicious attacks. If the client directly connects to microservices, each exposed microservice must face security issues.

In terms of design, the web api gateway is equivalent to the facade mode provided above, and also hides details from the client. In addition to the common public functions provided above, there are some practical functions as follows:

Throttling

There is also a need for throttling for large Internet projects. To prevent the site from being pushed across by unknown large traffic, a throttling policy may be adopted. The Gateway configures a threshold value, when the number of requests exceeds the threshold, an error is returned without going through the rest of the logic.

How can throttling be implemented? There are many traffic limiting solutions:

  • Hystrix can be used at the gateway layer.
  • If it is for a client to be determined, you can also use nginx to throttling.
  • Guava provides a RateLimiter, which is based on the algorithm of the token bucket. It places tokens in the queue at a fixed rate. You can use it to implement throttling by yourself and implement throttling by yourself.
Compatible with more protocols

For example, some services are SOAP, binary-based Thrift, and dubbo rpc implementations, which can be uniformly converted to HTTP for external provision.

Problems caused by microservices

With the development of business, the original simple system will become more and more complex. One team will become multiple teams, and there will be various problems in the development of multiple teams in one system at the same time, the increase in data volume also slows down the performance of a single database. As a result, the system is divided vertically by business. For example:

  • Product System
  • Price System
  • Promotion System
  • Order System
  • Inventory System
  • Comment System
  • Recommendation System
  • User System

These systems are currently popular in the form of microservices, exposing a batch of fine-grained interfaces to call other systems.

How does the front-end system call microservices?

For example, a user can view the details page of a product, which includes the basic product information, price information, promotion information, recommendation information, and comment information. The page consists of the preceding microservices, the front-end system needs to call a large number of microservices to obtain all the data of the product. This is not feasible for Internet projects requiring performance. http requests alone are more than doubled, it also increases the complexity of the client. It needs to know the details of all these microservices.

Do not let the front-end Know the existence of microservices

Even if the system is divided into multiple independent microservices at the business level, the front-end system should hide details and provide coarse-grained interfaces.

System Architecture

Here we will review a cross-border e-commerce system that I previously participated in. From the perspective of architecture, we still have a relatively standard practice. The front-end is an APP and H5, connected to a mobile api, which contains various sub-services.

In fact, we are targeting the project. The e-commerce system is connected to mobile api, and another offline store APP is connected to the newly developed store api. These two APIs are completely independent, not a company's one web api. Therefore, our two web APIs are project-level gateways, and some companies may provide company-level gateways.

Brief architecture description:

  • The front end is APP + H5
  • The H5 site uses Nginx as a reverse proxy to execute js and does not understand the APP, so it skipped.
  • Mobile API, the only entry for H5 and APP to connect to the backend, implemented using Spring MVC. It contains all the interfaces required by the front-end.
  • Microservice, DUBBO-implemented RPC, database middleware mybatis.
  • Cache, based on Spring Cache + redis.
  • The Retrieval System is based on elasticsearch.
  • Message System, RabbitMQ. Is a broker that updates business data to the retrieval system.

No Throttling is used. The data volume was too small at the time. The concern was not that the server was rushed across, but that no traffic was rushed.
It does not include some advanced usage of parallel request merging. It can be understood as a highly cohesive service.

Advantages of web api Gateway
  • Hide details and be client-friendly
  • Simplified client development complexity
  • Reduces interaction between the client and the server
  • You can perform all-in-one cut-off tasks to prevent each sub-system from being deployed separately.
  • Decouples the client from the server, making it easy to construct heterogeneous systems
Disadvantages of web api Gateway
  • You need to add a site with high availability and scalability.
  • The web api Gateway project is a serial task in development. Every interface exposed must update the web api gateway, if there is a need for different departments to update at the same time, there will be queuing updates.

The shortcomings above are tolerable compared with those of web api gateway.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.