【问题标题】:Swagger plus Ocelot招摇加豹猫
【发布时间】:2021-05-17 02:21:43
【问题描述】:

我正在尝试使用 ocelot 和 swagger (https://github.com/Burgyn/MMLib.SwaggerForOcelot) 实现网关。我能够在我的 localhost 中很好地配置所有内容,现在我需要将我的解决方案部署到 kubernetes,但是,因为并行开发了一些功能,我被要求将我的解决方案部署在一个子目录中,就像这样。

  • https://domain/Feature1/gateway1
  • https://domain/Feature2/gateway2 ...

他们没有按功能划分子域。

我的配置有效,但是 Swagger UI 存在问题,因为测试路径是这样映射的。

https://domain/api/service/方法

但我需要类似的东西

https://domain/Feature1/api/service/method

有可能吗?

【问题讨论】:

    标签: swagger ocelot


    【解决方案1】:

    我不确定我是否有帮助,但 MMLib.SwaggerForOcelot 支持虚拟目录。 Please try this

    【讨论】:

    • HI Mino,更具体地说,api(下游)在根localhot:5000中运行,所以,我认为使用虚拟目录不适用于这种情况。
    【解决方案2】:

    也许它可以帮助某人,这是我可以使这个设置工作的唯一方法。

    访问以下位置的 swagger 文档: https://domain/gatewayfeature1

    还有以下 API: https://domain/apisfeature1

    另一个功能也是如此。

    文档 https://domain/gatewayfeature2

    API https://domain/apisfeature2

    我使用 nginx 作为反向代理,使用 ocelot 作为我的 apis 和 GKE 集群的网关。

    我最终使用的入口 yaml 是 this by feature, "apifeature1", "apifeature2",...

    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      annotations:
        kubernetes.io/ingress.class: nginx
        nginx.ingress.kubernetes.io/rewrite-target: /$1
        nginx.ingress.kubernetes.io/use-regex: "true"
      name: test
      namespace: default
    spec:
      rules:
      - host: testdomain
        http:
          paths:
          - backend:
              serviceName: gateway-feature1
              servicePort: 80
            path: /docsfeature1/(.*)
          - backend:
              serviceName: gateway-feature1
              servicePort: 80
            path: /(apifeature1/.*)
    

    Ocelot Config 按功能“apifeature1”、“apifeature2”、...

    在这种情况下,我正在从配置中读取功能名称,因此 ocelot 配置就像这个示例一样结束。

    {
      "UpstreamPathTemplate": "/apifeature1/api/controller/{everything}",
      "UpstreamHttpMethod": [ "Get", "Post" ],
      "DownstreamPathTemplate": "/{everything}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "apifeature1",
          "Port": "80"
        }
      ],
      "SwaggerKey": "api"
    },
    

    以及按功能划分的网关。

    app.Use((context, next) =>
    {
        context.Request.PathBase = new PathString("/apifeature1");
        return next();
    }); 
    
    .
    .
    .
    
    app.UseSwaggerForOcelotUI(c =>
    {
        c.DownstreamSwaggerEndPointBasePath = "/apifeature1/swagger/docs";
        c.PathToSwaggerGenerator = "/apifeature1/swagger/docs";
        c.DocumentTitle = "DOCUMENT";
    });
    

    它有效,但是,我认为它可以改进。

    【讨论】:

      猜你喜欢
      • 2021-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-20
      • 2015-01-09
      相关资源
      最近更新 更多