软件体系架构 文章 和 翻译
- 软件架构体系
1.1. 系统与子系统
系统:泛指由一群有关联的个体组成,根据某种规则运作,能完成个别元件不能单独完成的工作的群体。
关联:系统是由一群有关联的个体组成的,没有关联的个体堆在一起不能成为一个系统。例如,把一个汽车发动机和一堆苹果放在一起不能称之为一个系统,把发动机、底盘、轮胎、车架组合起来才能成为一台汽车,构成一个系统。
规则:系统内的个体需要按照指定的规则运作,而不是单个个体各自为政。规则规定了系统内个体分工和协作的方式。例如,汽车发动机负责产生动力,然后通过变速器和传动轴,将动力输出到车轮上,从而驱动汽车前进。
能力:系统能力与个体能力有本质的差别,系统能力不是个体能力之和,而是产生了新的能力。例如,汽车能够载重前进,而发动机、变速器、传动轴、车轮本身都不具备这样的能力。
子系统:子系统也是由一群有关联的个体所组成的系统,多半会是更大系统中的一部分。子系统的定义和系统定义是一样的,只是观察的角度有差异,一个系统可能是另外一个更大系统的子系统。
以微信为例来做一个分析:
微信本身是一个系统,包含聊天、登录、支付、朋友圈等子系统。
朋友圈这个系统又包括动态、评论、点赞等子系统。
评论这个系统可能又包括防刷子系统、审核子系统、发布子系统、存储子系统。
评论审核子系统不再包含业务意义上的子系统,而是包括各个模块或者组件,这些模块或者组件本身也是另外一个维度上的系统。例如,MySQL、Redis 等是存储系统,但不是业务子系统
1.2. 模块、组件、服务
模块:是一套一致而互相有紧密关连的软件组织。它分别包含了程序和数据结构两部分。现代软件开发往往使用模块作为合成的单位
组件:自包含的、可编程的、可重用的、与语言无关的软件单元,组件可以很容易被用于组装应用程序中
模块和组件都是系统的组成部分,只是从不同的角度拆分系统而已。例如:
从逻辑的角度来拆分系统后,得到的单元就是“模块” ;从物理的角度来拆分系统后,得到的单元就是“组件” 。
划分模块的主要目的是职责分离;划分组件的主要目的是单元复用。
例如我们要做一个学生信息管理系统,这个系统从逻辑的角度来拆分,可以分为:登录注册模块、个人信息模块、个人成绩模块;从物理的角度来拆分,可以拆分为应用程序、 Nginx、Web 服务器、MySQL等
服务: 服务和组件有某种相似之处:它们都将被外部的应用程序使用。两者之间最大的差异在于:组件是在本地使用的(例如Jar文件);而服务是运行起来的,要通过同步或异步的远程接口来远程使用(例如RESTFul接口、web service、消息系统、RPC,或者socket)
服务是可以单独运行,并且对外提供功能的一种形式。可以将一个复杂的项目分解成多个服务。当某一个服务挂掉时不会拖垮整个系统。如果没有服务化,每当一个新的功能被添加到系统中就会影响到所有功能;如果采取服务化,每个服务只对其上下游的服务负责。
1. Software Architecture System
1.1 System and Subsystem
System: A system refers to a group composed of interrelated individuals that operate according to certain rules and can accomplish tasks that individual components cannot achieve alone. For example, an automobile engine and a pile of apples cannot be considered a system. However, when the engine, chassis, tires, and frame are combined, they form a car, which constitutes a system.
Interrelation: A system is composed of interrelated individuals. A mere collection of unrelated individuals does not form a system. For example, combining a car engine and a pile of apples does not create a system. Only when the engine, chassis, tires, and frame are assembled together can they form a car.
Rules: The individuals within a system must operate according to specific rules rather than functioning independently. These rules define the division of labor and collaboration among the components. For example, in a car, the engine is responsible for generating power, which is then transmitted through the gearbox and drive shaft to the wheels, propelling the car forward.
Capability: The capability of a system is fundamentally different from that of its individual components. The capability of a system is not the sum of the capabilities of its individual components but rather a new capability that emerges from their integration. For example, a car can carry a load and move forward, but the engine, gearbox, drive shaft, and wheels themselves do not possess this capability.
Subsystem: A subsystem is also a system composed of interrelated individuals and is often part of a larger system. The definition of a subsystem is the same as that of a system, but the perspective of observation is different. One system can be a subsystem of another larger system.
Example with WeChat:
WeChat itself is a system that includes subsystems such as chat, login, payment, and Moments. The Moments system further includes subsystems like posts, comments, and likes. The comment system may consist of sub-systems such as anti-spam, moderation, posting, and storage. The comment moderation subsystem does not contain business-related sub-systems but includes various modules or components. These modules or components are also systems in another dimension. For example, MySQL and Redis are storage systems but not business subsystems.
1.2 Module, Component, and Service
Module: A module is a set of software organizations that are consistent and closely related. It includes both programs and data structures. Modern software development often uses modules as the unit of integration.
Component: A component is a self-contained, programmable, reusable, and language-independent software unit that can be easily assembled into applications.
Modules and components are both parts of a system, but they represent different ways of decomposing a system. For example:
- When a system is decomposed logically, the resulting units are "modules"; when a system is decomposed physically, the resulting units are "components."
- The primary purpose of module decomposition is to separate responsibilities, while the main purpose of component decomposition is to enable unit reuse.
For example, if we develop a student information management system, it can be logically decomposed into modules such as login and registration, personal information, and personal grades. Physically, it can be decomposed into components such as the application, Nginx, web server, and MySQL.
Service: Services are somewhat similar to components in that they are both used by external applications. The main difference is that components are used locally (e.g., JAR files), while services are running and accessed remotely through synchronous or asynchronous interfaces (e.g., RESTful interfaces, web services, messaging systems, RPC, or sockets).
A service is a form of functionality that can run independently and provide functions to external applications. A complex project can be decomposed into multiple services. If one service fails, it will not bring down the entire system. Without service-oriented architecture, adding a new feature to a system would affect all existing functions. However, with service-oriented architecture, each service is only responsible for its upstream and downstream services.