本文摘自AWS白皮书,《Implementing Microservices on AWS》
摘要
微服务是软件开发的一种架构和组织方法,它可以加速部署周期、增强创新和自主性,提高软件应用程序的可维护性和可扩展性,通过使用敏捷方法帮助团队独立工作,扩展组织交付的软件和服务。微服务方案中,软件由小服务组成,小服务通过定义良好的、可以独立部署的API进行通信。这些服务由小型自主团队拥有。这种组织方法是成功扩展组织的关键。
在客户构建微服务时,我们观察到三种常见的模式:API驱动、事件驱动和数据流。在这篇文章中,我们将介绍三种方法,并总结微服务的共同特征,讨论构建微服务的主要挑战,并描述产品团队如何利用Amazon Web Services(AWS)来克服这些挑战。
介绍
微服务架构对于软件工程来说并不是一种全新的方法,而是各种成功和成熟概念的组合,比如:
- Agile software development 敏捷软件开发
- Service-oriented architectures 面向服务的体系结构
- API-first design API的设计
- Continuous Integration/Continuous Delivery (CI/CD) 持续集成/持续交付
AWS上的简单微服务
典型的应用程序使用不同的层构建-用户接口层、业务层和持久层。微服务体系结构的核心思想是将功能拆分为内聚的"垂直领域"-不是通过技术层,而是通过实现特定领域。
用户接口
web应用程序通常使用JavaScript框架来实现与REST或RESTful API通信的单页面应用程序。静态web内容可以使用Amazon Simple Storage Service (Amazon S3)和Amazon CloudFront提供。
微服务
我们常说api是微服务的大门,API充当一组编程接口(通常是RESTful web服务API)背后的应用程序逻辑的入口点。
数据存储
数据存储用于存储微服务所需的数据。流行的会话数据存储是内存缓存,如Memcached或Redis。AWS提供这两种技术,作为管理的Amazon ElastiCache服务的一部分。
降低操作复杂度
我们所描述的体系结构已经在使用托管服务,但是我们仍然需要管理亚马逊弹性计算云(Amazon EC2)实例。我们可以进一步减少运行所需的操作工作,通过使用完全无服务器的体系结构来维护和监视微服务。
无服务器微服务
"没有服务器比没有服务器更容易管理"。清除服务器是消除操作复杂性的一种好方法。Lambda与API网关紧密集成。从API网关到Lambda进行同步调用的能力支持创建完全无服务器的应用程序。
分布式系统组件
在了解了AWS如何解决与单个微服务相关的挑战之后,我们现在希望关注跨服务的挑战,比如服务发现、数据一致性、异步通信以及分布式监视和审计。
服务发现
微服务体系结构的主要挑战之一是允许服务彼此发现和交互。Amazon ECS现在包含了集成服务发现,使您的容器化服务更容易发现并彼此连接。
分布式数据管理
单片应用程序通常由大型关系数据库支持,该数据库定义了所有应用程序组件通用的单个数据模型。在微服务方法中,这种中央数据库将妨碍建立分散和独立组件的目标。每个微服务组件都应该有自己的数据持久层。
异步通信和轻量级的消息
在传统的单片应用程序中,通信是直接的—应用程序的一部分使用方法调用或内部事件分发机制与其他部分通信。如果使用解耦的微服务实现相同的应用程序,则必须使用网络通信实现应用程序不同部分之间的通信。
分布式监控
微服务体系结构由许多需要监视的不同分布式组件。您可以使用Amazon CloudWatch来收集和跟踪指标、集中和监视日志文件、设置警报并自动响应AWS环境中的更改。
结论
微服务架构是一种分布式设计方法,旨在克服传统单片架构的局限性。微服务帮助扩展应用程序和组织,同时改进周期时间。然而,它们也带来了一些挑战,可能增加额外的体系结构复杂性和操作负担。
AWS提供了大量的托管服务,可以帮助产品团队构建微服务体系结构,并最小化体系结构和操作复杂性。本篇文章将指导您了解相关AWS服务,以及如何使用AWS服务实现典型模式,如服务发现或事件源等。
This article is excerpted from the AWS whitepaper, "Implementing Microservices on AWS"
Summary
Microservices are an architectural and organizational approach to software development that can accelerate deployment cycles, enhance innovation and autonomy, and improve the maintainability and scalability of software applications. By using agile methods, microservices help teams work independently and scale the software and services delivered by the organization. In the microservices approach, software is composed of small services that communicate through well-defined, independently deployable APIs. These services are owned by small autonomous teams. This organizational approach is key to successfully scaling organizations.
When customers build microservices, we observe three common patterns: API-driven, event-driven, and data streaming. In this article, we will introduce all three approaches, summarize the common characteristics of microservices, discuss the main challenges of building microservices, and describe how product teams can leverage Amazon Web Services (AWS) to overcome these challenges.
Introduction
Microservices architecture is not an entirely new approach to software engineering, but rather a combination of various successful and mature concepts, such as:
- Agile software development
- Service-oriented architectures
- API-first design
- Continuous Integration/Continuous Delivery (CI/CD)
Simple Microservices on AWS
Typical applications are built using different layers—user interface layer, business layer, and persistence layer. The core idea of microservices architecture is to split functionality into cohesive "vertical domains"—not by technical layers, but by implementing specific domains.
User Interface
Web applications typically use JavaScript frameworks to implement single-page applications that communicate with REST or RESTful APIs. Static web content can be served using Amazon Simple Storage Service (Amazon S3) and Amazon CloudFront.
Microservices
We often say that APIs are the gateway to microservices. An API acts as the entry point for application logic behind a set of programming interfaces (usually RESTful web service APIs).
Data Storage
Data storage is used to store the data needed by microservices. Popular session data stores are in-memory caches such as Memcached or Redis. AWS provides both technologies as part of the managed Amazon ElastiCache service.
Reducing Operational Complexity
The architecture we described already uses managed services, but we still need to manage Amazon Elastic Compute Cloud (Amazon EC2) instances. We can further reduce the operational work required to maintain and monitor microservices by using a fully serverless architecture.
Serverless Microservices
"No server is easier to manage than no server." Eliminating servers is a good way to eliminate operational complexity. Lambda integrates tightly with API Gateway. The ability to make synchronous calls from API Gateway to Lambda enables the creation of fully serverless applications.
Distributed System Components
After understanding how AWS addresses challenges related to individual microservices, we now want to focus on cross-service challenges, such as service discovery, data consistency, asynchronous communication, and distributed monitoring and auditing.
Service Discovery
One of the main challenges of microservices architecture is allowing services to discover and interact with each other. Amazon ECS now includes integrated service discovery, making it easier for your containerized services to discover and connect to each other.
Distributed Data Management
Monolithic applications are typically backed by a large relational database that defines a single data model common to all application components. In the microservices approach, this central database would hinder the goal of building decentralized and independent components. Each microservice component should have its own data persistence layer.
Asynchronous Communication and Lightweight Messaging
In traditional monolithic applications, communication is direct—parts of the application communicate with other parts using method calls or internal event dispatching mechanisms. If the same application is implemented using decoupled microservices, network communication must be used to implement communication between different parts of the application.
Distributed Monitoring
Microservices architecture consists of many different distributed components that need monitoring. You can use Amazon CloudWatch to collect and track metrics, centralize and monitor log files, set alarms, and automatically respond to changes in your AWS environment.
Conclusion
Microservices architecture is a distributed design approach aimed at overcoming the limitations of traditional monolithic architecture. Microservices help scale applications and organizations while improving cycle times. However, they also bring some challenges that may add extra architectural complexity and operational burden.
AWS provides a large number of managed services that can help product teams build microservices architecture and minimize architectural and operational complexity. This article will guide you through relevant AWS services and how to use AWS services to implement typical patterns such as service discovery or event sourcing.