When discussing software architecture and integrations, you’ve likely heard two fancy-sounding terms thrown around:APIsandmicroservices

这两个概念是web应用程序开发的中心opment and design today, and there’s certainly overlap in their uses. However, it’s important to recognize the differences between microservices and APIs and how they’re applied. This way, you can have more productive conversations with developers and better understand the applications you use and sell.

In this guide, we’ll start by defining both APIs and microservices separately — what they do, and how they work, and why they’re important. Next, we’ll see how APIs and microservices fit together in a modern software ecosystem.

Download Now: Free Intro Guide to HTML & CSS

What is an API?

一个应用程序接口, or API, is the part of an application that communicates with other applications. More technically speaking, an API is a group of protocols and methods that define how two applications share and modify each other’s data.

APIs are necessary in our modern digital infrastructure because they enable standardized and efficient communication between applications which might differ in function and construction.

一个API sits between a software’s core components and the public, and external developers can access certain parts of an application’s backend without needing to understand how everything works inside the app. This is what makes an API aninterface为职业grammers.

What Are APIs Used For?

If you use software, you use APIs. That’s because APIs enable software integrations — they allow otherwise separate software entities to share information and function together.

Imagine you’re online shopping and ready to check out. You see that the store you’re on gives the option to pay through PayNow, a payment processor, and you already have an account on PayNow.com with your payment info set up. How convenient!

Since PayNow is a different company from the store you’re currently on, an API facilitates an interaction between the store and PayNow. First, the store uses PayNow’s payment gateway API to request your payment information. Next, the PayNow API fields the request, validates it, fetches the information from its customer database, and sends it back to the store. Finally, the store uses your card information to complete the transaction.

Thanks to PayNow, your store gets all the info it needs to complete your checkout without having to access PayNow’s private database itself, and without requiring you to navigate off the store website.

这样的交流几乎在任何两个单独的应用程序一起工作时都会发生。其他一些现实世界的示例包括embedded YouTube video,使用航空公司API访问飞行时间和价格的旅行网站,使用Google和/或Facebook的API之一的网站允许社交登录, and a navigation app accessing a public transit system’s API for real-time transportation data.

REST APIs

由于API更像是一个概念,因此程序员可以为其应用构建API。但是,大多数依靠框架来创建它们。

代表代表性状态转移的REST是开发API的框架,符合静止的API称为REST API。REST API是用于跨平台集成的最常见API类型,也用于微服务。

You can read ourfull guide of REST APIsfor a detailed explanation of this framework. But, to give a brief overview: REST outlines a set of constraints for building APIs that make them efficient and secure. REST APIs work by fielding HTTP requests and returning responses inJSON(JavaScript对象表示法)format. HTTP (Hypertext Transfer Protocol) is already the standard protocol for web-based data transfer. So, with some knowledge of HTTP, developers can easily learn how to build or interact with a REST API.

外部与内部API

现在,全面披露:到目前为止,我谈论的API都是特定类型的API的实例,称为Aweb API。Web APIs facilitate communication between web servers. My examples are also allopen APIs,这意味着它们可供任何人使用,包括第三方软件开发人员。bob电竞官方下载

但是,不必公开使用API​​。Internal APIs用于在应用程序内进行通信,并且访问通常仅限于组织的员工和授权的开发人员。知道这一点,我们现在可以开始解开微服务。

What is a Microservice?

A microservice is a style of software architecture that divides an application’s different functions into smaller components called “services.” When an application is built this way, it’s said to follow amicroservice architecture

(Quick note: Developers often refer to these smaller components as “microservices” themselves. To avoid confusion, I’ll stick to the term “services” when describing these components, and “microservice” when referencing the entire system architecture.)

例如,诸如Paynow之类的应用程序的微服务体系结构可以包括用于用户帐户管理,与在线商家集成以及用户身份验证的单个服务。每种服务都像较大系统中的自己的小软件一样工作。bob电竞官方下载

Each service within the larger microservices has just one task, but the scope of these tasks is up to the app’s developers. A basic software application may rely on a few services, as is with PayNow. Or, in the case of large software companies, an application might comprise hundreds of granular services with highly specific functions.

