Alanxtl opened a new issue, #3672:
URL: https://github.com/apache/dubbo-go/issues/3672

   ## 背景
   
   当前 dubbo-go extension 的配置方式没有统一接口。以 Hystrix 为例,用户需要同时:
   
   - 通过副作用 import 注册 filter;
   - 直接依赖 `github.com/afex/hystrix-go/hystrix` 调用 `ConfigureCommand`;
   - 手工传入 `client.WithFilter("hystrix_consumer")`;
   - Provider 侧手工传入 `server.WithFilter("hystrix_provider")`;
   - 使用 `dubbo.Load()` 时,无法通过 Dubbo YAML 传入 Hystrix 配置。
   
   `hystrix_consumer` / `hystrix_provider` 等名称属于扩展内部实现细节,不应该要求用户感知。
   
   ## 目标
   
   建立 dubbo-go 与 dubbo-go-extensions 之间统一的扩展配置/option 协议:
   
   1. 扩展可以提供自己的 `WithXXX` typed options;
   2. `client.NewClient` 和 `server.NewServer` 使用同一个扩展入口;
   3. 核心根据创建上下文自动判断 consumer/provider;
   4. 用户不再手工传入扩展内部 filter 名称;
   5. `dubbo.Load()` 可以加载扩展配置;
   6. 保持现有 `WithFilter`、`hystrix.ConfigureCommand` 等 API 兼容。
   
   本任务先以 Hystrix 作为第一个实现和验证案例。
   
   ## 目标 API
   
   ### Consumer
   
   ```go
   import (
       "dubbo.apache.org/dubbo-go/v3/client"
       hystrix "github.com/apache/dubbo-go-extensions/filter/hystrix"
   )
   
   cli, err := client.NewClient(
       client.WithExtension(
           hystrix.WithConfig(
               hystrix.WithCommandName(
                   "dubbo:consumer:greet.GreetService:::Greet",
               ),
               hystrix.WithTimeout(1000),
               hystrix.WithMaxConcurrentRequests(10),
               hystrix.WithRequestVolumeThreshold(5),
               hystrix.WithSleepWindow(5000),
               hystrix.WithErrorPercentThreshold(50),
           ),
       ),
   )
   ```
   
   用户不再需要:
   
   ```go
   client.WithFilter("hystrix_consumer")
   ```
   
   ### Provider
   
   ```go
   import (
       "dubbo.apache.org/dubbo-go/v3/server"
       hystrix "github.com/apache/dubbo-go-extensions/filter/hystrix"
   )
   
   srv, err := server.NewServer(
       server.WithExtension(
           hystrix.WithConfig(
               hystrix.WithCommandName(
                   "dubbo:provider:greet.GreetService:::Greet",
               ),
               hystrix.WithTimeout(1000),
           ),
       ),
   )
   ```
   
   用户不再需要:
   
   ```go
   server.WithFilter("hystrix_provider")
   ```
   
   ### API 语义
   
   ```text
   client.WithExtension(...)  -> dubbo-go 的 consumer 扩展入口
   server.WithExtension(...)  -> dubbo-go 的 provider 扩展入口
   hystrix.WithConfig(...)    -> 绑定 Hystrix 扩展配置
   hystrix.WithTimeout(...)   -> 配置 Hystrix 参数
   ```
   
   不建议在 dubbo-go 核心中提供 `extension.WithHystrix`。具体扩展的绑定方法应由具体扩展包提供,避免用户误以为 
Hystrix 属于核心 `common/extension` 包。
   
   不提供 Hystrix 的 Instance 级 filter API。一个 Instance 既可能创建 client,也可能创建 
