<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>源码指南 on Apache Dubbo</title><link>https://deploy-preview-3229--dubbo.netlify.app/zh-cn/overview/mannual/golang-sdk/sourcecode/</link><description>Recent content in 源码指南 on Apache Dubbo</description><generator>Hugo</generator><language>zh-cn</language><atom:link href="https://deploy-preview-3229--dubbo.netlify.app/zh-cn/overview/mannual/golang-sdk/sourcecode/index.xml" rel="self" type="application/rss+xml"/><item><title>架构</title><link>https://deploy-preview-3229--dubbo.netlify.app/zh-cn/overview/mannual/golang-sdk/sourcecode/architecture/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://deploy-preview-3229--dubbo.netlify.app/zh-cn/overview/mannual/golang-sdk/sourcecode/architecture/</guid><description>&lt;p>相关示例：&lt;/p>
&lt;ul>
&lt;li>快速开始 RPC 应用：&lt;a href="https://github.com/apache/dubbo-go-samples/tree/main/helloworld" target="_blank">dubbo-go-samples/helloworld&lt;/a>&lt;/li>
&lt;li>基于注册中心的微服务应用：&lt;a href="https://github.com/apache/dubbo-go-samples/tree/main/registry/nacos" target="_blank">dubbo-go-samples/registry/nacos&lt;/a>&lt;/li>
&lt;li>多协议应用：&lt;a href="https://github.com/apache/dubbo-go-samples/tree/main/rpc/multi-protocols" target="_blank">dubbo-go-samples/rpc/multi-protocols&lt;/a>&lt;/li>
&lt;/ul>
&lt;p>Dubbo Go 当前围绕几个运行时概念组织：应用实例、provider、consumer、protocol、registry、metadata 和治理扩展。现在的应用通常通过 &lt;code>dubbo.go&lt;/code>、&lt;code>server&lt;/code>、&lt;code>client&lt;/code> 中的 API 入口构建；使用配置文件的场景仍然会走 &lt;code>config.RootConfig&lt;/code>。&lt;/p>
&lt;h2 id="主要运行层次">主要运行层次&lt;/h2>
&lt;table>
 &lt;thead>
 &lt;tr>
 &lt;th style="text-align: left">层次&lt;/th>
 &lt;th style="text-align: left">主要包&lt;/th>
 &lt;th style="text-align: left">职责&lt;/th>
 &lt;/tr>
 &lt;/thead>
 &lt;tbody>
 &lt;tr>
 &lt;td style="text-align: left">应用实例&lt;/td>
 &lt;td style="text-align: left">&lt;code>dubbo.go&lt;/code>, &lt;code>options.go&lt;/code>&lt;/td>
 &lt;td style="text-align: left">持有 application、protocol、registry、metadata、metrics、tracing、router、shutdown 等选项。&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td style="text-align: left">Provider API&lt;/td>
 &lt;td style="text-align: left">&lt;code>server&lt;/code>&lt;/td>
 &lt;td style="text-align: left">注册 service handler，构造 service options，创建 invoker 并暴露服务。&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td style="text-align: left">Consumer API&lt;/td>
 &lt;td style="text-align: left">&lt;code>client&lt;/code>&lt;/td>
 &lt;td style="text-align: left">创建 client，引用服务，创建生成代码服务或泛化服务，并发起远程调用。&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td style="text-align: left">Protocol&lt;/td>
 &lt;td style="text-align: left">&lt;code>protocol/base&lt;/code>, &lt;code>protocol/triple&lt;/code>, &lt;code>protocol/dubbo&lt;/code>, &lt;code>protocol/rest&lt;/code>&lt;/td>
 &lt;td style="text-align: left">Provider 侧将 invoker 转为网络服务，consumer 侧将 invoker 转为网络调用。&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td style="text-align: left">Registry 与发现&lt;/td>
 &lt;td style="text-align: left">&lt;code>registry&lt;/code>, &lt;code>registry/protocol&lt;/code>, &lt;code>registry/servicediscovery&lt;/code>, &lt;code>registry/directory&lt;/code>&lt;/td>
 &lt;td style="text-align: left">负责注册、订阅、服务发现和注册中心驱动的调用。&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td style="text-align: left">Metadata&lt;/td>
 &lt;td style="text-align: left">&lt;code>metadata&lt;/code>, &lt;code>metadata/report&lt;/code>, &lt;code>metadata/mapping&lt;/code>&lt;/td>
 &lt;td style="text-align: left">负责服务定义、service-to-application 映射和 metadata 上报。&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td style="text-align: left">治理扩展&lt;/td>
 &lt;td style="text-align: left">&lt;code>filter&lt;/code>, &lt;code>cluster&lt;/code>, &lt;code>cluster/router&lt;/code>, &lt;code>cluster/loadbalance&lt;/code>, &lt;code>common/extension&lt;/code>&lt;/td>
 &lt;td style="text-align: left">提供 filter、router、load balance、cluster 策略和扩展注册。&lt;/td>
 &lt;/tr>
 &lt;/tbody>
