标签: ocelot

是否可以为SwaggerUI动态添加SwaggerEndpoints?

我们正在 .NET Core 中构建面向服务的架构。我们决定使用 Ocelot 作为我们的 API 网关。我已将 Ocelot 与 Consul 集成以进行服务发现。现在我正在尝试为所有下游服务创建一个统一的 Swagger UI。

在服务发现之前,我们的 Swagger 设置如下:

// Enable middleware to serve generated Swagger as a JSON endpoint
app.UseSwagger(c => { c.RouteTemplate = "{documentName}/swagger.json"; });

// Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)
app.UseSwaggerUI(c =>
{
  c.SwaggerEndpoint("/docs/customer/swagger.json", "Customers Api Doc");
  c.SwaggerEndpoint("/docs/employee/swagger.json", "Employee Api Doc");
  c.SwaggerEndpoint("/docs/report/swagger.json", "Reports Api Doc");
});
Run Code Online (Sandbox Code Playgroud)

在 Swagger UI 上,这提供了一个“选择规范”下拉列表。开发人员喜欢这个功能,我们希望保留它。然而,现在我们已经删除了手动配置以支持服务发现,我们还希望动态更新这些端点。

使用当前可用的 Swagger 解决方案,这可能吗?我还没有看到任何与服务发现或能够动态配置 UI 相关的内容。想法和建议?

更新

我想出了一个方法来做到这一点。这有点hack-ish,我希望有一种方法可以做到这一点,而不是那么严厉。

public class Startup 
{
    static object LOCK …
Run Code Online (Sandbox Code Playgroud)

service-discovery swagger swagger-2.0 asp.net-core ocelot

5
推荐指数
1
解决办法
4741
查看次数

Identityserver 4 和 Ocelot

我正在尝试按照https://ocelot.readthedocs.io/en/latest/features/authentication.html使用 Ocelot 和 IS4

使用时

public void ConfigureServices(IServiceCollection services)
{
    var authenticationProviderKey = "TestKey";

    services.AddAuthentication()
        .AddJwtBearer(authenticationProviderKey, x =>
        {
        });
}
Run Code Online (Sandbox Code Playgroud)

并在 ocelot.json 中使用“TestKey”,它在启动应用程序时抛出错误

无法启动 Ocelot,错误是:TestKey,AllowedScopes:[] is unsupported authentication provider

知道出了什么问题吗?我需要在我的 IdentityServer 应用程序中特别设置一些东西吗?

c# .net-core identityserver4 api-gateway ocelot

5
推荐指数
1
解决办法
2641
查看次数

Ocelot 可以有自己的控制器/操作吗?

我正在尝试组合一个 API 网关,该网关也可用于混合对多个 API 的调用。我还希望拥有自动生成的 Swagger,因此我希望结合 Ocelot 的授权和速率限制功能,但也希望能够在 .NET 代码中创建边缘自定义 API 来处理 API 调用的翻译和组合。

Ocelot 可以做到这一点吗?有什么例子吗?Ocelot 是否只能作为单独的应用程序与下游 API 一起使用?我试图避免仅针对混搭和复合 API 调用而存在的额外跳跃。

asp.net-core ocelot

5
推荐指数
2
解决办法
3443
查看次数

Ocelot 无法匹配上游路径的路由配置

你好,现在我正在尝试使用 Ocelot gateway 通常我在不同的服务器上有一个 api,例如

https://server_domain.net/kpiDashboardApi
Run Code Online (Sandbox Code Playgroud)

没有网关,如果我直接发送请求到下面的链接,它可以工作

https://server_domain.net/kpiDashboardApi/Report/DocumentTypeCounts
Run Code Online (Sandbox Code Playgroud)

我想从本地的 Ocelot 到达此 api 中的端点,我使用下面的配置 json

{
  "Routes": [
    {
      "DownstreamPathTemplate": "/kpiDashboardApi/Report/DocumentTypeCounts",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "https://server_domain.net/kpiDashboardApi",
          "Port": 443
        }
      ],
      "UpstreamPathTemplate": "/Report/DocumentTypeCounts",
      "UpstreamHttpMethod": [ "Post" ]
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "https://localhost:5001"
  }
}
Run Code Online (Sandbox Code Playgroud)

当你发送请求到https://localhost:5001/kpiDashboardApi/Report/DocumentTypeCounts

它返回 404 并在下面的控制台中返回错误