server,角色应由 `client.NewClient` / `server.NewServer` 的上下文决定。Instance 级 API 只保留给 
tracing、metrics 等角色无关的扩展。
   
   ## YAML 设计
   
   不增加 `commands` 包装层,每个 Hystrix command 直接以资源名作为配置 key:
   
   ```yaml
   dubbo:
     extensions:
       hystrix:
         "dubbo:consumer:greet.GreetService:::Greet":
           timeout: 1000
           max-concurrent-requests: 10
           request-volume-threshold: 5
           sleep-window: 5000
           error-percent-threshold: 50
   
         "dubbo:consumer:com.example.UserService:::GetUser":
           timeout: 2000
           max-concurrent-requests: 20
   ```
   
   YAML 解析必须保留 command resource name 的完整字符串,不能让 `.` 或 `:` 被核心配置路径解析器拆分。
   
   使用 YAML 时,用户只需要导入扩展以完成注册:
   
   ```go
   import _ "github.com/apache/dubbo-go-extensions/filter/hystrix"
   
   func main() {
       if err := dubbo.Load(); err != nil {
           panic(err)
       }
   }
   ```
   
   配置加载完成后:
   
   - client 上下文自动绑定 `hystrix_consumer`;
   - server 上下文自动绑定 `hystrix_provider`;
   - 用户无需在 YAML 中配置这两个内部 filter 名称。
   
   ## 初步接口设计
   
   ### dubbo-go 核心
   
   在 `common/extension` 中定义通用扩展协议,但不包含任何具体扩展名称:
   
   ```go
   type Option interface {
       Prefix() string
       Apply(*Context) error
   }
   
   type Definition struct {
       Prefix string
       New    func() any
       Init   func(any) error
   
       ConsumerFilters func(any) []string
       ProviderFilters func(any) []string
   }
   ```
   
   核心提供:
   
   ```go
   func client.WithExtension(opts ...extension.Option) ClientOption
   func server.WithExtension(opts ...extension.Option) ServerOption
   ```
   
   核心职责:
   
   - 保存并应用扩展 option;
   - 加载 `dubbo.extensions`;
   - 为扩展创建独立配置实例;
   - 根据 client/server 上下文获取 consumer/provider filter;
   - 将扩展 filter 合并到最终 filter chain;
   - 对重复 filter 做去重;
   - 保留用户显式 filter 配置的覆盖能力。
   
   ### Hystrix 扩展
   
   Hystrix 包定义自己的 option:
   
   ```go
   type Option func(*Config)
   
   func WithConfig(opts ...Option) extension.Option
   func WithCommandName(name string) Option
   func WithTimeout(timeout int) Option
   func WithMaxConcurrentRequests(value int) Option
   func WithRequestVolumeThreshold(value int) Option
   func WithSleepWindow(value int) Option
   func WithErrorPercentThreshold(value int) Option
   ```
   
   Hystrix 配置对象负责:
   
   - 保存每个 resource name 对应的 command 配置;
   - 将配置应用到 `hystrix.ConfigureCommand`;
   - 声明 consumer/provider 对应的 filter;
   - 保持现有 filter 注册逻辑兼容。
   
   ## 兼容性要求
   
   以下方式继续可用:
   
   ```go
   _ "github.com/apache/dubbo-go-extensions/filter/hystrix"
   
   hystrix.ConfigureCommand(name, config)
   
   client.WithFilter("hystrix_consumer")
   server.WithFilter("hystrix_provider")
   ```
   
   新接口只是提供更高层的统一封装,不立即删除旧 API。
   
   ## 实施拆分
   
   ### dubbo-go
   
   - 新增通用 extension option/definition 协议;
   - 新增 `client.WithExtension`;
   - 新增 `server.WithExtension`;
   - 在 client/server 初始化流程中接入扩展配置;
   - 在 `dubbo.Load()` 中解析 `dubbo.extensions`;
   - 处理配置优先级:默认值 < YAML < option;
   - 处理 filter 合并、去重和显式覆盖;
   - 增加核心协议和 loader 测试。
   
   ### dubbo-go-extensions
   
   - 为 Hystrix 增加配置定义;
   - 增加 Hystrix typed options;
   - 增加 YAML 配置解析;
   - 根据 client/server 上下文自动绑定 filter;
   - 更新中英文 README;
   - 增加 option、YAML、filter 自动绑定测试。
   
   ## 验收标准
   
   - 用户使用 client API 时不需要写 `hystrix_consumer`;
   - 用户使用 server API 时不需要写 `hystrix_provider`;
   - 同一套 Hystrix 配置 option 可以接入 client/server;
   - `dubbo.Load()` 可以加载至少两个 Hystrix command;
   - command resource name 中的 `.`、`:` 不会被错误拆分;
   - 旧 API 行为不变;
   - 未导入 Hystrix 时,核心不依赖 Hystrix;
   - dubbo-go 核心不包含 `WithHystrix` 等具体扩展名称。
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to