【问题标题】:ASP.NET Core gateway ocelot API doesn't workASP.NET Core 网关 ocelot API 不起作用
【发布时间】:2021-05-09 05:20:45
【问题描述】:

我正在使用微服务创建一个项目,现在我希望能够测试我使用 ocelot 作为编排器创建的 API 网关,但是我无法通过浏览器对其进行测试,API 无法访问它,有人可以帮忙吗?

ocelot.json:

{
  "Routes": [
    // Billing.API
    {
      "DownstreamPathTemplate": "/api/v1/Billing",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "billingapi",
          "Port": "80"
        }
      ],
      "UpstreamPathTemplate": "/Billing",
      "UpstreamHttpMethod": [ "GET" ]
    }
  ]
}

当我运行项目并在浏览器 URL 中传递 /api/v1/Billing

当我通过 DownstreamPathTemplate 属性时。 没有任何东西退还给我。

【问题讨论】:

  • 您确定主机名“billingapi”存在吗?同时发布您的错误信息

标签: asp.net asp.net-core microservices


【解决方案1】:

我建议先尝试通用模板,看看它是否有效:

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

来自关于下游的 Ocelot 文档:

The DownstreamPathTemplate, DownstreamScheme and DownstreamHostAndPorts define the URL that a request will be forwarded to.

对于上游:

The UpstreamPathTemplate is the URL that Ocelot will use to identify which DownstreamPathTemplate to use for a given request. 

【讨论】:

  • requestId: 0HM69A4IV0LEC:00000007, previousRequestId: no previous request id, message: Error Code: ConnectionToDownstreamServiceError Message: Error connection to downstream service, exception: System.Net.Http.HttpRequestException: This host is not known . (billingapi:80)
  • 我更新了我的答案,使其更笼统。但现在在 url 你必须添加 billingapi/api/v1/endpointname
【解决方案2】:

您应该在浏览器上传递 UpstreamPathTemplate URL 以路由计费 API。在您的情况下,请求 HTTP:///Billings 将被路由到 HTTP://billingapi/api/v1/Billing

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-20
    • 2019-09-30
    • 2021-12-11
    • 2022-01-24
    • 2020-10-15
    • 2020-03-24
    • 2018-12-29
    • 2021-02-22
    相关资源
    最近更新 更多