【架构设计】单体软件向微服务化演变
- 手机
- 2025-08-06 03:33:01

单体软件
假设单体软件的各模块如下,其中服务包含许多功能模块,如用户管理模块、商品模块、订单模块、仓库模块;
#mermaid-svg-MzWKwMCwfo3PWMGH {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-MzWKwMCwfo3PWMGH .error-icon{fill:#552222;}#mermaid-svg-MzWKwMCwfo3PWMGH .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-MzWKwMCwfo3PWMGH .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-MzWKwMCwfo3PWMGH .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-MzWKwMCwfo3PWMGH .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-MzWKwMCwfo3PWMGH .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-MzWKwMCwfo3PWMGH .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-MzWKwMCwfo3PWMGH .marker{fill:#333333;stroke:#333333;}#mermaid-svg-MzWKwMCwfo3PWMGH .marker.cross{stroke:#333333;}#mermaid-svg-MzWKwMCwfo3PWMGH svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-MzWKwMCwfo3PWMGH .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-MzWKwMCwfo3PWMGH .cluster-label text{fill:#333;}#mermaid-svg-MzWKwMCwfo3PWMGH .cluster-label span{color:#333;}#mermaid-svg-MzWKwMCwfo3PWMGH .label text,#mermaid-svg-MzWKwMCwfo3PWMGH span{fill:#333;color:#333;}#mermaid-svg-MzWKwMCwfo3PWMGH .node rect,#mermaid-svg-MzWKwMCwfo3PWMGH .node circle,#mermaid-svg-MzWKwMCwfo3PWMGH .node ellipse,#mermaid-svg-MzWKwMCwfo3PWMGH .node polygon,#mermaid-svg-MzWKwMCwfo3PWMGH .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-MzWKwMCwfo3PWMGH .node .label{text-align:center;}#mermaid-svg-MzWKwMCwfo3PWMGH .node.clickable{cursor:pointer;}#mermaid-svg-MzWKwMCwfo3PWMGH .arrowheadPath{fill:#333333;}#mermaid-svg-MzWKwMCwfo3PWMGH .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-MzWKwMCwfo3PWMGH .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-MzWKwMCwfo3PWMGH .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-MzWKwMCwfo3PWMGH .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-MzWKwMCwfo3PWMGH .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-MzWKwMCwfo3PWMGH .cluster text{fill:#333;}#mermaid-svg-MzWKwMCwfo3PWMGH .cluster span{color:#333;}#mermaid-svg-MzWKwMCwfo3PWMGH div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-MzWKwMCwfo3PWMGH :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 请求 转发 客户端 代理层 服务 数据库 服务化服务化是指对单体服务进行拆分,将一个服务软件拆分为多个相互关联的服务,他们之间相互协作,能正常完成原单体服务的所有业务。
服务化后,有如下优点:
服务化后的各服务能独立提供服务,某个服务损毁后,不影响其他业务的使用,软件整体的可用性提高了。服务化后的软件代码依据各子服务管理,相对来说代码量少了,开发复杂性会成指数型减弱服务化后的软件,各子服务独立维护,因此各服务更新维护也比较简单服务化后的软件,由于各服务在不同节点独立运行,计算与网络资源成倍数形式增长。理论上来说,这使得软件的服务能力成倍提高以下是对单体服务中的服务进行服务拆分的示意图。
#mermaid-svg-dtY8ccRcgFpfKxl8 {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-dtY8ccRcgFpfKxl8 .error-icon{fill:#552222;}#mermaid-svg-dtY8ccRcgFpfKxl8 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-dtY8ccRcgFpfKxl8 .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-dtY8ccRcgFpfKxl8 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-dtY8ccRcgFpfKxl8 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-dtY8ccRcgFpfKxl8 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-dtY8ccRcgFpfKxl8 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-dtY8ccRcgFpfKxl8 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-dtY8ccRcgFpfKxl8 .marker.cross{stroke:#333333;}#mermaid-svg-dtY8ccRcgFpfKxl8 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-dtY8ccRcgFpfKxl8 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-dtY8ccRcgFpfKxl8 .cluster-label text{fill:#333;}#mermaid-svg-dtY8ccRcgFpfKxl8 .cluster-label span{color:#333;}#mermaid-svg-dtY8ccRcgFpfKxl8 .label text,#mermaid-svg-dtY8ccRcgFpfKxl8 span{fill:#333;color:#333;}#mermaid-svg-dtY8ccRcgFpfKxl8 .node rect,#mermaid-svg-dtY8ccRcgFpfKxl8 .node circle,#mermaid-svg-dtY8ccRcgFpfKxl8 .node ellipse,#mermaid-svg-dtY8ccRcgFpfKxl8 .node polygon,#mermaid-svg-dtY8ccRcgFpfKxl8 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-dtY8ccRcgFpfKxl8 .node .label{text-align:center;}#mermaid-svg-dtY8ccRcgFpfKxl8 .node.clickable{cursor:pointer;}#mermaid-svg-dtY8ccRcgFpfKxl8 .arrowheadPath{fill:#333333;}#mermaid-svg-dtY8ccRcgFpfKxl8 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-dtY8ccRcgFpfKxl8 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-dtY8ccRcgFpfKxl8 .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-dtY8ccRcgFpfKxl8 .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-dtY8ccRcgFpfKxl8 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-dtY8ccRcgFpfKxl8 .cluster text{fill:#333;}#mermaid-svg-dtY8ccRcgFpfKxl8 .cluster span{color:#333;}#mermaid-svg-dtY8ccRcgFpfKxl8 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-dtY8ccRcgFpfKxl8 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 微服务化 商品服务 网关 用户服务 订单服务 仓库服务 客户端 数据库 部分服务分集群化 服务集群化软件服务化后,使得软件的可用性、服务能力大大提高,然而其更多的价值在于使得软件的开发、维护更为简单。
在软件服务化后,随着软件使用的需求持续增长,依然会面临服务能力不足的问题。为了解决该问题,大家都会想到服务扩容,那么如何扩容呢?假定软件被拆分了10多个子服务,甚至更多。将所有服务都扩容一套嘛?
答案是否定的,扩容不是说说那么简单。众多子服务同时扩容,首先要面临成本问题。
一般情况下,服务扩容是根据各子服务的使用情况来指定灵活的扩容方案。对软件中服务压力大,硬件资源不足的节点进行灵活扩容。
如下是商品服务服务扩容的示意图,下图表示软件将商品服务扩增到了三个节点,这个三个节点通过负载均衡与软件中的其他服务相关协同。
商品服务以多个节点共同提供服务。这个服务群形成一个集群,即商品服务集群。
#mermaid-svg-7dUsi3dHhqS3c4Tc {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-7dUsi3dHhqS3c4Tc .error-icon{fill:#552222;}#mermaid-svg-7dUsi3dHhqS3c4Tc .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-7dUsi3dHhqS3c4Tc .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-7dUsi3dHhqS3c4Tc .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-7dUsi3dHhqS3c4Tc .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-7dUsi3dHhqS3c4Tc .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-7dUsi3dHhqS3c4Tc .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-7dUsi3dHhqS3c4Tc .marker{fill:#333333;stroke:#333333;}#mermaid-svg-7dUsi3dHhqS3c4Tc .marker.cross{stroke:#333333;}#mermaid-svg-7dUsi3dHhqS3c4Tc svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-7dUsi3dHhqS3c4Tc .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-7dUsi3dHhqS3c4Tc .cluster-label text{fill:#333;}#mermaid-svg-7dUsi3dHhqS3c4Tc .cluster-label span{color:#333;}#mermaid-svg-7dUsi3dHhqS3c4Tc .label text,#mermaid-svg-7dUsi3dHhqS3c4Tc span{fill:#333;color:#333;}#mermaid-svg-7dUsi3dHhqS3c4Tc .node rect,#mermaid-svg-7dUsi3dHhqS3c4Tc .node circle,#mermaid-svg-7dUsi3dHhqS3c4Tc .node ellipse,#mermaid-svg-7dUsi3dHhqS3c4Tc .node polygon,#mermaid-svg-7dUsi3dHhqS3c4Tc .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-7dUsi3dHhqS3c4Tc .node .label{text-align:center;}#mermaid-svg-7dUsi3dHhqS3c4Tc .node.clickable{cursor:pointer;}#mermaid-svg-7dUsi3dHhqS3c4Tc .arrowheadPath{fill:#333333;}#mermaid-svg-7dUsi3dHhqS3c4Tc .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-7dUsi3dHhqS3c4Tc .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-7dUsi3dHhqS3c4Tc .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-7dUsi3dHhqS3c4Tc .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-7dUsi3dHhqS3c4Tc .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-7dUsi3dHhqS3c4Tc .cluster text{fill:#333;}#mermaid-svg-7dUsi3dHhqS3c4Tc .cluster span{color:#333;}#mermaid-svg-7dUsi3dHhqS3c4Tc div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-7dUsi3dHhqS3c4Tc :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 商品服务集群 微服务化 商品服务01 商品服务02 商品服务n 商品服务 负载均衡 网关 用户服务 订单服务 仓库服务 数据库 客户端 数据库集群化随着服务的增长,对数据库的读写需求也会持续增长。其增长趋势与服务的使用趋势成正比。
当数据库服务能力遇到瓶颈时,也可以对其进行服务扩容。
因为数据层的服务扩容首先要保证扩容后所有数据服务之间的数据一致性问题 ,因此服务的集群化和数据层的集群化通常不一样。
服务集群化后,通常各个子服务之间的关系是均等的,它们可无差别的提供服务,任何一个服务损毁,都不影响整体软件的运行,除非所有集群中的所有服务都损毁。数据层的服务集群化后,通常对服务进行主从节点划分。主节点主要负责数据写入,从节点主要负责数据读取。注意,除了主从这种模式外,还有去中心式服务模式。去中心式服务模式理论上每个服务都能进行数据读写。 #mermaid-svg-FxFiukgPiMbGZLxr {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-FxFiukgPiMbGZLxr .error-icon{fill:#552222;}#mermaid-svg-FxFiukgPiMbGZLxr .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-FxFiukgPiMbGZLxr .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-FxFiukgPiMbGZLxr .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-FxFiukgPiMbGZLxr .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-FxFiukgPiMbGZLxr .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-FxFiukgPiMbGZLxr .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-FxFiukgPiMbGZLxr .marker{fill:#333333;stroke:#333333;}#mermaid-svg-FxFiukgPiMbGZLxr .marker.cross{stroke:#333333;}#mermaid-svg-FxFiukgPiMbGZLxr svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-FxFiukgPiMbGZLxr .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-FxFiukgPiMbGZLxr .cluster-label text{fill:#333;}#mermaid-svg-FxFiukgPiMbGZLxr .cluster-label span{color:#333;}#mermaid-svg-FxFiukgPiMbGZLxr .label text,#mermaid-svg-FxFiukgPiMbGZLxr span{fill:#333;color:#333;}#mermaid-svg-FxFiukgPiMbGZLxr .node rect,#mermaid-svg-FxFiukgPiMbGZLxr .node circle,#mermaid-svg-FxFiukgPiMbGZLxr .node ellipse,#mermaid-svg-FxFiukgPiMbGZLxr .node polygon,#mermaid-svg-FxFiukgPiMbGZLxr .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-FxFiukgPiMbGZLxr .node .label{text-align:center;}#mermaid-svg-FxFiukgPiMbGZLxr .node.clickable{cursor:pointer;}#mermaid-svg-FxFiukgPiMbGZLxr .arrowheadPath{fill:#333333;}#mermaid-svg-FxFiukgPiMbGZLxr .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-FxFiukgPiMbGZLxr .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-FxFiukgPiMbGZLxr .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-FxFiukgPiMbGZLxr .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-FxFiukgPiMbGZLxr .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-FxFiukgPiMbGZLxr .cluster text{fill:#333;}#mermaid-svg-FxFiukgPiMbGZLxr .cluster span{color:#333;}#mermaid-svg-FxFiukgPiMbGZLxr div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-FxFiukgPiMbGZLxr :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 数据库集群 微服务化 数 据 库 主 节 点 数据库从节点01 数据库从节点02 数据库从节点n 商品服务 网关 用户服务 订单服务 仓库服务 客户端【架构设计】单体软件向微服务化演变由讯客互联手机栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“【架构设计】单体软件向微服务化演变”