The label

API gateway

intentions

Aggregate calls to microservices in a single location (API gateway). The user makes only one call, and then the API gateway invokes each associated microservice.

explain

With the microservice pattern, clients may need data from multiple different microservices. If the client invokes each microservice directly, this can prolong the load time because the client must make a network request for each microservice being invoked. Furthermore, client calls to each microservice directly bind the client to that microservice – if the implementation of the internal microservice changes (for example, if two microservices are combined at some point). If the location of microservices (host and port) changes, the use of these microservices must be updated.

The API Gateway pattern is intended to alleviate some of these problems. In the API Gateway in pattern, an additional entity (API gateway) is placed between the client and the microservice. The job of the API gateway is to aggregate calls to microservices. Instead of the client calling each microservice individually, calling the API gateway only once. The API gateway then invokes each microservice required by the client.

Real world examples

We are implementing the microservices and API gateway pattern for an e-commerce site. In this system, the API gateway invokes image and pricing microservices.

In simple terms

For systems implemented using microservices architecture, an API gateway is a single entry point aggregating calls to a single microservice.

Wikipedia says

An API gateway is a server and security policy that acts as an API front end, receives API requests, enforces restrictions, passes requests to back-end services, and passes responses back to the requester. Gateways typically include a transformation engine for choreography and modification, instant requests and responses. The gateway can also provide functions such as collecting data, analyzing data, and providing caching. Gateways can provide supporting functions such as authentication, authorization, security, auditing, and regulatory compliance.

applicability

Use the API gateway pattern when you are using a microservice architecture and need to provide an aggregation point for microservice invocations.


That’s the github project’s explanation

Personal understanding of gateways

Gateway function is dynamic proxy, is exposed to the external call service, all requests entry, users can not directly call the internal service interface, must pass through the gateway, and then forwarded to the specific service by the gateway

The network relationship of the intermediate network

  • The gateway service communicates with the external network
  • Internal services are not exposed to the outside world
  • The gateway service and the internal service network are connected on the Intranet

The gateway also performs some general operations, such as traffic limiting, permission verification, and general logs.

The specific process is shown in the figure below:

You learned how to code calls between services, as well as some understanding of Maven operations (this is explained in another article).

@Slf4j @Component public class PriceClientImpl implements PriceClient { @Autowired private RestTemplate restTemplate; @override public String getPrice() {// Set headers headers = new headers (); headers.setContentType(MediaType.APPLICATION_JSON); // Construct the requested object HttpEntity HttpEntity = new HttpEntity(headers); // Call ResponseEntity<String> ResponseEntity = via spring's restTemplate restTemplate.exchange("http://localhost:50006/price", HttpMethod.GET, httpEntity, String.class); logResponse(responseEntity); return responseEntity.getBody(); } Catch (RestClientException e) {// Invoke the exception log. Error ("Failure occurred while getting price info", e); } return null; } @param responseEntity */ private void logResponse(responseEntity) {if (isSuccessResponse(responseEntity.getStatusCode().value())) { log.info("Price info received successfully"); } else { log.warn("Price info request failed"); @param responseCode * @return */ private Boolean isSuccessResponse(int)  responseCode) { return responseCode >= 200 && responseCode <= 299; }}Copy the code

Github address of the project