警告:Ocelot.DownstreamRouteFinder.Middleware.DownstreamRouteFinderMiddleware[0] requestId:0HMFMPNHV9VHO:00000001,previousRequestId:没有先前的请求 id,消息:DownstreamRouteFinderMiddleware 设置管道错误。IDownstreamRouteFinder 返回错误代码:UnableToFindDownstreamRouteError 消息:无法匹配上游路径的路由配置:/kpiDashboardApi/Report/DocumentTypeCounts,动词:POST。警告:Ocelot.Responder.Middleware.ResponderMiddleware[0] requestId:0HMFMPNHV9VHO:00000001,previousRequestId:没有以前的请求 id,消息:错误代码:UnableToFindDownstreamRouteError 消息:无法匹配上游路径的路由配置:/kpiDashboardApi/Report/DocumentTypeCounts,动词: 邮政。在 ResponderMiddleware 中发现错误。设置错误响应请求路径:/kpiDashboardApi/Report/DocumentTypeCounts,请求方式:POST

我找不到缺失的部分。我的错误在哪里?

提前致谢

c# .net-core ocelot

5
推荐指数
1
解决办法
5700
查看次数

API 网关 Ocelot .Net Core 6.1 设置

.Net 6 已删除启动类,我无法找到如何在新的 .Net 6 结构中配置 Ocelot。我找到了两种方法

 using Ocelot.DependencyInjection;
using Ocelot.Middleware;

var builder = WebApplication.CreateBuilder(args);

builder.Configuration.AddOcelot()// 1.ocelot.json goes where?
// Add services to the container.

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddOcelot(); // 2.what is the use of this
Run Code Online (Sandbox Code Playgroud)

请告诉我

asp.net-core ocelot .net-6.0

5
推荐指数
2
解决办法
9919
查看次数

如何检查数组或 Ocelot 网关中的任何声明值?

假设我有 2 个这样的端点

  1. 创建订单:需要声明order_perm,其值在数组["order_create", "order_edit"]中
  2. 订单搜索:只需要声明order_perm存在

在情况 1 中,我将上面的数组传递给RouteClaimsRequirement,如下所示

"RouteClaimsRequirement": {
        "order_perm": ["order_create", "order_edit"]
      }
Run Code Online (Sandbox Code Playgroud)

但当应用程序启动时它会崩溃,并且在情况 2 中。我这样设置

"RouteClaimsRequirement": {
            "order_perm": ""
          }
Run Code Online (Sandbox Code Playgroud)

但用户声称“order_perm”:“create_order”授权失败。

RouteClaimsRequirement是否支持这些用例?如果是这样我该怎么做?

asp.net-core api-gateway ocelot

4
推荐指数
1
解决办法
2565
查看次数

Ocelot RouteClaimsRequirement 无法识别我的声明并返回 403 Forbidden

我已经在 Linux 容器中配置了 ocelot 和多个微服务。为了限制我正在使用的一些微服务RouteClaimsRequirement。我拥有管理员角色作为声明,但是当我使用角色管理员发送令牌时,Ocelot 返回 403 Forbidden,这是符合RouteClaimsRequirement. RouteClaimsRequirment如果我从ocelot.json一切正常中删除。

   {
  "DownstreamPathTemplate": "/api/v1/product/{everything}",
  "DownstreamScheme": "https",
  "DownstreamHostAndPorts": [
    {
      "Host": "product",
      "Port": 443
    }
  ],
  "UpstreamPathTemplate": "/product/{everything}",
  "UpstreamHttpMethod": [ "Get", "Post", "Delete" ],
  "AuthenticationOptions": {
    "AuthenticationProviderKey": "Bearer",
    "AllowedScopes": []
  },
  "RouteClaimsRequirement": {  <---- Problem Part
    "Role": "Administrator"
  },
  "DangerousAcceptAnyServerCertificateValidator": true,
  "RateLimitOptions": {
    "ClientWhitelist": [],
    "EnableRateLimiting": true,
    "Period": "5s",
    "PeriodTimespan": 6,
    "Limit": 8
  }
}
Run Code Online (Sandbox Code Playgroud)

ocelot 项目启动类如下所示:

public void ConfigureServices(IServiceCollection services)
=> services
    .AddCors() …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core ocelot

4
推荐指数
1
解决办法
2771
查看次数

Environment variable in ocelot config file

I have multiple microservices which are accessible by clients through Ocelot gateway. Inside configuration file there are properties to specify downstream host and port. This have to be done for EACH route.

The problem is that if the service's hostname or port changes, I will have to modify every single route associated with this particular service.

So, the question is, Is it possible to introduce ENV variable inside ocelot.json configuration file? In that case I will have to modify only …

.net-core asp.net-core-webapi ocelot

4
推荐指数
1
解决办法
1469
查看次数

使用 Ocelot API 网关连接到 SignalR 集线器

我正在尝试通过 Ocelot API 网关将 Blazor 客户端连接到简单微服务中的 SignalR 集线器。我对所有 ASP.NET Core 项目都使用 SSL。

网关在调用 https 端点时工作正常,当我直接从网关浏览器调用 signalR hub 端点(正确显示 Ocelot 路由)时,我收到“需要连接 ID”。

