前言

过去几年,对 SOA 的几本书一直没有读明白,在工作中,也一直在不断去思考和研究,现在更多的是遇到微服务这个概念,所以需要好好研究一下微服务。

研究微服务,自然躲不开要读一读马丁富勒的这篇论文了,之前读过几遍,但是感觉还是有必要记录一下笔记。

其实在参考里面的这篇原文的地址里面,已经有了中文的翻译,不过感觉还是应该自己详细研读一下,更得来的比较深刻。

自然,对这么有深度的文章的学习,也不可能一蹴而就,只能是愚公移山式地,逐步完成。

博客

博客地址:IT老兵驿站

正文

a definition of this new architectural term

The term “Microservice Architecture” has sprung up over the last few years to describe a particular way of designing software applications as suites of independently deployable services. While there is no precise definition of this architectural style, there are certain common characteristics around organization around business capability, automated deployment, intelligence in the endpoints, and decentralized control of languages and data.

这里是说微服务架构这个术语在过去一些年很风靡,尽管对其还没有一个精确的定义,但是对于业务能力、自动化部署、节点的智能、语言和数据的去中心化的控制却有了相当的共识。

“Microservices” - yet another new term on the crowded streets of software architecture. Although our natural inclination is to pass such things by with a contemptuous glance, this bit of terminology describes a style of software systems that we are finding more and more appealing. We’ve seen many projects use this style in the last few years, and results so far have been positive, so much so that for many of our colleagues this is becoming the default style for building enterprise applications. Sadly, however, there’s not much information that outlines what the microservice style is and how to do it.

“Although our natural inclination is to pass such things by with a contemptuous glance” ---- 这一点挺有意思,无论中国人,还是外国人,对待新事物的态度看来是一致的,就是投以轻蔑的一瞥。这里作者也认为在软件架构中的概念已经很多了,大家可能有些麻木了,但是微服务真的是我们需要和一直想要追求的东西。

In short, the microservice architectural style [1] is an approach to developing a single application as a suite of small services, each running in its own process and communicating with lightweight mechanisms, often an HTTP resource API. These services are built around business capabilities and independently deployable by fully automated deployment machinery. There is a bare minimum of centralized management of these services, which may be written in different programming languages and use different data storage technologies.

微服务架构是一种方法,用来开发由一套小的 service 组成的一个应用,每一个 service 运行在它自己的进程中,互相通信使用一种轻量级的机制,一般是 HTTP resource API。对于这里有一点疑问,轻量级的通讯机制是指什么呢,gPRC 或者别的一些 RPC 机制算不算是呢?那么重量级的通讯机制又指什么呢?而且,后面不是说使用到了消息队列嘛,那怎么去使用这些通信组件呢?

这些服务都是围绕着业务能力和独立部署(完全自动化部署机制)来构建。只有一个最小化的中心化管理,并且可以使用各种语言和数据存储机制来实现。这里有一点疑问,这里和 SOA 的设计理念就有一些冲突了,SOA 是说在一个企业或者一个领域内,最好是应该对齐的。

To start explaining the microservice style it’s useful to compare it to the monolithic style: a monolithic application built as a single unit. Enterprise Applications are often built in three main parts: a client-side user interface (consisting of HTML pages and javascript running in a browser on the user’s machine) a database (consisting of many tables inserted into a common, and usually relational, database management system), and a server-side application. The server-side application will handle HTTP requests, execute domain logic, retrieve and update data from the database, and select and populate HTML views to be sent to the browser. This server-side application is a monolith - a single logical executable[2]. Any changes to the system involve building and deploying a new version of the server-side application.

“monolithic style”:传统的风格,一体化编程,三层结构,这里可以参考一本书《企业级架构》,那里讲了基本的企业级三层架构,一般的 Java 项目都是遵循着那本书的规则。

Such a monolithic server is a natural way to approach building such a system. All your logic for handling a request runs in a single process, allowing you to use the basic features of your language to divide up the application into classes, functions, and namespaces. With some care, you can run and test the application on a developer’s laptop, and use a deployment pipeline to ensure that changes are properly tested and deployed into production. You can horizontally scale the monolith by running many instances behind a load-balancer.

传统的实现方式。你的所有的对于处理一个请求的逻辑都运行在一个单独的进程中(其实,这一点对于 Java 来说,是一个线程),允许你把应用程序分割为 classes,functions,namespaces,等等。

Monolithic applications can be successful, but increasingly people are feeling frustrations with them - especially as more applications are being deployed to the cloud . Change cycles are tied together - a change made to a small part of the application, requires the entire monolith to be rebuilt and deployed. Over time it’s often hard to keep a good modular structure, making it harder to keep changes that ought to only affect one module within that module. Scaling requires scaling of the entire application rather than parts of it that require greater resource.

