Skip to content

230521_serverless,microservices,RESTFUL

what is serverless and microservices#

Serverless and microservices are two architectural styles for building modern applications, but they have some differences and similarities.
无服务器和微服务是用于构建现代应用程序的两种体系结构样式,但它们有一些差异和相似之处。

Serverless:#

Serverless computing is a model of cloud computing that abstracts away the infrastructure layer, allowing developers to focus on writing code without worrying about managing the infrastructure. Instead of dealing with servers and operating systems, developers write functions using a serverless platform such as AWS Lambda, Google Cloud Functions, or Azure Functions. The code runs in response to events, and the platform automatically scales the compute resources, managing the infrastructure and automatically allocating resources as needed. Therefore, serverless refers to the idea that you don't have to manage servers or infrastructure yourself.
无服务器计算是一种云计算模型,它抽象出基础架构层,使开发人员能够专注于编写代码,而无需担心管理基础架构。开发人员不使用服务器和操作系统,而是使用无服务器平台(如AWS Lambda,Google Cloud Functions或Azure Functions)编写函数。代码运行以响应事件,平台会自动缩放计算资源,管理基础结构并根据需要自动分配资源。因此,无服务器是指您不必自己管理服务器或基础结构的想法。

Microservices:#

Microservices is an architectural style that structures applications as a collection of small, independent services that communicate with each other through APIs. Each service is loosely coupled and can be independently developed and deployed. This architecture helps to promote the modularity of applications, enabling developers to build complex applications by composing small reusable services. Each service does one thing well and can be scaled independently, which makes applications more agile and maintainable.
微服务是一种架构风格,它将应用程序构建为通过 API 相互通信的小型独立服务的集合。每个服务都是松散耦合的,可以独立开发和部署。此体系结构有助于促进应用程序的模块化,使开发人员能够通过组合小型可重用服务来构建复杂的应用程序。每个服务都做好一件事,并且可以独立扩展,这使得应用程序更加敏捷和可维护。

Similarities:#

Both serverless and microservices aim to promote the modularity and composability of applications. The two architectures are designed to make applications more agile, flexible, and responsive to changing business needs. Both architectures are often used with cloud-based technologies to enable scalability and high availability.
无服务器和微服务都旨在促进应用程序的模块化和可组合性。这两种体系结构旨在使应用程序更加敏捷、灵活,并能响应不断变化的业务需求。这两种体系结构通常与基于云的技术一起使用,以实现可伸缩性和高可用性。

Differences:#

  • Serverless refers to the idea that you don't have to manage servers or infrastructure yourself, while microservices refer to an architectural style that structures applications as a collection of independent services.
    无服务器是指您不必自己管理服务器或基础结构的想法,而微服务是指将应用程序构建为独立服务集合的体系结构样式。

  • In the case of serverless, the infrastructure layer is abstracted away, and the developer cannot directly manage it. On the contrary, in the case of microservices, developers have more control over deploying and managing the infrastructure that is running the services.
    在无服务器的情况下,基础结构层被抽象出来,开发人员无法直接管理它。相反,对于微服务,开发人员可以更好地控制部署和管理运行服务的基础结构。

  • Serverless typically refers to the use of Functions as a Service (FaaS), while microservices are not limited to a specific technology or platform.
    无服务器通常是指使用功能即服务 (FaaS),而微服务不限于特定的技术或平台。

In summary, while both serverless and microservices promote application modularity and scalability, they differ in their approach to architecture and infrastructure management.
总之,虽然无服务器和微服务都促进了应用程序的模块化和可伸缩性,但它们在架构和基础架构管理方法上有所不同。

what is REST, what is restful api, what is restful web service#

REST, or Representational State Transfer, is an architectural style for building distributed systems based on HTTP and other web standards. RESTful APIs and RESTful web services are two common methods of implementing REST. REST或具象状态传输是一种基于HTTP和其他Web标准构建分布式系统的架构风格。RESTful API 和 RESTful Web 服务是实现 REST 的两种常用方法。

RESTful API:#

A RESTful API is an application programming interface (API) that follows REST principles. A RESTful API provides a uniform interface for interacting with resources or data elements, using HTTP methods such as GET, POST, PUT, DELETE, and PATCH. These methods allow clients to perform operations on a web server by sending a request to a specific URL and receiving a response. Many applications use RESTful APIs as a way to interact with other services, such as social media networks or cloud services. RESTful API 是遵循 REST 原则的应用程序编程接口 (API)。RESTful API 提供了一个统一的接口,用于使用 GET、POST、PUT、DELETE 和 PATCH 等 HTTP 方法与资源或数据元素进行交互。这些方法允许客户端通过向特定 URL 发送请求并接收响应来在 Web 服务器上执行操作。许多应用程序使用 RESTful API 作为与其他服务(如社交媒体网络或云服务)交互的一种方式。

RESTful Web Service: RESTful Web Service:#

