<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>dinstone</title>
    <description>dinstone&apos;s home page</description>
    <link>https://dinstone.github.io//</link>
    <atom:link href="https://dinstone.github.io//feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Sat, 21 Dec 2024 08:04:11 +0000</pubDate>
    <lastBuildDate>Sat, 21 Dec 2024 08:04:11 +0000</lastBuildDate>
    <generator>Jekyll v3.10.0</generator>
    
      <item>
        <title>中小企业云原生持续交付之路</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#持续交付介绍&quot; id=&quot;markdown-toc-持续交付介绍&quot;&gt;持续交付介绍&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#基础设施&quot; id=&quot;markdown-toc-基础设施&quot;&gt;基础设施&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#需求协作管理&quot; id=&quot;markdown-toc-需求协作管理&quot;&gt;需求协作管理&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#源码分支管理&quot; id=&quot;markdown-toc-源码分支管理&quot;&gt;源码分支管理&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#系统配置管理&quot; id=&quot;markdown-toc-系统配置管理&quot;&gt;系统配置管理&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#应用构建管理&quot; id=&quot;markdown-toc-应用构建管理&quot;&gt;应用构建管理&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#应用环境管理&quot; id=&quot;markdown-toc-应用环境管理&quot;&gt;应用环境管理&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#测试验证管理&quot; id=&quot;markdown-toc-测试验证管理&quot;&gt;测试验证管理&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#部署发布管理&quot; id=&quot;markdown-toc-部署发布管理&quot;&gt;部署发布管理&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#应用监控管理&quot; id=&quot;markdown-toc-应用监控管理&quot;&gt;应用监控管理&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#应用日志管理&quot; id=&quot;markdown-toc-应用日志管理&quot;&gt;应用日志管理&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;持续交付介绍&quot;&gt;持续交付介绍&lt;/h2&gt;

&lt;h2 id=&quot;基础设施&quot;&gt;基础设施&lt;/h2&gt;

&lt;h3 id=&quot;需求协作管理&quot;&gt;需求协作管理&lt;/h3&gt;

国内最具影响力的7款开源免费团队协作工具
https://sspai.com/post/81657

&lt;h3 id=&quot;源码分支管理&quot;&gt;源码分支管理&lt;/h3&gt;

Gitea：https://blog.csdn.net/huyuchengus/article/details/124519103
https://about.gitea.com/products/gitea/

&lt;h3 id=&quot;系统配置管理&quot;&gt;系统配置管理&lt;/h3&gt;

Ansible

&lt;h3 id=&quot;应用构建管理&quot;&gt;应用构建管理&lt;/h3&gt;

Jenkins

&lt;h3 id=&quot;应用环境管理&quot;&gt;应用环境管理&lt;/h3&gt;

K8S
Docker

&lt;h3 id=&quot;测试验证管理&quot;&gt;测试验证管理&lt;/h3&gt;

&lt;h3 id=&quot;部署发布管理&quot;&gt;部署发布管理&lt;/h3&gt;

Harbor
https://gitcode.com/goharbor/harbor/overview

Helm：Helm是Kubernetes的包管理器，用于部署、升级和管理K8s应用程序。通过Helm，团队可以定义、安装和升级K8s应用程序的最基本单位——charts。Charts包含了应用程序所需的所有Kubernetes资源定义，使得部署过程更加规范化和可重复。

&lt;h3 id=&quot;应用监控管理&quot;&gt;应用监控管理&lt;/h3&gt;

Prometheus
Grafana

&lt;h3 id=&quot;应用日志管理&quot;&gt;应用日志管理&lt;/h3&gt;

ELK（Elasticsearch、Logstash、Kibana）
</description>
        <pubDate>Sun, 14 Apr 2024 00:00:00 +0000</pubDate>
        <link>https://dinstone.github.io//2024/04/14/CD-2CycleModel/</link>
        <guid isPermaLink="true">https://dinstone.github.io//2024/04/14/CD-2CycleModel/</guid>
        
        <category>方法论</category>
        
        <category>云原生</category>
        
        <category>持续交付</category>
        
        
        <category>方法论</category>
        
      </item>
    
      <item>
        <title>面向领域的六边形架构</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#doha&quot; id=&quot;markdown-toc-doha&quot;&gt;DOHA&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#逻辑架构&quot; id=&quot;markdown-toc-逻辑架构&quot;&gt;逻辑架构&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#实现架构&quot; id=&quot;markdown-toc-实现架构&quot;&gt;实现架构&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#快速开始&quot; id=&quot;markdown-toc-快速开始&quot;&gt;快速开始&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#模板工程&quot; id=&quot;markdown-toc-模板工程&quot;&gt;模板工程&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;doha&quot;&gt;DOHA&lt;/h2&gt;

DOHA (Domain-Oriented Hexagonal Architecture) 面向领域的六边形架构的核心在于使用领域驱动设计（DDD）的思想来设计和组织业务逻辑，并结合六边形架构的端口适配器模式来实现业务逻辑与外部接口的分离，为构建健壮、可维护、可扩展的复杂应用系统提供了有力的支持。

&lt;img src=&quot;https://dinstone.github.io//img/arch/ha.jpg&quot; alt=&quot;六边形架构&quot; /&gt;

2005 年，Alistair Cockburn 提出的六边形架构，将应用程序巧妙地划分为内部的业务逻辑层和外部的适配器层。内部聚焦于业务逻辑，而外部具有一个或多个入站适配器（而不是表示层），通过调用业务逻辑来处理来自外部的请求。同样，外部具有一个或多个出站适配器（而不是数据持久层），业务逻辑调用这些出站适配器来访问外部应用程序。

为了解耦内部和外部之间的依赖，业务逻辑层抽象出端口来应对业务逻辑与外部的交互，这些端口实质上代表了一种协议，通常以接口 API 的形式呈现。

六边形架构的一个重要好处是业务逻辑不依赖于表示层逻辑或数据访问层逻辑。它将业务逻辑与适配器中包含的表示层和数据访问层的逻辑分离开来。由于这种分离，单独测试业务逻辑就容易得多了。

六边形架构的另一个好处是它是描述微服务架构中每个服务的架构的好方法。可以通过多个适配器调用业务逻辑，每个适配器实现特定的 API 或用户界面。业务逻辑还可以通过端口调用多个适配器，每个适配器调用不同的外部系统。

六边形架构以业务为核心，解耦外部依赖，分离了业务复杂度和技术复杂度等。然而，六边形架构本身并不涉及内部业务逻辑的设计模式，因此在落地过程中，需要我们自己选择业务逻辑的设计模式。通常，对于简单应用可以选择面向事务脚本模式来组织业务逻辑，而对于复杂应用来说，面向领域建模模式是首选。

&lt;h2 id=&quot;逻辑架构&quot;&gt;逻辑架构&lt;/h2&gt;

&lt;img src=&quot;https://dinstone.github.io//img/arch/doha-l.jpg&quot; alt=&quot;逻辑架构&quot; /&gt;

DOHA 架构将应用细化为五个分层： Inbound Adapter 层、Inbound Port 层、Domain 层、Outbound Port 层、Outbound Adapter层。

Inbound Adapter层和 Outbound Adapter层作为适配器层，依赖业务层（Inbound Port 层，Outbound Port 层和 Domain 层）。

&lt;h2 id=&quot;实现架构&quot;&gt;实现架构&lt;/h2&gt;

&lt;img src=&quot;https://dinstone.github.io//img/arch/doha-p.jpg&quot; alt=&quot;实现架构&quot; /&gt;

DOHA 将工程分为：

&lt;ul&gt;
  &lt;li&gt;
    doha-handler：应用程序入口，入站适配器实现都在这里，负责处理外部请求，依赖其它工程。
  &lt;/li&gt;
  &lt;li&gt;
    doha-interface：提供给第三方应用依赖的编程接口，如 RPC、Event、MQ 等，不依赖其它工程。
  &lt;/li&gt;
  &lt;li&gt;
    doha-invoker：出站适配器实现都在这里，负责调用外部系统，依赖 doha-business 工程。
  &lt;/li&gt;
  &lt;li&gt;
    doha-business：业务逻辑实现的地方，包括入站端口服务、领域模型、出站端口接口，不依赖其它工程。
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;快速开始&quot;&gt;快速开始&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;安装DOHA工程模板&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git clone https://github.com/dinstone/doha.git

&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;doha/doha-archetype/

mvn clean &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ol&gt;
  &lt;li&gt;应用模板创建工程&lt;/li&gt;
&lt;/ol&gt;

&lt;div class=&quot;language-shell highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mvn archetype:generate  &lt;span class=&quot;nt&quot;&gt;-DarchetypeGroupId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;io.doha.template  &lt;span class=&quot;nt&quot;&gt;-DarchetypeArtifactId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;doha-template-archetype  &lt;span class=&quot;nt&quot;&gt;-DarchetypeVersion&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;2.0.0 &lt;span class=&quot;nt&quot;&gt;-DgroupId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;demo.service &lt;span class=&quot;nt&quot;&gt;-DartifactId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;order-service &lt;span class=&quot;nt&quot;&gt;-Dpackage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;demo.service.order
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;模板工程&quot;&gt;模板工程&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    Java : https://github.com/dinstone/doha
  &lt;/li&gt;
  &lt;li&gt;
    Go   : https://github.com/dinstone/doha-go
  &lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Sat, 30 Mar 2024 00:00:00 +0000</pubDate>
        <link>https://dinstone.github.io//2024/03/30/doha-architect/</link>
        <guid isPermaLink="true">https://dinstone.github.io//2024/03/30/doha-architect/</guid>
        
        <category>方法论</category>
        
        <category>六边形架构</category>
        
        
        <category>方法论</category>
        
      </item>
    
      <item>
        <title>FMEA在软件架构中的应用</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#概述&quot; id=&quot;markdown-toc-概述&quot;&gt;概述&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#fmea简介&quot; id=&quot;markdown-toc-fmea简介&quot;&gt;FMEA简介&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#fmea核心&quot; id=&quot;markdown-toc-fmea核心&quot;&gt;FMEA核心&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#fmea应用&quot; id=&quot;markdown-toc-fmea应用&quot;&gt;FMEA应用&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#附录&quot; id=&quot;markdown-toc-附录&quot;&gt;附录&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;概述&quot;&gt;概述&lt;/h2&gt;

在过去的技术工作中，FMEA是我工具箱中经常用到的一个非常好用的分析工具，可以帮我们审视系统的问题现状和演进目标。

FMEA（故障模式与影响分析）是硬件设计师广泛使用的一种方法，用于建模和避免现场故障。这种方法越来越适用于对软件系统进行建模，以提高可靠性。在提高软件可靠性的方法、工具和实践中，FMEA可以说是成本最低、最容易学习和最有效的。

&lt;h2 id=&quot;fmea简介&quot;&gt;FMEA简介&lt;/h2&gt;

FMEA（Failure Mode and Effects Analysis）故障模式与影响分析，是由美国军方于 20 世纪 40 年代开始使用的一种过程分析工具，用于识别产品或服务的设计、制造或装配过程中的所有可能的故障。

故障模式 ：指的是某事物可能失败的方式或模式。故障是指任何可能发生的错误或缺陷，可以是潜在的，也可以是实际的。

影响分析 ：指的是分析和研究发生失败（或者潜在故障）造成的影响和后果。

故障模式与影响分析（FMEA）是一种在故障发生之前就发现潜在故障的方法。虽然 FMEA 并不是为软件而生，但是同样可以运用于软件领域。通俗的来讲，FMEA 就是一种分析方法，这种方法可以通过假设某组件故障，然后分析影响的途径，从而可以及早发现和识别系统问题、更好地规划后续工作、达到提高系统或者产品的可靠性的目的。

&lt;h2 id=&quot;fmea核心&quot;&gt;FMEA核心&lt;/h2&gt;

被分析系统/子系统的所有者、项目负责人、QA和至少一名领域专家通常会参加会议。

FMEA过程：