这种方式的问题:一个小的修改,需要这个项目完全地被重新构建和部署。很难保持一个好的模块结构,使得一些修改,本该只影响一个模块的,却影响到了多个模块----这里是说这种三层架构的设计问题吧,在这种三层架构内,修改逻辑,往往是从 view 到 model 层都要修改,感觉是一种有些“垂直化”的修改,势必容易影响原本的别的逻辑----这是我的理解,还需要继续向下研究。另外,扩展也会是整个程序一起扩展,而不是某个遇到瓶颈的部分来扩展,这样会需要更多的资源。

但其实如果服务化划分的不好的话,这个问题仍然是存在的,一个修改,上游服务会修改,下游服务也需要修改。

These frustrations have led to the microservice architectural style: building applications as suites of services. As well as the fact that services are independently deployable and scalable, each service also provides a firm module boundary, even allowing for different services to be written in different programming languages. They can also be managed by different teams .

那么微服务的架构风格是:每一个 service 可以独立部署、可扩展,提供一个稳定的模块边界线----这才是关键性的问题,即服务的划分,职责的划分问题,需要划分清楚每个服务的职责,这样哪怕用不同的语言、不同的团队来实现,不会影响业务。(关于这一点,个人思考:但是,从一个企业的角度出发,是要考虑管理和维护的成本的。)

We do not claim that the microservice style is novel or innovative, its roots go back at least to the design principles of Unix. But we do think that not enough people consider a microservice architecture and that many software developments would be better off if they used it.

我们并不是宣称微服务是一种新奇的东西或者说是一种创新,对它的寻根可以追溯到 Unix 的设计理念。这里其实应该是指 《Unix 编程艺术》里面提到的那些理念。

Characteristics of a Microservice Architecture 微服务架构的特征

We cannot say there is a formal definition of the microservices architectural style, but we can attempt to describe what we see as common characteristics for architectures that fit the label. As with any definition that outlines common characteristics, not all microservice architectures have all the characteristics, but we do expect that most microservice architectures exhibit most characteristics. While we authors have been active members of this rather loose community, our intention is to attempt a description of what we see in our own work and in similar efforts by teams we know of. In particular we are not laying down some definition to conform to.

微服务的正式的定义其实没有定论,我们只是尝试去描述一些公共的特征,也不是说所有的微服务都需要遵循所有的这些特征,但是我们希望大多数的微服务架构能够展示这些特征。

Componentization via Services 通过服务模块化

For as long as we’ve been involved in the software industry, there’s been a desire to build systems by plugging together components, much in the way we see things are made in the physical world. During the last couple of decades we’ve seen considerable progress with large compendiums of common libraries that are part of most language platforms.

只要我们还在从事软件工业,就总会有一种愿望去通过插拔组件来构建系统,就像我们在现实世界中看到的一样。

When talking about components we run into the difficult definition of what makes a component. Our definition is that a component is a unit of software that is independently replaceable and upgradeable.

提到组件,最大的困难就是什么构成了组件。而我们的定义是组件就是一个单元的软件,可以无依赖地被更替和更新。

Microservice architectures will use libraries, but their primary way of componentizing their own software is by breaking down into services. We define libraries as components that are linked into a program and called using in-memory function calls, while services are out-of-process components who communicate with a mechanism such as a web service request, or remote procedure call. (This is a different concept to that of a service object in many OO programs [3].)

这三段讲了一下 component 和 service 的区别,或者说 Componentization 的演进之路。

微服务架构会使用库,但是他们组件化他们的软件的主要的方法是把其拆解成服务。我们将库定义为链接到程序中并使用内存中的函数调用调用的组件,而服务是与诸如 Web 服务请求或远程过程调用之类的机制通信的进程外组件。

One main reason for using services as components (rather than libraries) is that services are independently deployable. If you have an application [4] that consists of a multiple libraries in a single process, a change to any single component results in having to redeploy the entire application. But if that application is decomposed into multiple services, you can expect many single service changes to only require that service to be redeployed. That’s not an absolute, some changes will change service interfaces resulting in some coordination, but the aim of a good microservice architecture is to minimize these through cohesive service boundaries and evolution mechanisms in the service contracts.

独立部署(independently deployable)是使用 service 来作为 components 的一个主要原因(这里是跟把一个 library 作为 component 来对比的)。其实这里的核心是如何设计包含很好的 cohesive service boundaries 凝聚的服务边界和 evolution mechanisms 进化机制的 service contracts 服务合约。