为什么使用微服务体系结构?

To understand why a microservice architecture is beneficial for software developers, we first need to understand its predecessor, themonolith

A monolithic application is the opposite of a microservice — instead of assigning different tasks to different self-contained services, every function of the application is handled by a single program.

虽然以这种方式开始开发应用程序可能是有意义的 - 为什么要创建多个程序来担心?- 随着功能和复杂性的增长,对整体的拥护者将遇到问题。将应用程序的各个方面填充到一个程序中,因此很难编程和发布更新,跟踪更改,确定问题,为开发人员委派任务以及通常理解代码。

In other words, everything is so connected inside the monolith that it can be hard to untangle. This created the need for a new type of architecture, hence the rise of microservices. Compared to a monolith, the microservice architecture improves:

  • 更新:在微服务应用程序中,更新单个服务不需要修改整个系统。这节省了时间,金钱和调试工作。它还可以实现滚动更新,而不是不频繁的主要更新。
  • Simplicity:A developer doesn’t have to understand the entire system architecture to understand one aspect of the software.
  • Team organization:Microservices define boundaries between developer responsibilities. DevOps teams can be assigned to one or more microservices, instead of some portion of a nebulous monolith.
  • Security:如果一项服务受到妥协,(理想情况下)将不会显着影响任何其他服务。
  • Robustness:同样,如果一项服务中断,其他服务就不会受到伤害。
  • 灵活性:如果团队希望以一种特定的方式构建服务(例如,使用不同的语言或框架),他们就不必担心这可能如何影响其他组件。

总结所有内容:通过分开责任,微服务加快并简化软件开发过程。bob电竞官方下载

Of course, a collection of isolated modules won’t do much good for an application, which is why these services are linked together by — you guessed it — APIs.

微服务与API

Before comparing these concepts, let’s quickly review:

  • API是与其他应用程序通信的Web应用程序的一部分。软件bob电竞官方下载的API定义了一组可接受的请求,要对API提出并对这些请求的响应。
  • A microservice is an approach to building an application that breaks down an application’s functions into modular, self-contained programs. Microservices make it easier to create and maintain software.

虽然不同的东西,但经常将微服务和API配对,因为services within a microservice use APIs to communicate with each other。Similar to how an application uses a public API to integrate with a different application, one component of a microservice uses a private API to access a different component of the same microservice.

在微服务中,每个服务都有自己的API,可以确定其可能收到的请求以及其响应方式。这些API通常遵循休息原则。以下是与内部API一起持有的基本微服务的视觉示例:

visual diagram of microservices vs apis and how they're used together in an application

请注意,只有一个模块与第三方开发人员进行交互。在我们的示例中,此特定服务处理与其他应用程序的集成。因此,该特定的API是面向公众的,而此微服务中的所有其他API都是私人的。

It’s important to note that no two microservices are alike, and all utilize APIs differently. Some might assign multiple APIs to one service, or use a single API for accessing multiple services. The visualization above is to help you grasp the overall concept of microservices and APIs, but not every application follows a one-to-one API-to-service pairing.

最后,请记住,API超出了微服务。正如我们所讨论的那样,Web API启用了系统之间的数据共享,这对于许多Web应用程序都是必需的。此外,API可以在内部使用,但没有微服务实现。

软件体系结构的未来(和现在)bob电竞官方下载

Over the past decade, leading software companies — your Amazons, Netflixes, and Spotifys — have adopted the microservice approach. Sure, their implementations are更复杂,但基本原则是相同的:将应用程序的任务分解为软件子组件使一切变得更简单,更有效,并且API将其全部捆绑在一起。bob电竞官方下载

If your organization is creating or updating its software infrastructure, it helps to understand these concepts. To this day, APIs and microservices are reshaping not just how software works together, but how people collaborate as well.

New Call-to-action

css introduction

Originally published Nov 23, 2020 7:00:00 AM, updated November 23 2020

主题:

Application Programming Interface (API)