不幸的是,当我尝试从 blazor 客户端应用程序连接到集线器时,出现以下错误

失败:Ocelot.Errors.Middleware.ExceptionHandlerMiddleware[0] requestId:0HM4U0GLR9ACR:00000001,previousRequestId:没有以前的请求ID,消息:全局错误处理程序中捕获异常,异常消息:仅以“ws://”或“wss”开头的Uris支持 ://'。(参数'uri'),异常堆栈:在Ocelot.WebSockets.Middleware.WebSocketsProxyMiddleware.Proxy(HttpContext context,String serverEndpoint)在Ocelot.WebSockets.Middleware的System.Net.WebSockets.ClientWebSocket.ConnectAsync(Uri uri,CancellationToken CancellationToken) .WebSocketsProxyMiddleware.Invoke(HttpContext httpContext) 在 Microsoft.AspNetCore.MiddlewareAnalysis.AnalysisMiddleware.Invoke(HttpContext httpContext) 在 Ocelot.DownstreamUrlCreator.Middleware.DownstreamUrlCreatorMiddleware.Invoke(HttpContext httpContext) 在 Microsoft.AspNetCore.MiddlewareAnalysis.AnalysisMiddleware.Invoke(HttpContext httpContext)在 Ocelot.LoadBalancer.Middleware.LoadBalancingMiddleware.Invoke(HttpContext httpContext) 在 Microsoft.AspNetCore.MiddlewareAnalysis.AnalysisMiddleware.Invoke(HttpContext httpContext) 在 Ocelot.Request.Middleware.DownstreamRequestInitialiserMiddleware.Invoke(HttpContext httpContext) 在 Microsoft.AspNetCore.MiddlewareAnalysis.AnalysisMiddleware .Invoke(HttpContext httpContext) 在 Ocelot.Multiplexer.MultiplexingMiddleware.Invoke(HttpContext httpContext) 在 Microsoft.AspNetCore.MiddlewareAnalysis.AnalysisMiddleware.Invoke(HttpContext httpContext) 在 Ocelot.DownstreamRouteFinder.Middleware.DownstreamRouteFinderMiddleware.Invoke(HttpContext httpContext) 在 Microsoft.AspNetCore Microsoft.AspNetCore.Builder.Extensions.MapWhenMiddleware.Invoke(HttpContext context)在 Microsoft.AspNetCore.MiddlewareAnalysis.AnalysisMiddleware.Invoke(HttpContext httpContext)

以下是我的代码。

Ocelot API 启动文件

public class Startup
{
    public void ConfigureServices(IServiceCollection …
Run Code Online (Sandbox Code Playgroud)

microservices asp.net-core asp.net-core-signalr ocelot blazor-client-side

4
推荐指数
1
解决办法
7474
查看次数

Ocelot 与 Azure Active Directory 身份验证 .Net Core 3.1 集成

背景:我的公司正在转向 API 网关来管理我们网络中的所有服务。目前,每项服务均使用 Azure AD 进行身份验证。一切都是单一租户,只允许公司用户。 问题:我的理解正确吗? 问题:当我调用 http:localhost:5000/authResource/get 时,我收到来自 Ocelot 的 401 响应。跟踪错误后,我发现当 Ocelot 身份验证中间件尝试使用 AzureADJwtBearer 方案进行身份验证时,我得到了 null。

我遵循了与 Ocelot 和 azure ad 相关的问题的建议,但即使遵循了这些建议,我也无法让任何东西发挥作用。(主要来自这个答案:How set up Ocelot Api Gateway with Azure Active Directory)我想我可能误解了身份验证应该如何工作。我目前的理解是,我告诉 Ocelot 使用 AzureADJwtBearer 方案针对我的 API 之一进行身份验证。在我的配置中,我有该特定 api 设置的信息(ClientId、TenantId 等)。

一旦我调用该路由,我希望 Ocelot 致电 Microsoft 以开始身份验证。此时,我期望与 API 提供的流程相同,即我拨打电话并获取 Microsoft 登录页面,然后在输入用户名和密码后将我重定向回应用程序。

在将我重定向回 Ocelot 后,我​​猜(这是我模糊的部分),我希望 ocelot 存储 Microsoft 发回的访问令牌以获取我刚刚请求的特定资源。然后我希望 ocelot 将令牌附加到 Auth 标头,然后发送对我最初请求的资源的请求。

为了澄清这一点,我将包含我的启动文件和 ocelot.json 文件的代码。来自 Startup.cs

public void ConfigureServices(IServiceCollection services)
        {

            services.AddProtectedWebApi(Configuration)
                .AddProtectedApiCallsWebApis(Configuration)
                .AddInMemoryTokenCaches(); …
Run Code Online (Sandbox Code Playgroud)

azure-active-directory api-gateway ocelot asp.net-core-3.1

3
推荐指数
1
解决办法
3747
查看次数