&lt;ol&gt;
  &lt;li&gt;定义故障模式–可能会出现什么问题？&lt;/li&gt;
  &lt;li&gt;定义影响–故障发生后会发生什么？&lt;/li&gt;
  &lt;li&gt;描述目标–谁将遭受失败？&lt;/li&gt;
  &lt;li&gt;找出根本原因–为什么会发生这种情况？&lt;/li&gt;
  &lt;li&gt;优先考虑风险–发生的可能性有多大？&lt;/li&gt;
  &lt;li&gt;定义解决方案–-如何防止这种情况发生？&lt;/li&gt;
  &lt;li&gt;定义当前的预防和检测方法–目前需要做什么？&lt;/li&gt;
&lt;/ol&gt;

在整个会议期间重复这些步骤，填充如下FMEA表格，其中列出了系统中风险及其相关故障的优先级列表。

FMEA表格：

&lt;img src=&quot;https://dinstone.github.io//img/arch/fmea-table.jpg&quot; alt=&quot;FMEA-Table&quot; /&gt;

说明：

&lt;ol&gt;
  &lt;li&gt;
    每个故障都有三个数字等级，等级为1到10：

    &lt;ul&gt;
      &lt;li&gt;严重程度（S）：1（微不足道）至10（灾难性）&lt;/li&gt;
      &lt;li&gt;发生的可能性（O）：1（不太可能）到10（不可避免）&lt;/li&gt;
      &lt;li&gt;可检测性（D）：1（保证可检测）到10（不可检测）&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    将S、O、D三个数字相乘以创建风险优先级数字RPN （S x O x D=RPN），通过RPN可以排定优先级。
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;fmea应用&quot;&gt;FMEA应用&lt;/h2&gt;

FMEA 是一个非常强大的工具，广泛应用于设计评审和技术规划中，帮助团队系统地审视系统的问题现状并设定清晰的演进目标。

在设计评审阶段，FMEA可以协助团队识别出产品或系统中潜在的故障模式，并评估这些故障模式对系统性能、安全性、可靠性等方面的影响。通过FMEA，团队可以较早发现设计中的问题，从而在设计阶段就进行修正，避免后续开发过程中的重大变更和成本增加。

而在技术规划中，FMEA同样发挥着重要作用。它可以帮助团队明确系统的演进目标，并识别出实现这些目标过程中可能遇到的潜在故障模式和风险。通过FMEA的分析，团队可以制定针对性的预防措施和应对策略，确保系统的演进过程平稳且可控。

使用FMEA进行问题现状审视和演进目标设定时，通常需要遵循以下步骤：

&lt;ul&gt;
  &lt;li&gt;定义范围：明确分析的对象和边界，确保FMEA的焦点明确。&lt;/li&gt;
  &lt;li&gt;故障模式识别：列出所有可能的故障模式，这些故障模式可能是设计缺陷、开发问题、测试问题等。&lt;/li&gt;
  &lt;li&gt;影响分析：评估每个故障模式对系统性能、安全性、可靠性等方面的影响，确定其严重度等级。&lt;/li&gt;
  &lt;li&gt;根因探究：分析导致每个故障模式发生的原因，找出潜在的设计或开发缺陷。&lt;/li&gt;
  &lt;li&gt;制定措施：针对每个故障模式及其原因，制定预防措施和应对策略，以降低故障发生的概率或减轻其影响。&lt;/li&gt;
  &lt;li&gt;优先级排序：根据故障模式的严重度、发生频率和可探测性，对预防措施进行优先级排序，确保优先解决最关键的问题。&lt;/li&gt;
&lt;/ul&gt;

通过FMEA的分析过程，团队可以更加清晰地了解系统的现状和问题所在，从而制定出更加有针对性的技术规划。同时，FMEA还可以作为持续改进的工具，帮助团队不断优化产品设计和技术方案，提升系统的整体性能和质量。

&lt;h2 id=&quot;附录&quot;&gt;附录&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;a href=&quot;http://uploads.pnsqc.org/2013/papers/t-026_Strong_paper.pdf&quot;&gt;Using FMEA to Improve Software Reliability&lt;/a&gt;
  &lt;/li&gt;
  &lt;li&gt;
    可靠性：主要指的是软件在异常情况下或在被非法、非常规使用时维持自身功能的能力。这种能力体现在两个方面：容错和健壮性。容错能力意味着软件在发生故障时仍能保持正常运行，并在内部完成故障的修复工作。而健壮性则是保护软件不受非正常使用方式或非法输入影响的能力，确保软件在各种使用方式下都能准确迁移至系统定义的状态。
  &lt;/li&gt;
  &lt;li&gt;
    稳定性：主要指的是软件系统在一定的时间内无故障运行的能力，要求软件在用户的使用周期内能够长期稳定运行。这要求系统具有一定的容错能力，能够抵御各种故障和异常情况，确保软件的连续性和可用性。
  &lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Fri, 08 Mar 2024 00:00:00 +0000</pubDate>
        <link>https://dinstone.github.io//2024/03/08/FMEA-architect/</link>
        <guid isPermaLink="true">https://dinstone.github.io//2024/03/08/FMEA-architect/</guid>
        
        <category>方法论</category>
        
        <category>FMEA</category>
        
        <category>架构</category>
        
        <category>技术规划</category>
        
        
        <category>方法论</category>
        
      </item>
    
      <item>
        <title>艰难的2023年，艰辛的2024年</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#6年多美团生涯收获感慨与未来之路&quot; id=&quot;markdown-toc-6年多美团生涯收获感慨与未来之路&quot;&gt;6年多美团生涯：收获、感慨与未来之路&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#2个月传统企业经历收获感慨与未来之路&quot; id=&quot;markdown-toc-2个月传统企业经历收获感慨与未来之路&quot;&gt;2个月传统企业经历：收获、感慨与未来之路&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;6年多美团生涯收获感慨与未来之路&quot;&gt;6年多美团生涯：收获、感慨与未来之路&lt;/h2&gt;

在美团的6年多时光，我经历了多次的高潮与低谷，如同一段跌宕起伏的交响曲。23年3月份纵情向前，距今已有一段时间了，但在闲暇之余，我仍会去细细品味和总结那段时间的收获和感慨。以下是我在美团6年多的10条感悟，希望对刚步入职场的年轻人或正在职业生涯中崭露头角的后起之秀有所启示和帮助。

&lt;ol&gt;
  &lt;li&gt;
    知己识人：这是职业生涯的基石。古人云：“自知者明，知人者智”，了解自己的优点、劣势、价值观和激情，才能找到最适合自己的位置。在职场中，了解同事和上司的能力和性格，有助于更好地协作和沟通，了解客户的需求和期望，可以更好地为他们提供服务。
  &lt;/li&gt;
  &lt;li&gt;
    有效沟通：清晰、准确、及时地沟通，是解决任何问题的第一步。
  &lt;/li&gt;
  &lt;li&gt;
    拥抱变化：变化是唯一不变的。适应变化，把握机会，是成功的关键。
  &lt;/li&gt;
  &lt;li&gt;
    保持谦虚：无论你多么优秀，总有比你更优秀的人。保持谦虚，才能不断进步。
  &lt;/li&gt;
  &lt;li&gt;
    持续学习：职场如逆水行舟，不进则退。要持续学习新的知识和技能，跟上时代的步伐。
  &lt;/li&gt;
  &lt;li&gt;
    目标驱动：以目标为导向，始终关注目标的实现。目标驱动的人在工作和生活中通常具有更高的主动性和积极性，因为他们明白自己为何而做，以及所做工作的意义和价值。
  &lt;/li&gt;
  &lt;li&gt;
    团队协作：没有完美的个人，但团队可以完美。学会倾听、理解和尊重他人，是团队协作的关键。
  &lt;/li&gt;
  &lt;li&gt;
    追求双赢：在商业世界中，双赢是最佳策略。寻求共同利益，才能长久合作。
  &lt;/li&gt;
  &lt;li&gt;
    利他思维：先做人，再做事。始终坚持“人人为我，我为人人”的原则，关心他人，乐于助人。关心他人，也是关心自己。
  &lt;/li&gt;
  &lt;li&gt;
    乐于分享：分享是一个输出的过程，不仅体现了一个人的大度和慷慨，还锻炼了自己的写作和表达能力，更能够赢得他人的尊重和信任，为自己的职业发展铺设坚实的基石。
  &lt;/li&gt;
&lt;/ol&gt;

这10条感悟不仅是对我在美团6年多的深情回眸，也是我对未来之路的一份期许。希望它们能成为你职场生涯中的一盏明灯，指引你前行。

&lt;h2 id=&quot;2个月传统企业经历收获感慨与未来之路&quot;&gt;2个月传统企业经历：收获、感慨与未来之路&lt;/h2&gt;

23年10月份入职了一家传统企业，做数字化转型的架构师。入职一个多月发现的和碰到的各种奇葩事件，真的是让人大跌眼镜。

&lt;ul&gt;
  &lt;li&gt;
    管理者勾心斗角，已经到了人尽皆知的地步，老板都无力改变，只能裹挟着往前走。研发部门的一个产品总监，联合HR、市场的VP搞走了2个CIO，2次上位代管研发部门，2次被老板安排的新CIO降级，目前第3个CIO来了半年了也拿他没办法，目前来看，再有半年如果CIO搞不定人，那可能就会被人搞定。
  &lt;/li&gt;
  &lt;li&gt;
    传统小厂思维僵化，利益盘根错节，没有人关心公司战略，只在乎自己的利益最大化。传统小厂根本就没有所谓的组织结构简单，层级较少，决策迅速、灵活等优点，反而是管理落后混乱，思维模式很封闭，不想改变，也不愿为公司战略贡献力量，部门之间互相甩锅，反正就是不想为业务目标达成负责。
  &lt;/li&gt;
  &lt;li&gt;
    谦虚、利他、乐于分享、双赢思维等优秀特质在传统企业的人上身是不会有的。这些好的特质本来就是稀缺的，在传统企业中，有此特质的优秀人才更是凤毛麟角，就算招来了能一直坚持下，估计是不会有的吧。
  &lt;/li&gt;
&lt;/ul&gt;

所以说，没有对比就没有差距，由于在这里的收获都是负面的，才使我真切体会到了美团大厂文化理念的美好。未来之路，继续坚持做正确的事，坚持正确的做事，剩下的就交给时间吧！
</description>
        <pubDate>Sun, 31 Dec 2023 00:00:00 +0000</pubDate>
        <link>https://dinstone.github.io//2023/12/31/summary-think/</link>
        <guid isPermaLink="true">https://dinstone.github.io//2023/12/31/summary-think/</guid>
        
        <category>Think</category>
        
        <category>语录</category>
        
        
        <category>Think</category>
        
      </item>
    
      <item>
        <title>真正的千万级分布式延迟任务系统 Grape</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#什么是延迟任务&quot; id=&quot;markdown-toc-什么是延迟任务&quot;&gt;什么是延迟任务&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#延迟任务典型场景&quot; id=&quot;markdown-toc-延迟任务典型场景&quot;&gt;延迟任务典型场景&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#延迟任务系统的特性&quot; id=&quot;markdown-toc-延迟任务系统的特性&quot;&gt;延迟任务系统的特性&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#延迟任务系统的解决方案&quot; id=&quot;markdown-toc-延迟任务系统的解决方案&quot;&gt;延迟任务系统的解决方案&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#设计与实现&quot; id=&quot;markdown-toc-设计与实现&quot;&gt;设计与实现&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#核心概念&quot; id=&quot;markdown-toc-核心概念&quot;&gt;核心概念&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#整体架构设计&quot; id=&quot;markdown-toc-整体架构设计&quot;&gt;整体架构设计&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#任务生命周期&quot; id=&quot;markdown-toc-任务生命周期&quot;&gt;任务生命周期&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#关键实现与技巧&quot; id=&quot;markdown-toc-关键实现与技巧&quot;&gt;关键实现与技巧&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#总结与未来规划&quot; id=&quot;markdown-toc-总结与未来规划&quot;&gt;总结与未来规划&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

2017年初，参考&lt;a href=&quot;https://github.com/beanstalkd/beanstalkd&quot;&gt;Beanstalkd&lt;/a&gt;的延迟任务管理机制，基于Redis实现了一个高并发、高性能、高可用的分布式延迟任务系统。在点餐、收银、支付等多个高并发产品中经受住了考验，先后经历了2次重构后于2019年以Grape项目开源，Github 项目地址：https://github.com/dinstone/grape 。

Grape 主要提供以下特性:

&lt;ol&gt;
  &lt;li&gt;任务具备延时、过期重试、重试次数、预留时间等功能。&lt;/li&gt;
  &lt;li&gt;通过Vert.x异步IO提供高性能 HTTP Restful API。&lt;/li&gt;
  &lt;li&gt;任务调度支持毫秒级精度，0毫秒延迟，Redis损耗小。&lt;/li&gt;
  &lt;li&gt;具备横向扩展能力，丰富的业务和性能指标监控接口。&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;什么是延迟任务&quot;&gt;什么是延迟任务&lt;/h2&gt;

想必大家都在网上购过物，比如，你在淘宝上买了件商品，收到货物后，即使你不主动去点 “确认收货”，经过若干天后，系统也会自动确认收货，完结订单。

这是怎么做到的呢？其实，这个背后隐藏着一个常用技术，就是延迟任务。顾明思议，我们把需要延迟执行的任务叫做延迟任务。

请注意，延迟任务不是定时任务，它们之间是有显著的区别：

&lt;ol&gt;
  &lt;li&gt;定时任务有明确的触发时间，延迟任务没有。&lt;/li&gt;
  &lt;li&gt;定时任务有执行周期，而延迟任务在某事件触发后一段时间内执行，没有执行周期。&lt;/li&gt;
  &lt;li&gt;定时任务一般执行的是批处理操作的任务，而延迟任务一般是针对单个处理操作的任务。&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;延迟任务典型场景&quot;&gt;延迟任务典型场景&lt;/h2&gt;

使用延迟任务的典型场景有以下这些：

&lt;ol&gt;
  &lt;li&gt;红包 24 小时未被查收，需要执行退款业务。&lt;/li&gt;
  &lt;li&gt;提交订单 30 分钟后，用户如果没有付钱，系统需要自动取消订单，归还库存。&lt;/li&gt;
  &lt;li&gt;直播时间快到了，需要给关注的用户发送消息，提醒上线。&lt;/li&gt;
  &lt;li&gt;支付完成后，需要异步回调通知业务系统的支付完成状态，在至少一次的QoS要求下，需要按指数幂退时间间隔延迟执行回调。&lt;/li&gt;
&lt;/ol&gt;

仔细审查这些业务场景，会发现每个业务处理都是在一定时间后执行特定的任务，而触发的时间不是固定的，如果做成定时任务来处理会很麻烦，而且时效和性能无法很好的保证。因此，创建一个解决此类问题的延迟任务系统才是最佳实践。

&lt;h2 id=&quot;延迟任务系统的特性&quot;&gt;延迟任务系统的特性&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;延迟任务的生产和消费需要支持高并发、高性能、高可用。延迟任务的提交通常参与到核心的业务流程中，如果不能保证并发和性能，将对核心业务有影响。&lt;/li&gt;
  &lt;li&gt;延迟任务的生产需要支持重复提交，很多场景下可能会存在重试的情况。&lt;/li&gt;
  &lt;li&gt;延迟任务的消费需要支持可靠消费，也就是说要需要明确的知道是否成功执行了任务。通常，这需要系统在消费时暂存延迟任务信息，在执行成功后提交反馈。&lt;/li&gt;
  &lt;li&gt;延迟任务具备延时、自动重试、优先级等功能。&lt;/li&gt;
  &lt;li&gt;延迟任务系统需要具备横向扩展能力，丰富的业务和性能指标监控能力。&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;延迟任务系统的解决方案&quot;&gt;延迟任务系统的解决方案&lt;/h2&gt;

基于以上特性要求，调研了一些现有开源方案: &lt;a href=&quot;https://blog.csdn.net/woniu211111/article/details/128489716&quot;&gt;领导：谁再用 Redis 实现过期订单关闭，立马滚蛋！&lt;/a&gt;

&lt;ol&gt;
  &lt;li&gt;RabbitMQ 通过死信和死信路由实现。无法保证时效性。&lt;/li&gt;
  &lt;li&gt;DelayQueue 实现。需要自己实现可靠消费，不具备分布式能力，需要自己实现高可用。&lt;/li&gt;
  &lt;li&gt;Beanstalkd 实现。延迟任务管理机制契合大部分特性。但由于不支持复制功能，服务存在单点问题且数据可靠性也无法满足。&lt;/li&gt;
  &lt;li&gt;Disque 实现。Redis 作者开源的分布式内存队列。Disque 采用和 Redis Cluster 类似无中心设计，所有节点都可以写入并复制到其他节点。不管是从功能上、设计还是可靠性都是比较好的选择。但是2016年之后就不咋维护了，&lt;/li&gt;
&lt;/ol&gt;

在调研了以上开源实现后发现，要实现一个满足以上特性的通用延迟任务系统，最好是具备Beanstalkd的任务管理机制，同时又有Disque的横向扩展能力和高可用性。

很显然，没有一个开源方案是完整的。那么接下来的问题就变成了，是基于Beanstalkd做二次开发集成Disque特性，还是基于Disque做二次开发集成Beanstalkd的任务管理特性？

很显然，不管选择那种二开方案，都是一项艰巨的挑战（我们根本就不具备二开的能力），没有丝毫的犹豫，放弃二开，直接原创。

鉴于研究了Beanstalkd和Disque特性和实现机理，决定基于它们做个微原创，当时有2个方案：

第一个方案是包装Beanstalkd实例做成延迟任务Broker集群，在Broker中管理任务的路由和队列消费的容错处理，从而实现横向扩展和高可用。该方案需要对Beanstalkd实例横向扩展和宕机做自动感知，会引入过多的中间件和实现逻辑，实现复杂度太高，整个系统的可用性很难保证。

第二个方案是基于Redis，使用有序集合zset实现延迟任务队列，也就是说将Beanstalkd的队列管理机制基于Redis的数据结构实现一遍就好。实现相对第一个方案，开发工作量小，而且可用性由Redis来保证（相当于集成了Disque）。

所以，最终选择了第二个方案，自研基于Redis的分布式延迟任务系统。

&lt;h2 id=&quot;设计与实现&quot;&gt;设计与实现&lt;/h2&gt;

&lt;h3 id=&quot;核心概念&quot;&gt;核心概念&lt;/h3&gt;

Grape中的核心概念有：

&lt;ul&gt;
  &lt;li&gt;Job ：业务定义的延迟任务，有以下属性：
    &lt;ul&gt;
      &lt;li&gt;id，任务ID，由业务系统指定，全局唯一。&lt;/li&gt;
      &lt;li&gt;dtr (delay to run)，任务延时运行时间，单位是毫秒。&lt;/li&gt;
      &lt;li&gt;ttr (time to run)，任务预期执行时间，单位是毫秒。超过 ttr 则触发任务重试。&lt;/li&gt;
      &lt;li&gt;noe (number of executions)，任务执行次数，默认从0开始，消费一次自增一。业务系统可以根据该属性做业务处理，如指数幂退调用等。&lt;/li&gt;
      &lt;li&gt;data (job content)，任务内容，字节数组。业务系统可以提交一些任务执行需要的参数，避免再次查询数据库准备数据。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    Tube ：同一个主题的延迟任务队列，用于区分和管理不同业务分组的延迟任务。
  &lt;/li&gt;
  &lt;li&gt;
    Broker ：延迟队列代理，用于管理Tube集合的运行。
  &lt;/li&gt;
  &lt;li&gt;Server ：Grape服务实例，为指定 namespace 的Broker提供 Restfull 的 Job API、Tube API、管理API和管理UI。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;整体架构设计&quot;&gt;整体架构设计&lt;/h3&gt;

&lt;img src=&quot;https://dinstone.github.io//img/arch/grape-arch.png&quot; alt=&quot;Grape&quot; /&gt;

Grape 是一个无状态的HTTP服务，很容易横向扩展。业务系统可以通过7层负载均衡器访问Grape-Server集群。

&lt;ul&gt;
  &lt;li&gt;
    Grape-Server主要由API处理器和Broker组件构成。
  &lt;/li&gt;
  &lt;li&gt;Broker内部由Scheduler和Tube组件构成。
    &lt;ul&gt;
      &lt;li&gt;Scheduler负责发现和加载Tube实例，以便更多的Broker实例分担Tube实例的管理压力，同时对外提供API服务。&lt;/li&gt;
      &lt;li&gt;Tube组件是延迟任务的管理器，其内部维护了3个Sorted Set队列：Deleay Queue、Remain Queue、Failed Queue。根据延迟任务的生命状态机制，任务在各个队列之间流动。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Redis存储主要包含了三类数据，分别是tube的名称集合、tube的队列模型和延迟任务模型。
    &lt;ul&gt;
      &lt;li&gt;tube的名称集合，是一个Set类的集合，代表当前由多少可用的延迟队列。&lt;/li&gt;
      &lt;li&gt;tube的队列模型，3个队列都是Sorted Set数据结构，Key是tube名称，数据是以延迟时间排序的任务ID。&lt;/li&gt;
      &lt;li&gt;延迟任务模型，每个任务是一个HashMap结构的数据，Key是任务ID，Value是任务的各个属性。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;任务生命周期&quot;&gt;任务生命周期&lt;/h3&gt;

Grape借鉴了Beanstalkd的任务管理机制，因此在Grape中，延迟任务可能处于四种状态之一：“delay”、“ready”、“remain”或“failed”。围绕任务状态的迁移动作，满足了很多通用场景对延迟任务的诉求。

&lt;img src=&quot;https://dinstone.github.io//img/arch/grape-status.png&quot; alt=&quot;Status&quot; /&gt;

&lt;ul&gt;
  &lt;li&gt;
    delay 状态，业务系统生成（produce）延迟任务后进入该状态。处于该状态的任务可以被删除（delete）。当前时刻大于任务延时后，任务迁移至ready状态。
  &lt;/li&gt;
  &lt;li&gt;
    ready 状态，业务系统生成（produce）延时为0毫秒的延迟任务进入该状态，或已过了延时的延迟任务进入该状态。进入该状态的任务可以被消费。消费任务（consume）将进入remain状态。
  &lt;/li&gt;
  &lt;li&gt;
    remain 状态，任务在消费期间会出现4种情况：

    &lt;ol&gt;
      &lt;li&gt;任务执行时间操过了ttr，或者执行任务的线程跑飞、业务进程宕机等异常情况下，任务会被调度到ready状态，使得任务可以被再次消费。&lt;/li&gt;
      &lt;li&gt;任务执行很顺利，但还没有拿到期望的结果，希望能再次重试，那么可以指定延时并释放（release）任务至delay状态。&lt;/li&gt;
      &lt;li&gt;任务执行很顺利，拿到了期望的结果，那么可以结束（finish）任务。&lt;/li&gt;
      &lt;li&gt;任务执行失败了，暂时无法处理，可能需要人工介入，可以先雪藏（bury）起来，则任务迁移至failed状态。&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
  &lt;li&gt;
    failed 状态，该状态的任务通常为异常任务，可通过查看（peek）任务来决策，如果需要继续执行，则打回（kick）到delay状态，如果不在需要执行，则可丢弃（discard）掉任务。
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;关键实现与技巧&quot;&gt;关键实现与技巧&lt;/h3&gt;

更多的实现细节和技巧可以参阅源码工程，这里仅介绍2个关键实现和技巧。

1.Tube的队列模型

&lt;img src=&quot;https://dinstone.github.io//img/arch/grape-tube.png&quot; alt=&quot;Tube&quot; /&gt;

Tube中一共维护了3个队列，每个队列对应一个Redis的Sorted Set数据集合，Score是任务的延迟时间，Value是任务ID。

&lt;ul&gt;
  &lt;li&gt;
    delay队列，存储delay和ready状态的延迟任务。produce提交的任务进入该队列后，不用做主动调度，就可以在consume的时候消费了。因为任务都是按延迟时间排序的，排在前面的任务，延时大于当前时间的都是ready的任务，所以是可以直接消费的。这样的实现即减少了调度的压力，也提升了调度的精度，而且也降低了Redis的IO。
  &lt;/li&gt;
  &lt;li&gt;
    retain队列，存储retain状态的任务。remain队列为什么也需要排序？对消费中的任务做ttr延迟时间排序，加快了retain状态到ready状态的转移。调度器不用扫描每个任务的ttr延时，通过Sorted Set数据结构可以直接获取remain队列的前n个到期任务。
  &lt;/li&gt;
  &lt;li&gt;
    failed队列，存储bury的任务。任务以提交时间排序，方便查看。
  &lt;/li&gt;