Another consequence of using services as components is a more explicit component interface. Most languages do not have a good mechanism for defining an explicit Published Interface. Often it’s only documentation and discipline that prevents clients breaking a component’s encapsulation, leading to overly-tight coupling between components. Services make it easier to avoid this by using explicit remote call mechanisms.

另外一个把 services 来当做 components 的结果是会有更加明确的组件接口。

Using services like this does have downsides. Remote calls are more expensive than in-process calls, and thus remote APIs need to be coarser-grained, which is often more awkward to use. If you need to change the allocation of responsibilities between components, such movements of behavior are harder to do when you’re crossing process boundaries.

这么使用 services 也有负面影响。远程调用要把进程间调用成本更高,所以远程的 APIs 需要粗粒度一些,这可能是说要具备更高的兼容性。

At a first approximation, we can observe that services map to runtime processes, but that is only a first approximation. A service may consist of multiple processes that will always be developed and deployed together, such as an application process and a database that’s only used by that service.

service 和 runtime processes (运行时进程)的关系。
At a first approximation—在最初的一种近似下,service 被映射成一个运行时进程,但是这也只能是一种近似。

Organized around Business Capabilities 围绕业务能力来组织

When looking to split a large application into parts, often management focuses on the technology layer, leading to UI teams, server-side logic teams, and database teams. When teams are separated along these lines, even simple changes can lead to a cross-team project taking time and budgetary approval. A smart team will optimise around this and plump for the lesser of two evils - just force the logic into whichever application they have access to. Logic everywhere in other words. This is an example of Conway’s Law[5] in action.

当把一个大型的应用分割成部分,通常管理会聚焦于技术层次,然后就会有 UI 团队,server-side 逻辑团队,和数据库团队。当团队如果是按照这样的规则来拆分,那么一个很小的修改就会导致跨团队的项目花费时间和预算。一个智能的团队应该是围绕 this 来优化(这里的 this 应该就是指标题中的 Business),从两者之中 plump for (选择)其轻。只需将逻辑强制到他们有权访问的任何应用程序中即可,换句话说,逻辑无处不在。(后面这句话,还需要再理解理解)

The microservice approach to division is different, splitting up into services organized around business capability. Such services take a broad-stack implementation of software for that business area, including user-interface, persistant storage, and any external collaborations. Consequently the teams are cross-functional, including the full range of skills required for the development: user-experience, database, and project management.

微服务的拆分方法不同,但最终都应该是围绕着业务能力来组织。服务采用了广泛的实现,包括用户界面,一致性存储,和外部协作。因此,团队是跨职能的,包括开发所需的全部技能:用户体验、数据库和项目管理。

One company organised in this way is www.comparethemarket.com. Cross functional teams are responsible for building and operating each product and each product is split out into a number of individual services communicating via a message bus.

Large monolithic applications can always be modularized around business capabilities too, although that’s not the common case. Certainly we would urge a large team building a monolithic application to divide itself along business lines. The main issue we have seen here, is that they tend to be organised around too many contexts. If the monolith spans many of these modular boundaries it can be difficult for individual members of a team to fit them into their short-term memory. Additionally we see that the modular lines require a great deal of discipline to enforce. The necessarily more explicit separation required by service components makes it easier to keep the team boundaries clear.

这里讲了两个事情,但中心思想是一个,围绕着业务来组织,但是实现却截然相反。

这个概念和我15年写的一篇博客有相同的观点,技术是围绕着业务来组建的,而团队是围绕着业务来组建的。

这两个概念应该怎么理解呢?

团队围绕着业务来组织,那么就不应该分成那么多个部门,那么多层级,10年在一家支付公司工作的时候,一个新业务的实现,从上到下,要有副总裁、总监、研发部门和业务部门的经理一起参与;横向呢,要有好几个部门的人打交道,那个时候的工作效率极其低下,各个部门不能说各怀心思吧,但总是会维护各自的利益,而一个业务还要副总裁去批示,这个流程,几乎赶上政府部门了。

后来去到互联网公司,这个公司里面,分成了五十几条产品线,没有那么多的自上而下的职级划分,明显效率就高了很多----不过这家公司,现在也已经走上了层层职级划分的老路。

收藏过一篇腾讯的吴宵光的文章,讲了腾讯事业部的建立过程,其思路也是这样,围绕着业务来组织。

而对于程序来说,围绕着业务来组织,则正好相反,是讲每个 service 就围绕着一个业务来组织,清清楚楚、明明白白,互不影响,互不扯皮。

How big is a microservice?
Although “microservice” has become a popular name for this architectural style, its name does lead to an unfortunate focus on the size of service, and arguments about what constitutes “micro”. In our conversations with microservice practitioners, we see a range of sizes of services. The largest sizes reported follow Amazon’s notion of the Two Pizza Team (i.e. the whole team can be fed by two pizzas), meaning no more than a dozen people. On the smaller size scale we’ve seen setups where a team of half-a-dozen would support half-a-dozen services.

This leads to the question of whether there are sufficiently large differences within this size range that the service-per-dozen-people and service-per-person sizes shouldn’t be lumped under one microservices label. At the moment we think it’s better to group them together, but it’s certainly possible that we’ll change our mind as we explore this style further.

这里涉及到了一个题外话,微服务应该有多大?为什么叫微服务呢?
这里是按照参与服务开发的任务来介绍了一下大小,最大没有超过12个人,小的6个人。

Products not Projects 产品而不是项目

Most application development efforts that we see use a project model: where the aim is to deliver some piece of software which is then considered to be completed. On completion the software is handed over to a maintenance organization and the project team that built it is disbanded.

以前的开发是把软件交付出去,而开发团队随着交付后而解散。

Microservice proponents tend to avoid this model, preferring instead the notion that a team should own a product over its full lifetime. A common inspiration for this is Amazon’s notion of “you build, you run it” where a development team takes full responsibility for the software in production. This brings developers into day-to-day contact with how their software behaves in production and increases contact with their users, as they have to take on at least some of the support burden.

而微服务的支持者意图是避免这种方式,更乐意采用一个团队应该一直固化在这个产品周围,一直对这个产品来负责。

The product mentality, ties in with the linkage to business capabilities. Rather than looking at the software as a set of functionality to be completed, there is an on-going relationship where the question is how can software assist its users to enhance the business capability.

There’s no reason why this same approach can’t be taken with monolithic applications, but the smaller granularity of services can make it easier to create the personal relationships between service developers and their users.

这段讲的挺有意思,以产品的概念而不是项目去重新设计开发、交付、维护,不是说交付给别人,而是谁开发,谁维护。

而这一点对于 monolithic 单体那种程序是不现实的,因为微服务的粒度要小,更容易来创建这种开发者和用户之间的关系。因为团队很小,而且是围绕着产品的业务能力,可以更快去贴近用户的需求。

Smart endpoints and dumb pipes 智能的终端和哑管道

When building communication structures between different processes, we’ve seen many products and approaches that stress putting significant smarts into the communication mechanism itself. A good example of this is the Enterprise Service Bus (ESB), where ESB products often include sophisticated facilities for message routing, choreography, transformation, and applying business rules.

这里提到了 ESB,我并不太了解 ESB,但是参考维基和这里的概念,ESB 是一种通讯机制(遵循了 SOA 的设计理念),融入了很多重要的,意义重大的智能(smarts),例如消息路由,编排,转换,和应用业务规则----而微服务的设计理念则不是这样,这样就说明了微服务和 SOA 还有有些区别的。

(2021-10-03,通过这段时间的阅读知道了 ESB 在 SOA 中是系统总线,所有的服务都要和它打交道,它来负责服务的路由)

The microservice community favours an alternative approach: smart endpoints and dumb pipes. Applications built from microservices aim to be as decoupled and as cohesive as possible - they own their own domain logic and act more as filters in the classical Unix sense - receiving a request, applying logic as appropriate and producing a response. These are choreographed using simple RESTish protocols rather than complex protocols such as WS-Choreography or BPEL or orchestration by a central tool.

微服务采用了一种替代方案:智能的端点(endpoints)和哑管道,这里的管道即是指上面的 ESB 所承载的作用,是通讯设施,而哑的意思是不去理会业务是什么。业务是内聚的,而同时机制是业务无知的,使用了 RESTish 的协议,而不是 WS-Choreography 或者 BPEL (BPEL 被认为是服务化过程)或者被中心工具来编排。

2021-10-04
对于上面这段,以前没有完全理解,现在感觉明白一些了,微服务这里,没有总线的概念,不需要 ESB 来管理路由,每个服务自己来管理(smarts 的意义),而通讯则是使用遵循 RESTful 的 HTTP 这样的协议,而不再是复杂的诸如 WS-Choreography(这里是指 WebService 编排,使用 XML 来实现的那些 WebService 协议)或者 BPEL 或者其他管理工具。

这里的 WS-Choreography 是不是就是上文所说的那种较重的通信机制呢?这样的通信协议里面包含了很多业务逻辑。

The two protocols used most commonly are HTTP request-response with resource API’s and lightweight messaging[8]. The best expression of the first is Microservice teams use the principles and protocols that the world wide web (and to a large extent, Unix) is built on. Often used resources can be cached with very little effort on the part of developers or operations folk.

两个常用的通讯协议是 RESTful 化的 HTTP 和轻量级消息传递(这里举的例子是 protobuf,还有下面的介绍)。

