聊聊dubbo-go-proxy的plugins

.container .card .information strong · · 381 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

本文主要研究一下dubbo-go-proxy的plugins

PluginsGroup

dubbo-go-proxy-filter@v0.1.0-rc1.0.20210120132524-c63f4eb13725/pkg/api/config/api_config.go

// PluginsGroup defines the plugins group info
type PluginsGroup struct {
    GroupName string   `json:"groupName" yaml:"groupName"`
    Plugins   []Plugin `json:"plugins" yaml:"plugins"`
}

type Plugin struct {
    Name               string `json:"name" yaml:"name"`
    Version            string `json:"version" yaml:"version"`
    Priority           int    `json:"priority" yaml:"priority"`
    ExternalLookupName string `json:"externalLookupName" yaml:"externalLookupName"`
}
PluginsGroup定义了GroupName和Plugins;Plugin定义了Name、Version、Priority、ExternalLookupName属性

PluginsWithFunc

dubbo-go-proxy/pkg/filter/plugins/plugins.go

type PluginsWithFunc struct {
    Name     string
    Priority int
    fn       context.FilterFunc
}
PluginsWithFunc定义了Name、Priority、context.FilterFunc属性

InitPluginsGroup

dubbo-go-proxy/pkg/filter/plugins/plugins.go

// InitPluginsGroup prase api_config.yaml(pluginsGroup) to map[string][]PluginsWithFunc
func InitPluginsGroup(groups []config.PluginsGroup, filePath string) {

    if "" == filePath || len(groups) == 0 {
        return
    }

    // load file.so
    pls, err := plugin.Open(filePath)
    if nil != err {
        panic(err)
    }

    for _, group := range groups {
        pwdMap := make(map[string]PluginsWithFunc, len(group.Plugins))

        // trans to context.FilterFunc
        for _, pl := range group.Plugins {
            pwf := PluginsWithFunc{pl.Name, pl.Priority, loadExternalPlugin(&pl, pls)}
            pwdMap[pl.Name] = pwf
        }

        groupWithPluginsMap[group.GroupName] = pwdMap
    }
}
InitPluginsGroup根据配置文件及filePath来初始化PluginsGroup;它通过plugin.Open(filePath)来加载plugin,之后遍历配置的groups,按groupName来加载对应的plugin map,它使用PluginsWithFunc来描述包装plugin,通过loadExternalPlugin方法来设置context.FilterFunc

loadExternalPlugin

dubbo-go-proxy/pkg/filter/plugins/plugins.go

func loadExternalPlugin(p *config.Plugin, pl *plugin.Plugin) context.FilterFunc {
    if nil != p {
        logger.Infof("loadExternalPlugin name is :%s,version:%s,Priority:%d", p.Name, p.Version, p.Priority)
        sb, err := pl.Lookup(p.ExternalLookupName)
        if nil != err {
            panic(err)
        }

        sbf := sb.(func() filter.Filter)
        logger.Infof("loadExternalPlugin %s success", p.Name)
        return sbf().Do()
    }

    panic(errEmptyPluginConfig)
}
loadExternalPlugin方法根据ExternalLookupName通过plugin的Lookup方法来加载对应的func,最后通过filter.Filter的Do方法返回context.FilterFunc

api_config.yaml

name: proxy
description: proxy sample
resources:
  - path: '/api/v1/test-dubbo/user'
    type: restful
    description: user
    plugins:
      groupNames:
        - group2
      pluginNames:
        - rate limit
        - access
    methods:
      - httpVerb: GET
        onAir: true
        timeout: 1000ms
        inboundRequest:
          requestType: http
          queryStrings:
            - name: name
              required: true
        integrationRequest:
          requestType: http
          host: 127.0.0.1:8889
          path: /UserProvider/GetUserByName
          mappingParams:
            - name: queryStrings.name
              mapTo: queryStrings.name
          group: "test"
          version: 1.0.0
      - httpVerb: POST
        onAir: true
        timeout: 1000ms
        inboundRequest:
          requestType: http
          queryStrings:
            - name: name
              required: true
        integrationRequest:
          requestType: http
          host: 127.0.0.1:8889
          path: /UserProvider/CreateUser
          group: "test"
          version: 1.0.0
pluginFilePath: ""
pluginsGroup:
  - groupName: "group1"
    plugins:
      - name: "rate limit"
        version: "0.0.1"
        priority: 1000
        externalLookupName: "ExternalPluginRateLimit"
      - name: "access"
        version: "0.0.1"
        priority: 1000
        externalLookupName: "ExternalPluginAccess"
  - groupName: "group2"
    plugins:
      - name: "blacklist"
        version: "0.0.1"
        priority: 1000
        externalLookupName: "ExternalPluginBlackList"
pluginsGroup定义了两个group,其中group1定义了ExternalPluginRateLimit、ExternalPluginAccess两个plugin;group2定义了ExternalPluginBlackList一个plugin

小结

dubbo-go-proxy的提供了plugins,它根据api_config.yaml定义的pluginFilePath及pluginsGroup来进行plugin的初始化;InitPluginsGroup方法先根据filePath加载plugin,之后通过loadExternalPlugin方法按ExternalLookupName来找到对应的context.FilterFunc。

doc


有疑问加站长微信联系(非本文作者)

本文来自:Segmentfault

感谢作者:.container .card .information strong

查看原文:聊聊dubbo-go-proxy的plugins

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

381 次点击  
加入收藏 微博
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传