&lt;/ul&gt;

2.Broker的调度管理

Broker 作为延迟队列Tube的管理器，负责发现和执行Tube中任务的调度。

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Broker&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;RedisClient&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;redisClient&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;namespace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scheduledSize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;o&quot;&gt;...&lt;/span&gt;

	&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;scheduledExecutor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Executors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;newScheduledThreadPool&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scheduledSize&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ThreadFactory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

		&lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AtomicInteger&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AtomicInteger&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;

		&lt;span class=&quot;nd&quot;&gt;@Override&lt;/span&gt;
		&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Thread&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;newThread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Runnable&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;threadPrefix&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;incrementAndGet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
		&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;o&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

Broker启动后，使用固定频率毎2秒执行一次检查，如果有新的Tube创建，那么就创建一个Tube的调度任务添加到调度器中，Tube的调度任务将以固定延迟时间来执行。这样就使得namespace下的所有Broker都负担起任务调度，提升了系统的性能和吞吐。

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Broker&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;scheduledExecutor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;scheduleAtFixedRate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Runnable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

		&lt;span class=&quot;nd&quot;&gt;@Override&lt;/span&gt;
		&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
				&lt;span class=&quot;n&quot;&gt;dispatch&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
			&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Exception&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
				&lt;span class=&quot;no&quot;&gt;LOG&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;warn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;dispatch {} error: {}&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;namespace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getMessage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
			&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
		&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;o&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TimeUnit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;SECONDS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

	&lt;span class=&quot;no&quot;&gt;LOG&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Broker[{}] is started&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;namespace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Broker&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;stop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;scheduledExecutor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;shutdown&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;scheduledExecutor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;awaitTermination&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Integer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;MAX_VALUE&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TimeUnit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;HOURS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;InterruptedException&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;no&quot;&gt;LOG&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Broker[{}] is shutdown&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;namespace&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;dispatch&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;c1&quot;&gt;// discovery new tubes&lt;/span&gt;
	&lt;span class=&quot;nc&quot;&gt;Set&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tubeSet&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tubeSet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tubeName&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tubeSet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tubeTaskFutureMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;containsKey&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tubeName&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;nc&quot;&gt;ScheduledTask&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;task&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ScheduledTask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;createTube&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tubeName&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
			&lt;span class=&quot;nc&quot;&gt;ScheduledFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;?&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;future&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;scheduledExecutor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;scheduleWithFixedDelay&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;TimeUnit&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;SECONDS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
			&lt;span class=&quot;n&quot;&gt;tubeTaskFutureMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tubeName&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;future&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
	&lt;span class=&quot;c1&quot;&gt;// remove old tubes&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Iterator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Entry&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ScheduledFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;?&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iterator&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tubeTaskFutureMap&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;entrySet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;iterator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iterator&lt;/span&gt;
			&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;hasNext&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;nc&quot;&gt;Entry&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ScheduledFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;?&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;next&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;iterator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tubeSet&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;contains&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getKey&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
			&lt;span class=&quot;k&quot;&gt;continue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
		&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

		&lt;span class=&quot;c1&quot;&gt;// cancel tube scheduled task&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;next&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getValue&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;cancel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
		&lt;span class=&quot;c1&quot;&gt;// delete tube task future&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;iterator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;remove&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ScheduledTask&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;implements&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Runnable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

	&lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Tube&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tube&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

	&lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;ScheduledTask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Tube&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tube&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;tube&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tube&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
	&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

	&lt;span class=&quot;nd&quot;&gt;@Override&lt;/span&gt;
	&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;tube&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;schedule&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
	&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;总结与未来规划&quot;&gt;总结与未来规划&lt;/h2&gt;

在我们的使用场景中，2个Broker实例和一个2G内存的Redis实例就能够支撑千万级的延迟任务，经受住了业务系统峰值1500TPS的考验。

当前业务指标的监控仅有任务队列的堆积数量，技术指标还不是很完善。未来期望在监控指标和可视化方面做更多的设计和实现。

延迟任务系统作为互联网应用的常用基础设施，推荐每位工程师、架构师和技术管理者都能了解它。

最后，欢迎大家来关注和使用Grape，更加欢迎 Issue 和 PR。项目地址：https://github.com/dinstone/grape 。
</description>
        <pubDate>Fri, 07 Jul 2023 00:00:00 +0000</pubDate>
        <link>https://dinstone.github.io//2023/07/07/delay-task-with-redis/</link>
        <guid isPermaLink="true">https://dinstone.github.io//2023/07/07/delay-task-with-redis/</guid>
        
        <category>grape</category>
        
        <category>delay</category>
        
        <category>job</category>
        
        
        <category>grape</category>
        
      </item>
    
      <item>
        <title>分层架构演进</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#概述&quot; id=&quot;markdown-toc-概述&quot;&gt;概述&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#架构模式演进&quot; id=&quot;markdown-toc-架构模式演进&quot;&gt;架构模式演进&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#mvc架构&quot; id=&quot;markdown-toc-mvc架构&quot;&gt;MVC架构&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#三层架构&quot; id=&quot;markdown-toc-三层架构&quot;&gt;三层架构&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#ddd分层架构&quot; id=&quot;markdown-toc-ddd分层架构&quot;&gt;DDD分层架构&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#六边形架构&quot; id=&quot;markdown-toc-六边形架构&quot;&gt;六边形架构&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#洋葱圈架构&quot; id=&quot;markdown-toc-洋葱圈架构&quot;&gt;洋葱圈架构&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#整洁架构&quot; id=&quot;markdown-toc-整洁架构&quot;&gt;整洁架构&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#架构原则和协作&quot; id=&quot;markdown-toc-架构原则和协作&quot;&gt;架构原则和协作&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#分层的依据与原则&quot; id=&quot;markdown-toc-分层的依据与原则&quot;&gt;分层的依据与原则&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#层与层之间的协作&quot; id=&quot;markdown-toc-层与层之间的协作&quot;&gt;层与层之间的协作&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#总结&quot; id=&quot;markdown-toc-总结&quot;&gt;总结&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;概述&quot;&gt;概述&lt;/h2&gt;

前面聊过了应用架构风格的演进，其中提到的分层架构风格，其实现先后经历了MVC架构、三层架构、DDD架构、六边形架构等模式，那么，今天就聊一聊分层架构的演进。

在今天看来，分层架构是运用最为广泛的架构模式。首先，几乎每个软件系统都通过层（Layer）来隔离不同的关注点，在层中应对不同需求的变化，使得这种变化可以独立演进。此外，软件系统架构要考虑技术复杂性与业务复杂性的分离，通常引起技术实现发生变化的原因与引起业务发生变化的原因显然不同，而分层架构模式是隔离技术复杂度的利器。

将一个软件系统进行分层设计，对每个开发人员来说，已然成了很自然的事情，都不需要问为什么了，而且很多人都能信手拈来很多架构模式，如经典的三层架构、DDD的四层架构、六边形架构、洋葱圈架构、整洁架构。

但细细想来，为啥有这么多的模式，它们之间有什么联系吗？

&lt;h2 id=&quot;架构模式演进&quot;&gt;架构模式演进&lt;/h2&gt;

首先我们先看一下分层架构的演进，学习一下这些架构模式的分层关注点，以及层之间的依赖关系，然后尝试总结和定义分层原则。

&lt;h3 id=&quot;mvc架构&quot;&gt;MVC架构&lt;/h3&gt;

&lt;img src=&quot;https://dinstone.github.io//img/arch/mvc.png&quot; alt=&quot;mvc&quot; /&gt;

2000年之前，软件应用架构多采用MVC架构。MVC模式（Model View Controller）模型-视图-控制器模式，是将数据模型与界面显示分离的一种架构模式。MCV模式最大的优势所在，就是将视图和模型分离，大大提高了代码的可重用性（多个视图可以共享一个模型）。

&lt;h3 id=&quot;三层架构&quot;&gt;三层架构&lt;/h3&gt;

&lt;img src=&quot;https://dinstone.github.io//img/arch/scjg.png&quot; alt=&quot;scjg&quot; /&gt;

2003年之前，主流架构模式是三层架构。经典三层架构为了符合“高内聚，低耦合”思想，自顶向下由用户界面层（User Interface Layer）、业务逻辑层（Business Logic Layer）与数据访问层（Data Access Layer）组成。层与层之间的依赖是向下的，上层对下层的调用，是通过接口实现的，而真正提供服务的是下层的接口实现类。它有效地隔离了业务逻辑与数据访问逻辑，使得这两个不同关注点能够相对自由和独立地演化。

&lt;h3 id=&quot;ddd分层架构&quot;&gt;DDD分层架构&lt;/h3&gt;

&lt;img src=&quot;https://dinstone.github.io//img/arch/ddd-d.png&quot; alt=&quot;ddd&quot; /&gt;

2003年，Eric Evans在其著作《领域驱动设计》中提出了DDD分层架构，旨在解决传统三层架构中业务逻辑层难以维护和演进的问题。DDD的核心在于对业务领域的深入建模和精准划分，并通过分层架构的方式实现代码的清晰组织。在DDD分层架构中，业务逻辑层得到了进一步的丰富和深化，被细分为应用层和领域层，从而使得业务逻辑的处理更加专业和高效。

此外，DDD还将传统三层架构中的数据访问层更名为基础设施层，这一变革不仅突破了之前数据库管理系统的限制，还大大扩展了基础设施层的内涵。基础设施层现在负责封装技术复杂度，为上层提供统一的接口和封装，使得整个系统更加灵活和可扩展。

通过DDD分层架构，开发人员能够更好地关注业务领域本身，降低技术实现的复杂度，提高软件的质量和可维护性。同时，这种架构模式也使得系统的演进变得更加平滑和可控，为企业的长远发展奠定了坚实的基础。

&lt;img src=&quot;https://dinstone.github.io//img/arch/ddd-layer.png&quot; alt=&quot;ddd&quot; /&gt;

&lt;ol&gt;
  &lt;li&gt;接口层：和三层架构第一层基本保持不变。&lt;/li&gt;
  &lt;li&gt;应用层：从三层架构里的业务逻辑层剥离出的应用服务，应用服务负责接收外部请求，协调领域对象（聚合和领域服务），以及处理事务和安全性等方面的问题。应用服务通常用于暴露接口给外部系统（例如用户界面、API等）。&lt;/li&gt;
  &lt;li&gt;领域层：从三层架构里的业务逻辑层剥离出的核心业务逻辑，也即领域模型（聚合、聚合根、实体、值对象、领域服务）所在层。聚合是一个边界内的领域对象的集合，是领域逻辑的核心单元，其目的是将领域模型模块化。领域服务是一个面向领域的服务，负责处理特定的领域逻辑，通常被用于处理跨实体的复杂业务操作，领域服务通常不保存状态，而是执行一些操作或计算。&lt;/li&gt;
  &lt;li&gt;基础设施层：包含原三层架构的数据访问层，也包括从三层架构里的业务逻辑层剥离出的技术框架、中间件系统、以及依赖其它应用系统的相关代码。&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;六边形架构&quot;&gt;六边形架构&lt;/h3&gt;

&lt;img src=&quot;https://dinstone.github.io//img/arch/lbxjg.png&quot; alt=&quot;lbx&quot; /&gt;

2005年，Alistair Cockburn提出的六边形架构将系统巧妙地划分为内部和外部两大部分。内部聚焦于核心的业务逻辑，而外部则负责业务逻辑的入口调用和出口依赖的管理。内部与外部之间的通信是通过精心设计的端口来实现的，这些端口实质上代表了一种协议，通常以接口API的形式呈现。

为了确保不同形态的外部系统能够顺畅地访问内部业务逻辑，或者在内部业务逻辑需要依赖外部系统时能够无缝集成，六边形架构引入了适配器的概念。适配器作为桥梁，使得内部与外部之间的交互变得灵活而可控。因此，六边形架构也常被称为端口适配器架构。

通过这种架构模式，六边形架构不仅增强了系统的可扩展性和可维护性，还使得业务逻辑与外部系统的解耦成为可能，为复杂系统的设计和开发提供了一种高效且可靠的解决方案。

&lt;img src=&quot;https://dinstone.github.io//img/arch/lbxjgx.png&quot; alt=&quot;lbx&quot; /&gt;

六边形架构主要关注如何将应用程序的核心业务逻辑与外部系统解耦，使其更加灵活、可扩展和可维护。

然而，六边形架构本身并不涉及内部业务逻辑的具体实现细节。内部业务逻辑的实现需要依赖于深入的业务领域知识和适当的技术选型。在这方面，领域驱动设计（DDD）为我们提供了有力的指导。

因此，在实际应用中，我们可以结合六边形架构和DDD的最佳实践，将六边形架构的内部业务逻辑进一步细化为DDD的应用层和领域层。应用层负责处理用户的输入和输出，协调领域层的对象来执行相应的业务操作；而领域层则专注于业务领域的核心概念和规则，确保业务逻辑的完整性和一致性。通过这种结合，我们可以充分利用六边形架构和DDD的优势，构建出既灵活又健壮的软件系统。

&lt;h3 id=&quot;洋葱圈架构&quot;&gt;洋葱圈架构&lt;/h3&gt;

&lt;img src=&quot;https://dinstone.github.io//img/arch/ycjg.png&quot; alt=&quot;yc&quot; /&gt;

2008年，Jeffrey Palermo巧妙地结合了六边形架构与DDD分层架构，提出了洋葱圈架构。这一架构与六边形架构有着诸多相似之处，但它在内部业务逻辑的划分上更为精细，增加了一个领域服务层。这种分层设计使得每层的依赖关系变得清晰明了，外层依赖于内层，构建了一个有序且高效的系统结构。

然而，这并不意味着在访问内部的领域模型时，必须严格经过应用服务层和领域服务层。实际上，对于一些没有涉及复杂业务逻辑的场景，我们并不需要特意构建领域服务层。在这种情况下，应用服务层可以直接与领域模型进行交互，从而简化了系统结构，提高了开发效率。

这种灵活性正是洋葱圈架构的魅力所在。它允许我们根据具体业务需求，灵活地调整系统结构，确保在满足功能需求的同时，保持系统的清晰度和可维护性。因此，在设计和实现洋葱圈架构时，我们应充分理解其核心理念，并根据实际情况进行合理的调整和优化。

&lt;h3 id=&quot;整洁架构&quot;&gt;整洁架构&lt;/h3&gt;

&lt;img src=&quot;https://dinstone.github.io//img/arch/zjjg.png&quot; alt=&quot;cla&quot; /&gt;

2012年，Rob Martin提出了整洁架构，它与洋葱圈架构有着诸多相似之处。整洁架构的核心原则在于确保代码依赖关系始终从外向内流动，每一圈层都代表着软件系统的不同组成部分。随着圈层向内深入，抽象程度逐渐提高，外层侧重于实现具体的机制，而内层则聚焦于策略层面的设计和实现。

这种分层设计使得各层之间的依赖关系清晰明了，有效地实现了领域层与业务之外其他因素的隔离。通过整洁架构，我们可以确保领域层专注于核心业务逻辑的处理，而不受外部技术细节或特定实现方式的干扰。

整洁架构的引入，不仅提高了软件系统的可维护性和可扩展性，还使得开发人员能够更加专注于业务领域的深入探索和创新。因此，整洁架构已成为现代软件设计中备受推崇的一种架构模式，为构建高质量、可靠的软件系统提供了有力的支持。

&lt;h2 id=&quot;架构原则和协作&quot;&gt;架构原则和协作&lt;/h2&gt;

分层架构越来越普及，而我们的架构却变得越来越笨拙，为什么会这样呢？其实就是因为大部分的软件设计师并未理解分层架构的本质，只是照葫芦画瓢简单地将分层模式应用到系统中。要么采用经典的三层架构，要么遵循领域驱动设计改进的四层架构，要么应用六边形架构，但这样的分层设计带来了什么，如此分层究竟有何道理却从未思考过。因此，这里我们提出2个基本问题，并尝试回答一下：

&lt;ol&gt;
  &lt;li&gt;分层的依据与原则是什么？&lt;/li&gt;
  &lt;li&gt;层与层之间是怎样协作的？&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;分层的依据与原则&quot;&gt;分层的依据与原则&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;
    分层的第一个依据是：基于不同的关注点和不同的调用目的划分层次。
分层架构中的层次越往上，其抽象层次就越面向用户，面向业务；分层架构中的层次越往下，其抽象层次就变得越通用，面向技术和设备。以DDD分层架构为例，之所以引入应用层（Application Layer），就是为了给调用者提供完整的业务用例。
  &lt;/li&gt;
  &lt;li&gt;
    分层的第二个依据是：针对变化原因和频率划分层次。
分层时应针对不同的变化原因确定层次的边界，严禁层次之间互相干扰，或者至少将变化对各层带来的影响降到最低。例如数据库结构的修改自然会影响到基础设施层的数据模型以及领域层的领域模型，但当我们仅修改基础设施层的数据库访问实现逻辑时，就不应该影响到领域层了。
  &lt;/li&gt;
  &lt;li&gt;
    分层的第一个原则是：层与层之间的关系应该是正交的。
所谓“正交”，并非二者之间没有关系，而是垂直相交的两条直线。唯一相关的依赖点是这两条直线的相交点，即两层之间的协作点。正交的两条直线，无论哪条直线进行延伸，都不会对另一条直线产生任何影响（指直线的投影）。如果非正交，即“斜交”，当一条直线延伸时，它总是会投影到另一条直线，这就意味着另一条直线会受到它变化的影响。
  &lt;/li&gt;
  &lt;li&gt;
    分层的第二个原则是：保证同一层的组件处于同一个抽象层次。
该原则要求同一个抽象层的组件面向的能力是一致的，要么是面向用户、要么面向应用、要么面向业务、要么是面向技术设备，这样上层才能更好的组合下次的能力。
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;层与层之间的协作&quot;&gt;层与层之间的协作&lt;/h3&gt;

&lt;ol&gt;
  &lt;li&gt;
    上层依赖下层，外层依赖内层。
通过观察上面各种架构分层可以发现，分层架构的依赖大多都是自顶向下传递的，这也符合大多数人对分层的认知模型。从抽象层次看，层次越处于下端，就会变得越通用越公共，与具体的业务隔离得越远。出于重用的考虑，这些通用和公共的功能往往会被单独剥离出来形成平台或框架，在系统边界内的低层，除了面向高层提供足够的实现外，就都成了平台或框架的调用者。换言之，越是通用的层，越有可能与外部平台或框架形成强依赖。若依赖的传递方向仍然采用自顶向下，就会导致系统的业务对象也随之依赖于外部平台或框架。为什么是这样呢？我们通常理解的依赖，是指被依赖方需要为依赖方（调用方）提供功能支撑，而依赖的抽象是由下层定义的，这样的话，下层的实现变化就会传导到上层。
  &lt;/li&gt;
  &lt;li&gt;
    依赖倒置原则（Dependency Inversion Principle，DIP）
它要求“高层模块不应该依赖于低层模块，二者都应该依赖于抽象”。因此，依赖倒置原则隐含的本质是：高层要依赖不变或稳定的接口，通常这些抽象接口是由高层来定义的，而底层来实现这些稳定的接口。
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;总结&quot;&gt;总结&lt;/h2&gt;

不管是三层架构、DDD分层架构、六边形架构、洋葱圈架构、还是整洁架构，都提倡以业务为核心，解耦外部依赖，分离业务复杂度和技术复杂度。

现在我们搞清楚了如何分层，也就能对上面的各种分层架构做一些思考了。那么上面的这些架构能否满足你的设计需要呢？如果不能，你是否可以设计自己的架构模式呢？
</description>
        <pubDate>Tue, 06 Jun 2023 00:00:00 +0000</pubDate>
        <link>https://dinstone.github.io//2023/06/06/layer-arch-evolution/</link>
        <guid isPermaLink="true">https://dinstone.github.io//2023/06/06/layer-arch-evolution/</guid>
        
        <category>分层</category>
        
        <category>架构</category>
        
        
        <category>架构</category>
        
      </item>
    
      <item>
        <title>应用架构演进</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#概述&quot; id=&quot;markdown-toc-概述&quot;&gt;概述&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#架构演进&quot; id=&quot;markdown-toc-架构演进&quot;&gt;架构演进&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#单体应用架构&quot; id=&quot;markdown-toc-单体应用架构&quot;&gt;单体应用架构&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#垂直应用架构&quot; id=&quot;markdown-toc-垂直应用架构&quot;&gt;垂直应用架构&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#分层服务架构&quot; id=&quot;markdown-toc-分层服务架构&quot;&gt;分层服务架构&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#面向服务架构&quot; id=&quot;markdown-toc-面向服务架构&quot;&gt;面向服务架构&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#微服务架构&quot; id=&quot;markdown-toc-微服务架构&quot;&gt;微服务架构&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#总结&quot; id=&quot;markdown-toc-总结&quot;&gt;总结&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;概述&quot;&gt;概述&lt;/h2&gt;

在学习软件架构设计的过程中，总是能听到单体架构、分布式架构、SOA架构、微服务架构 等各种架构方法论，而这么多架构方法都有什么区别和联系呢？它们是一起涌现的还是依次出现的？为什么会出现这种情况呢？

另外，大家应该都听前辈说过 “架构是设计出来的，也是演进出来的”。一个运行中的应用，肯定存在一个架构，而这个架构是由研发人员设计出来的，它能满足当前的质量属性约束，为业务运行带来价值。但是，随着业务需求的变更、应用系统的迭代和业务量的增长，之前的应用架构还能满足质量属性约束吗？如果不能，该怎么办呢？很显然，需要做应用架构调整，来解决当下碰到的问题，如此便有了架构演进的诉求。那么是否存在一个银弹，可以一劳永逸的解决所有架构问题呢？很显然，答案是否定的，因为应用架构的方法论也一直在发展中。

因此，我们有必要审视一下应用架构方法论的演进，除了回答上面提到的问题，看看能否从中领会到一些有用的东西。那么，带着问题，我们开始吧。

&lt;h2 id=&quot;架构演进&quot;&gt;架构演进&lt;/h2&gt;

随着互联网的普及，Web应用成了软件开发的主流，而Web应用的架构方法自然也就成了焦点。下面我们一起研究一下它的演进路径。

&lt;img src=&quot;https://dinstone.github.io//img/arch/evolution.png&quot; alt=&quot;aae&quot; /&gt;

&lt;h3 id=&quot;单体应用架构&quot;&gt;单体应用架构&lt;/h3&gt;

&lt;img src=&quot;https://dinstone.github.io//img/arch/single.png&quot; alt=&quot;aae&quot; /&gt;

特点：

&lt;ol&gt;
  &lt;li&gt;
    所有应用功能都部署在一起。
  &lt;/li&gt;
  &lt;li&gt;
    应用可通过水平复制或数据分区的扩展方式提升系统吞吐和可用性。
  &lt;/li&gt;
  &lt;li&gt;
    架构设计的重点是对应用做分层设计，先后经历了MVC架构、分层架构、DDD架构、六边形架构等模式。
  &lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
  &lt;li&gt;
    MVC架构（二层架构）将视图和模型分离，多个视图可以共享一个模型，解决视图和模型耦合的问题。
  &lt;/li&gt;
  &lt;li&gt;
    分层架构（三层架构）进一步对模型进行分层，采用面向接口编程，各层之间采用接口相互访问，解决了业务逻辑可测试性的问题。
  &lt;/li&gt;
  &lt;li&gt;
    六边形架构以业务逻辑层为中心组织逻辑视图。业务逻辑层通过暴露的出入站端口（port）与适配器层的出入站适配器（adapter）交互，完成系统之间的调用。
  &lt;/li&gt;
&lt;/ul&gt;

优点：