The second approach in common use is messaging over a lightweight message bus. The infrastructure chosen is typically dumb (dumb as in acts as a message router only) - simple implementations such as RabbitMQ or ZeroMQ don’t do much more than provide a reliable asynchronous fabric - the smarts still live in the end points that are producing and consuming messages; in the services.

第二种方法还有包括 RabbitMQ 或者 ZeroMQ,这些消息仅仅是提供一个可靠的异步通信的支持,而 smarts 还是存在于终端中,存在于服务中。

In a monolith, the components are executing in-process and communication between them is via either method invocation or function call. The biggest issue in changing a monolith into microservices lies in changing the communication pattern. A naive conversion from in-memory method calls to RPC leads to chatty communications which don’t perform well. Instead you need to replace the fine-grained communication with a coarser -grained approach.

传输消息的装置,应该做到对业务无知,有一个单词是讲无知论的,我之前一直不理解这个单词的意思,现在越来越明白----agnostic。

Microservices and SOA
When we’ve talked about microservices a common question is whether this is just Service Oriented Architecture (SOA) that we saw a decade ago. There is merit to this point, because the microservice style is very similar to what some advocates of SOA have been in favor of. The problem, however, is that SOA means too many different things, and that most of the time that we come across something called “SOA” it’s significantly different to the style we’re describing here, usually due to a focus on ESBs used to integrate monolithic applications.

In particular we have seen so many botched implementations of service orientation - from the tendency to hide complexity away in ESB’s [6], to failed multi-year initiatives that cost millions and deliver no value, to centralised governance models that actively inhibit change, that it is sometimes difficult to see past these problems.

Certainly, many of the techniques in use in the microservice community have grown from the experiences of developers integrating services in large organisations. The Tolerant Reader pattern is an example of this. Efforts to use the web have contributed, using simple protocols is another approach derived from these experiences - a reaction away from central standards that have reached a complexity that is, frankly, breathtaking. (Any time you need an ontology to manage your ontologies you know you are in deep trouble.)

This common manifestation of SOA has led some microservice advocates to reject the SOA label entirely, although others consider microservices to be one form of SOA [7], perhaps service orientation done right. Either way, the fact that SOA means such different things means it’s valuable to have a term that more crisply defines this architectural style.

这里讲了微服务和 SOA 的一些关系,还没有明确的一个定论。有人认为微服务是一种 SOA,维基上也是这么说的;另外一种声音是说微服务不同于 SOA。如果从上面的 ESB 这块来看,微服务和 SOA 是不同的。

Decentralized Governance 去中心化的管理

One of the consequences of centralised governance is the tendency to standardise on single technology platforms. Experience shows that this approach is constricting - not every problem is a nail and not every solution a hammer. We prefer using the right tool for the job and while monolithic applications can take advantage of different languages to a certain extent, it isn’t that common.

中心化的管理的一个后果就是,存在一个趋势,使用单一技术平台来标准化的趋势。经验显示,这可能并不合适。我们更倾向于说,针对不同的需求有可能需要采用不同的语言,是存在这个可能性的,但可能不常见。

这里是说,根据业务的特点,来选择符合这个特点的技术。
例如用 NodeJS + MongoDB 来做电商接入,因为 Node 的高并发能力和 MongoDB 的 NoSQL 能力符合电商的很多特点,复杂而多变的属性----这个地方我曾经尝试使用范式,来用 MySQL 完成设计,发现感觉总是不太适应;而使用 C/C++ 来完成接近实时的功能,例如需要高实时性、高并发的期货交易系统。

Splitting the monolith’s components out into services we have a choice when building each of them. You want to use Node.js to standup a simple reports page? Go for it. C++ for a particularly gnarly near-real-time component? Fine. You want to swap in a different flavour of database that better suits the read behaviour of one component? We have the technology to rebuild him.

这个 gnarly 怎么理解呢?

Of course, just because you can do something, doesn’t mean you should - but partitioning your system in this way means you have the option.

Teams building microservices prefer a different approach to standards too. Rather than use a set of defined standards written down somewhere on paper they prefer the idea of producing useful tools that other developers can use to solve similar problems to the ones they are facing. These tools are usually harvested from implementations and shared with a wider group, sometimes, but not exclusively using an internal open source model. Now that git and github have become the de facto version control system of choice, open source practices are becoming more and more common in-house .

Netflix is a good example of an organisation that follows this philosophy. Sharing useful and, above all, battle-tested code as libraries encourages other developers to solve similar problems in similar ways yet leaves the door open to picking a different approach if required. Shared libraries tend to be focused on common problems of data storage, inter-process communication and as we discuss further below, infrastructure automation.

