5

我使用 Krakend 作为 API-Gateway,我的配置如下所示:

{
  "plugin": {
    "folder": "/etc/krakend/plugins/authenticator/",
    "pattern":".so"
  },
  "port": 8080,
  "extra_config": {
    "github_com/devopsfaith/krakend/transport/http/server/handler": {
      "name": "authenticator"
    }
  },

  "endpoints": [
    {
      "output_encoding": "no-op",
      "backend": [
        {
          "encoding": "no-op",
          "host": [
            "127.0.0.1:8080"
          ],
          "url_pattern": "/api/v1/address/{id}",
          "method": "GET"
        }
      ],
      "endpoint": "/api/v1/addresses/{id}",
      "method": "GET"
    }
  ],

  "name": "gateway",
  "timeout": "30s",
  "version": 2
}

我想为每个端点传递一些元数据并在我的预定义插件中访问它。在这种情况下authenticator插件。

4

1 回答 1

-1

您尝试实现的目标是完全可能的,并且是所有组件在 KrakenD 中的工作方式。您的插件可以使用您定义的命名空间访问 KrakenD 配置。例如,您可以像这样设置元数据(我假设您的 Go 代码中有 a pluginName = "slifer2015-authenticator"):

{
    "endpoints": [
        {
            "output_encoding": "no-op",
            "backend": [
                {
                    "encoding": "no-op",
                    "host": [
                        "127.0.0.1:8080"
                    ],
                    "url_pattern": "/api/v1/address/{id}"
                }
            ],
            "endpoint": "/api/v1/addresses/{id}",
            "extra_config": {
                "github_com/devopsfaith/krakend/transport/http/server/handler": {
                    "name": [
                        "slifer2015-authenticator",
                        "some-other-plugin-here"
                    ],
                    "slifer2015-authenticator": {
                        "Metadata1": "value1",
                        "Metadata2": {
                            "Some": 10,
                            "Thing": 100,
                            "Here": "60s"
                        }
                    }
                }
            }
        }
    ]
}

然后,extra当注册器启动时,您的元数据在参数中可用,在您选择的键内。

func (r registerer) registerHandlers(ctx context.Context, extra map[string]interface{}, h http.Handler) (http.Handler, error) {
``
于 2022-01-08T13:41:31.467 回答