&lt;ol&gt;
  &lt;li&gt;
    架构简单，前期开发成本低，小型项目的首选。
  &lt;/li&gt;
  &lt;li&gt;
    开发效率高，模块之间交互采用本地方法调用。
  &lt;/li&gt;
  &lt;li&gt;
    容易部署，运维成本小。打包为一个完整的包即可运行。
  &lt;/li&gt;
  &lt;li&gt;
    容易测试，在本地就可以启动完整的应用，集成测试方便。
  &lt;/li&gt;
&lt;/ol&gt;

缺点：

&lt;ol&gt;
  &lt;li&gt;
    应用的可测试性、可扩展性和可维护性影响软件交付速度。全部功能集成在一个工程中，对于大型、复杂项目不易测试、扩展及维护。
  &lt;/li&gt;
  &lt;li&gt;
    版本迭代速度逐渐变慢。修改一个地方就要将整个应用全部编译、打包、部署、启动，开发及测试周期过长。
  &lt;/li&gt;
  &lt;li&gt;
    无法按需伸缩。通过集群的方式扩展，无法针对某业务功能按需伸缩。
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;垂直应用架构&quot;&gt;垂直应用架构&lt;/h3&gt;

&lt;img src=&quot;https://dinstone.github.io//img/arch/vertical.png&quot; alt=&quot;aae&quot; /&gt;

特点：

&lt;ol&gt;
  &lt;li&gt;
    通常按用户角色使用场景划分多个互相独立的单体应用。
  &lt;/li&gt;
  &lt;li&gt;
    应用之间的公共业务逻辑，通过数据复制集成或通过共享代码集成。
  &lt;/li&gt;
&lt;/ol&gt;

优点：

&lt;ol&gt;
  &lt;li&gt;
    每个单体应用采用分层架构，可独立部署、演进和扩展。
  &lt;/li&gt;
  &lt;li&gt;
    每个单体应用可按需伸缩。不同业务单体的架构约束不一样，可区别对待。
  &lt;/li&gt;
  &lt;li&gt;
    每个单体应用可采用不同的技术栈。
  &lt;/li&gt;
&lt;/ol&gt;

缺点：

&lt;ol&gt;
  &lt;li&gt;
    单体应用之间存在数据冗余、功能冗余，耦合性高。
  &lt;/li&gt;
  &lt;li&gt;
    数据模型变更会影响整个应用的稳定，变更风险高。
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;分层服务架构&quot;&gt;分层服务架构&lt;/h3&gt;

&lt;img src=&quot;https://dinstone.github.io//img/arch/layered.png&quot; alt=&quot;aae&quot; /&gt;

特点：

&lt;ol&gt;
  &lt;li&gt;
    将垂直应用中的公用核心业务逻辑抽取出来，作为独立的服务。
  &lt;/li&gt;
  &lt;li&gt;
    应用和服务之间通过定义良好的接口交互。此时，分布式服务框架（RPC）是关键。
  &lt;/li&gt;
&lt;/ol&gt;

优点：

&lt;ol&gt;
  &lt;li&gt;
    提高了业务的复用，使得应用能快速的响应需求。服务可独立部署、演进和扩展。
  &lt;/li&gt;
  &lt;li&gt;
    垂直单体应用可按需伸缩。不同业务单体的架构约束不一样，可区别对待。
  &lt;/li&gt;
  &lt;li&gt;
    垂直单体应用和公共服务可采用不同的技术栈。
  &lt;/li&gt;
&lt;/ol&gt;

缺点：

&lt;ol&gt;
  &lt;li&gt;
    本地事务变成分布式事务。分布式架构带来的事务问题。
  &lt;/li&gt;
  &lt;li&gt;
    本地调用变成远程调用。分布式架构带来的稳定性问题。
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;面向服务架构&quot;&gt;面向服务架构&lt;/h3&gt;

&lt;img src=&quot;https://dinstone.github.io//img/arch/soa.png&quot; alt=&quot;aae&quot; /&gt;

特点：

&lt;ol&gt;
  &lt;li&gt;
    基于分布式架构思想，将重复的功能拆分成多个服务。
  &lt;/li&gt;
  &lt;li&gt;
    系统与服务之间采用 Web Service、 RPC 等方式通信。
  &lt;/li&gt;
  &lt;li&gt;
    ESB（企业服务总线）作为系统与服务之间通信的桥梁。
  &lt;/li&gt;
&lt;/ol&gt;

优点：

&lt;ol&gt;
  &lt;li&gt;
    将重复的功能抽取为服务，提高了开发效率，提升了系统的复用性、可维护性。
  &lt;/li&gt;
  &lt;li&gt;
    可以针对不同服务的特点按需伸缩。
  &lt;/li&gt;
  &lt;li&gt;
    ESB降低了系统中的接口耦合。
  &lt;/li&gt;
&lt;/ol&gt;

缺点：

&lt;ol&gt;
  &lt;li&gt;
    系统与服务的界限模糊，会导致抽取的服务的粒度过大，系统与服务之间耦合性高。
  &lt;/li&gt;
  &lt;li&gt;
    ESB是系统稳定的关键，所有服务调用都经过它。
  &lt;/li&gt;
  &lt;li&gt;
    ESB的服务接口协议种类繁多，不利于系统维护。
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&quot;微服务架构&quot;&gt;微服务架构&lt;/h3&gt;

&lt;img src=&quot;https://dinstone.github.io//img/arch/msa.png&quot; alt=&quot;aae&quot; /&gt;

特点：

&lt;ol&gt;
  &lt;li&gt;
    业务功能按领域拆分为一组服务。
  &lt;/li&gt;
  &lt;li&gt;
    每一个服务都是由一组专注的、高内聚的功能职责组成。
  &lt;/li&gt;
  &lt;li&gt;
    服务之间采用RESTful、 RPC等轻量级协议传输。
  &lt;/li&gt;
  &lt;li&gt;
    通常采用前后端分离架构。
  &lt;/li&gt;
&lt;/ol&gt;

优点：

&lt;ol&gt;
  &lt;li&gt;
    灵活性：由于每个服务都是独立的，因此可以快速地开发和部署新的服务，也可以快速地修改和升级现有的服务。这使得微服务架构更加灵活，可以快速地适应不同的需求和变化。
  &lt;/li&gt;
  &lt;li&gt;
    可扩展性：每个服务都可以独立地进行扩展，而不必扩展整个系统。这使得微服务架构更加可靠、高效和可扩展，可以满足大流量和高并发的需求。
  &lt;/li&gt;
  &lt;li&gt;
    可靠性：由于每个服务都是独立的，因此一个服务的故障不会影响整个系统的运行。这使得微服务架构更加可靠，能够更好地处理系统中的故障和问题。
  &lt;/li&gt;
  &lt;li&gt;
    技术多样性：每个服务都可以使用不同的编程语言、框架和技术，这使得微服务架构更加灵活，可以使用最适合每个服务的技术。
  &lt;/li&gt;
  &lt;li&gt;
    易于管理：由于每个服务都是独立的，因此可以更好地管理和监控每个服务的状态和性能，这使得微服务架构更加易于管理和维护。
  &lt;/li&gt;
&lt;/ol&gt;

缺点：

&lt;ol&gt;
  &lt;li&gt;
    服务的拆分和定义是一项挑战。服务拆分和定义更像是一门艺术。
  &lt;/li&gt;
  &lt;li&gt;
    分布式系统带来的各种复杂性，使得开发、测试和部署变得困难。
  &lt;/li&gt;
  &lt;li&gt;
    当部署跨越多个服务的功能时，需要谨慎协调更多开发团队。
  &lt;/li&gt;
  &lt;li&gt;
    开发者需要考虑到底应该在应用的什么阶段使用微服务架构。
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;总结&quot;&gt;总结&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;
    架构方法的演进是基于务实的解决问题的思想发展而来，当然更多的是向外思考追求解决方案的结果。虽然在解决核心问题的过程中引入了新的问题，但是本质上还是促进了大型复杂系统的设计。
  &lt;/li&gt;
  &lt;li&gt;
    通过对各种架构风格的介绍，也能够看清各自的特性、优缺点和技术要求，所以需要根据业务的不同阶段，团队的规模大小、技术能力，选择适合自己的架构风格才是硬道理。
  &lt;/li&gt;
&lt;/ol&gt;
</description>
        <pubDate>Tue, 23 May 2023 00:00:00 +0000</pubDate>
        <link>https://dinstone.github.io//2023/05/23/web-arch-evolution/</link>
        <guid isPermaLink="true">https://dinstone.github.io//2023/05/23/web-arch-evolution/</guid>
        
        <category>架构</category>
        
        <category>web</category>
        
        <category>应用</category>
        
        
        <category>架构</category>
        
      </item>
    
      <item>
        <title>深入理解kubernetes架构设计</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#概述&quot; id=&quot;markdown-toc-概述&quot;&gt;概述&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#技术定位&quot; id=&quot;markdown-toc-技术定位&quot;&gt;技术定位&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#设计原则&quot; id=&quot;markdown-toc-设计原则&quot;&gt;设计原则&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#架构设计&quot; id=&quot;markdown-toc-架构设计&quot;&gt;架构设计&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#运行原理&quot; id=&quot;markdown-toc-运行原理&quot;&gt;运行原理&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;概述&quot;&gt;概述&lt;/h2&gt;

随着互联网技术的飞速发展，微服务架构成了当下各个企业的标准开发模式，在微服务落地的过程中，容器化应用部署成了首选，随着业务的发展，系统中的容器越来越多。于是，容器的资源调度，部署运行，扩容缩容就成了我们要面临的问题。

我们都知道，当下 Kubernetes 作为容器集群的管理平台被广泛应用，但是，您可能并不了解它的组件以及这些组件的交互方式，不能很好的理解它的设计原理，也就不能理解它为什么是一个用于大规模运行分布式应用和服务的开源容器编排平台。

&lt;h2 id=&quot;技术定位&quot;&gt;技术定位&lt;/h2&gt;

Kubernetes（简称k8s）是一个开源的容器编排平台，用于自动化应用程序的部署、扩展和管理。Kubernetes 拥有许多功能，包括水平扩展、自动化升级、服务发现和负载均衡等，它可以运行在多个云平台、物理服务器或虚拟机上。Kubernetes 支持多种容器运行时技术，其中最为广泛使用的就是Docker。通过Kubernetes，我们可以对容器化应用进行分布式管理，实现应用的高可用、弹性伸缩和故障恢复等功能。

&lt;img src=&quot;https://dinstone.github.io//img/k8s/msc.png&quot; alt=&quot;msc&quot; /&gt;

Kubernetes 是一个多语言的通用容器管理平台，能够运行本地云和传统的容器化应用程序。但是随着能力不断增强，逐渐的覆盖了所有微服务关注点，成为了下一代构建云原生技术中台的基础平台。

&lt;img src=&quot;https://dinstone.github.io//img/k8s/msa.png&quot; alt=&quot;msa&quot; /&gt;

&lt;img src=&quot;https://dinstone.github.io//img/k8s/msk.png&quot; alt=&quot;msk&quot; /&gt;

&lt;h2 id=&quot;设计原则&quot;&gt;设计原则&lt;/h2&gt;

Kubernetes 设计基于 3 个原则：

&lt;ol&gt;
  &lt;li&gt;安全。它应遵循最新的安全最佳实践。&lt;/li&gt;
  &lt;li&gt;易用。它应能通过一些简单的命令进行操作。&lt;/li&gt;
  &lt;li&gt;可扩展。不应偏向于某一个提供商，而是能通过配置文件进行自定义。&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;架构设计&quot;&gt;架构设计&lt;/h2&gt;

&lt;img src=&quot;https://dinstone.github.io//img/k8s/k8s.png&quot; alt=&quot;k8s&quot; /&gt;

Kubernetes 集群是用来管理容器集群的平台。既然是管理集群，那么就存在控制节点和计算节点，在这些节点上运行着控制面相关的组件和计算面相关的容器应用。

其中控制节点，即Master节点，负责管理和控制集群节点，由3个紧密协作的组件组成，分别是负责API服务的kube-api-server、负责调度的 kube-scheduler、负责容器编排的 kube-controller-manager。整个集群的持久化数据，则由 kube-api-server 处理后保存在 etcd 中。