For the microservice community, overheads are particularly unattractive. That isn’t to say that the community doesn’t value service contracts. Quite the opposite, since there tend to be many more of them. It’s just that they are looking at different ways of managing those contracts. Patterns like Tolerant Reader and Consumer-Driven Contracts are often applied to microservices. These aid service contracts in evolving independently. Executing consumer driven contracts as part of your build increases confidence and provides fast feedback on whether your services are functioning. Indeed we know of a team in Australia who drive the build of new services with consumer driven contracts. They use simple tools that allow them to define the contract for a service. This becomes part of the automated build before code for the new service is even written. The service is then built out only to the point where it satisfies the contract - an elegant approach to avoid the ‘YAGNI’[9] dilemma when building new software. These techniques and the tooling growing up around them, limit the need for central contract management by decreasing the temporal coupling between services.

Many languages, many options
The growth of JVM as a platform is just the latest example of mixing languages within a common platform. It’s been common practice to shell-out to a higher level language to take advantage of higher level abstractions for decades. As is dropping down to the metal and writing performance sensitive code in a lower level one. However, many monoliths don’t need this level of performance optimisation nor are DSL’s and higher level abstractions that common (to our dismay). Instead monoliths are usually single language and the tendency is to limit the number of technologies in use [10].

Perhaps the apogee of decentralised governance is the build it / run it ethos popularised by Amazon. Teams are responsible for all aspects of the software they build including operating the software 24/7. Devolution of this level of responsibility is definitely not the norm but we do see more and more companies pushing responsibility to the development teams. Netflix is another organisation that has adopted this ethos[11]. Being woken up at 3am every night by your pager is certainly a powerful incentive to focus on quality when writing your code. These ideas are about as far away from the traditional centralized governance model as it is possible to be.

上面这段是说去中心化管理的最高境界就是 7*24 小时(这里为什么是 24/7)地维护它,要放权给开发团队,如果不想每天早上 3 点被叫醒,就需要投入更多的努力去保证你的代码质量,而这些则是和传统的中心化管理方式是相去甚远的。

Decentralized Data Management 去中心化的数据管理

Decentralization of data management presents in a number of different ways. At the most abstract level, it means that the conceptual model of the world will differ between systems. This is a common issue when integrating across a large enterprise, the sales view of a customer will differ from the support view. Some things that are called customers in the sales view may not appear at all in the support view. Those that do may have different attributes and (worse) common attributes with subtly different semantics.

大同世界里面,大家因为立场不同,看待一个问题的角度都是不同的,那么对于不同的系统来说,看待和处理数据的方式也是不同的。

This issue is common between applications, but can also occur within applications, particular when that application is divided into separate components. A useful way of thinking about this is the Domain-Driven Design notion of Bounded Context. DDD divides a complex domain up into multiple bounded contexts and maps out the relationships between them. This process is useful for both monolithic and microservice architectures, but there is a natural correlation between service and context boundaries that helps clarify, and as we describe in the section on business capabilities, reinforce the separations.

DDD Domain-Driven Design notion of Bounded Context 的设计概念。

As well as decentralizing decisions about conceptual models, microservices also decentralize data storage decisions. While monolithic applications prefer a single logical database for persistant data, enterprises often prefer a single database across a range of applications - many of these decisions driven through vendor’s commercial models around licensing. Microservices prefer letting each service manage its own database, either different instances of the same database technology, or entirely different database systems - an approach called Polyglot Persistence. You can use polyglot persistence in a monolith, but it appears more frequently with microservices.

Decentralizing responsibility for data across microservices has implications for managing updates. The common approach to dealing with updates has been to use transactions to guarantee consistency when updating multiple resources. This approach is often used within monoliths.

Using transactions like this helps with consistency, but imposes significant temporal coupling, which is problematic across multiple services. Distributed transactions are notoriously difficult to implement and as a consequence microservice architectures emphasize transactionless coordination between services, with explicit recognition that consistency may only be eventual consistency and problems are dealt with by compensating operations.

Choosing to manage inconsistencies in this way is a new challenge for many development teams, but it is one that often matches business practice. Often businesses handle a degree of inconsistency in order to respond quickly to demand, while having some kind of reversal process to deal with mistakes. The trade-off is worth it as long as the cost of fixing mistakes is less than the cost of lost business under greater consistency.

Infrastructure Automation 基础设施的自动化

Infrastructure automation techniques have evolved enormously over the last few years - the evolution of the cloud and AWS in particular has reduced the operational complexity of building, deploying and operating microservices.

Many of the products or systems being build with microservices are being built by teams with extensive experience of Continuous Delivery and it’s precursor, Continuous Integration. Teams building software this way make extensive use of infrastructure automation techniques. This is illustrated in the build pipeline shown below.

