You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by "jasondeng1997 (via GitHub)" <gi...@apache.org> on 2023/06/11 11:00:00 UTC

[GitHub] [dubbo-go-contrib] jasondeng1997 opened a new issue, #2: dubbo-go-contrib v1 架构设计方案

jasondeng1997 opened a new issue, #2:
URL: https://github.com/apache/dubbo-go-contrib/issues/2

   dubbo-go-contrib v1由以下接口列表组成:
   ● cluster- 异步消息,路由以及负载均衡算法
   ● common - 用于公共接口抽象
   ● config - 用于动态配置的
   ● codec - 用于消息编解码的
   ● logger - 调式日志, 跟踪, 统计信息
   ● netwrok - 多云的网络
   ● registry - 服务发现的注册表
   ● metadata - 用于元数据中心设计
   ● selector - 用于负载平衡(从cluster抽出来)
   ● server - 用于处理请求和通知
   ● store - 用于数据存储
   ● sync - 用于同步, 锁和选举领导
   ● transport - 用于同步通信,tls协议等
   ● metric 服务观测指标
   ● remoting 网络库相关
   ● tools  生成代码工具等
   ● xds  xDS 协议重构 dubbo 的路由协议
   插件
   dubbo-go-contrib为所有工具提供可插拔架构。这意味着底层的实现可以被换出。
   
    例子
   dubbo-go-contrib
   ● nacos- 使用nacos进行服务发现
   ● Kubernetes - 使用Kubernetes进行服务发现
   ● - rocketmq 消息传输
   工具包
   ● - 可配置的http路由和代理
   ● 生成代码工具
    知识库
   开源插件可以在https://github.com/apache/dubbo-go-contrib上找到。
   1. 框架
   dubbo-go-contrib是一个可插拔的工具包和框架. 内部功能可以与任何 [插件](https://github.com/micro/go-plugins) 置换.
   该工具包具有单独的插件接口. 可参阅 https://github.com/apache/dubbo-go-contrib了解更多信息.
   以下是有关 dubbo-go-contrib插件使用情况的信息.
   1.1. 使用
   插件可以通过以下方式添加到 dubbo-go-contrib. 通过这样做它们可以通过命令行参数或环境变量进行设置.
   导入 Go 程序中的插件, 然后调用服务. 初始化以分析命令行和环境变量.
   import (
       "github.com/apache/dubbo-go-contrib/v1"
       _ "github.com/apache/dubbo-go-contrib/rabbitmq"
       _ "github.com/apache/dubbo-go-contrib/registry/kubernetes"
       _ "github.com/apache/dubbo-go-contrib/transport/nats"
   )
   
   func main() {
       service := micro.NewService(
           // Set service name
           micro.Name("my.service"),
       )
   
       // Parse CLI flags
       service.Init()
   }
   1.1.1. 标志
   将插件指定为标志
   go run service.go --broker=rabbitmq --registry=kubernetes --transport=nats
   1.1.2. 环境变量
   使用环境变量指定插件
   dubbo-go-contrib_BROKER=rabbitmq
   dubbo-go-contrib_REGISTRY=kubernetes \ 
   dubbo-go-contrib_TRANSPORT=nats \ 
   go run service.go
   1.1.3. 选项
   创建新服务时, 导入并设置为选项
   import (
       "github.com/apache/dubbo-go-contrib/v1"
       "github.com/apache/dubbo-go-contrib/registry/kubernetes"
   )
   
   func main() {
       registry := kubernetes.NewRegistry() //a default to using env vars for master API
   
       service := micro.NewService(
           // Set service name
           micro.Name("my.service"),
           // Set service registry
           micro.Registry(registry),
       )
   }
   1.2. 构建
   修改 main.go 文件来包含插件是一种反模式的做法. 最佳做法建议将插件包含在单独的文件中, 并与其一起重新生成. 这允许构建插件的自动化和问题之间的干净分离.
   创建文件 plugins.go
   package main
   
   import (
       _ "github.com/apache/dubbo-go-contrib/broker/rabbitmq"
       _ "github.com/apache/dubbo-go-contrib/registry/kubernetes"
       _ "github.com/apache/dubbo-go-contrib/transport/nats"
   )
   使用插件构建.
   go build -o service main.go plugins.go
   使用插件运行
   dubbo-go-contrib_BROKER=rabbitmq
   dubbo-go-contrib_REGISTRY=kubernetes
   dubbo-go-contrib_TRANSPORT=nats
   service
   1.3. 重建工具包
   如果要集成插件, 只需将它们链接到单独的文件中并重新生成
   创建 plugins.go 文件
   import (
           // etcd v3 registry
           _ "github.com/apache/dubbo-go-contrib/registry/etcdv3"
           // nats transport
           _ "github.com/apache/dubbo-go-contrib/transport/nats"
           // kafka broker
           _ "github.com/apache/dubbo-go-contrib/broker/kafka"
   )
   生成二进制
   // For local use
   go build -i -o dubbo-go-contrib ./main.go ./plugins.go
   
   // For docker image
   CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-w' -i -o dubbo-go-contrib ./main.go ./plugins.go
   插件的标记用法
   dubbo-go-contrib--registry=etcdv3 --transport=nats --broker=kafka
   1.4. 仓库
   [dubbo-go-contrib](https://github.com/apache/dubbo-go-contrib) 的插件可以在此仓库 https://github.com/apache/dubbo-go-contrib找到.
   2. 运行时
   插件是将外部代码集成到 [dubbo-go-contrib](https://github.com/apache/dubbo-go-contrib) 工具包中的一种方式. 这与 [dubbo-go-contrib](https://github.com/apache/dubbo-go-contrib)插件完全分离. 使用插件可以在此处添加其他标志, 命令和 HTTP 处理程序到工具包.
   2.1工作原理
   在 [dubbo-go-contrib](https://github.com/apache/dubbo-go-contrib)下有一个全局插件管理器, 它由将在整个工具包中使用的插件组成. 插件可以通过调用进行注册. 每个组件 (api, web, proxy, cli, bot) 都有一个单独的插件管理器, 用于注册插件, 该插件应仅作为该组件的一部分添加. 它们可以通过调用 plugin.Register, api.Register, web.Register 等方式使用.
   下面是接口
   // Plugin is the interface for plugins to dubbo-go-contrib. It differs from in that it's for
   // the dubbo-go-contrib API, Web, Proxy, CLI. It's a method of building middleware for the HTTP side.
   type Plugin interface {
       // Global Flags
       Flags() []cli.Flag
       // Sub-commands
       Commands() []cli.Command
       // Handle is the middleware handler for HTTP requests. We pass in
       // the existing handler so it can be wrapped to create a call chain.
       Handler() Handler
       // Init called when command line args are parsed.
       // The initialised cli.Context is passed in.
       Init(*cli.Context) error
       // Name of the plugin
       String() string
   }
   
   // Manager is the plugin manager which stores plugins and allows them to be retrieved.
   // This is used by all the components of micro.
   type Manager interface {
           Plugins() map[string]Plugin
           Register(name string, plugin Plugin) error
   }
   
   // Handler is the plugin middleware handler which wraps an existing http.Handler passed in.
   // Its the responsibility of the Handler to call the next http.Handler in the chain.
   type Handler func(http.Handler) http.Handler
   2.2. 如何使用
   下面是一个简单的插件示例, 该插件添加一个标志, 然后打印值
   2.2.1. 插件
   在顶层目录中创建 plugins.go 文件
   package main
   
   import (
       "log"
       "github.com/dubbo-go-contrib/cli"
       "github.com/apache/dubbo-go-contrib/plugin"
   )
   
   func init() {
       plugin.Register(plugin.NewPlugin(
           plugin.WithName("example"),
           plugin.WithFlag(cli.StringFlag{
               Name:   "example_flag",
               Usage:  "This is an example plugin flag",
               EnvVar: "EXAMPLE_FLAG",
               Value: "avalue",
           }),
           plugin.WithInit(func(ctx *cli.Context) error {
               log.Println("Got value for example_flag", ctx.String("example_flag"))
               return nil
           }),
       ))
   }
   2.2.2. 构建代码
   只需使用插件构建[dubbo-go-contrib](https://github.com/apache/dubbo-go-contrib)
   go build -o micro ./main.go ./plugin.go
   2.3. 仓库
   工具包的插件可以在此仓库中[dubbo-go-contrib](https://github.com/apache/dubbo-go-contrib)找到.


-- 
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: notifications-unsubscribe@dubbo.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org