kong 网关 request-transform 插件正则配置

2021-04-19 17:36:15 +08:00
 oldboy627

大家好,目前正在整合 kong 网关,使用 request-transform 插件匹配和修改 route 路径。

前端发送请求为 http://www.xxx.com/code/api/xxx/xxx/, 当经过 kong 转发到后端 service 的时候,路径应该变为 /code/api/meltSound/ 也就是说

route path: http://www.xxx.com/code/api/xxx/xxx

--- >

server path: http://www.xxx.com/code/api/meltSound/xxx/xxx

ingress 中的 route 配置为:


apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    konghq.com/strip-path: "true"
  name: melt-api
  namespace: app
spec:
  ingressClassName: kong
  rules:
  - host:  
    http:
      paths:
      - path: /code/api/
        pathType: Prefix
        backend:
          serviceName: network-gateway
          servicePort: 9527
          

在 request-transform 插件中设置为:


apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
 name: add-melt
config: 
 replace:
   uri: '/code/api/meltSound'

plugin: request-transformer

目前我这个配置没有生效,所以请问下大家要如何设置?

1448 次点击
所在节点    程序员
3 条回复
foMM
2021-04-19 19:49:05 +08:00
你这不是还没把插件设在置 ingress 上,同时也没有使用 kong ingress controller 在你的 ingress 上,试试下面这个

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: melt-api
annotations:
kubernetes.io/ingress.class: "kong"
konghq.com/plugins: add-melt
konghq.com/strip-path: "true"
foMM
2021-04-19 19:49:45 +08:00
话说回复的话怎么粘贴代码?没有 indentation 真是蛋疼
oldboy627
2021-04-19 20:01:54 +08:00
@foMM 其实是加了的,只是漏掉忘记写在 V2EX 上了。

现在问题解决了,解决方案是使用 request transform 插件配合正则表达式来匹配。

完整的配置文件是这样子

ingress 中的 route 配置为:

```yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
konghq.com/strip-path: "true"
konghq.com/plugins: "add-melt"
name: melt-api
namespace: app
spec:
ingressClassName: kong
rules:
- host:
http:
paths:
- path: /code/api/(?<url>\S+)$
pathType: Prefix
backend:
serviceName: network-gateway
servicePort: 9527
```

在 request-transform 插件中设置为:

```yaml
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
name: add-melt
config:
replace:
uri: '/code/api/meltSound/$(uri_captures.url)'
plugin: request-transformer

```

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/771725

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX