微服务之松耦合

server/2025/1/15 6:53:12/

参考:https://microservices.io/post/architecture/2023/03/28/microservice-architecture-essentials-loose-coupling.html

There’s actually two different types of coupling:

runtime coupling - influences availability
design-time coupling - influences development velocity

Runtime coupling and availability

Runtime coupling between services is the degree to which the availability of one service is affected by the availability of another service. Or to be more precise, it’s degree to which the availability of an operation implemented by one service is affected by the availability of another service
在这里插入图片描述
微服务化以后导致一个请求会同步调用多个服务,请求的时常和可用性都会降低。

Reducing runtime coupling

在这里插入图片描述
使用异步的方式可以提高请求的可用性,但是客户端变得更复杂了,必须轮询服务器获得请求的状态。
这里建议的依然是orderService调用Consumer Service,只是放到异步线程了而已。其实到底是OrderService调用ConsumerService的RPC api, 还是Consumer Service监听OrderService的事件,要看业务逻辑而定,如果ConsumerService是OrderService的强依赖(说明OrderService domain必须要理解ConsumerService domain的概念和API),那么RPC的方式是合理的,反之,异步消息的方式更合理。
DDD总是推荐异步消息,因为一个transaction只更新一个aggregate。

Design-time coupling and development velocity

The degree of design-time coupling between a pair of software elements - classes…services - is the likelihood that they need to change together for the same reason. Design-time coupling between services in a microservice architecture is especially problematic. Let’s look at why it’s a problem and how we can minimize it.
这其实就是《整洁架构》里面所说的单一指责原则,或者说共同闭包原则,如果两个模块总是因为同一个原因改变,那么他们就应该属于一个模块,或者说,在微服务架构下,一个服务内部。
If two services are loosely coupled, then a change to one service rarely requires a change to the other service. However, if two services are tightly coupled, then a change to one service often requires a change to the other service. These types of lock step changes are expensive since it typically involves breaking API changes.

For example, let’s imagine that the Order Service and Customer Service are tightly coupled. Each time a breaking changes needs to be made to the Customer Service the sequence of steps is as follows:

Change the Customer Service to add a new major version of its API. The service must implement both the old and new version of its APIs until all clients have been migrated over.

  • Migrate the Order Service to the new API version
  • Remove the old API version from the Customer Service
  • What’s even worse, is that quite often the services are owned by different teams, which requires those teams to coordinate the changes. In other words, design-time coupling between services undermines team autonomy.

Reducing design-time coupling

Minimizing design-time coupling is one of the dark matter attractive forces that resists decomposition. There are a couple of different ways to minimize design-time coupling between services.

design subdomains to be loosely coupled - loosely coupled subdomains can be packaged as different services. Loose design-time coupling is usually achieved by each subdomain having a stable API that encapsulates its implementation.

package subdomains that are tightly coupled in the same service - If two subdomains are tightly coupled, then packaging them together in the same service will avoid design-time coupling between services.

解决方法也很简单,就是遵循DDD的设计,子模块(微服务)之间应该有合理的边界,每个子模块有相对独立的业务逻辑,子模块的API应该相对稳定。遵守单一指责原则,共同闭包原则。


http://www.ppmy.cn/server/158482.html

相关文章

通信网络安全分层及关键技术解决

要实现信息化,就必须重视信息网络安全。信息网络安全绝不仅是IT行业的问题,而是一个社会问题,是一个包括多学科的系统安全工程问题,并直接关系到国家安全。因此,知名安全专家沈昌祥院士呼吁,要像重视两弹一…

【机器人】SceneGrasp 同时支持3D物体重建、6D位姿估计、抓取点估计

本文分享SceneGrasp,它来自IROS2023,同时支持物体分类、3D物体重建、6D位姿估计、抓取点估计。 它是一种快速、高效且同时处理多个任务的方法,能够使机器人更好地理解和操作其环境。 同时处理多个任务,实现任务之间共享信息&…

通过mysql的*.frm和*ibd文件恢复原有数据

背景:MySQL数据丢失,只剩下frm和ibd结尾的文件,由于先前没有备份也没开启binlog现在只能通过这两个文件来恢复数据。 如果也是MySQL数据丢失,但是有frm和ibd文件可以通过此文档来解决MySQL数据丢失的问题 安装MySQL Utilities …

用AI技术提升Flutter开发效率:ScriptEcho的力量

引言 在当今快速发展的技术时代,Flutter作为一种跨平台开发框架,正在越来越多的开发者中崭露头角。它不仅能够为开发者提供一套代码同时部署到iOS和Android平台的解决方案,还能帮助企业节省人力成本和开发时间。然而,对于新手开发…

(蓝桥杯)使用前缀和与二分查找解决子数组和问题——不太甜的糖果

题目描述 小Y走啊走啊,翻山越岭、跋山涉水,终于,小Y累了。虽然,糖果的诱惑强大,但他的两条腿已经不听使唤,只能坐在地上叹气,内心无比焦急…… 突然眼前一黑,小Y没有昏过去,但是眼前出现了一个糖人。在这无人之地,小Y没有别的办法,只得求助糖人。 善良的糖人没法拒绝…

Elasticsearch入门篇

1.介绍 Elasticsearch 1.1.什么是 Elasticsearch? Elasticsearch 是一种开源的搜索和分析引擎,专门用于处理大规模的数据,并以非常快的速度进行搜索和分析。它基于一个叫做 Apache Lucene 的库构建,但与 Lucene 相比,…

基于Java+SpringMvc+Vue技术的宠物分享平台

博主介绍:硕士研究生,专注于信息化技术领域开发与管理,会使用java、标准c/c等开发语言,以及毕业项目实战✌ 从事基于java BS架构、CS架构、c/c 编程工作近16年,拥有近12年的管理工作经验,拥有较丰富的技术架…

【2025最新计算机毕业设计】基于SpringBoot+Vue奶茶点单系统(高质量源码,提供文档,免费部署到本地)

作者简介:✌CSDN新星计划导师、Java领域优质创作者、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和学生毕业项目实战,高校老师/讲师/同行前辈交流。✌ 主要内容:🌟Java项目、Python项目、前端项目、PHP、ASP.NET、人工智能…