Since this isn’t an article on Continuous Delivery we will call attention to just a couple of key features here. We want as much confidence as possible that our software is working, so we run lots of automated tests. Promotion of working software ‘up’ the pipeline means we automate deployment to each new environment.

A monolithic application will be built, tested and pushed through these environments quite happlily. It turns out that once you have invested in automating the path to production for a monolith, then deploying more applications doesn’t seem so scary any more. Remember, one of the aims of CD is to make deployment boring, so whether its one or three applications, as long as its still boring it doesn’t matter[12].

Another area where we see teams using extensive infrastructure automation is when managing microservices in production. In contrast to our assertion above that as long as deployment is boring there isn’t that much difference between monoliths and microservices, the operational landscape for each can be strikingly different.

这里讲的似乎是要使用 CI/CD 这样的基础设施,这块其实无关于是微服务还是 monolithic 的服务。

Design for failure 为失败做好设计

A consequence of using services as components, is that applications need to be designed so that they can tolerate the failure of services. Any service call could fail due to unavailability of the supplier, the client has to respond to this as gracefully as possible. This is a disadvantage compared to a monolithic design as it introduces additional complexity to handle it. The consequence is that microservice teams constantly reflect on how service failures affect the user experience. Netflix’s Simian Army induces failures of services and even datacenters during the working day to test both the application’s resilience and monitoring.

This kind of automated testing in production would be enough to give most operation groups the kind of shivers usually preceding a week off work. This isn’t to say that monolithic architectural styles aren’t capable of sophisticated monitoring setups - it’s just less common in our experience.

Since services can fail at any time, it’s important to be able to detect the failures quickly and, if possible, automatically restore service. Microservice applications put a lot of emphasis on real-time monitoring of the application, checking both architectural elements (how many requests per second is the database getting) and business relevant metrics (such as how many orders per minute are received). Semantic monitoring can provide an early warning system of something going wrong that triggers development teams to follow up and investigate.

This is particularly important to a microservices architecture because the microservice preference towards choreography and event collaboration leads to emergent behavior. While many pundits praise the value of serendipitous emergence, the truth is that emergent behavior can sometimes be a bad thing. Monitoring is vital to spot bad emergent behavior quickly so it can be fixed.

Synchronous calls considered harmful 同步调用被认为是有害的

Any time you have a number of synchronous calls between services you will encounter the multiplicative effect of downtime. Simply, this is when the downtime of your system becomes the product of the downtimes of the individual components. You face a choice, making your calls asynchronous or managing the downtime. At www.guardian.co.uk they have implemented a simple rule on the new platform - one synchronous call per user request while at Netflix, their platform API redesign has built asynchronicity into the API fabric.

任何时候,如果在服务间有很多的同步调用,你将会遭遇很多宕机时间(downtime)的影响,一个独立组件的宕机就会造成整个系统的宕机。

Monoliths can be built to be as transparent as a microservice - in fact, they should be. The difference is that you absolutely need to know when services running in different processes are disconnected. With libraries within the same process this kind of transparency is less likely to be useful.

Microservice teams would expect to see sophisticated monitoring and logging setups for each individual service such as dashboards showing up/down status and a variety of operational and business relevant metrics. Details on circuit breaker status, current throughput and latency are other examples we often encounter in the wild.

Evolutionary Design 进化的设计

Microservice practitioners, usually have come from an evolutionary design background and see service decomposition as a further tool to enable application developers to control changes in their application without slowing down change. Change control doesn’t necessarily mean change reduction - with the right attitudes and tools you can make frequent, fast, and well-controlled changes to software.

这段很值得回味,通常微服务的实践者,都是来自于一个进化的设计背景(理解和支持进化设计的概念的背景),会把服务的分解看作一个更进一步的工具来让程序开发者去控制变化而不用减慢变化的速度。

Whenever you try to break a software system into components, you’re faced with the decision of how to divide up the pieces - what are the principles on which we decide to slice up our application? The key property of a component is the notion of independent replacement and upgradeability[13] - which implies we look for points where we can imagine rewriting a component without affecting its collaborators. Indeed many microservice groups take this further by explicitly expecting many services to be scrapped rather than evolved in the longer term.

无论什么时候,你打算去把一个软件系统拆分成组件,你都要面对一个决定,如何去分解–我们决定去分解我们的应用的原则是什么呢?关键点是独立的可替换和更新。

The Guardian website is a good example of an application that was designed and built as a monolith, but has been evolving in a microservice direction. The monolith still is the core of the website, but they prefer to add new features by building microservices that use the monolith’s API. This approach is particularly handy for features that are inherently temporary, such as specialized pages to handle a sporting event. Such a part of the website can quickly be put together using rapid development languages, and removed once the event is over. We’ve seen similar approaches at a financial institution where new services are added for a market opportunity and discarded after a few months or even weeks.

