php小編草今天為大家介紹的是關於KrakenD外掛開發中的一個常見問題:"為 KrakenD 實作外掛程式時無效的節點類型恐慌」。 KrakenD是一個快速、高效能的API網關,它提供了豐富的功能和靈活的插件系統。然而,在開發KrakenD插件時,有時會遇到節點類型無效的問題,這可能會導致插件無法正常運作。在本文中,我們將探討這個問題的原因以及解決方案,幫助開發者克服這個恐慌。
我正在開發一個無重定向的外掛程式。我正在使用 krakend-ce 2.2.1(使用 golang 1.19),我感到恐慌:
gw_krakend_1 | [krakend] 2023/03/15 - 21:09:06.675 ? debug no_redirect_plugin: request received https://127.0.0.1:8443/abc gw_krakend_1 | [krakend] 2023/03/15 - 21:09:06.689 ? debug no_redirect_plugin: redirect detected https://127.0.0.1:8443/abc gw_krakend_1 | [krakend] 2023/03/15 - 21:09:06.689 ? debug status code 302 gw_krakend_1 | 2023/03/15 21:09:06 http: panic serving [::1]:54778: invalid node type gw_krakend_1 | goroutine 84 [running]: gw_krakend_1 | net/http.(*conn).serve.func1() gw_krakend_1 | /usr/local/go/src/net/http/server.go:1854 +0xbf gw_krakend_1 | panic({0x28cbb60, 0x34b5810}) gw_krakend_1 | /usr/local/go/src/runtime/panic.go:890 +0x263 gw_krakend_1 | github.com/gin-gonic/gin.(*node).findcaseinsensitivepathrec(0x0?, {0xc0016ac2ec?, 0x0?}, {0xc0010fe800?, 0xc0016ac2ed?, 0xc000ced928?}, {0x0, 0x0, 0x0, 0x0}, ...) gw_krakend_1 | /go/pkg/mod/github.com/gin-gonic/[email protected]/tree.go:862 +0xa9d gw_krakend_1 | github.com/gin-gonic/gin.(*node).findcaseinsensitivepath(0xc0016ac2ec?, {0xc0016ac2ec, 0x5}, 0x30?) gw_krakend_1 | /go/pkg/mod/github.com/gin-gonic/[email protected]/tree.go:669 +0x9c gw_krakend_1 | github.com/gin-gonic/gin.redirectfixedpath(0xc000664300, 0xc0016ac2ec?, 0x60?) gw_krakend_1 | /go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:684 +0x5b gw_krakend_1 | github.com/gin-gonic/gin.(*engine).handlehttprequest(0xc000602b60, 0xc000664300)
看起來與它類似https://github.com/gin-gonic/gin/issues/2959,但gin的版本已經在先前版本的krakend上升級了。如果它們真的是大寫字母那就很奇怪了,沒有插件它可以完美地工作。我還修剪了最後一個 / (由於某種原因它在某個點添加了)
順便說一句,我正在使用相同版本的 krakend 編譯外掛。
package main import ( "context" "errors" "fmt" "io" "net/http" "net/url" "strings" ) func main() {} var clientregisterer = registerer("no_redirect_plugin") type registerer string type logger interface { debug(v ...interface{}) info(v ...interface{}) warning(v ...interface{}) error(v ...interface{}) critical(v ...interface{}) fatal(v ...interface{}) } var logger logger = nil func (registerer) registerlogger(v interface{}) { l, ok := v.(logger) if !ok { return } logger = l logger.info(fmt.sprintf("[plugin: %s] logger loaded", clientregisterer)) } func (r registerer) registerclients(f func( name string, handler func(context.context, map[string]interface{}) (http.handler, error), )) { f(string(r), r.registerclients) } func (r registerer) registerclients(_ context.context, extra map[string]interface{}) (http.handler, error) { name, ok := extra["name"].(string) if !ok { return nil, errors.new("wrong config") } if name != string(r) { return nil, fmt.errorf("unknown register %s", name) } httpclient := &http.client{ checkredirect: func(req *http.request, via []*http.request) error { // trim the last "/" character from the url if it exists urlstr := strings.trimright(req.url.string(), "/") req.url, _ = url.parse(urlstr) logger.debug("no_redirect_plugin: redirect detected", req.url.string()) return http.erruselastresponse }, } return http.handlerfunc(func(w http.responsewriter, req *http.request) { // trim the last "/" character from the url if it exists urlstr := strings.trimright(req.url.string(), "/") req.url, _ = url.parse(urlstr) logger.debug("no_redirect_plugin: request received", req.url.string()) resp, err := httpclient.do(req) if err != nil { logger.debug("error while proxying request", err.error()) http.error(w, err.error(), http.statusinternalservererror) return } defer resp.body.close() for k, hs := range resp.header { for _, h := range hs { w.header().add(k, h) } } w.writeheader(resp.statuscode) logger.debug("status code", resp.statuscode) if resp.body == nil { return } _, err = io.copy(w, resp.body) if err != nil { logger.debug("error while proxying request 2", err.error()) http.error(w, err.error(), http.statusinternalservererror) return } }), nil }
我的端點定義如下:
{ "endpoint": "/", "input_headers":[ "*" ], "input_query_strings":[ "*" ], "method": "GET", "output_encoding": "no-op", "extra_config": {}, "backend": [{ "url_pattern": "", "encoding": "no-op", "sd": "static", "method": "GET", "extra_config": { "plugin/http-client": { "name": "no_redirect_plugin" } }, "host": [ "{{ env "HOST" }}" ], "disable_host_sanitize": false }] },{ "endpoint": "/{level1}", "input_headers":[ "*" ], "input_query_strings":[ "*" ], "method": "GET", "output_encoding": "no-op", "extra_config": { }, "backend": [{ "url_pattern": "/{level1}", "encoding": "no-op", "sd": "static", "method": "GET", "extra_config": { "plugin/http-client": { "name": "no_redirect_plugin" } }, "host": [ "{{ env "HOST" }}" ], "disable_host_sanitize": false }] },{ "endpoint": "/{level1}/{level2}", "input_headers":[ "*" ], "input_query_strings":[ "*" ], "method": "GET", "output_encoding": "no-op", "extra_config": { }, "backend": [{ "url_pattern": "/{level1}/{level2}", "encoding": "no-op", "sd": "static", "method": "GET", "extra_config": { "plugin/http-client": { "name": "no_redirect_plugin" } }, "host": [ "{{ env "HOST" }}" ], "disable_host_sanitize": false }] }
編輯:我的瀏覽器仍然顯示/abc/ 而不是/abc,這可能會在路線之間產生可能的碰撞(如下所示:https://github.com/krakendio/krakend-ce/issues /386)無論如何我不知道知道在哪裡添加斜杠(我以為我把它永久修剪了......似乎我沒有)
edit2:我發現了這個 https://www.krakend.io/docs/service-settings/router-options/ 並使用“disable_redirect_fixed_path”:true 和“disable_redirect_trailing_slash”:true,它不再恐慌......現在我有另一個問題:無限重定向(開玩笑只是10)當我的巨石試圖重定向到/a/或任何結尾帶有斜杠的路線時,這在插件,因為krakend 過去常常通過自己的方式處理重定向...
我猜這裡的主要問題是路由衝突,當 /{level1} 和 /{level1}/{level2} 同時與 /abc/ 匹配時
想法?
很好,為了解決這個問題,我嘗試使用這個 建立一個通配符no_redirect 外掛程式https://www.php.cn/link/ba530cdf0a884348613f2aaa3a5ba5e8配置,但即使使用copilot&gpt4 我也失敗了,我敢說你可以實現這個。所以我用另一種方法解決了這個問題:我將網關放在我的巨石前面,每次請求失敗時我都會添加路由......因為我們沒有記錄的路由(*哭泣)。對於需要無重定向插件的文件,它運作得很好。一個很棒的解決方案,但它到目前為止一直有效,而且沒有我想像的那麼長
以上是為 KrakenD 實作插件時無效的節點類型恐慌的詳細內容。更多資訊請關注PHP中文網其他相關文章!