关于golang:聊聊dubbogoproxy的plugins

48次阅读

共计 3434 个字符,预计需要花费 9 分钟才能阅读完成。

本文次要钻研一下 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

  • dubbo-go-proxy

正文完
 0