A RESTful Web Service is a service that adheres to the REST architecture. A RESTful Web Service is a way to expose data or functionality as a web service, in which operations are performed on a set of resources through HTTP methods, like GET, POST, PUT, DELETE, and other web standards. RESTful Web Services use HTTP requests to GET, PUT, POST, and DELETE data typically in JSON or XML format. These services are designed to provide interoperability between different systems, languages, and platforms. RESTful Web Services are stateless, meaning that the client maintains no session information with the server and all required information must be included in each request. RESTful Web 服务是一种遵循 REST 体系结构的服务。RESTful Web 服务是一种将数据或功能公开为 Web 服务的方法,其中通过 HTTP 方法(如 GET、POST、PUT、DELETE 和其他 Web 标准)对一组资源执行操作。RESTful Web Services 使用 HTTP 请求来获取、放置、发布和删除数据,通常采用 JSON 或 XML 格式。这些服务旨在提供不同系统、语言和平台之间的互操作性。RESTful Web 服务是无状态的,这意味着客户端不维护与服务器的会话信息,并且每个请求中必须包含所有必需的信息。

In summary, REST is an architectural style for building distributed systems based on HTTP and other web standards that promotes scalability, modifiability, and reliability. RESTful APIs and RESTful Web Services are two different forms of REST architecture that use HTTP methods to interact with web resources, while RESTful web services expose data or functionality through a web service, and RESTful APIs provide a standard interface for interacting with resources. 总之,REST 是一种基于 HTTP 和其他 Web 标准构建分布式系统的架构风格,可提高可伸缩性、可修改性和可靠性。RESTful API 和 RESTful Web Services 是两种不同形式的 REST 架构,它们使用 HTTP 方法与 Web 资源交互,而 RESTful Web 服务通过 Web 服务公开数据或功能,RESTful API 提供与资源交互的标准接口。

What are the architectural constraints of REST#

  1. Client-server: Separation of concerns between the client and server, allowing for independent development and evolution.
    客户端-服务器:客户端和服务器之间的关注点分离,允许独立开发和演变。

  2. Statelessness: Communication between the client and server is done in a stateless manner, where each request carries the necessary information required to process that request. The server does not store any client context.
    无状态:客户端和服务器之间的通信以无状态方式完成,其中每个请求都携带处理该请求所需的必要信息。服务器不存储任何客户端上下文。

  3. Cacheability: Responses from server-side should be cacheable to improve network efficiency, and reducing server-side workload when a cached response can be used.
    可缓存性:来自服务器端的响应应该是可缓存的,以提高网络效率,并在可以使用缓存的响应时减少服务器端工作负载。

  4. Layered System: System decomposition into a set of connected layers, each of which has a specific role and communicates only with the immediately adjacent layer(s).
    分层系统:系统分解为一组连接层,每个层都有特定的角色,并且仅与紧邻的层通信。

  5. Code on demand (optional): The ability to transfer executable code from the server to the client as a part of the response payload.
    按需代码(可选):将可执行代码作为响应有效负载的一部分从服务器传输到客户端的功能。

  6. Uniform Interface: Use of a shared and well-defined interface for accessing resources, defined by the following four sub-constraints:
    统一接口:使用共享且定义明确的接口来访问资源,

if The server does not store any client context. how do they use users info to make personalize service 如果服务器不存储任何客户端上下文。以及他们如何使用用户信息来提供个性化服务#

In a RESTful architecture, the server is stateless, which means that it does not store any client context. However, the server can still use user information to provide personalized services by leveraging techniques such as authentication and authorization. 在 RESTful 体系结构中,服务器是无状态的,这意味着它不存储任何客户端上下文。但是,服务器仍可以通过利用身份验证和授权等技术使用用户信息来提供个性化服务。

Authentication: The server can require authentication before providing personalized services to the user. The user first needs to identify themselves and prove their identity using a username and password, or other authentication means, before accessing the personalized services. 身份验证:服务器可以在向用户提供个性化服务之前要求身份验证。在访问个性化服务之前,用户首先需要使用用户名和密码或其他身份验证方式来识别自己并证明自己的身份。

Authorization: Once the user is authenticated, the server can use the authorization mechanisms to determine the appropriate level of access for the user. This can be done by assigning roles or permissions to the user, or by using other authorization mechanisms. 授权:对用户进行身份验证后,服务器可以使用授权机制来确定用户的适当访问级别。这可以通过向用户分配角色或权限或使用其他授权机制来完成。

Cookies and sessions: RESTful web services can also use cookies and sessions to store user-specific data on the client-side. Client applications can then send this data back to the server with each request to provide personalized services. However, this approach is not considered to be pure RESTful architecture as it is stateful. Cookie 和会话:RESTful Web 服务还可以使用 Cookie 和会话在客户端存储用户特定的数据。然后,客户端应用程序可以在每次请求时将此数据发送回服务器,以提供个性化服务。但是,这种方法不被认为是纯粹的 RESTful 体系结构,因为它是有状态的。

Use of client-provided data: The server can use the data provided by the client in the request to provide personalized responses. For example, a web application may ask the user to provide information about themselves (like their name, age, location) via an HTML form and then use that data to personalize the response. 使用客户端提供的数据:服务器可以使用客户端在请求中提供的数据来提供个性化的响应。例如,Web 应用程序可能会要求用户通过 HTML 表单提供有关他们自己的信息(例如他们的姓名、年龄、位置),然后使用该数据来个性化响应。

In summary, while the server in a RESTful architecture is stateless, it can still provide personalized services to the user by using authentication and authorization mechanisms, cookies, sessions, and client-provided data. 总之,虽然 RESTful 架构中的服务器是无状态的,但它仍然可以通过使用身份验证和授权机制、cookie、会话和客户端提供的数据为用户提供个性化服务。