其中计算节点，即Worker节点，会运行一个名为kubelet的控制面核心组件。kubelet组件主要负责同容器运行时（比如docker）交互，交互通过一个称作CRI（Container Runtime Interface）的远程调用接口，该接口定义了容器运行时的各项核心操作，比如启动一个客器需要的所有参数。

这也是为何 Kubernetes 项目并不关心你部署的是什么容器运行时、使用了什么技术实现，只要你的容器运行时能够运行标准的容器镜像，它就可以通过实现 CRI 接入 Kubernetes 项目。

&lt;h2 id=&quot;运行原理&quot;&gt;运行原理&lt;/h2&gt;

我们通过 Master 对每个Worker节点发送命令。简单来说，Master 就是管理者，Worker 就是被管理者。

Worker 可以是一台机器或者一台虚拟机。在 Worker 上面可以运行多个 Pod，Pod是Kubernetes中最小的部署单位。每个Pod都由一个或多个容器组成，并共享相同的网络命名空间，以便它们可以相互通信。
</description>
        <pubDate>Sun, 23 Apr 2023 00:00:00 +0000</pubDate>
        <link>https://dinstone.github.io//2023/04/23/inside-kubernetes-architect/</link>
        <guid isPermaLink="true">https://dinstone.github.io//2023/04/23/inside-kubernetes-architect/</guid>
        
        <category>k8s</category>
        
        <category>docker</category>
        
        
        <category>k8s</category>
        
      </item>
    
      <item>
        <title>古董机做成Ubuntu服务器</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#背景&quot; id=&quot;markdown-toc-背景&quot;&gt;背景&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#安装过程&quot; id=&quot;markdown-toc-安装过程&quot;&gt;安装过程&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#系统设置&quot; id=&quot;markdown-toc-系统设置&quot;&gt;系统设置&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#1-wifi设置&quot; id=&quot;markdown-toc-1-wifi设置&quot;&gt;1. Wifi设置&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#2-cpu过高的问题&quot; id=&quot;markdown-toc-2-cpu过高的问题&quot;&gt;2. CPU过高的问题&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#3-时间设置&quot; id=&quot;markdown-toc-3-时间设置&quot;&gt;3. 时间设置&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#4-盒盖不睡眠&quot; id=&quot;markdown-toc-4-盒盖不睡眠&quot;&gt;4. 盒盖不睡眠&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#参考&quot; id=&quot;markdown-toc-参考&quot;&gt;参考&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;背景&quot;&gt;背景&lt;/h1&gt;

自从上次把我的古董笔记本电脑X201i升级后，一直安装的是Ubuntu桌面系统。今天突发奇想装个Gitlab玩玩，在安装docker镜像时发现，系统待机状况下CPU资源负载就高达23%左右，4G的内存仅剩1.5G了，这怎么行呢。

后来想反正以后也是当服务器用了，不如装个Linux的服务器版，鉴于Ubuntu的成功安装经验，所以直接下载Ubuntu的server版得了，省的再倒腾其他OS了，可没想到，安装过程也是一波三折，费了不少时间，不过结果是喜人的，谨记如下，分享之。

&lt;h1 id=&quot;安装过程&quot;&gt;安装过程&lt;/h1&gt;

&lt;ol&gt;
  &lt;li&gt;
    首先去官网下载最新的Ubuntu服务器版：ubuntu-22.04.2-live-server-amd64.iso。地址：https://ubuntu.com/download/server 。
  &lt;/li&gt;
  &lt;li&gt;
    使用Rufus制作Ubuntu的USB启动盘。Rufus很小，无需安装，随下随用，引导类型选择刚下载的ubuntu server ISO文件，其余默认即可，然后开始制作中，大约十分钟即可完成。也可以参考：https://zhuanlan.zhihu.com/p/498100251 。
  &lt;/li&gt;
  &lt;li&gt;
    有了USB的安装盘，安装过程就很简单了，一路可以默认，选择“done”就行。具体安装过程也可参考: https://blog.csdn.net/weixin_43824829/article/details/126975368 。
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h1 id=&quot;系统设置&quot;&gt;系统设置&lt;/h1&gt;

&lt;h2 id=&quot;1-wifi设置&quot;&gt;1. Wifi设置&lt;/h2&gt;

安装过程中，可以设置WiFi，如果跳过了设置，可以在系统安装完后进行设置。

&lt;ul&gt;
  &lt;li&gt;
    查看无线网卡名字

    ifconfig -a 或 ip a
  &lt;/li&gt;
  &lt;li&gt;
    安装WPA2加密工具

    sudo apt-get install wpasupplicant

    sudo apt-get install network-manager
  &lt;/li&gt;
  &lt;li&gt;
    修改wifi配置文件

    cd /etc/netplan/

    sudo vim /etc/netplan/00-intaller-config-wifi.yaml
  &lt;/li&gt;
  &lt;li&gt;
    wifi配置文件是yaml格式的，注意格式，给你的无线网卡“wlp2s0”添加如下内容SSID信息：
  &lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-yaml highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;na&quot;&gt;network&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;na&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;wifis&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;wlp2s0&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;optional&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;true&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;dhcp4&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;true&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;access-points&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
              &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;wifi&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;ssid&quot;&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;:&lt;/span&gt;
                  &lt;span class=&quot;na&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;wifi&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;密码&quot;&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;addresses&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;192.168.18.20/24&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;]&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;routes&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;pi&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;default&lt;/span&gt;
              &lt;span class=&quot;na&quot;&gt;via&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;192.168.18.1&lt;/span&gt;
          &lt;span class=&quot;na&quot;&gt;nameservers&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;na&quot;&gt;addresses&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;pi&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;1.1.1.1&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;8.8.8.8&lt;/span&gt;&lt;span class=&quot;pi&quot;&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;验证和应用设置&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    sudo netplan try
    sudo netplan apply
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;
    更多参考：

    &lt;blockquote&gt;
      https://blog.csdn.net/weixin_44244400/article/details/125116027
https://www.dandelioncloud.cn/article/details/1595000146184294401
    &lt;/blockquote&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;2-cpu过高的问题&quot;&gt;2. CPU过高的问题&lt;/h2&gt;

系统安装成功后，CPU负载和内存占用确实下来了，但是只要SSH一连接，CPU就飙升到20%以上，top查看发现是systemd-logind进程做怪，网上也有很多类似的问题。既然找到了罪魁后手，那就解决systemd-logind占用cpu过高的问题。参考网友的方法直接停止该服务就行：https://www.landui.com/help/show-3135

执行如下命令搞定：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;// 停止该服务

  sudo systemctl stop systemd-logind
  
// 永久停用该服务

  sudo systemctl mask systemd-logind
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;3-时间设置&quot;&gt;3. 时间设置&lt;/h2&gt;

ubuntu server设置时区和更新时间：

&lt;ul&gt;
  &lt;li&gt;
    查看时区

    date -R
  &lt;/li&gt;
  &lt;li&gt;
    调整时区

    sudo cp /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;4-盒盖不睡眠&quot;&gt;4. 盒盖不睡眠&lt;/h2&gt;

编辑配置文件：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo vim /etc/systemd/logind.conf
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

默认是这三种模式：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;HandleLidSwitch=suspend                     盒盖后挂起
HandleLidSwitchExternalPower=suspend        连接外接电源时，盒盖后挂起
HandleLidSwitchDocked=ignore                连接到扩展坞时，盒盖后无动作
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

修改为：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;HandleLidSwitch=ignore                 
HandleLidSwitchExternalPower=ignore
HandleLidSwitchDocked=ignore
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

如果需要，你可以根据自己的喜好将这些参数的值更改为其中之一：

​​suspend​​：合盖时挂起

​​lock​​：合盖时锁定

​​ignore​​：什么都不做

​​poweroff​​：关机

​​hibernate​​：合盖时休眠

&lt;h1 id=&quot;参考&quot;&gt;参考&lt;/h1&gt;

&lt;ul&gt;
  &lt;li&gt;
    https://blog.csdn.net/endswell/article/details/126656840
  &lt;/li&gt;
  &lt;li&gt;
    https://www.shuzhiduo.com/A/Ae5R4gR7zQ/
  &lt;/li&gt;
&lt;/ul&gt;
</description>
        <pubDate>Mon, 20 Mar 2023 00:00:00 +0000</pubDate>
        <link>https://dinstone.github.io//2023/03/20/x201-ubuntu-server/</link>
        <guid isPermaLink="true">https://dinstone.github.io//2023/03/20/x201-ubuntu-server/</guid>
        
        <category>ubuntu</category>
        
        
        <category>Ubuntu</category>
        
      </item>
    
      <item>
        <title>由浅入深构建 RPC 框架</title>
        <description>&lt;ul id=&quot;markdown-toc&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#rpc-功能目标&quot; id=&quot;markdown-toc-rpc-功能目标&quot;&gt;RPC 功能目标&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#rpc-调用过程&quot; id=&quot;markdown-toc-rpc-调用过程&quot;&gt;RPC 调用过程&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#rpc-架构设计&quot; id=&quot;markdown-toc-rpc-架构设计&quot;&gt;RPC 架构设计&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#rpc-实现设计&quot; id=&quot;markdown-toc-rpc-实现设计&quot;&gt;RPC 实现设计&lt;/a&gt;    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;#server组件设计&quot; id=&quot;markdown-toc-server组件设计&quot;&gt;Server组件设计&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#client组件设计&quot; id=&quot;markdown-toc-client组件设计&quot;&gt;Client组件设计&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#协议组件设计&quot; id=&quot;markdown-toc-协议组件设计&quot;&gt;协议组件设计&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#stub组件设计&quot; id=&quot;markdown-toc-stub组件设计&quot;&gt;Stub组件设计&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;#传输组件设计&quot; id=&quot;markdown-toc-传输组件设计&quot;&gt;传输组件设计&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#总结&quot; id=&quot;markdown-toc-总结&quot;&gt;总结&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;rpc-功能目标&quot;&gt;RPC 功能目标&lt;/h2&gt;

在构建高效能RPC框架之前，我们先了解一下RPC是什么，以及运行机制是怎样的？

很早的时候，为了简化使用其它联网主机上的应用程序，工程师们设计了一种能够通过A计算机调用B计算机上边应用程序的技术，这种技术不需要开发人员对于网络通讯了解过多，并且调用其他机器上边程序的时候和调用本地的程序一样方便好用。

现代微服务架构模式下，各服务部署在不同机器上，服务间的调用免不了网络通信过程，服务消费方每调用一个服务都要写一坨网络通信相关的代码，不仅复杂而且极易出错。如果有一种方式能让我们像调用本地服务一样调用远程服务，而让调用者对网络通信这些细节透明，那么将极大的提高生产力。这种方式的实现其实就是 RPC 框架，在各大互联网公司中被广泛使用，如阿里巴巴的 hsf、dubbo（开源）、Facebook 的 thrift（开源）、Google grpc（开源）、Twitter 的 finagle（开源）等。

由此可见，RPC（Remote Procedure Call）远程过程调用，像调用本地服务(方法)一样调用服务器的服务(方法)。其主要功能目标是让构建分布式计算（应用）更容易，在提供强大的远程调用能力时不损失本地调用的语义简洁性。为实现该目标，RPC 框架需提供一种透明调用机制让使用者不必显式的区分本地调用和远程调用。

&lt;h2 id=&quot;rpc-调用过程&quot;&gt;RPC 调用过程&lt;/h2&gt;

为了简化RPC调用，需要RPC框架对调用过程和通信细节进行封装，让网络通信细节对使用者透明。我们先看下一个 RPC 调用的流程涉及到哪些通信细节：

&lt;img src=&quot;https://dinstone.github.io//img/rpc/process.jpg&quot; alt=&quot;rpc-process&quot; /&gt;