This emphasis on replaceability is a special case of a more general principle of modular design, which is to drive modularity through the pattern of change [14]. You want to keep things that change at the same time in the same module. Parts of a system that change rarely should be in different services to those that are currently undergoing lots of churn. If you find yourself repeatedly changing two services together, that’s a sign that they should be merged.

Putting components into services adds an opportunity for more granular release planning. With a monolith any changes require a full build and deployment of the entire application. With microservices, however, you only need to redeploy the service(s) you modified. This can simplify and speed up the release process. The downside is that you have to worry about changes to one service breaking its consumers. The traditional integration approach is to try to deal with this problem using versioning, but the preference in the microservice world is to only use versioning as a last resort. We can avoid a lot of versioning by designing services to be as tolerant as possible to changes in their suppliers.

Are Microservices the Future? 微服务是我们的未来吗

Our main aim in writing this article is to explain the major ideas and principles of microservices. By taking the time to do this we clearly think that the microservices architectural style is an important idea - one worth serious consideration for enterprise applications. We have recently built several systems using the style and know of others who have used and favor this approach.

Those we know about who are in some way pioneering the architectural style include Amazon, Netflix, The Guardian, the UK Government Digital Service, realestate.com.au, Forward and comparethemarket.com. The conference circuit in 2013 was full of examples of companies that are moving to something that would class as microservices - including Travis CI. In addition there are plenty of organizations that have long been doing what we would class as microservices, but without ever using the name. (Often this is labelled as SOA - although, as we’ve said, SOA comes in many contradictory forms. [15])

Despite these positive experiences, however, we aren’t arguing that we are certain that microservices are the future direction for software architectures. While our experiences so far are positive compared to monolithic applications, we’re conscious of the fact that not enough time has passed for us to make a full judgement.

Our colleague Sam Newman spent most of 2014 working on a book that captures our experiences with building microservices. This should be your next step if you want a deeper dive into the topic.

Often the true consequences of your architectural decisions are only evident several years after you made them. We have seen projects where a good team, with a strong desire for modularity, has built a monolithic architecture that has decayed over the years. Many people believe that such decay is less likely with microservices, since the service boundaries are explicit and hard to patch around. Yet until we see enough systems with enough age, we can’t truly assess how microservice architectures mature.

There are certainly reasons why one might expect microservices to mature poorly. In any effort at componentization, success depends on how well the software fits into components. It’s hard to figure out exactly where the component boundaries should lie. Evolutionary design recognizes the difficulties of getting boundaries right and thus the importance of it being easy to refactor them. But when your components are services with remote communications, then refactoring is much harder than with in-process libraries. Moving code is difficult across service boundaries, any interface changes need to be coordinated between participants, layers of backwards compatibility need to be added, and testing is made more complicated.

Another issue is If the components do not compose cleanly, then all you are doing is shifting complexity from inside a component to the connections between components. Not just does this just move complexity around, it moves it to a place that’s less explicit and harder to control. It’s easy to think things are better when you are looking at the inside of a small, simple component, while missing messy connections between services.

Finally, there is the factor of team skill. New techniques tend to be adopted by more skillful teams. But a technique that is more effective for a more skillful team isn’t necessarily going to work for less skillful teams. We’ve seen plenty of cases of less skillful teams building messy monolithic architectures, but it takes time to see what happens when this kind of mess occurs with microservices. A poor team will always create a poor system - it’s very hard to tell if microservices reduce the mess in this case or make it worse.

One reasonable argument we’ve heard is that you shouldn’t start with a microservices architecture. Instead begin with a monolith, keep it modular, and split it into microservices once the monolith becomes a problem. (Although this advice isn’t ideal, since a good in-process interface is usually not a good service interface.)

So we write this with cautious optimism. So far, we’ve seen enough about the microservice style to feel that it can be a worthwhile road to tread. We can’t say for sure where we’ll end up, but one of the challenges of software development is that you can only make decisions based on the imperfect information that you currently have to hand.

总结

这篇文章内容太多,只能是一天一天聚沙成塔、集腋成裘地去理解,没打算完整地去翻译,翻译得过细,再读时,感觉会忽略原文,还是记录一些从阅读原文中获得的一些理解更为合理。

参考

https://martinfowler.com/articles/microservices.html
https://www.w3.org/TR/ws-chor-model/ 介绍了 WS-Choreography
https://en.wikipedia.org/wiki/Business_Process_Execution_Language 介绍了 BPEL
https://www.quora.com/What-is-metadata-in-SOA-patterns

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