&lt;/table>
&lt;h2 id="provider-流程">Provider 流程&lt;/h2>
&lt;p>当前 server API 下，provider 通常会经历以下流程：&lt;/p></description></item><item><title>网络协议</title><link>https://deploy-preview-3229--dubbo.netlify.app/zh-cn/overview/mannual/golang-sdk/sourcecode/protocol/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://deploy-preview-3229--dubbo.netlify.app/zh-cn/overview/mannual/golang-sdk/sourcecode/protocol/</guid><description>&lt;p>相关示例：&lt;/p>
&lt;ul>
&lt;li>Triple RPC：&lt;a href="https://github.com/apache/dubbo-go-samples/tree/main/rpc/triple" target="_blank">dubbo-go-samples/rpc/triple&lt;/a>&lt;/li>
&lt;li>多协议服务：&lt;a href="https://github.com/apache/dubbo-go-samples/tree/main/rpc/multi-protocols" target="_blank">dubbo-go-samples/rpc/multi-protocols&lt;/a>&lt;/li>
&lt;li>gRPC 互通：&lt;a href="https://github.com/apache/dubbo-go-samples/tree/main/rpc/grpc" target="_blank">dubbo-go-samples/rpc/grpc&lt;/a>&lt;/li>
&lt;/ul>
&lt;p>在当前 Dubbo Go 中，protocol 层是 invocation 模型和底层网络协议之间的桥梁。它一方面负责把 provider 暴露成网络服务，另一方面负责把 consumer 侧的引用转换成可调用的远程 invoker。&lt;/p>
&lt;h2 id="核心协议抽象">核心协议抽象&lt;/h2>
&lt;p>当前协议接口定义在 &lt;code>protocol/base/base_protocol.go&lt;/code>：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#93a1a1;background-color:#002b36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-go" data-lang="go">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#268bd2">type&lt;/span> Protocol &lt;span style="color:#268bd2">interface&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>	&lt;span style="color:#268bd2">Export&lt;/span>(invoker Invoker) Exporter
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>	&lt;span style="color:#268bd2">Refer&lt;/span>(url &lt;span style="color:#719e07">*&lt;/span>common.URL) Invoker
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>	&lt;span style="color:#268bd2">Destroy&lt;/span>()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>它的职责很直接：&lt;/p>
&lt;ul>
&lt;li>&lt;code>Export&lt;/code>：把 provider 侧 invoker 暴露成网络服务&lt;/li>
&lt;li>&lt;code>Refer&lt;/code>：为目标 URL 创建 consumer 侧 invoker&lt;/li>
&lt;li>&lt;code>Destroy&lt;/code>：销毁该协议持有的 invoker 和 exporter&lt;/li>
&lt;/ul>
&lt;p>此外还有一个可选能力：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#93a1a1;background-color:#002b36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-go" data-lang="go">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#268bd2">type&lt;/span> RegistryUnregisterer &lt;span style="color:#268bd2">interface&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>	&lt;span style="color:#268bd2">UnregisterRegistries&lt;/span>()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>它主要用于优雅停机阶段，让协议在完整 teardown 之前，先把已经暴露出去的服务从注册中心摘除。&lt;/p>
&lt;h2 id="provider-侧流程">Provider 侧流程&lt;/h2>
&lt;p>在 provider 侧，Dubbo Go 通常会经过下面这条链路：&lt;/p>
&lt;ol>
&lt;li>&lt;code>server.Register(...)&lt;/code> 或 &lt;code>server.RegisterService(...)&lt;/code> 创建 service metadata 和 invoker。&lt;/li>
&lt;li>&lt;code>ServiceOptions.Export()&lt;/code> 选择 protocol 和 registry 行为。&lt;/li>
&lt;li>具体协议实现调用 &lt;code>Export(invoker)&lt;/code>。&lt;/li>
&lt;li>暴露后的服务开始监听配置好的网络端点。&lt;/li>
&lt;/ol>
&lt;p>不同协议提供不同的传输层和编解码方式，但都会遵循同一个 &lt;code>Export&lt;/code> 契约。&lt;/p></description></item><item><title>注册中心</title><link>https://deploy-preview-3229--dubbo.netlify.app/zh-cn/overview/mannual/golang-sdk/sourcecode/registry/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://deploy-preview-3229--dubbo.netlify.app/zh-cn/overview/mannual/golang-sdk/sourcecode/registry/</guid><description>&lt;p>相关示例：&lt;/p>
&lt;ul>
&lt;li>Nacos 注册中心：&lt;a href="https://github.com/apache/dubbo-go-samples/tree/main/registry/nacos" target="_blank">dubbo-go-samples/registry/nacos&lt;/a>&lt;/li>
&lt;li>Zookeeper 注册中心：&lt;a href="https://github.com/apache/dubbo-go-samples/tree/main/registry/zookeeper" target="_blank">dubbo-go-samples/registry/zookeeper&lt;/a>&lt;/li>
&lt;li>应用级服务发现：&lt;a href="https://github.com/apache/dubbo-go-samples/tree/main/registry/nacos" target="_blank">dubbo-go-samples/registry/nacos&lt;/a>&lt;/li>
&lt;/ul>
&lt;p>相关文档：&lt;/p>
&lt;ul>
&lt;li>&lt;a href="https://deploy-preview-3229--dubbo.netlify.app/zh-cn/overview/mannual/golang-sdk/tutorial/service-discovery/application-level-service-discovery/">应用级服务发现&lt;/a>&lt;/li>
&lt;/ul>
&lt;p>在当前 Dubbo Go 中，registry 层已经不只是“接口级地址注册”这么简单。它同时参与应用级服务发现、metadata 初始化、订阅刷新，以及基于注册中心的协议暴露流程。&lt;/p>
&lt;h2 id="核心-registry-接口">核心 Registry 接口&lt;/h2>
&lt;p>主接口定义在 &lt;code>registry/registry.go&lt;/code>：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#93a1a1;background-color:#002b36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-go" data-lang="go">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#268bd2">type&lt;/span> Registry &lt;span style="color:#268bd2">interface&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>	common.Node
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>	&lt;span style="color:#268bd2">Register&lt;/span>(url &lt;span style="color:#719e07">*&lt;/span>common.URL) &lt;span style="color:#dc322f">error&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>	&lt;span style="color:#268bd2">UnRegister&lt;/span>(url &lt;span style="color:#719e07">*&lt;/span>common.URL) &lt;span style="color:#dc322f">error&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>	&lt;span style="color:#268bd2">Subscribe&lt;/span>(&lt;span style="color:#719e07">*&lt;/span>common.URL, NotifyListener) &lt;span style="color:#dc322f">error&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>	&lt;span style="color:#268bd2">UnSubscribe&lt;/span>(&lt;span style="color:#719e07">*&lt;/span>common.URL, NotifyListener) &lt;span style="color:#dc322f">error&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>	&lt;span style="color:#268bd2">LoadSubscribeInstances&lt;/span>(&lt;span style="color:#719e07">*&lt;/span>common.URL, NotifyListener) &lt;span style="color:#dc322f">error&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>和旧版说明相比，一个明显变化是 &lt;code>LoadSubscribeInstances(...)&lt;/code> 已经进入接口契约。原因是订阅本身是异步的，Dubbo Go 在正式进入异步通知流之前，可能需要先同步加载一次初始实例列表。&lt;/p>
&lt;h2 id="当前职责范围">当前职责范围&lt;/h2>
&lt;p>现在 registry 层覆盖了几类紧密相关的职责：&lt;/p>
&lt;ul>
&lt;li>provider 注册与反注册&lt;/li>
&lt;li>consumer 订阅与取消订阅&lt;/li>
&lt;li>异步更新前的初始实例加载&lt;/li>
&lt;li>应用级服务发现&lt;/li>
&lt;li>与 registry 绑定的 metadata 上报&lt;/li>
&lt;/ul>
&lt;p>从源码目录看，相关主包包括：&lt;/p>
&lt;ul>
&lt;li>&lt;code>registry&lt;/code>&lt;/li>
&lt;li>&lt;code>registry/protocol&lt;/code>&lt;/li>
&lt;li>&lt;code>registry/servicediscovery&lt;/code>&lt;/li>
&lt;li>&lt;code>registry/directory&lt;/code>&lt;/li>
&lt;/ul>
&lt;h2 id="注册模式">注册模式&lt;/h2>
&lt;p>当前 Dubbo Go 通过 &lt;code>registry/options.go&lt;/code> 支持多种注册模型：&lt;/p>
&lt;ul>
&lt;li>&lt;code>registry.WithRegisterService()&lt;/code>&lt;/li>
&lt;li>&lt;code>registry.WithRegisterInterface()&lt;/code>&lt;/li>
&lt;li>&lt;code>registry.WithRegisterServiceAndInterface()&lt;/code>&lt;/li>
&lt;/ul>
&lt;p>它们分别对应：&lt;/p>
&lt;ul>
&lt;li>&lt;code>service&lt;/code>：注册应用级实例&lt;/li>
&lt;li>&lt;code>interface&lt;/code>：注册接口级 provider URL&lt;/li>
&lt;li>&lt;code>all&lt;/code>：迁移阶段同时注册两种模型&lt;/li>
&lt;/ul>
&lt;p>这也是它和早期文档的一个关键差异。旧文档大多只讲了接口级注册。&lt;/p></description></item><item><title>3.x 核心能力</title><link>https://deploy-preview-3229--dubbo.netlify.app/zh-cn/overview/mannual/golang-sdk/sourcecode/3.0_feature/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://deploy-preview-3229--dubbo.netlify.app/zh-cn/overview/mannual/golang-sdk/sourcecode/3.0_feature/</guid><description>&lt;p>相关示例：&lt;/p>
&lt;ul>
&lt;li>Triple 协议：&lt;a href="https://github.com/apache/dubbo-go-samples/tree/main/rpc/triple" target="_blank">dubbo-go-samples/rpc/triple&lt;/a>&lt;/li>
&lt;li>基于 Nacos 的应用级服务发现：&lt;a href="https://github.com/apache/dubbo-go-samples/tree/main/registry/nacos" target="_blank">dubbo-go-samples/registry/nacos&lt;/a>&lt;/li>
&lt;li>Service Mesh 部署：&lt;a href="https://github.com/apache/dubbo-go-samples/tree/main/mesh" target="_blank">dubbo-go-samples/mesh&lt;/a>&lt;/li>
&lt;/ul>
&lt;p>Dubbo Go 3.x 的核心能力已经围绕 Triple 协议、应用级服务发现、metadata 驱动的服务自省，以及 SDK 侧治理能力展开。这些内容不再只是 3.0 版本的新特性，而是当前 Dubbo Go 应用的常规开发模型。&lt;/p>
&lt;h2 id="triple-协议">Triple 协议&lt;/h2>
&lt;p>Triple 是 Dubbo Go 3.x 推荐使用的 RPC 协议，主要实现位于：&lt;/p>
&lt;ul>
&lt;li>&lt;code>protocol/triple&lt;/code>&lt;/li>
&lt;li>&lt;code>protocol/triple/triple_protocol&lt;/code>&lt;/li>
&lt;li>&lt;code>tools/protoc-gen-go-triple&lt;/code>&lt;/li>
&lt;/ul>
&lt;p>Triple 基于 HTTP/1.1 和 HTTP/2 语义，并在线路协议上兼容 gRPC。对于 protobuf 服务，用户先定义 &lt;code>.proto&lt;/code> 文件，再通过 &lt;code>protoc-gen-go&lt;/code> 和 &lt;code>protoc-gen-go-triple&lt;/code> 生成 Go 代码，最后将生成的 handler 注册到 Dubbo Go server。&lt;/p>
&lt;p>运行时链路遵循 Dubbo 通用协议抽象：&lt;/p>
&lt;ul>
&lt;li>provider 侧：&lt;code>server.Register(...)&lt;/code> 构造 service options 和 invoker，再通过协议 &lt;code>Export&lt;/code> 暴露服务；&lt;/li>
&lt;li>consumer 侧：生成的 client 调用 &lt;code>client.Connection&lt;/code>，由它构造 invocation 并委托给协议 &lt;code>Refer&lt;/code> 返回的 invoker；&lt;/li>
&lt;li>request header、response header、trailer 等 metadata 通过 Triple metadata API 暴露。&lt;/li>
&lt;/ul>
&lt;h2 id="应用级服务发现">应用级服务发现&lt;/h2>
&lt;p>应用级服务发现是当前 Dubbo Go 默认的服务发现模型。provider 注册应用实例和 metadata；consumer 解析 service 到 application 的映射，再通过 metadata 还原可调用的服务 URL。&lt;/p></description></item><item><title>泛化调用</title><link>https://deploy-preview-3229--dubbo.netlify.app/zh-cn/overview/mannual/golang-sdk/sourcecode/generic/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://deploy-preview-3229--dubbo.netlify.app/zh-cn/overview/mannual/golang-sdk/sourcecode/generic/</guid><description>&lt;p>相关示例：&lt;/p>
&lt;ul>
&lt;li>泛化调用示例：&lt;a href="https://github.com/apache/dubbo-go-samples/tree/main/generic" target="_blank">dubbo-go-samples/generic&lt;/a>&lt;/li>
&lt;li>Triple 示例：&lt;a href="https://github.com/apache/dubbo-go-samples/tree/main/rpc/triple" target="_blank">dubbo-go-samples/rpc/triple&lt;/a>&lt;/li>
&lt;/ul>
&lt;p>当前 Dubbo Go 的泛化调用主要围绕 &lt;code>client.NewGenericService(...)&lt;/code> 和 &lt;code>filter/generic.GenericService&lt;/code> 展开。早期基于 &lt;code>ReferenceConfigBuilder&lt;/code>、&lt;code>GenericLoad()&lt;/code>、&lt;code>GetRPCService()&lt;/code> 的示例属于兼容旧 API 的写法，已经不是推荐入口。&lt;/p>
&lt;h2 id="核心-api">核心 API&lt;/h2>
&lt;p>主要运行时入口在 &lt;code>client/client.go&lt;/code>：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#93a1a1;background-color:#002b36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-go" data-lang="go">&lt;span style="display:flex;">&lt;span>genericService, err &lt;span style="color:#719e07">:=&lt;/span> cli.&lt;span style="color:#268bd2">NewGenericService&lt;/span>(
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>	&lt;span style="color:#2aa198">&amp;#34;org.apache.dubbo.samples.UserProvider&amp;#34;&lt;/span>,
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>	client.&lt;span style="color:#268bd2">WithProtocolTriple&lt;/span>(),
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>	client.&lt;span style="color:#268bd2">WithSerialization&lt;/span>(constant.Hessian2Serialization),
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>	client.&lt;span style="color:#268bd2">WithURL&lt;/span>(&lt;span style="color:#2aa198">&amp;#34;tri://127.0.0.1:20000&amp;#34;&lt;/span>),
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>)
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>&lt;code>NewGenericService(...)&lt;/code> 默认会做三件事：&lt;/p>
&lt;ul>
&lt;li>设置 &lt;code>NONIDL&lt;/code>&lt;/li>
&lt;li>打开 generic invocation&lt;/li>
&lt;li>使用 &lt;code>hessian2&lt;/code> 序列化&lt;/li>
&lt;/ul>
&lt;p>它内部会创建 &lt;code>filter/generic.GenericService&lt;/code>，然后接入 Dubbo Go 正常的 reference 和 invocation 流程。&lt;/p>
&lt;h2 id="genericservice">GenericService&lt;/h2>
&lt;p>核心类型如下：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#93a1a1;background-color:#002b36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-go" data-lang="go">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#268bd2">type&lt;/span> GenericService &lt;span style="color:#268bd2">struct&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>	Invoke &lt;span style="color:#268bd2">func&lt;/span>(ctx context.Context, methodName &lt;span style="color:#dc322f">string&lt;/span>, types []&lt;span style="color:#dc322f">string&lt;/span>, args []hessian.Object) (any, &lt;span style="color:#dc322f">error&lt;/span>) &lt;span style="color:#2aa198">`dubbo:&amp;#34;$invoke&amp;#34;`&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>它和 Dubbo 的泛化调用模型保持一致：调用方提供：&lt;/p>
&lt;ul>
&lt;li>方法名&lt;/li>
&lt;li>参数类型名列表&lt;/li>
&lt;li>参数值列表&lt;/li>
&lt;/ul>
&lt;p>框架再把这些内容编码成对远端服务的 &lt;code>$invoke&lt;/code> 请求。&lt;/p></description></item><item><title>泛化调用兼容说明</title><link>https://deploy-preview-3229--dubbo.netlify.app/zh-cn/overview/mannual/golang-sdk/sourcecode/generic-2/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://deploy-preview-3229--dubbo.netlify.app/zh-cn/overview/mannual/golang-sdk/sourcecode/generic-2/</guid><description>&lt;p>相关示例：&lt;/p>
&lt;ul>
&lt;li>泛化调用示例：&lt;a href="https://github.com/apache/dubbo-go-samples/tree/main/generic" target="_blank">dubbo-go-samples/generic&lt;/a>&lt;/li>
&lt;/ul>
&lt;p>这个页面主要保留给旧链接兼容使用。&lt;/p>
&lt;p>当前 Dubbo Go 请优先阅读&lt;a href="https://deploy-preview-3229--dubbo.netlify.app/zh-cn/overview/mannual/golang-sdk/sourcecode/generic/">泛化调用&lt;/a>。旧示例里使用的：&lt;/p>
&lt;ul>
&lt;li>&lt;code>config.NewReferenceConfigBuilder()&lt;/code>&lt;/li>
&lt;li>&lt;code>GenericLoad()&lt;/code>&lt;/li>
&lt;li>&lt;code>GetRPCService()&lt;/code>&lt;/li>
&lt;/ul>
&lt;p>属于早期以配置对象为中心的 API 路径。现在更推荐的运行时入口是：&lt;/p>
&lt;ul>
&lt;li>&lt;code>client.NewGenericService(...)&lt;/code>&lt;/li>
&lt;li>&lt;code>filter/generic.GenericService.Invoke(...)&lt;/code>&lt;/li>
&lt;li>&lt;code>filter/generic.GenericService.InvokeWithType(...)&lt;/code>&lt;/li>
&lt;/ul>
&lt;p>如果你要找一个可以直接参考的完整示例，优先看 &lt;code>dubbo-go-samples/generic&lt;/code>。&lt;/p></description></item><item><title>AOP 与可扩展机制</title><link>https://deploy-preview-3229--dubbo.netlify.app/zh-cn/overview/mannual/golang-sdk/sourcecode/aop_and_extension/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://deploy-preview-3229--dubbo.netlify.app/zh-cn/overview/mannual/golang-sdk/sourcecode/aop_and_extension/</guid><description>&lt;p>相关示例：&lt;/p>
&lt;ul>
&lt;li>自定义 filter：&lt;a href="https://github.com/apache/dubbo-go-samples/tree/main/filter/custom" target="_blank">dubbo-go-samples/filter/custom&lt;/a>&lt;/li>
&lt;li>条件路由：&lt;a href="https://github.com/apache/dubbo-go-samples/tree/main/router/condition" target="_blank">dubbo-go-samples/router/condition&lt;/a>&lt;/li>
&lt;li>标签路由：&lt;a href="https://github.com/apache/dubbo-go-samples/tree/main/router/tag" target="_blank">dubbo-go-samples/router/tag&lt;/a>&lt;/li>
&lt;/ul>
&lt;p>Dubbo Go 通过 Go 包初始化和统一的扩展注册中心，来承载大部分可插拔能力。filter、router、protocol、registry、load balance、cluster 策略、tracing exporter、metadata report 等能力，都是按这个模型加载的。&lt;/p>
&lt;h2 id="extension-注册中心">extension 注册中心&lt;/h2>
&lt;p>当前实现位于 &lt;code>common/extension&lt;/code>。它已经不再依赖每个类型单独维护的原始 map，而是统一使用泛型注册中心：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#93a1a1;background-color:#002b36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-go" data-lang="go">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#268bd2">var&lt;/span> loadbalances = NewRegistry[&lt;span style="color:#268bd2">func&lt;/span>() loadbalance.LoadBalance](&lt;span style="color:#2aa198">&amp;#34;loadbalance&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#268bd2">func&lt;/span> &lt;span style="color:#268bd2">SetLoadbalance&lt;/span>(name &lt;span style="color:#dc322f">string&lt;/span>, fcn &lt;span style="color:#268bd2">func&lt;/span>() loadbalance.LoadBalance) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>	loadbalances.&lt;span style="color:#268bd2">Register&lt;/span>(name, fcn)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#268bd2">func&lt;/span> &lt;span style="color:#268bd2">GetLoadbalance&lt;/span>(name &lt;span style="color:#dc322f">string&lt;/span>) loadbalance.LoadBalance {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>	&lt;span style="color:#719e07">return&lt;/span> loadbalances.&lt;span style="color:#268bd2">MustGet&lt;/span>(name)()
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>同样的模式也用在：&lt;/p>
&lt;ul>
&lt;li>&lt;code>common/extension/filter.go&lt;/code>&lt;/li>
&lt;li>&lt;code>common/extension/protocol.go&lt;/code>&lt;/li>
&lt;li>&lt;code>common/extension/registry.go&lt;/code>&lt;/li>
&lt;li>&lt;code>common/extension/router_factory.go&lt;/code>&lt;/li>
&lt;/ul>
&lt;p>这样做之后，Dubbo Go 的扩展注册模型更统一，也支持某些类别上的注销、列举已注册名称等辅助能力。&lt;/p>
&lt;h2 id="init-的作用">&lt;code>init()&lt;/code> 的作用&lt;/h2>
&lt;p>内置实现通常都会在自己的包里通过 &lt;code>init()&lt;/code> 完成注册：&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#93a1a1;background-color:#002b36;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-go" data-lang="go">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#268bd2">func&lt;/span> &lt;span style="color:#268bd2">init&lt;/span>() {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>	extension.&lt;span style="color:#268bd2">SetLoadbalance&lt;/span>(constant.LoadBalanceKeyRandom, NewRandomLoadBalance)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>运行时里很多能力都遵循这个模式：&lt;/p>
&lt;ul>
&lt;li>&lt;code>protocol/triple/triple.go&lt;/code> 注册 Triple&lt;/li>
&lt;li>&lt;code>protocol/dubbo/dubbo_protocol.go&lt;/code> 注册 Dubbo&lt;/li>
&lt;li>&lt;code>protocol/rest/rest_protocol.go&lt;/code> 注册 REST&lt;/li>
&lt;li>&lt;code>registry/nacos/registry.go&lt;/code>、&lt;code>registry/zookeeper/registry.go&lt;/code>、&lt;code>registry/etcdv3/registry.go&lt;/code> 注册 registry&lt;/li>
&lt;li>&lt;code>filter/generic/filter.go&lt;/code>、&lt;code>filter/metrics/filter.go&lt;/code>、&lt;code>filter/graceful_shutdown/...&lt;/code> 注册 filter&lt;/li>
&lt;li>&lt;code>cluster/router/condition/factory.go&lt;/code>、&lt;code>cluster/router/tag/factory.go&lt;/code>、&lt;code>cluster/router/script/factory.go&lt;/code> 注册 router factory&lt;/li>
&lt;/ul>
&lt;p>只要这些包被导入，框架后续就能按名称查找到对应实现。&lt;/p></description></item><item><title>应用和接口</title><link>https://deploy-preview-3229--dubbo.netlify.app/zh-cn/overview/mannual/golang-sdk/sourcecode/app_and_interface/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://deploy-preview-3229--dubbo.netlify.app/zh-cn/overview/mannual/golang-sdk/sourcecode/app_and_interface/</guid><description>&lt;p>相关示例：&lt;/p>
&lt;ul>
&lt;li>Hello world 应用：&lt;a href="https://github.com/apache/dubbo-go-samples/tree/main/helloworld" target="_blank">dubbo-go-samples/helloworld&lt;/a>&lt;/li>
&lt;li>基于 YAML 的配置：&lt;a href="https://github.com/apache/dubbo-go-samples/tree/main/config_yaml" target="_blank">dubbo-go-samples/config_yaml&lt;/a>&lt;/li>
&lt;li>基于注册中心的应用：&lt;a href="https://github.com/apache/dubbo-go-samples/tree/main/registry/nacos" target="_blank">dubbo-go-samples/registry/nacos&lt;/a>&lt;/li>
&lt;/ul>
&lt;p>当前 Dubbo Go 依然保留“应用级配置”和“接口级服务引用”这两个层次，但它的运行时模型已经比早期版本里那种简单的“application vs interface”说明更完整。现在真正的源码入口，是 &lt;code>options.go&lt;/code> 里的应用实例选项，以及兼容配置文件模式的 &lt;code>config/root_config.go&lt;/code>。&lt;/p>
&lt;h2 id="应用级配置">应用级配置&lt;/h2>
&lt;p>应用级配置会被同一个 Dubbo Go 进程里的所有服务和引用共享。&lt;/p>
&lt;p>在配置文件模式下，这套模型由 &lt;code>config.RootConfig&lt;/code> 表达，主要字段包括：&lt;/p>
&lt;ul>
&lt;li>&lt;code>Application&lt;/code>&lt;/li>
&lt;li>&lt;code>Protocols&lt;/code>&lt;/li>
&lt;li>&lt;code>Registries&lt;/code>&lt;/li>
&lt;li>&lt;code>ConfigCenter&lt;/code>&lt;/li>
&lt;li>&lt;code>MetadataReport&lt;/code>&lt;/li>
&lt;li>&lt;code>Provider&lt;/code>&lt;/li>
&lt;li>&lt;code>Consumer&lt;/code>&lt;/li>
&lt;li>&lt;code>Otel&lt;/code>&lt;/li>
&lt;li>&lt;code>Metrics&lt;/code>&lt;/li>
&lt;li>&lt;code>Tracing&lt;/code>&lt;/li>
&lt;li>&lt;code>Logger&lt;/code>&lt;/li>
&lt;li>&lt;code>Shutdown&lt;/code>&lt;/li>
&lt;li>&lt;code>Router&lt;/code>&lt;/li>
&lt;li>&lt;code>TLSConfig&lt;/code>&lt;/li>
&lt;/ul>
&lt;p>在 API 模式下，对应的是 &lt;code>options.go&lt;/code> 里的 &lt;code>InstanceOptions&lt;/code>，它通常通过下面这些 helper 填充：&lt;/p>
&lt;ul>
&lt;li>&lt;code>dubbo.WithProtocol(...)&lt;/code>&lt;/li>
&lt;li>&lt;code>dubbo.WithRegistry(...)&lt;/code>&lt;/li>
&lt;li>&lt;code>dubbo.WithConfigCenter(...)&lt;/code>&lt;/li>
&lt;li>&lt;code>dubbo.WithMetadataReport(...)&lt;/code>&lt;/li>
&lt;li>&lt;code>dubbo.WithMetrics(...)&lt;/code>&lt;/li>
&lt;li>&lt;code>dubbo.WithTracing(...)&lt;/code>&lt;/li>
&lt;li>&lt;code>dubbo.WithShutdown(...)&lt;/code>&lt;/li>
&lt;li>&lt;code>dubbo.WithRouter(...)&lt;/code>&lt;/li>
&lt;li>&lt;code>dubbo.WithRemoteMetadata()&lt;/code>&lt;/li>
&lt;li>&lt;code>dubbo.WithMetadataServiceProtocol(...)&lt;/code>&lt;/li>
&lt;/ul>
&lt;p>这些选项描述的是整个应用实例的运行环境，而不是某一个单独服务。&lt;/p>
&lt;h2 id="接口级配置">接口级配置&lt;/h2>
&lt;p>接口级配置则绑定在单个暴露服务或单个远程引用上。&lt;/p>
&lt;p>在 provider 侧，核心单位是注册出来的 service：&lt;/p>
&lt;ul>
&lt;li>&lt;code>server.Register(...)&lt;/code>&lt;/li>
&lt;li>&lt;code>server.RegisterService(...)&lt;/code>&lt;/li>
&lt;li>&lt;code>server.ServiceOptions&lt;/code>&lt;/li>
&lt;/ul>
&lt;p>这一层承载接口名、实现 handler、protocol ID、registry ID、序列化方式，以及服务 metadata 等信息。&lt;/p></description></item></channel></rss>