&lt;ol&gt;
  &lt;li&gt;服务调用者（caller）从服务消费方（client）获取服务代理（stub），并以本地调用方式调用服务；&lt;/li&gt;
  &lt;li&gt;client stub 将服务名称、方法名称、参数等请求数据组装成能够进行网络传输的消息（message）；&lt;/li&gt;
  &lt;li&gt;client network 找到服务地址并构建连接，然后将请求消息序列化后发送到服务端；&lt;/li&gt;
  &lt;li&gt;server network 收到请求消息后进行反序列化，并将请求消息交给server stub处理；&lt;/li&gt;
  &lt;li&gt;server stub 收到请求消息后，查找对应的服务实现者（callee），并调用服务实现者的方法；&lt;/li&gt;
  &lt;li&gt;服务实现者（callee）注册到服务提供方（server），执行本地调用后将结果返回给 server stub；&lt;/li&gt;
  &lt;li&gt;server stub 将返回结果打包成能够进行网络传输的响应消息；&lt;/li&gt;
  &lt;li&gt;server network 将响应消息序列化后发送给消费端；&lt;/li&gt;
  &lt;li&gt;client stub 接收到响应消息，从响应消息中拿到结果；&lt;/li&gt;
  &lt;li&gt;服务调用者（caller）得到最终结果；&lt;/li&gt;
&lt;/ol&gt;

RPC框架的目标就是要 2~9 这些步骤都封装起来，让用户对这些细节透明。

&lt;h2 id=&quot;rpc-架构设计&quot;&gt;RPC 架构设计&lt;/h2&gt;
思考上面的调用过程，我们能够清晰的划分出RPC的职责边界，同时也能分析出RPC框架的架构主要包含以下几个核心组件：

&lt;img src=&quot;https://dinstone.github.io//img/rpc/logic.jpg&quot; alt=&quot;rpc-logic&quot; /&gt;

&lt;ul&gt;
  &lt;li&gt;Client Stub组件：负责收集请求参数和处理响应结果，需要明确远程服务的服务器地址，该组件为内部组件，需依赖协议组件的消息抽象。&lt;/li&gt;
  &lt;li&gt;Server Stub组件：负责调用真正的服务实现方法，需要明确服务接口的本地实现。该组件为内部组件，需依赖协议组件的消息抽象。&lt;/li&gt;
  &lt;li&gt;Client Network组件：负责管理到服务器的连接，对请求消息和响应消息进行编解码。该组件为内部组件，需依赖协议组件的序列化、压缩编码工具。&lt;/li&gt;
  &lt;li&gt;Server Network组件：负责接收来客户端的连接，对请求消息和响应消息进行编解码。该组件为内部组件，需依赖协议组件的序列化、压缩编码工具。&lt;/li&gt;
  &lt;li&gt;协议组件：负责定义请求、响应消息的RPC抽象，以及实现消息的序列化、压缩等编码方式。&lt;/li&gt;
  &lt;li&gt;Client组件：服务调用者（caller）开始消费服务前，首先要获得服务的代理实现（client stub）。所以该组件的职责就是创建和管理服务接口的代理实现，为服务调用者提供远程服务接口的引入能力，需要框架暴露对外的API。&lt;/li&gt;
  &lt;li&gt;Server组件：服务实现者（callee）对外提供服务前，需将其实现注册到一个地方，以方便Server Stub组件查找。所以该组件职责是管理服务的接口契约和接口实现的映射，为服务实现者提供远程服务接口的导出能力，需要框架暴露对外的API。&lt;/li&gt;
&lt;/ul&gt;

在分层架构的基础上，思考各组件运行机制，继续细化组件内外结构，可以得出如下运行架构图：

&lt;img src=&quot;https://dinstone.github.io//img/rpc/runtime.jpg&quot; alt=&quot;rpc-runtime&quot; /&gt;

&lt;ol&gt;
  &lt;li&gt;
    Server：负责导出（export）远程接口，将接口暴露出去供远程调用。

    服务实现者通常需要以某种形式提供服务调用的相关信息，包括但不限于服务接口定义、数据结构定义、数据序列化的服务定义文件。例如 Thrift、Protobuf 的 IDL 文件，Web service 的 WSDL 文件，服务调用者需要通过一定的途径获取远程服务调用的相关信息，才能正确发起调用。
  &lt;/li&gt;
  &lt;li&gt;
    Client：负责导入（import）远程接口的 stub 代理实现。

    目前，大部分跨语言平台 RPC 框架采用根据 IDL 定义，通过代码生成来实现 stub 代码，这种方式下实际导入的过程就是通过代码生成器在编译期完成的。代码生成的方式对跨语言平台 RPC 框架而言是必然的选择，而对于同一语言平台的 RPC 则可以通过共享接口来导入 stub 代理，这里的导入方式本质也是一种代码生成技术，只不过是在运行时生成，比静态编译期的代码生成看起来更简洁些。
  &lt;/li&gt;
  &lt;li&gt;
    Proxy：远程接口的代理实现。

    java 至少提供了两种技术来实现动态代码生成，一种是 jdk 动态代理，另外一种是字节码生成。 动态代理相比字节码生成使用起来更方便，但动态代理方式在性能上是要逊色于直接的字节码生成，而字节码生成在代码可读性上要差很多。两者权衡起来，个人认为牺牲一些性能来获得代码可读性和可维护性显得更重要。
  &lt;/li&gt;
  &lt;li&gt;
    Invoker：执行服务接口调用。

    &lt;ul&gt;
      &lt;li&gt;客户端实现：负责发送调用请求到服务端并等待调用结果返回。&lt;/li&gt;
      &lt;li&gt;服务端实现：负责调用服务端接口的具体实现并返回调用结果。&lt;/li&gt;
    &lt;/ul&gt;

    通常，这里也是服务治理的最佳实现位置，如服务发现、服务路由、负载均衡、服务重试、熔断限流。
  &lt;/li&gt;
  &lt;li&gt;
    Protocol：定义请求消息、响应消息，及对消息的编码（序列化、压缩）。

    &lt;ul&gt;
      &lt;li&gt;这里的请求消息、响应消息是RPC应用层的协议抽象，有别于传输层的数据格式，也是解耦传输层的设计。&lt;/li&gt;
      &lt;li&gt;序列化、压缩，是针对应用层消息对象与传输层字节数据之间的转换，跨语言的关键在消息的序列化方式。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;
    Connector：负责维持客户端和服务端的连接通道和发送数据到服务端。

    传输层协议是决定跨平台性的关键。可基于 HTTP 或 TCP 协议协议实现，Web Service 就是基于 HTTP 协议的 RPC，它具有良好的跨平台性，但其性能却不如基于 TCP 协议的 RPC。
  &lt;/li&gt;
  &lt;li&gt;
    Acceptor：负责接收客户端请求数据并返回响应结果。

    通常，基于TCP协议的传输层会保持长连接来提升性能，但是连接经过一些负载中转设备，有可能连接一段时间不活跃时会被这些中间设备中断。 为了保持连接有必要定时为每个连接发送心跳数据以维持连接不中断。

    心跳消息的设计应该限制在传输层，它对应用层应该保持透明。
  &lt;/li&gt;
  &lt;li&gt;
    Processor：负责在服务端控制调用过程，包括管理调用线程池、超时时间等。

    服务端的调用过程控制主要围绕性能展开（快速启动、快速执行、快速结束），可考虑3个因素：
    &lt;ul&gt;
      &lt;li&gt;效率提升：每个请求应该尽快被执行，因此我们不能每请求来再创建线程去执行，需要提供线程池服务。&lt;/li&gt;
      &lt;li&gt;资源隔离：当导出多个远程接口时，如何避免单一接口调用占据所有线程资源，而引发其他接口执行阻塞。&lt;/li&gt;
      &lt;li&gt;超时控制：当某个接口执行缓慢，而客户端已经超时放弃等待后，服务端的线程继续执行此时显得毫无意义。&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;rpc-实现设计&quot;&gt;RPC 实现设计&lt;/h2&gt;

&lt;img src=&quot;https://dinstone.github.io//img/rpc/layer.png&quot; alt=&quot;rpc-layer&quot; /&gt;

为了使RPC架构更灵活，便于以后功能扩展，我们需要考虑插件化架构，可以将每个功能点抽象成一个接口作为插件的契约，然后把这个功能的接口与实现分离，并提供接口的默认实现，满足大多数场景。

这样一来，我们的设计就遵循了开闭原则，用户可以非常方便地通过插件扩展实现自己的功能，而且不需要修改核心功能的本身；其次就是保持了核心包的精简，依赖外部包更少，可以有效减少开发人员引入 RPC 框架导致的包版本冲突问题。

&lt;h3 id=&quot;server组件设计&quot;&gt;Server组件设计&lt;/h3&gt;
Server组件具有导出服务接口的能力，将其能力抽象为ServiceProvider接口，同时创建接口实现Server类。
&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ServiceProvider&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;exporting&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;clazz&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;instance&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;destroy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;ServiceProvider&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;server&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Server&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4455&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt; 
&lt;span class=&quot;nc&quot;&gt;DemoService&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;demo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DemoServiceImpl&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;server&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;exporting&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;DemoService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;demo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;client组件设计&quot;&gt;Client组件设计&lt;/h3&gt;

通常 RPC 调用有以下两种方式：

&lt;ul&gt;
  &lt;li&gt;
    同步调用：客户端等待调用执行完成并返回结果。
  &lt;/li&gt;
  &lt;li&gt;
    异步调用：客户端调用后不用等待执行结果返回，但依然可以通过回调等方式获取返回结果。
  &lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;FooService&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;hello&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;register&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

Client组件的导入服务代理的能力抽象为ServiceConsumer接口，同时提供接口默认实现Client类。

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ServiceConsumer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;importing&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sic&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;destroy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;ServiceConsumer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;localhost&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4455&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt; 
&lt;span class=&quot;nc&quot;&gt;FooService&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fooService&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;importing&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;FooService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 同步调用&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reply&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fooService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;hello&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;dinstone&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reply&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// 异步调用&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fooService&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;register&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;dinstone&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;out&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;println&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;协议组件设计&quot;&gt;协议组件设计&lt;/h3&gt;
消息抽象定义了代表请求的Invocation类。序列化可以抽象为Serializer接口，压缩可以抽象为Compressor接口，方便不同算法实现扩展，当前流行的跨语言序列化实现有Json、Protobuf、Thrift等。

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Invocation&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;implements&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Serializable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;service&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parameter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Serializer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;cm&quot;&gt;/**
     * The serializer type
     *
     * @return
     */&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;serializerType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;?&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;contentType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;IOException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;contentBytes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;?&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;contentType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;IOException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Compressor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;cm&quot;&gt;/**
     * The compressor type
     *
     * @return
     */&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;compressorType&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;

    
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;IOException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;abstract&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;IOException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;stub组件设计&quot;&gt;Stub组件设计&lt;/h3&gt;
Server端Processor接口：
&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Processor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;process&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;MessageContext&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

Client端Proxy接口：
&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Proxy&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;proxy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Method&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Throwable&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

Invoker接口：
&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Invoker&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;invoke&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Invocation&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;传输组件设计&quot;&gt;传输组件设计&lt;/h3&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Acceptor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;bind&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;InetSocketAddress&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serviceAddress&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Processor&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;processor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;destroy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Connector&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;nc&quot;&gt;CompletableFuture&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Reply&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;InetSocketAddress&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;serviceAddress&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Call&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Exception&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;destroy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;总结&quot;&gt;总结&lt;/h2&gt;

至此我们输出了一个 RPC框架的基本架构，并详细分析了需要考虑的一些实现细节，尤其对跨语言、跨平台性实现做了深入设计，为框架的扩展性提供了基础。

&lt;ol&gt;
  &lt;li&gt;跨语言的关键是序列化的安全性、兼容性。&lt;/li&gt;
  &lt;li&gt;跨平台的关键是传输协议的通用性。&lt;/li&gt;
  &lt;li&gt;向下兼容的关键是API层的稳定性。&lt;/li&gt;
  &lt;li&gt;可扩展的关键是插件化的架构设计。&lt;/li&gt;
&lt;/ol&gt;
</description>
        <pubDate>Thu, 02 Feb 2023 00:00:00 +0000</pubDate>
        <link>https://dinstone.github.io//2023/02/02/rpc-build-inside/</link>
        <guid isPermaLink="true">https://dinstone.github.io//2023/02/02/rpc-build-inside/</guid>
        
        <category>RPC</category>
        
        <category>Focus</category>
        
        
        <category>RPC</category>
        
      </item>
    
  </channel>
</rss>
